* Added image support to InfoBlocks (useful e.g. to show a picture of the power-up...
[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 "lisp/parser.hpp"
29 #include "lisp/lisp.hpp"
30 #include "audio/sound_manager.hpp"
31 #include "main.hpp"
32 #include "control/joystickkeyboardcontroller.hpp"
33 #include "exceptions.hpp"
34
35 static const float DEFAULT_SPEED = .02;
36 static const float SCROLL = 60;
37 static const float ITEMS_SPACE = 4;
38
39 static void split_text(const std::string& text, std::vector<std::string>& lines)
40 {
41   // Split text string lines into a vector
42   lines.clear();
43   std::string::size_type i, l;
44   i = 0;
45   while(true) {
46     l = text.find("\n", i);
47
48     if(l == std::string::npos) {
49       lines.push_back(text.substr(i, text.size()-i));
50       break;
51     }
52
53     lines.push_back(text.substr(i, l-i));
54     i = l+1;
55   }
56 }
57
58 void display_text_file(const std::string& filename)
59 {
60   const Font* heading_font = white_big_text;
61   const Font* normal_font = white_text;
62   const Font* small_font = white_small_text;
63   const Font* reference_font = blue_text;
64   float defaultspeed = DEFAULT_SPEED;
65   float speed = defaultspeed;
66   
67   std::string text;
68   std::string background_file;
69   std::vector<std::string> lines;
70   std::map<std::string, Surface*> images;
71
72   lisp::Parser parser;
73   try {
74     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
75
76     const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
77     if(!text_lisp)
78       throw std::runtime_error("File isn't a supertux-text file");
79     
80     if(!text_lisp->get("text", text))
81       throw std::runtime_error("file doesn't contain a text field");
82     if(!text_lisp->get("background", background_file))
83       throw std::runtime_error("file doesn't contain a background file");
84     if(text_lisp->get("speed", defaultspeed))
85       defaultspeed /= 50;
86   } catch(std::exception& e) {
87     std::cerr << "Couldn't load file '" << filename << "': " << e.what() <<
88       "\n";
89     return;
90   }
91
92   // Split text string lines into a vector
93   split_text(text, lines);
94
95   for(size_t i = 0; i < lines.size(); ++i) {
96     const std::string& line = lines[i];
97     if(line.size() == 0)
98       continue;
99     if(line[0] == '!') {
100       std::string imagename = line.substr(1, line.size()-1);
101       std::cout << "Imagename: " << imagename << "\n";
102       images.insert(std::make_pair(imagename, new Surface(imagename)));
103     }
104   }
105
106   // load background image
107   Surface* background = new Surface("images/background/" + background_file);
108
109   bool done = false;
110   float scroll = 0;
111   float left_border = 50;
112
113   DrawingContext context;
114   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
115
116   Uint32 lastticks = SDL_GetTicks();
117   while(!done) {
118     main_controller->update();
119     /* in case of input, exit */
120     SDL_Event event;
121     while(SDL_PollEvent(&event)) {
122       main_controller->process_event(event);
123       if(event.type == SDL_QUIT)
124         throw graceful_shutdown();
125     }
126
127     if(main_controller->hold(Controller::UP)) {
128       speed = -defaultspeed*5;
129     } else if(main_controller->hold(Controller::DOWN)) {
130       speed = defaultspeed*5;
131     } else {
132       speed = defaultspeed;
133     }
134     if(main_controller->pressed(Controller::JUMP)
135        || main_controller->pressed(Controller::ACTION)
136        || main_controller->pressed(Controller::MENU_SELECT))
137       scroll += SCROLL;    
138     if(main_controller->pressed(Controller::PAUSE_MENU))
139       done = true;
140     
141     /* draw the credits */
142     context.draw_surface(background, Vector(0,0), 0);
143
144     float y = 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;
149         continue;
150       }
151       
152       const Font* font = 0;
153       const Surface* image = 0;
154       bool center = true;
155       switch(line[0])
156       {
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;
162         case '!': {
163             std::string imagename = line.substr(1, line.size()-1);
164             image = images[imagename];
165             break;
166         }
167         default:
168           std::cerr << "Warning: text contains an unformated line.\n";
169           font = normal_font;
170           center = false;
171           break;
172       }
173      
174       if(font != 0) {
175         if(center) {
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);
180         } else {
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);
185         }
186         y += font->get_height() + ITEMS_SPACE;
187       }
188       if(image != 0) {
189         context.draw_surface(image,
190             Vector( (SCREEN_WIDTH - image->get_width()) / 2,
191                     SCREEN_HEIGHT + y - scroll), 255);
192         y += image->get_height() + ITEMS_SPACE;
193       }
194     }
195     
196     context.do_drawing();
197     sound_manager->update();
198     
199     if(SCREEN_HEIGHT+y-scroll < 0 && 20+SCREEN_HEIGHT+y-scroll < 0)
200       done = 1;
201     
202     Uint32 ticks = SDL_GetTicks();
203     scroll += speed * (ticks - lastticks);
204     lastticks = ticks;
205     if(scroll < 0)
206       scroll = 0;
207     
208     SDL_Delay(10);
209   }
210
211   for(std::map<std::string, Surface*>::iterator i = images.begin();
212       i != images.end(); ++i)
213     delete i->second;
214
215   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
216   delete background;
217 }
218
219 InfoBox::InfoBox(const std::string& text)
220   : firstline(0)
221 {
222   split_text(text, lines);
223   
224   for(size_t i = 0; i < lines.size(); ++i) {
225     if(lines[i].size() == 0)
226       continue;
227     if(lines[i][0] == '!') {
228       std::string imagename = lines[i].substr(1, lines[i].size()-1);
229       images.insert(std::make_pair(imagename, new Surface(imagename)));
230     }
231   }
232 }
233
234 InfoBox::~InfoBox()
235 {
236   for(std::map<std::string, Surface*>::iterator i = images.begin();
237     i != images.end(); ++i)
238     delete i->second;
239 }
240
241 void
242 InfoBox::draw(DrawingContext& context)
243 {
244   const Font* heading_font = white_big_text;
245   const Font* normal_font = white_text;
246   const Font* small_font = white_small_text;
247   const Font* reference_font = blue_text;
248   
249   float x1 = 200;
250   float y1 = 100;
251   float width = 400;
252   float height = 200;
253   
254   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
255       Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
256
257   float y = y1;
258   for(size_t i = firstline; i < lines.size(); ++i) {
259     const std::string& line = lines[i];
260     if(y >= y1 + height)
261       break;
262
263     if(line.size() == 0) {
264       y += normal_font->get_height() + ITEMS_SPACE;    
265       continue;                                        
266     }
267
268     const Font* font = 0;
269     const Surface* image = 0;
270     bool center = true;
271     switch(line[0])
272     {
273       case ' ': font = small_font; break;
274       case '\t': font = normal_font; break;
275       case '-': font = heading_font; break;
276       case '*': font = reference_font; break;
277       case '#': font = normal_font; center = false; break;
278       case '!': {
279         std::string imagename = line.substr(1, line.size()-1);
280         image = images[imagename];
281         break;
282       }
283       default:
284         std::cerr << "Warning: text contains an unformatted line.\n";
285         font = normal_font;
286         center = false;
287         break;
288     }
289     
290     if(image != 0) {
291       context.draw_surface(image,
292       Vector( (SCREEN_WIDTH - image->get_width()) / 2,
293               y), LAYER_GUI);
294       y += image->get_height() + ITEMS_SPACE;
295     } else if(center) {
296       context.draw_text(font,
297           line.substr(1, line.size()-1),
298           Vector(SCREEN_WIDTH/2, y),
299           CENTER_ALLIGN, LAYER_GUI);
300       y += font->get_height() + ITEMS_SPACE;
301     } else {
302       context.draw_text(font,
303           line.substr(1, line.size()-1),
304           Vector(x1, y),
305           LEFT_ALLIGN, LAYER_GUI);
306       y += font->get_height() + ITEMS_SPACE;
307     }   
308   }
309 }
310
311 void
312 InfoBox::scrollup()
313 {
314   if(firstline > 0)
315     firstline--;
316 }
317
318 void
319 InfoBox::scrolldown()
320 {
321   if(firstline < lines.size()-1)
322     firstline++;
323 }
324
325 void
326 InfoBox::pageup()
327 {
328 }
329
330 void
331 InfoBox::pagedown()
332 {
333 }
334