Added a new SoundManager based on OpenAL. I also simplified the API along the
[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.h"
23
24 #include <stdexcept>
25 #include "resources.h"
26 #include "video/font.h"
27 #include "video/drawing_context.h"
28 #include "lisp/parser.h"
29 #include "lisp/lisp.h"
30 #include "audio/sound_manager.h"
31 #include "main.h"
32 #include "control/joystickkeyboardcontroller.h"
33
34 static const float DEFAULT_SPEED = .02;
35 static const float SCROLL = 60;
36 static const float ITEMS_SPACE = 4;
37
38 static void split_text(const std::string& text, std::vector<std::string>& lines)
39 {
40   // Split text string lines into a vector
41   lines.clear();
42   std::string::size_type i, l;
43   i = 0;
44   while(true) {
45     l = text.find("\n", i);
46
47     if(l == std::string::npos) {
48       lines.push_back(text.substr(i, text.size()-i));
49       break;
50     }
51
52     lines.push_back(text.substr(i, l-i));
53     i = l+1;
54   }
55 }
56
57 void display_text_file(const std::string& filename)
58 {
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;
65   
66   std::string text;
67   std::string background_file;
68   std::vector<std::string> lines;
69
70   lisp::Parser parser;
71   try {
72     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
73
74     const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
75     if(!text_lisp)
76       throw std::runtime_error("File isn't a supertux-text file");
77     
78     if(!text_lisp->get("text", text))
79       throw std::runtime_error("file doesn't contain a text field");
80     if(!text_lisp->get("background", background_file))
81       throw std::runtime_error("file doesn't contain a background file");
82     if(text_lisp->get("speed", defaultspeed))
83       defaultspeed /= 50;
84   } catch(std::exception& e) {
85     std::cerr << "Couldn't load file '" << filename << "': " << e.what() <<
86       "\n";
87     return;
88   }
89
90   // Split text string lines into a vector
91   split_text(text, lines);
92
93   // load background image
94   Surface* background 
95     = new Surface("images/background/" + background_file, false);
96
97   bool done = false;
98   float scroll = 0;
99   float left_border = 50;
100
101   DrawingContext context;
102   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
103
104   Uint32 lastticks = SDL_GetTicks();
105   while(!done) {
106     main_controller->update();
107     /* in case of input, exit */
108     SDL_Event event;
109     while(SDL_PollEvent(&event)) {
110       main_controller->process_event(event);
111       if(event.type == SDL_QUIT)
112         throw std::runtime_error("received window close");
113     }
114
115     if(main_controller->hold(Controller::UP)) {
116       speed = -defaultspeed*5;
117     } else if(main_controller->hold(Controller::DOWN)) {
118       speed = defaultspeed*5;
119     } else {
120       speed = defaultspeed;
121     }
122     if(main_controller->pressed(Controller::JUMP)
123        || main_controller->pressed(Controller::ACTION)
124        || main_controller->pressed(Controller::MENU_SELECT))
125       scroll += SCROLL;    
126     if(main_controller->pressed(Controller::PAUSE_MENU))
127       done = true;
128     
129     /* draw the credits */
130     context.draw_surface(background, Vector(0,0), 0);
131
132     float y = 0;
133     for(size_t i = 0; i < lines.size(); i++) {
134       const std::string& line = lines[i];
135       if(line.size() == 0) {
136         y += normal_font->get_height() + ITEMS_SPACE;
137         continue;
138       }
139       
140       const Font* font = 0;
141       bool center = true;
142       switch(line[0])
143       {
144         case ' ': font = small_font; break;
145         case '\t': font = normal_font; break;
146         case '-': font = heading_font; break;
147         case '*': font = reference_font; break;
148         case '#': font = normal_font; center = false; break;
149         default:
150           std::cerr << "Warning: text contains an unformated line.\n";
151           font = normal_font;
152           center = false;
153           break;
154       }
155       
156       if(center) {
157         context.draw_text(font,
158                           line.substr(1, line.size()-1),
159                           Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT + y - scroll),
160                           CENTER_ALLIGN, LAYER_FOREGROUND1);
161       } else {
162         context.draw_text(font,
163                           line.substr(1, line.size()-1),
164                           Vector(left_border, SCREEN_HEIGHT + y - scroll),
165                           LEFT_ALLIGN, LAYER_FOREGROUND1);
166       }
167       
168       y += font->get_height() + ITEMS_SPACE;
169     }
170     
171     context.do_drawing();
172     sound_manager->update();
173     
174     if(SCREEN_HEIGHT+y-scroll < 0 && 20+SCREEN_HEIGHT+y-scroll < 0)
175       done = 1;
176     
177     Uint32 ticks = SDL_GetTicks();
178     scroll += speed * (ticks - lastticks);
179     lastticks = ticks;
180     if(scroll < 0)
181       scroll = 0;
182     
183     SDL_Delay(10);
184   }
185   
186   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
187   delete background;
188 }
189
190 InfoBox::InfoBox(const std::string& text)
191   : firstline(0)
192 {
193   split_text(text, lines);
194 }
195
196 InfoBox::~InfoBox()
197 {
198 }
199
200 void
201 InfoBox::draw(DrawingContext& context)
202 {
203   const Font* heading_font = white_big_text;
204   const Font* normal_font = white_text;
205   const Font* small_font = white_small_text;
206   const Font* reference_font = blue_text;
207   
208   float x1 = 200;
209   float y1 = 100;
210   float width = 400;
211   float height = 200;
212   
213   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
214       Color(150, 180, 200, 125), LAYER_GUI-1);
215
216   float y = y1;
217   for(size_t i = firstline; i < lines.size(); ++i) {
218     const std::string& line = lines[i];
219     if(y >= y1 + height)
220       break;
221
222     if(line.size() == 0) {
223       y += normal_font->get_height() + ITEMS_SPACE;    
224       continue;                                        
225     }
226
227     const Font* font = 0;
228     bool center = true;
229     switch(line[0])
230     {
231       case ' ': font = small_font; break;
232       case '\t': font = normal_font; break;
233       case '-': font = heading_font; break;
234       case '*': font = reference_font; break;
235       case '#': font = normal_font; center = false; break;
236       default:
237         std::cerr << "Warning: text contains an unformated line.\n";
238         font = normal_font;
239         center = false;
240         break;
241     }
242     
243     if(center) {
244       context.draw_text(font,
245           line.substr(1, line.size()-1),
246           Vector(SCREEN_WIDTH/2, y),
247           CENTER_ALLIGN, LAYER_GUI);
248     } else {
249       context.draw_text(font,
250           line.substr(1, line.size()-1),
251           Vector(x1, y),
252           LEFT_ALLIGN, LAYER_GUI);
253     }
254       
255     y += font->get_height() + ITEMS_SPACE;
256   }
257 }
258
259 void
260 InfoBox::scrollup()
261 {
262   if(firstline > 0)
263     firstline--;
264 }
265
266 void
267 InfoBox::scrolldown()
268 {
269   if(firstline < lines.size()-1)
270     firstline++;
271 }
272
273 void
274 InfoBox::pageup()
275 {
276 }
277
278 void
279 InfoBox::pagedown()
280 {
281 }
282