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;
69 std::map<std::string, Surface*> images;
73 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
75 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
77 throw std::runtime_error("File isn't a supertux-text file");
79 if(!text_lisp->get("text", text))
80 throw std::runtime_error("file doesn't contain a text field");
81 if(!text_lisp->get("background", background_file))
82 throw std::runtime_error("file doesn't contain a background file");
83 if(text_lisp->get("speed", defaultspeed))
85 } catch(std::exception& e) {
86 std::cerr << "Couldn't load file '" << filename << "': " << e.what() <<
91 // Split text string lines into a vector
92 split_text(text, lines);
94 for(size_t i = 0; i < lines.size(); ++i) {
95 const std::string& line = lines[i];
99 std::string imagename = line.substr(1, line.size()-1);
100 std::cout << "Imagename: " << imagename << "\n";
101 images.insert(std::make_pair(imagename, new Surface(imagename, true)));
105 // load background image
107 = new Surface("images/background/" + background_file, false);
111 float left_border = 50;
113 DrawingContext context;
114 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
116 Uint32 lastticks = SDL_GetTicks();
118 main_controller->update();
119 /* in case of input, exit */
121 while(SDL_PollEvent(&event)) {
122 main_controller->process_event(event);
123 if(event.type == SDL_QUIT)
124 throw std::runtime_error("received window close");
127 if(main_controller->hold(Controller::UP)) {
128 speed = -defaultspeed*5;
129 } else if(main_controller->hold(Controller::DOWN)) {
130 speed = defaultspeed*5;
132 speed = defaultspeed;
134 if(main_controller->pressed(Controller::JUMP)
135 || main_controller->pressed(Controller::ACTION)
136 || main_controller->pressed(Controller::MENU_SELECT))
138 if(main_controller->pressed(Controller::PAUSE_MENU))
141 /* draw the credits */
142 context.draw_surface(background, Vector(0,0), 0);
145 for(size_t i = 0; i < lines.size(); i++) {
146 const std::string& line = lines[i];
147 if(line.size() == 0) {
148 y += normal_font->get_height() + ITEMS_SPACE;
152 const Font* font = 0;
153 const Surface* image = 0;
157 case ' ': font = small_font; break;
158 case '\t': font = normal_font; break;
159 case '-': font = heading_font; break;
160 case '*': font = reference_font; break;
161 case '#': font = normal_font; center = false; break;
163 std::string imagename = line.substr(1, line.size()-1);
164 image = images[imagename];
168 std::cerr << "Warning: text contains an unformated line.\n";
176 context.draw_text(font,
177 line.substr(1, line.size()-1),
178 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT + y - scroll),
179 CENTER_ALLIGN, LAYER_FOREGROUND1);
181 context.draw_text(font,
182 line.substr(1, line.size()-1),
183 Vector(left_border, SCREEN_HEIGHT + y - scroll),
184 LEFT_ALLIGN, LAYER_FOREGROUND1);
186 y += font->get_height() + ITEMS_SPACE;
189 context.draw_surface(image,
190 Vector( (SCREEN_WIDTH - image->w) / 2,
191 SCREEN_HEIGHT + y - scroll), 255);
192 y += image->h + ITEMS_SPACE;
196 context.do_drawing();
197 sound_manager->update();
199 if(SCREEN_HEIGHT+y-scroll < 0 && 20+SCREEN_HEIGHT+y-scroll < 0)
202 Uint32 ticks = SDL_GetTicks();
203 scroll += speed * (ticks - lastticks);
211 for(std::map<std::string, Surface*>::iterator i = images.begin();
212 i != images.end(); ++i)
215 SDL_EnableKeyRepeat(0, 0); // disables key repeating
219 InfoBox::InfoBox(const std::string& text)
222 split_text(text, lines);
230 InfoBox::draw(DrawingContext& context)
232 const Font* heading_font = white_big_text;
233 const Font* normal_font = white_text;
234 const Font* small_font = white_small_text;
235 const Font* reference_font = blue_text;
242 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
243 Color(150, 180, 200, 125), LAYER_GUI-1);
246 for(size_t i = firstline; i < lines.size(); ++i) {
247 const std::string& line = lines[i];
251 if(line.size() == 0) {
252 y += normal_font->get_height() + ITEMS_SPACE;
256 const Font* font = 0;
260 case ' ': font = small_font; break;
261 case '\t': font = normal_font; break;
262 case '-': font = heading_font; break;
263 case '*': font = reference_font; break;
264 case '#': font = normal_font; center = false; break;
266 std::cerr << "Warning: text contains an unformated line.\n";
273 context.draw_text(font,
274 line.substr(1, line.size()-1),
275 Vector(SCREEN_WIDTH/2, y),
276 CENTER_ALLIGN, LAYER_GUI);
278 context.draw_text(font,
279 line.substr(1, line.size()-1),
281 LEFT_ALLIGN, LAYER_GUI);
284 y += font->get_height() + ITEMS_SPACE;
296 InfoBox::scrolldown()
298 if(firstline < lines.size()-1)