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.h"
25 #include "resources.h"
26 #include "video/font.h"
27 #include "video/drawing_context.h"
28 #include "lisp/parser.h"
29 #include "lisp/lisp.h"
31 #include "control/joystickkeyboardcontroller.h"
33 static const float DEFAULT_SPEED = .02;
34 static const float SCROLL = 60;
35 static const float ITEMS_SPACE = 4;
37 static void split_text(const std::string& text, std::vector<std::string>& lines)
39 // Split text string lines into a vector
41 std::string::size_type i, l;
44 l = text.find("\n", i);
46 if(l == std::string::npos) {
47 lines.push_back(text.substr(i, text.size()-i));
51 lines.push_back(text.substr(i, l-i));
56 void display_text_file(const std::string& filename)
58 const Font* heading_font = white_big_text;
59 const Font* normal_font = white_text;
60 const Font* small_font = white_small_text;
61 const Font* reference_font = blue_text;
62 float defaultspeed = DEFAULT_SPEED;
63 float speed = defaultspeed;
66 std::string background_file;
67 std::vector<std::string> lines;
71 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
73 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
75 throw std::runtime_error("File isn't a supertux-text file");
77 if(!text_lisp->get("text", text))
78 throw std::runtime_error("file doesn't contain a text field");
79 if(!text_lisp->get("background", background_file))
80 throw std::runtime_error("file doesn't contain a background file");
81 if(text_lisp->get("speed", defaultspeed))
83 } catch(std::exception& e) {
84 std::cerr << "Couldn't load file '" << filename << "': " << e.what() <<
89 // Split text string lines into a vector
90 split_text(text, lines);
92 // load background image
94 = new Surface("images/background/" + background_file, false);
98 float left_border = 50;
100 DrawingContext context;
101 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
103 Uint32 lastticks = SDL_GetTicks();
105 main_controller->update();
106 /* in case of input, exit */
108 while(SDL_PollEvent(&event)) {
109 main_controller->process_event(event);
110 if(event.type == SDL_QUIT)
111 throw std::runtime_error("received window close");
114 if(main_controller->hold(Controller::UP)) {
115 speed = -defaultspeed*5;
116 } else if(main_controller->hold(Controller::DOWN)) {
117 speed = defaultspeed*5;
119 speed = defaultspeed;
121 if(main_controller->pressed(Controller::JUMP)
122 || main_controller->pressed(Controller::ACTION)
123 || main_controller->pressed(Controller::MENU_SELECT))
125 if(main_controller->pressed(Controller::PAUSE_MENU))
128 /* draw the credits */
129 context.draw_surface(background, Vector(0,0), 0);
132 for(size_t i = 0; i < lines.size(); i++) {
133 const std::string& line = lines[i];
134 if(line.size() == 0) {
135 y += normal_font->get_height() + ITEMS_SPACE;
139 const Font* font = 0;
143 case ' ': font = small_font; break;
144 case '\t': font = normal_font; break;
145 case '-': font = heading_font; break;
146 case '*': font = reference_font; break;
147 case '#': font = normal_font; center = false; break;
149 std::cerr << "Warning: text contains an unformated line.\n";
156 context.draw_text(font,
157 line.substr(1, line.size()-1),
158 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT + y - scroll),
159 CENTER_ALLIGN, LAYER_FOREGROUND1);
161 context.draw_text(font,
162 line.substr(1, line.size()-1),
163 Vector(left_border, SCREEN_HEIGHT + y - scroll),
164 LEFT_ALLIGN, LAYER_FOREGROUND1);
167 y += font->get_height() + ITEMS_SPACE;
170 context.do_drawing();
172 if(SCREEN_HEIGHT+y-scroll < 0 && 20+SCREEN_HEIGHT+y-scroll < 0)
175 Uint32 ticks = SDL_GetTicks();
176 scroll += speed * (ticks - lastticks);
184 SDL_EnableKeyRepeat(0, 0); // disables key repeating
188 InfoBox::InfoBox(const std::string& text)
191 split_text(text, lines);
199 InfoBox::draw(DrawingContext& context)
201 const Font* heading_font = white_big_text;
202 const Font* normal_font = white_text;
203 const Font* small_font = white_small_text;
204 const Font* reference_font = blue_text;
211 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
212 Color(150, 180, 200, 125), LAYER_GUI-1);
215 for(size_t i = firstline; i < lines.size(); ++i) {
216 const std::string& line = lines[i];
220 if(line.size() == 0) {
221 y += normal_font->get_height() + ITEMS_SPACE;
225 const Font* font = 0;
229 case ' ': font = small_font; break;
230 case '\t': font = normal_font; break;
231 case '-': font = heading_font; break;
232 case '*': font = reference_font; break;
233 case '#': font = normal_font; center = false; break;
235 std::cerr << "Warning: text contains an unformated line.\n";
242 context.draw_text(font,
243 line.substr(1, line.size()-1),
244 Vector(SCREEN_WIDTH/2, y),
245 CENTER_ALLIGN, LAYER_GUI);
247 context.draw_text(font,
248 line.substr(1, line.size()-1),
250 LEFT_ALLIGN, LAYER_GUI);
253 y += font->get_height() + ITEMS_SPACE;
265 InfoBox::scrolldown()
267 if(firstline < lines.size()-1)