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