Had to change the #includes of dependend headers from "dir/header.h" to "../dir...
[supertux.git] / lib / video / font.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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
21 #include <cstdlib>
22 #include <cstring>
23
24 #include "../app/globals.h"
25 #include "../video/screen.h"
26 #include "../video/font.h"
27 #include "../video/drawing_context.h"
28 #include "../utils/lispreader.h"
29
30 using namespace SuperTux;
31
32 Font::Font(const std::string& file, FontType ntype, int nw, int nh,
33         int nshadowsize)
34     : chars(0), shadow_chars(0), type(ntype), w(nw), h(nh),
35       shadowsize(nshadowsize)
36 {
37   chars = new Surface(file, true);
38  
39   switch(type) {
40     case TEXT:
41       first_char = 32;
42       break;
43     case NUM:
44       first_char = 48;
45       break;
46   }
47   last_char = first_char + (chars->h / h) * 16;
48   if(last_char > 127) // we have left out some control chars at 128-159
49     last_char += 32;
50    
51   // Load shadow font.
52   if(shadowsize > 0) {
53     SDL_Surface* conv = SDL_DisplayFormatAlpha(chars->impl->get_sdl_surface());
54     int pixels = conv->w * conv->h;
55     SDL_LockSurface(conv);
56     for(int i = 0; i < pixels; ++i) {
57       Uint32 *p = (Uint32 *)conv->pixels + i;
58       *p = *p & conv->format->Amask;
59     }
60     SDL_UnlockSurface(conv);
61     SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
62     shadow_chars = new Surface(conv, true);
63     SDL_FreeSurface(conv);
64   }
65 }
66
67 Font::~Font()
68 {
69   delete chars;
70   delete shadow_chars;
71 }
72
73 float
74 Font::get_height() const
75 {
76   return h;
77 }
78
79 float
80 Font::get_text_width(const std::string& text) const
81 {
82   return text.size() * w;
83 }
84
85 void
86 Font::draw(const std::string& text, const Vector& pos, Uint32 drawing_effect)
87 {
88   if(shadowsize > 0)
89     draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize),
90                drawing_effect);
91
92   draw_chars(chars, text, pos, drawing_effect);
93 }
94
95 void
96 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
97                  Uint32 drawing_effect)
98 {
99   SurfaceImpl* impl = pchars->impl;
100
101   Vector p = pos;
102   for(size_t i = 0; i < text.size(); ++i)
103   {
104     int c = (unsigned char) text[i];
105     if(c > 127) // correct for the 32 controlchars at 128-159
106       c -= 32;
107     // a non-printable character?
108     if(c == '\n') {
109       p.x = pos.x;
110       p.y += h + 2;
111       continue;
112     }
113     if(c == ' ' || c < first_char || c > last_char) {
114       p.x += w;
115       continue;
116     }
117     
118     int index = c - first_char;
119     int source_x = (index % 16) * w;
120     int source_y = (index / 16) * h;
121
122     impl->draw_part(source_x, source_y, p.x, p.y, w, h, 255, drawing_effect);
123     p.x += w;
124   }
125 }
126
127 /* --- SCROLL TEXT FUNCTION --- */
128
129 #define MAX_VEL     10
130 #define SPEED_INC   0.01
131 #define SCROLL      60
132 #define ITEMS_SPACE 4
133
134 void SuperTux::display_text_file(const std::string& file, float scroll_speed)
135 {
136   std::string text;
137   std::vector<std::string> names;
138
139   LispReader* reader = LispReader::load(datadir + "/" + file, "supertux-text");
140
141   if(!reader)
142     {
143     std::cerr << "Error: Could not open text. Ignoring...\n";
144     return;
145     }
146
147   reader->read_string("text", text, true);
148   std::string background_file;
149   reader->read_string("background", background_file, true);
150   delete reader;
151
152   // Split text string lines into a vector
153   names.clear();
154   unsigned int i, l;
155   i = 0;
156   while(true)
157     {
158     l = text.find("\n", i);
159
160     if(l == std::string::npos)
161       {
162       char temp[1024];
163       temp[text.copy(temp, text.size() - i, i)] = '\0';
164       names.push_back(temp);
165       break;
166       }
167
168     char temp[1024];
169     temp[text.copy(temp, l-i, i)] = '\0';
170     names.push_back(temp);
171
172     i = l+1;
173     }
174
175   // load background image
176   Surface* background = new Surface(datadir + "/images/background/" + background_file, false);
177
178   int done = 0;
179   float scroll = 0;
180   float speed = scroll_speed / 50;
181
182   DrawingContext context;
183   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
184
185   Uint32 lastticks = SDL_GetTicks();
186   while(!done)
187     {
188       /* in case of input, exit */
189       SDL_Event event;
190       while(SDL_PollEvent(&event))
191         switch(event.type)
192           {
193           case SDL_KEYDOWN:
194             switch(event.key.keysym.sym)
195               {
196               case SDLK_UP:
197                 speed -= SPEED_INC;
198                 break;
199               case SDLK_DOWN:
200                 speed += SPEED_INC;
201                 break;
202               case SDLK_SPACE:
203               case SDLK_RETURN:
204                 if(speed >= 0)
205                   scroll += SCROLL;
206                 break;
207               case SDLK_ESCAPE:
208                 done = 1;
209                 break;
210               default:
211                 break;
212               }
213             break;
214           case SDL_QUIT:
215             done = 1;
216             break;
217           default:
218             break;
219           }
220
221       if(speed > MAX_VEL)
222         speed = MAX_VEL;
223       else if(speed < -MAX_VEL)
224         speed = -MAX_VEL;
225
226       /* draw the credits */
227       context.draw_surface(background, Vector(0,0), 0);
228
229       float y = 0;
230       for(size_t i = 0; i < names.size(); i++) {
231         if(names[i].size() == 0) {
232           y += white_text->get_height() + ITEMS_SPACE;
233           continue;
234         }
235
236         Font* font = 0;
237         switch(names[i][0])
238         {
239           case ' ': font = white_small_text; break;
240           case '\t': font = white_text; break;
241           case '-': font = white_big_text; break;
242           case '*': font = blue_text; break;
243           default: font = blue_text; break;
244         }
245
246         context.draw_text_center(font,
247             names[i].substr(1, names[i].size()-1),
248             Vector(0, screen->h + y - scroll), LAYER_FOREGROUND1);
249         y += font->get_height() + ITEMS_SPACE;
250       }
251
252       context.do_drawing();
253
254       if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
255         done = 1;
256
257       Uint32 ticks = SDL_GetTicks();
258       scroll += speed * (ticks - lastticks);
259       lastticks = ticks;
260       if(scroll < 0)
261         scroll = 0;
262
263       SDL_Delay(10);
264     }
265
266   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
267   delete background;
268 }
269