<MatzeB> fixes some remaining memory leaks
[supertux.git] / src / text.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 <stdlib.h>
22 #include <string.h>
23 #include "globals.h"
24 #include "defines.h"
25 #include "screen.h"
26 #include "text.h"
27
28 Text::Text(const std::string& file, int kind_, int w_, int h_)
29 {
30   kind = kind_;
31   w = w_;
32   h = h_;
33
34   int mx, my;
35   SDL_Surface *conv;
36   int pixels;
37   int i;
38   
39   if(kind == TEXT_TEXT)
40     {
41       mx = 26;
42       my = 3;
43     }
44   else if(kind == TEXT_NUM)
45     {
46       mx = 10;
47       my = 1;
48     }
49   else
50     {
51       mx = 0;
52       my = 0;
53     }
54
55   chars = new Surface(file, USE_ALPHA);
56
57   // Load shadow font.
58   conv = SDL_DisplayFormatAlpha(chars->impl->get_sdl_surface());
59   pixels = conv->w * conv->h;
60   SDL_LockSurface(conv);
61   for(i = 0; i < pixels; ++i)
62     {
63       Uint32 *p = (Uint32 *)conv->pixels + i;
64       *p = *p & conv->format->Amask;
65     }
66   SDL_UnlockSurface(conv);
67   SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
68   shadow_chars = new Surface(conv, USE_ALPHA);
69
70   SDL_FreeSurface(conv);
71 }
72
73 Text::~Text()
74 {
75   delete chars;
76   delete shadow_chars;
77 }
78
79 void
80 Text::draw(const  char* text, int x, int y, int shadowsize, int update)
81 {
82   if(text != NULL)
83     {
84       if(shadowsize != 0)
85         draw_chars(shadow_chars, text,x+shadowsize,y+shadowsize, update);
86
87       draw_chars(chars, text,x,y, update);
88     }
89 }
90
91 void
92 Text::draw_chars(Surface* pchars,const  char* text, int x, int y, int update)
93 {
94   int i,j,len;
95
96   len = strlen(text);
97   int w = this->w;
98   int h = this->h;
99
100   if(kind == TEXT_TEXT)
101     {
102       for( i = 0, j = 0; i < len; ++i,++j)
103         {
104           if( text[i] >= 'A' && text[i] <= 'Z')
105             pchars->draw_part((int)(text[i] - 'A')*w, 0, x+(j*w), y, w, h, 255,  update);
106           else if( text[i] >= 'a' && text[i] <= 'z')
107             pchars->draw_part((int)(text[i] - 'a')*w, h, x+(j*w), y, w, h, 255,  update);
108           else if ( text[i] >= '!' && text[i] <= '9')
109             pchars->draw_part((int)(text[i] - '!')*w, h*2, x+(j*w), y, w, h, 255,  update);
110           else if ( text[i] == '?')
111             pchars->draw_part(25*w, h*2, x+(j*w), y, w, h, 255,  update);
112           else if ( text[i] == '\n')
113             {
114               y += h + 2;
115               j = 0;
116             }
117         }
118     }
119   else if(kind == TEXT_NUM)
120     {
121       for( i = 0, j = 0; i < len; ++i, ++j)
122         {
123           if ( text[i] >= '0' && text[i] <= '9')
124             pchars->draw_part((int)(text[i] - '0')*w, 0, x+(j*w), y, w, h, 255, update);
125           else if ( text[i] == '\n')
126             {
127               y += h + 2;
128               j = 0;
129             }
130         }
131     }
132 }
133
134 void
135 Text::draw_align(const char* text, int x, int y,
136                       TextHAlign halign, TextVAlign valign, int shadowsize, int update)
137 {
138   if(text != NULL)
139     {
140       switch (halign)
141         {
142         case A_RIGHT:
143           x += -(strlen(text)*w);
144           break;
145         case A_HMIDDLE:
146           x += -((strlen(text)*w)/2);
147           break;
148         case A_LEFT:
149           // default
150           break;
151         }
152
153       switch (valign)
154         {
155         case A_BOTTOM:
156           y -= h;
157           break;
158           
159         case A_VMIDDLE:
160           y -= h/2;
161
162         case A_TOP:
163           // default
164           break;
165         }
166
167       draw(text, x, y, shadowsize, update);
168     }
169 }
170
171 void
172 Text::drawf(const  char* text, int x, int y,
173                  TextHAlign halign, TextVAlign valign, int shadowsize, int update)
174 {
175   if(text != NULL)
176     {
177       if(halign == A_RIGHT)  /* FIXME: this doesn't work correctly for strings with newlines.*/
178         x += screen->w - (strlen(text)*w);
179       else if(halign == A_HMIDDLE)
180         x += screen->w/2 - ((strlen(text)*w)/2);
181
182       if(valign == A_BOTTOM)
183         y += screen->h - h;
184       else if(valign == A_VMIDDLE)
185         y += screen->h/2 - h/2;
186
187       draw(text,x,y,shadowsize, update);
188     }
189 }
190
191 /* --- ERASE TEXT: --- */
192
193 void
194 Text::erasetext(const  char * text, int x, int y, Surface * ptexture, int update, int shadowsize)
195 {
196   SDL_Rect dest;
197
198   dest.x = x;
199   dest.y = y;
200   dest.w = strlen(text) * w + shadowsize;
201   dest.h = h;
202
203   if (dest.w > screen->w)
204     dest.w = screen->w;
205
206   ptexture->draw_part(dest.x,dest.y,dest.x,dest.y,dest.w,dest.h, 255, update);
207
208   if (update == UPDATE)
209     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
210 }
211
212
213 /* --- ERASE CENTERED TEXT: --- */
214
215 void
216 Text::erasecenteredtext(const  char * text, int y, Surface * ptexture, int update, int shadowsize)
217 {
218   erasetext(text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);
219 }
220
221
222 /* --- SCROLL TEXT FUNCTION --- */
223
224 #define MAX_VEL 10
225 #define SPEED   1
226 #define SCROLL  60
227 #define ITEMS_SPACE 4
228
229 void display_text_file(const std::string& file, const std::string& surface)
230 {
231   Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
232   display_text_file(file, sur);
233   delete sur;
234 }
235
236 void display_text_file(const std::string& file, Surface* surface)
237 {
238   int done;
239   int scroll, speed;
240   int y;
241   int length;
242   FILE* fi;
243   char temp[1024];
244   string_list_type names;
245   char filename[1024];
246   string_list_init(&names);
247   sprintf(filename,"%s/%s", datadir.c_str(), file.c_str());
248   if((fi = fopen(filename,"r")) != NULL)
249     {
250       while(fgets(temp, sizeof(temp), fi) != NULL)
251         {
252           temp[strlen(temp)-1]='\0';
253           string_list_add_item(&names,temp);
254         }
255       fclose(fi);
256     }
257   else
258     {
259       string_list_add_item(&names,"File was not found!");
260       string_list_add_item(&names,filename);
261       string_list_add_item(&names,"Shame on the guy, who");
262       string_list_add_item(&names,"forgot to include it");
263       string_list_add_item(&names,"in your SuperTux distribution.");
264     }
265
266
267   scroll = 0;
268   speed = 2;
269   done = 0;
270
271   length = names.num_items;
272
273   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
274
275   while(done == 0)
276     {
277       /* in case of input, exit */
278       SDL_Event event;
279       while(SDL_PollEvent(&event))
280         switch(event.type)
281           {
282           case SDL_KEYDOWN:
283             switch(event.key.keysym.sym)
284               {
285               case SDLK_UP:
286                 speed -= SPEED;
287                 break;
288               case SDLK_DOWN:
289                 speed += SPEED;
290                 break;
291               case SDLK_SPACE:
292               case SDLK_RETURN:
293                 if(speed >= 0)
294                   scroll += SCROLL;
295                 break;
296               case SDLK_ESCAPE:
297                 done = 1;
298                 break;
299               default:
300                 break;
301               }
302             break;
303           case SDL_QUIT:
304             done = 1;
305             break;
306           default:
307             break;
308           }
309
310       if(speed > MAX_VEL)
311         speed = MAX_VEL;
312       else if(speed < -MAX_VEL)
313         speed = -MAX_VEL;
314
315       /* draw the credits */
316       surface->draw_bg();
317
318       y = 0;
319       for(int i = 0; i < length; i++)
320         {
321         switch(names.item[i][0])
322           {
323           case ' ':
324             white_small_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
325             y += white_small_text->h+ITEMS_SPACE;
326             break;
327           case '        ':
328             white_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
329             y += white_text->h+ITEMS_SPACE;
330             break;
331           case '-':
332             white_big_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 3);
333             y += white_big_text->h+ITEMS_SPACE;
334             break;
335           default:
336             blue_text->drawf(names.item[i], 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
337             y += blue_text->h+ITEMS_SPACE;
338             break;
339           }
340         }
341
342       flipscreen();
343
344       if(60+screen->h+y-scroll < 0 && 20+60+screen->h+y-scroll < 0)
345         done = 1;
346
347       scroll += speed;
348       if(scroll < 0)
349         scroll = 0;
350
351       SDL_Delay(35);
352     }
353   string_list_free(&names);
354
355   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
356   Menu::set_current(main_menu);
357 }
358