Added an alpha argument for drawing fonts.
[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_text_width(const std::string& text) const
75 {
76   /** Let's calculate the size of the biggest paragraph */
77   int l, hl;
78   hl = 0; l = -1;
79   while(true)
80     {
81     l = text.find("\n", l+1);
82     if(l == (int)std::string::npos)
83       break;
84     if(hl < l)
85       hl = l;
86     }
87   if(hl == 0)
88     hl = text.size();
89
90   return hl * w;
91 }
92
93 float
94 Font::get_text_height(const std::string& text) const
95 {
96   /** Let's calculate height of the text */
97   int l, hh;
98   hh = h; l = -1;
99   while(true)
100     {
101     l = text.find("\n", l+1);
102     if(l == (int)std::string::npos)
103       break;
104     hh += h + 2;
105     }
106
107   return hh;
108 }
109
110 float
111 Font::get_height() const
112 {
113   return h;
114 }
115
116 void
117 Font::draw(const std::string& text, const Vector& pos, Uint32 drawing_effect, int alpha)
118 {
119   if(shadowsize > 0)
120     draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize),
121                drawing_effect, alpha);
122
123   draw_chars(chars, text, pos, drawing_effect, alpha);
124 }
125
126 void
127 Font::draw_center(const std::string& text, const Vector& pos, Uint32 drawing_effect, int alpha)
128 {
129   /* Cut lines changes into seperate strings, needed to support centering text
130      with break lines.
131      Feel free to replace this hack with a more elegant solution
132   */
133   char temp[1024];
134   unsigned int i, l, y;
135   i = y = 0;
136   while(true)
137     {
138     l = text.find("\n", i);
139     if(l == std::string::npos)
140       {
141       temp[text.copy(temp, text.size() - i, i)] = '\0';
142       draw(temp, Vector(screen->w/2 - get_text_width(temp)/2 + pos.x, pos.y + y),
143            drawing_effect, alpha);
144       break;
145       }
146     temp[text.copy(temp, l - i, i)] = '\0';
147     draw(temp, Vector(screen->w/2 - get_text_width(temp)/2 + pos.x, pos.y + y),
148          drawing_effect, alpha);
149
150     i = l+1;
151     y += h + 2;
152     }
153 }
154
155 void
156 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
157                  Uint32 drawing_effect, int alpha)
158 {
159   SurfaceImpl* impl = pchars->impl;
160
161   Vector p = pos;
162   for(size_t i = 0; i < text.size(); ++i)
163   {
164     int c = (unsigned char) text[i];
165     if(c > 127) // correct for the 32 controlchars at 128-159
166       c -= 32;
167     // a non-printable character?
168     if(c == '\n') {
169       p.x = pos.x;
170       p.y += h + 2;
171       continue;
172     }
173     if(c == ' ' || c < first_char || c > last_char) {
174       p.x += w;
175       continue;
176     }
177     
178     int index = c - first_char;
179     int source_x = (index % 16) * w;
180     int source_y = (index / 16) * h;
181
182     impl->draw_part(source_x, source_y, p.x, p.y, w, h, alpha, drawing_effect);
183     p.x += w;
184   }
185 }
186
187 /* --- SCROLL TEXT FUNCTION --- */
188
189 #define MAX_VEL     10
190 #define SPEED_INC   0.01
191 #define SCROLL      60
192 #define ITEMS_SPACE 4
193
194 void SuperTux::display_text_file(const std::string& file, float scroll_speed, Font* heading_font, Font* normal_font, Font* small_font, Font* reference_font )
195 {
196   std::string text;
197   std::vector<std::string> names;
198
199   LispReader* reader = LispReader::load(datadir + "/" + file, "supertux-text");
200
201   if(!reader)
202     {
203     std::cerr << "Error: Could not open text. Ignoring...\n";
204     return;
205     }
206
207   reader->read_string("text", text, true);
208   std::string background_file;
209   reader->read_string("background", background_file, true);
210   delete reader;
211
212   // Split text string lines into a vector
213   names.clear();
214   unsigned int i, l;
215   i = 0;
216   while(true)
217     {
218     l = text.find("\n", i);
219
220     if(l == std::string::npos)
221       {
222       char temp[1024];
223       temp[text.copy(temp, text.size() - i, i)] = '\0';
224       names.push_back(temp);
225       break;
226       }
227
228     char temp[1024];
229     temp[text.copy(temp, l-i, i)] = '\0';
230     names.push_back(temp);
231
232     i = l+1;
233     }
234
235   // load background image
236   Surface* background = new Surface(datadir + "/images/background/" + background_file, false);
237
238   int done = 0;
239   float scroll = 0;
240   float speed = scroll_speed / 50;
241
242   DrawingContext context;
243   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
244
245   Uint32 lastticks = SDL_GetTicks();
246   while(!done)
247     {
248       /* in case of input, exit */
249       SDL_Event event;
250       while(SDL_PollEvent(&event))
251         switch(event.type)
252           {
253           case SDL_KEYDOWN:
254             switch(event.key.keysym.sym)
255               {
256               case SDLK_UP:
257                 speed -= SPEED_INC;
258                 break;
259               case SDLK_DOWN:
260                 speed += SPEED_INC;
261                 break;
262               case SDLK_SPACE:
263               case SDLK_RETURN:
264                 if(speed >= 0)
265                   scroll += SCROLL;
266                 break;
267               case SDLK_ESCAPE:
268                 done = 1;
269                 break;
270               default:
271                 break;
272               }
273             break;
274           case SDL_QUIT:
275             done = 1;
276             break;
277           default:
278             break;
279           }
280
281       if(speed > MAX_VEL)
282         speed = MAX_VEL;
283       else if(speed < -MAX_VEL)
284         speed = -MAX_VEL;
285
286       /* draw the credits */
287       context.draw_surface(background, Vector(0,0), 0);
288
289       float y = 0;
290       for(size_t i = 0; i < names.size(); i++) {
291         if(names[i].size() == 0) {
292           y += normal_font->get_height() + ITEMS_SPACE;
293           continue;
294         }
295
296         Font* font = 0;
297         switch(names[i][0])
298         {
299           case ' ': font = small_font; break;
300           case '\t': font = normal_font; break;
301           case '-': font = heading_font; break;
302           case '*': font = reference_font; break;
303           default: font = reference_font; break;
304         }
305
306         context.draw_text_center(font,
307             names[i].substr(1, names[i].size()-1),
308             Vector(0, screen->h + y - scroll), LAYER_FOREGROUND1);
309         y += font->get_height() + ITEMS_SPACE;
310       }
311
312       context.do_drawing();
313
314       if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
315         done = 1;
316
317       Uint32 ticks = SDL_GetTicks();
318       scroll += speed * (ticks - lastticks);
319       lastticks = ticks;
320       if(scroll < 0)
321         scroll = 0;
322
323       SDL_Delay(10);
324     }
325
326   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
327   delete background;
328 }
329