more adjustments to collision detection, tux is correctly placed above badguys again...
[supertux.git] / src / textscroller.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 // 
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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "textscroller.hpp"
23
24 #include <stdexcept>
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"
32 #include "main.hpp"
33 #include "control/joystickkeyboardcontroller.hpp"
34 #include "exceptions.hpp"
35
36 static const float DEFAULT_SPEED = .02;
37 static const float SCROLL = 60;
38 static const float ITEMS_SPACE = 4;
39
40 static void split_text(const std::string& text, std::vector<std::string>& lines)
41 {
42   // Split text string lines into a vector
43   lines.clear();
44   std::string::size_type i, l;
45   i = 0;
46   while(true) {
47     l = text.find("\n", i);
48
49     if(l == std::string::npos) {
50       lines.push_back(text.substr(i, text.size()-i));
51       break;
52     }
53
54     lines.push_back(text.substr(i, l-i));
55     i = l+1;
56   }
57 }
58
59 void display_text_file(const std::string& filename)
60 {
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;
67   
68   std::string text;
69   std::string background_file;
70   std::vector<std::string> lines;
71   std::map<std::string, Surface*> images;
72
73   lisp::Parser parser;
74   try {
75     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
76
77     const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
78     if(!text_lisp)
79       throw std::runtime_error("File isn't a supertux-text file");
80     
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))
86       defaultspeed /= 50;
87   } catch(std::exception& e) {
88     std::cerr << "Couldn't load file '" << filename << "': " << e.what() <<
89       "\n";
90     return;
91   }
92
93   // Split text string lines into a vector
94   split_text(text, lines);
95
96   for(size_t i = 0; i < lines.size(); ++i) {
97     const std::string& line = lines[i];
98     if(line.size() == 0)
99       continue;
100     if(line[0] == '!') {
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)));
104     }
105   }
106
107   // load background image
108   Surface* background = new Surface("images/background/" + background_file);
109
110   bool done = false;
111   float scroll = 0;
112   float left_border = 50;
113
114   DrawingContext context;
115   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
116
117   Uint32 lastticks = SDL_GetTicks();
118   while(!done) {
119     main_controller->update();
120     /* in case of input, exit */
121     SDL_Event event;
122     while(SDL_PollEvent(&event)) {
123       main_controller->process_event(event);
124       if(event.type == SDL_QUIT)
125         throw graceful_shutdown();
126     }
127
128     if(main_controller->hold(Controller::UP)) {
129       speed = -defaultspeed*5;
130     } else if(main_controller->hold(Controller::DOWN)) {
131       speed = defaultspeed*5;
132     } else {
133       speed = defaultspeed;
134     }
135     if(main_controller->pressed(Controller::JUMP)
136        || main_controller->pressed(Controller::ACTION)
137        || main_controller->pressed(Controller::MENU_SELECT))
138       scroll += SCROLL;    
139     if(main_controller->pressed(Controller::PAUSE_MENU))
140       done = true;
141     
142     /* draw the credits */
143     context.draw_surface(background, Vector(0,0), 0);
144
145     float y = 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;
150         continue;
151       }
152       
153       const Font* font = 0;
154       const Surface* image = 0;
155       bool center = true;
156       switch(line[0])
157       {
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;
163         case '!': {
164             std::string imagename = line.substr(1, line.size()-1);
165             image = images[imagename];
166             break;
167         }
168         default:
169           std::cerr << "Warning: text contains an unformated line.\n";
170           font = normal_font;
171           center = false;
172           break;
173       }
174      
175       if(font != 0) {
176         if(center) {
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);
181         } else {
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);
186         }
187         y += font->get_height() + ITEMS_SPACE;
188       }
189       if(image != 0) {
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;
194       }
195     }
196     
197     context.do_drawing();
198     sound_manager->update();
199     
200     if(SCREEN_HEIGHT+y-scroll < 0 && 20+SCREEN_HEIGHT+y-scroll < 0)
201       done = 1;
202     
203     Uint32 ticks = SDL_GetTicks();
204     scroll += speed * (ticks - lastticks);
205     lastticks = ticks;
206     if(scroll < 0)
207       scroll = 0;
208     
209     SDL_Delay(10);
210   }
211
212   for(std::map<std::string, Surface*>::iterator i = images.begin();
213       i != images.end(); ++i)
214     delete i->second;
215
216   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
217   delete background;
218 }
219
220 InfoBox::InfoBox(const std::string& text)
221   : firstline(0)
222 {
223   split_text(text, lines);
224
225   for(size_t i = 0; i < lines.size(); ++i) {
226     if(lines[i].size() == 0)
227       continue;
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)));
231     }
232   }
233
234   try
235   {
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");
239   }
240   catch (std::exception& e)
241   {
242     std::cout << "Could not load scrolling images: " << e.what() << std::endl;
243     arrow_scrollup = 0;
244     arrow_scrolldown = 0;
245   }
246 }
247
248 InfoBox::~InfoBox()
249 {
250   for(std::map<std::string, Surface*>::iterator i = images.begin();
251     i != images.end(); ++i)
252     delete i->second;
253   delete arrow_scrollup;
254   delete arrow_scrolldown;
255 }
256
257 void
258 InfoBox::draw(DrawingContext& context)
259 {
260   const Font* heading_font = white_big_text;
261   const Font* normal_font = white_text;
262   const Font* small_font = white_small_text;
263   const Font* reference_font = blue_text;
264
265   float x1 = 200;
266   float y1 = 100;
267   float width = 400;
268   float height = 200;
269
270   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
271       Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
272
273   float y = y1;
274   for(size_t i = firstline; i < lines.size(); ++i) {
275     const std::string& line = lines[i];
276     if(y >= y1 + height)
277       break;
278
279     if(line.size() == 0) {
280       y += normal_font->get_height() + ITEMS_SPACE;    
281       continue;                                        
282     }
283
284     const Font* font = 0;
285     const Surface* image = 0;
286     bool center = true;
287     switch(line[0])
288     {
289       case ' ': font = small_font; break;
290       case '\t': font = normal_font; break;
291       case '-': font = heading_font; break;
292       case '*': font = reference_font; break;
293       case '#': font = normal_font; center = false; break;
294       case '!': {
295         std::string imagename = line.substr(1, line.size()-1);
296         image = images[imagename];
297         break;
298       }
299       default:
300         std::cerr << "Warning: text contains an unformatted line.\n";
301         font = normal_font;
302         center = false;
303         break;
304     }
305
306     if(image != 0) {
307       context.draw_surface(image,
308       Vector( (SCREEN_WIDTH - image->get_width()) / 2,
309               y), LAYER_GUI);
310       y += image->get_height() + ITEMS_SPACE;
311     } else if(center) {
312       context.draw_text(font,
313           line.substr(1, line.size()-1),
314           Vector(SCREEN_WIDTH/2, y),
315           CENTER_ALLIGN, LAYER_GUI);
316       y += font->get_height() + ITEMS_SPACE;
317     } else {
318       context.draw_text(font,
319           line.substr(1, line.size()-1),
320           Vector(x1, y),
321           LEFT_ALLIGN, LAYER_GUI);
322       y += font->get_height() + ITEMS_SPACE;
323     }
324
325     // draw the scrolling arrows
326     if (arrow_scrollup && firstline > 0)
327       context.draw_surface(arrow_scrollup,
328       Vector( x1 + width  - arrow_scrollup->get_width(),  // top-right corner of box
329               y1), LAYER_GUI);
330
331     if (arrow_scrolldown && firstline < lines.size()-1)
332       context.draw_surface(arrow_scrolldown,
333       Vector( x1 + width  - arrow_scrolldown->get_width(),  // bottom-light corner of box
334               y1 + height - arrow_scrolldown->get_height()),
335               LAYER_GUI);
336   }
337 }
338
339 void
340 InfoBox::scrollup()
341 {
342   if(firstline > 0)
343     firstline--;
344 }
345
346 void
347 InfoBox::scrolldown()
348 {
349   if(firstline < lines.size()-1)
350     firstline++;
351 }
352
353 void
354 InfoBox::pageup()
355 {
356 }
357
358 void
359 InfoBox::pagedown()
360 {
361 }
362