4 // Copyright (C) 2005 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #include "textscroller.hpp"
25 #include "resources.hpp"
26 #include "video/font.hpp"
27 #include "video/drawing_context.hpp"
28 #include "lisp/parser.hpp"
29 #include "lisp/lisp.hpp"
30 #include "audio/sound_manager.hpp"
32 #include "control/joystickkeyboardcontroller.hpp"
34 static const float DEFAULT_SPEED = .02;
35 static const float SCROLL = 60;
36 static const float ITEMS_SPACE = 4;
38 static void split_text(const std::string& text, std::vector<std::string>& lines)
40 // Split text string lines into a vector
42 std::string::size_type i, l;
45 l = text.find("\n", i);
47 if(l == std::string::npos) {
48 lines.push_back(text.substr(i, text.size()-i));
52 lines.push_back(text.substr(i, l-i));
57 void display_text_file(const std::string& filename)
59 const Font* heading_font = white_big_text;
60 const Font* normal_font = white_text;
61 const Font* small_font = white_small_text;
62 const Font* reference_font = blue_text;
63 float defaultspeed = DEFAULT_SPEED;
64 float speed = defaultspeed;
67 std::string background_file;
68 std::vector<std::string> lines;
72 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
74 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
76 throw std::runtime_error("File isn't a supertux-text file");
78 if(!text_lisp->get("text", text))
79 throw std::runtime_error("file doesn't contain a text field");
80 if(!text_lisp->get("background", background_file))
81 throw std::runtime_error("file doesn't contain a background file");
82 if(text_lisp->get("speed", defaultspeed))
84 } catch(std::exception& e) {
85 std::cerr << "Couldn't load file '" << filename << "': " << e.what() <<
90 // Split text string lines into a vector
91 split_text(text, lines);
93 // load background image
95 = new Surface("images/background/" + background_file, false);
99 float left_border = 50;
101 DrawingContext context;
102 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
104 Uint32 lastticks = SDL_GetTicks();
106 main_controller->update();
107 /* in case of input, exit */
109 while(SDL_PollEvent(&event)) {
110 main_controller->process_event(event);
111 if(event.type == SDL_QUIT)
112 throw std::runtime_error("received window close");
115 if(main_controller->hold(Controller::UP)) {
116 speed = -defaultspeed*5;
117 } else if(main_controller->hold(Controller::DOWN)) {
118 speed = defaultspeed*5;
120 speed = defaultspeed;
122 if(main_controller->pressed(Controller::JUMP)
123 || main_controller->pressed(Controller::ACTION)
124 || main_controller->pressed(Controller::MENU_SELECT))
126 if(main_controller->pressed(Controller::PAUSE_MENU))
129 /* draw the credits */
130 context.draw_surface(background, Vector(0,0), 0);
133 for(size_t i = 0; i < lines.size(); i++) {
134 const std::string& line = lines[i];
135 if(line.size() == 0) {
136 y += normal_font->get_height() + ITEMS_SPACE;
140 const Font* font = 0;
144 case ' ': font = small_font; break;
145 case '\t': font = normal_font; break;
146 case '-': font = heading_font; break;
147 case '*': font = reference_font; break;
148 case '#': font = normal_font; center = false; break;
150 std::cerr << "Warning: text contains an unformated line.\n";
157 context.draw_text(font,
158 line.substr(1, line.size()-1),
159 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT + y - scroll),
160 CENTER_ALLIGN, LAYER_FOREGROUND1);
162 context.draw_text(font,
163 line.substr(1, line.size()-1),
164 Vector(left_border, SCREEN_HEIGHT + y - scroll),
165 LEFT_ALLIGN, LAYER_FOREGROUND1);
168 y += font->get_height() + ITEMS_SPACE;
171 context.do_drawing();
172 sound_manager->update();
174 if(SCREEN_HEIGHT+y-scroll < 0 && 20+SCREEN_HEIGHT+y-scroll < 0)
177 Uint32 ticks = SDL_GetTicks();
178 scroll += speed * (ticks - lastticks);
186 SDL_EnableKeyRepeat(0, 0); // disables key repeating
190 InfoBox::InfoBox(const std::string& text)
193 split_text(text, lines);
201 InfoBox::draw(DrawingContext& context)
203 const Font* heading_font = white_big_text;
204 const Font* normal_font = white_text;
205 const Font* small_font = white_small_text;
206 const Font* reference_font = blue_text;
213 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
214 Color(150, 180, 200, 125), LAYER_GUI-1);
217 for(size_t i = firstline; i < lines.size(); ++i) {
218 const std::string& line = lines[i];
222 if(line.size() == 0) {
223 y += normal_font->get_height() + ITEMS_SPACE;
227 const Font* font = 0;
231 case ' ': font = small_font; break;
232 case '\t': font = normal_font; break;
233 case '-': font = heading_font; break;
234 case '*': font = reference_font; break;
235 case '#': font = normal_font; center = false; break;
237 std::cerr << "Warning: text contains an unformated line.\n";
244 context.draw_text(font,
245 line.substr(1, line.size()-1),
246 Vector(SCREEN_WIDTH/2, y),
247 CENTER_ALLIGN, LAYER_GUI);
249 context.draw_text(font,
250 line.substr(1, line.size()-1),
252 LEFT_ALLIGN, LAYER_GUI);
255 y += font->get_height() + ITEMS_SPACE;
267 InfoBox::scrolldown()
269 if(firstline < lines.size()-1)