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 "video/surface.hpp"
29 #include "lisp/parser.hpp"
30 #include "lisp/lisp.hpp"
31 #include "audio/sound_manager.hpp"
33 #include "control/joystickkeyboardcontroller.hpp"
34 #include "exceptions.hpp"
36 static const float DEFAULT_SPEED = .02;
37 static const float SCROLL = 60;
38 static const float ITEMS_SPACE = 4;
40 static void split_text(const std::string& text, std::vector<std::string>& lines)
42 // Split text string lines into a vector
44 std::string::size_type i, l;
47 l = text.find("\n", i);
49 if(l == std::string::npos) {
50 lines.push_back(text.substr(i, text.size()-i));
54 lines.push_back(text.substr(i, l-i));
59 void display_text_file(const std::string& filename)
61 const Font* heading_font = white_big_text;
62 const Font* normal_font = white_text;
63 const Font* small_font = white_small_text;
64 const Font* reference_font = blue_text;
65 float defaultspeed = DEFAULT_SPEED;
66 float speed = defaultspeed;
69 std::string background_file;
70 std::vector<std::string> lines;
71 std::map<std::string, Surface*> images;
75 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
77 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
79 throw std::runtime_error("File isn't a supertux-text file");
81 if(!text_lisp->get("text", text))
82 throw std::runtime_error("file doesn't contain a text field");
83 if(!text_lisp->get("background", background_file))
84 throw std::runtime_error("file doesn't contain a background file");
85 if(text_lisp->get("speed", defaultspeed))
87 } catch(std::exception& e) {
88 std::cerr << "Couldn't load file '" << filename << "': " << e.what() <<
93 // Split text string lines into a vector
94 split_text(text, lines);
96 for(size_t i = 0; i < lines.size(); ++i) {
97 const std::string& line = lines[i];
101 std::string imagename = line.substr(1, line.size()-1);
102 std::cout << "Imagename: " << imagename << "\n";
103 images.insert(std::make_pair(imagename, new Surface(imagename)));
107 // load background image
108 Surface* background = new Surface("images/background/" + background_file);
112 float left_border = 50;
114 DrawingContext context;
115 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
117 Uint32 lastticks = SDL_GetTicks();
119 main_controller->update();
120 /* in case of input, exit */
122 while(SDL_PollEvent(&event)) {
123 main_controller->process_event(event);
124 if(event.type == SDL_QUIT)
125 throw graceful_shutdown();
128 if(main_controller->hold(Controller::UP)) {
129 speed = -defaultspeed*5;
130 } else if(main_controller->hold(Controller::DOWN)) {
131 speed = defaultspeed*5;
133 speed = defaultspeed;
135 if(main_controller->pressed(Controller::JUMP)
136 || main_controller->pressed(Controller::ACTION)
137 || main_controller->pressed(Controller::MENU_SELECT))
139 if(main_controller->pressed(Controller::PAUSE_MENU))
142 /* draw the credits */
143 context.draw_surface(background, Vector(0,0), 0);
146 for(size_t i = 0; i < lines.size(); i++) {
147 const std::string& line = lines[i];
148 if(line.size() == 0) {
149 y += normal_font->get_height() + ITEMS_SPACE;
153 const Font* font = 0;
154 const Surface* image = 0;
158 case ' ': font = small_font; break;
159 case '\t': font = normal_font; break;
160 case '-': font = heading_font; break;
161 case '*': font = reference_font; break;
162 case '#': font = normal_font; center = false; break;
164 std::string imagename = line.substr(1, line.size()-1);
165 image = images[imagename];
169 std::cerr << "Warning: text contains an unformated line.\n";
177 context.draw_text(font,
178 line.substr(1, line.size()-1),
179 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT + y - scroll),
180 CENTER_ALLIGN, LAYER_FOREGROUND1);
182 context.draw_text(font,
183 line.substr(1, line.size()-1),
184 Vector(left_border, SCREEN_HEIGHT + y - scroll),
185 LEFT_ALLIGN, LAYER_FOREGROUND1);
187 y += font->get_height() + ITEMS_SPACE;
190 context.draw_surface(image,
191 Vector( (SCREEN_WIDTH - image->get_width()) / 2,
192 SCREEN_HEIGHT + y - scroll), 255);
193 y += image->get_height() + ITEMS_SPACE;
197 context.do_drawing();
198 sound_manager->update();
200 if(SCREEN_HEIGHT+y-scroll < 0 && 20+SCREEN_HEIGHT+y-scroll < 0)
203 Uint32 ticks = SDL_GetTicks();
204 scroll += speed * (ticks - lastticks);
212 for(std::map<std::string, Surface*>::iterator i = images.begin();
213 i != images.end(); ++i)
216 SDL_EnableKeyRepeat(0, 0); // disables key repeating
220 InfoBox::InfoBox(const std::string& text)
223 split_text(text, lines);
225 for(size_t i = 0; i < lines.size(); ++i) {
226 if(lines[i].size() == 0)
228 if(lines[i][0] == '!') {
229 std::string imagename = lines[i].substr(1, lines[i].size()-1);
230 images.insert(std::make_pair(imagename, new Surface(imagename)));
236 // get the arrow sprites
237 arrow_scrollup = new Surface("images/engine/menu/scroll-up.png");
238 arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
240 catch (std::exception& e)
242 std::cout << "Could not load scrolling images: " << e.what() << std::endl;
244 arrow_scrolldown = 0;
250 for(std::map<std::string, Surface*>::iterator i = images.begin();
251 i != images.end(); ++i)
256 InfoBox::draw(DrawingContext& context)
258 const Font* heading_font = white_big_text;
259 const Font* normal_font = white_text;
260 const Font* small_font = white_small_text;
261 const Font* reference_font = blue_text;
268 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
269 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
272 for(size_t i = firstline; i < lines.size(); ++i) {
273 const std::string& line = lines[i];
277 if(line.size() == 0) {
278 y += normal_font->get_height() + ITEMS_SPACE;
282 const Font* font = 0;
283 const Surface* image = 0;
287 case ' ': font = small_font; break;
288 case '\t': font = normal_font; break;
289 case '-': font = heading_font; break;
290 case '*': font = reference_font; break;
291 case '#': font = normal_font; center = false; break;
293 std::string imagename = line.substr(1, line.size()-1);
294 image = images[imagename];
298 std::cerr << "Warning: text contains an unformatted line.\n";
305 context.draw_surface(image,
306 Vector( (SCREEN_WIDTH - image->get_width()) / 2,
308 y += image->get_height() + ITEMS_SPACE;
310 context.draw_text(font,
311 line.substr(1, line.size()-1),
312 Vector(SCREEN_WIDTH/2, y),
313 CENTER_ALLIGN, LAYER_GUI);
314 y += font->get_height() + ITEMS_SPACE;
316 context.draw_text(font,
317 line.substr(1, line.size()-1),
319 LEFT_ALLIGN, LAYER_GUI);
320 y += font->get_height() + ITEMS_SPACE;
323 // draw the scrolling arrows
324 if (arrow_scrollup && firstline > 0)
325 context.draw_surface(arrow_scrollup,
326 Vector( x1 + width - arrow_scrollup->get_width(), // top-right corner of box
329 if (arrow_scrolldown && firstline < lines.size()-1)
330 context.draw_surface(arrow_scrolldown,
331 Vector( x1 + width - arrow_scrolldown->get_width(), // bottom-light corner of box
332 y1 + height - arrow_scrolldown->get_height()),
345 InfoBox::scrolldown()
347 if(firstline < lines.size()-1)