Added a "graceful exit" exception to be called when the user asks to prematurely...
[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
225 InfoBox::~InfoBox()
226 {
227 }
228
229 void
230 InfoBox::draw(DrawingContext& context)
231 {
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;
236   
237   float x1 = 200;
238   float y1 = 100;
239   float width = 400;
240   float height = 200;
241   
242   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
243       Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
244
245   float y = y1;
246   for(size_t i = firstline; i < lines.size(); ++i) {
247     const std::string& line = lines[i];
248     if(y >= y1 + height)
249       break;
250
251     if(line.size() == 0) {
252       y += normal_font->get_height() + ITEMS_SPACE;    
253       continue;                                        
254     }
255
256     const Font* font = 0;
257     bool center = true;
258     switch(line[0])
259     {
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;
265       default:
266         std::cerr << "Warning: text contains an unformated line.\n";
267         font = normal_font;
268         center = false;
269         break;
270     }
271     
272     if(center) {
273       context.draw_text(font,
274           line.substr(1, line.size()-1),
275           Vector(SCREEN_WIDTH/2, y),
276           CENTER_ALLIGN, LAYER_GUI);
277     } else {
278       context.draw_text(font,
279           line.substr(1, line.size()-1),
280           Vector(x1, y),
281           LEFT_ALLIGN, LAYER_GUI);
282     }
283       
284     y += font->get_height() + ITEMS_SPACE;
285   }
286 }
287
288 void
289 InfoBox::scrollup()
290 {
291   if(firstline > 0)
292     firstline--;
293 }
294
295 void
296 InfoBox::scrolldown()
297 {
298   if(firstline < lines.size()-1)
299     firstline++;
300 }
301
302 void
303 InfoBox::pageup()
304 {
305 }
306
307 void
308 InfoBox::pagedown()
309 {
310 }
311