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)));
105 // load background image
106 Surface* background = new Surface("images/background/" + background_file);
110 float left_border = 50;
112 DrawingContext context;
113 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
115 Uint32 lastticks = SDL_GetTicks();
117 main_controller->update();
118 /* in case of input, exit */
120 while(SDL_PollEvent(&event)) {
121 main_controller->process_event(event);
122 if(event.type == SDL_QUIT)
123 throw std::runtime_error("received window close");
126 if(main_controller->hold(Controller::UP)) {
127 speed = -defaultspeed*5;
128 } else if(main_controller->hold(Controller::DOWN)) {
129 speed = defaultspeed*5;
131 speed = defaultspeed;
133 if(main_controller->pressed(Controller::JUMP)
134 || main_controller->pressed(Controller::ACTION)
135 || main_controller->pressed(Controller::MENU_SELECT))
137 if(main_controller->pressed(Controller::PAUSE_MENU))
140 /* draw the credits */
141 context.draw_surface(background, Vector(0,0), 0);
144 for(size_t i = 0; i < lines.size(); i++) {
145 const std::string& line = lines[i];
146 if(line.size() == 0) {
147 y += normal_font->get_height() + ITEMS_SPACE;
151 const Font* font = 0;
152 const Surface* image = 0;
156 case ' ': font = small_font; break;
157 case '\t': font = normal_font; break;
158 case '-': font = heading_font; break;
159 case '*': font = reference_font; break;
160 case '#': font = normal_font; center = false; break;
162 std::string imagename = line.substr(1, line.size()-1);
163 image = images[imagename];
167 std::cerr << "Warning: text contains an unformated line.\n";
175 context.draw_text(font,
176 line.substr(1, line.size()-1),
177 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT + y - scroll),
178 CENTER_ALLIGN, LAYER_FOREGROUND1);
180 context.draw_text(font,
181 line.substr(1, line.size()-1),
182 Vector(left_border, SCREEN_HEIGHT + y - scroll),
183 LEFT_ALLIGN, LAYER_FOREGROUND1);
185 y += font->get_height() + ITEMS_SPACE;
188 context.draw_surface(image,
189 Vector( (SCREEN_WIDTH - image->get_width()) / 2,
190 SCREEN_HEIGHT + y - scroll), 255);
191 y += image->get_height() + ITEMS_SPACE;
195 context.do_drawing();
196 sound_manager->update();
198 if(SCREEN_HEIGHT+y-scroll < 0 && 20+SCREEN_HEIGHT+y-scroll < 0)
201 Uint32 ticks = SDL_GetTicks();
202 scroll += speed * (ticks - lastticks);
210 for(std::map<std::string, Surface*>::iterator i = images.begin();
211 i != images.end(); ++i)
214 SDL_EnableKeyRepeat(0, 0); // disables key repeating
218 InfoBox::InfoBox(const std::string& text)
221 split_text(text, lines);
229 InfoBox::draw(DrawingContext& context)
231 const Font* heading_font = white_big_text;
232 const Font* normal_font = white_text;
233 const Font* small_font = white_small_text;
234 const Font* reference_font = blue_text;
241 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
242 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
245 for(size_t i = firstline; i < lines.size(); ++i) {
246 const std::string& line = lines[i];
250 if(line.size() == 0) {
251 y += normal_font->get_height() + ITEMS_SPACE;
255 const Font* font = 0;
259 case ' ': font = small_font; break;
260 case '\t': font = normal_font; break;
261 case '-': font = heading_font; break;
262 case '*': font = reference_font; break;
263 case '#': font = normal_font; center = false; break;
265 std::cerr << "Warning: text contains an unformated line.\n";
272 context.draw_text(font,
273 line.substr(1, line.size()-1),
274 Vector(SCREEN_WIDTH/2, y),
275 CENTER_ALLIGN, LAYER_GUI);
277 context.draw_text(font,
278 line.substr(1, line.size()-1),
280 LEFT_ALLIGN, LAYER_GUI);
283 y += font->get_height() + ITEMS_SPACE;
295 InfoBox::scrolldown()
297 if(firstline < lines.size()-1)