TODO update
[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_INC   0.01
226 #define SCROLL      60
227 #define ITEMS_SPACE 4
228
229 void display_text_file(const std::string& file, const std::string& surface, float scroll_speed)
230 {
231   Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
232   display_text_file(file, sur, scroll_speed);
233   delete sur;
234 }
235
236 void display_text_file(const std::string& file, Surface* surface, float scroll_speed)
237 {
238   int done;
239   float scroll;
240   float speed;
241   int y;
242   int length;
243   FILE* fi;
244   char temp[1024];
245   string_list_type names;
246   char filename[1024];
247   string_list_init(&names);
248   sprintf(filename,"%s/%s", datadir.c_str(), file.c_str());
249   if((fi = fopen(filename,"r")) != NULL)
250     {
251       while(fgets(temp, sizeof(temp), fi) != NULL)
252         {
253           temp[strlen(temp)-1]='\0';
254           string_list_add_item(&names,temp);
255         }
256       fclose(fi);
257     }
258   else
259     {
260       string_list_add_item(&names,"File was not found!");
261       string_list_add_item(&names,filename);
262       string_list_add_item(&names,"Shame on the guy, who");
263       string_list_add_item(&names,"forgot to include it");
264       string_list_add_item(&names,"in your SuperTux distribution.");
265     }
266
267
268   scroll = 0;
269   speed = scroll_speed / 50;
270   done = 0;
271
272   length = names.num_items;
273
274   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
275
276   Uint32 lastticks = SDL_GetTicks();
277   while(done == 0)
278     {
279       /* in case of input, exit */
280       SDL_Event event;
281       while(SDL_PollEvent(&event))
282         switch(event.type)
283           {
284           case SDL_KEYDOWN:
285             switch(event.key.keysym.sym)
286               {
287               case SDLK_UP:
288                 speed -= SPEED_INC;
289                 break;
290               case SDLK_DOWN:
291                 speed += SPEED_INC;
292                 break;
293               case SDLK_SPACE:
294               case SDLK_RETURN:
295                 if(speed >= 0)
296                   scroll += SCROLL;
297                 break;
298               case SDLK_ESCAPE:
299                 done = 1;
300                 break;
301               default:
302                 break;
303               }
304             break;
305           case SDL_QUIT:
306             done = 1;
307             break;
308           default:
309             break;
310           }
311
312       if(speed > MAX_VEL)
313         speed = MAX_VEL;
314       else if(speed < -MAX_VEL)
315         speed = -MAX_VEL;
316
317       /* draw the credits */
318       surface->draw_bg();
319
320       y = 0;
321       for(int i = 0; i < length; i++)
322         {
323         switch(names.item[i][0])
324           {
325           case ' ':
326             white_small_text->drawf(names.item[i]+1, 0, screen->h+y-int(scroll),
327                 A_HMIDDLE, A_TOP, 1);
328             y += white_small_text->h+ITEMS_SPACE;
329             break;
330           case '        ':
331             white_text->drawf(names.item[i]+1, 0, screen->h+y-int(scroll),
332                 A_HMIDDLE, A_TOP, 1);
333             y += white_text->h+ITEMS_SPACE;
334             break;
335           case '-':
336             white_big_text->drawf(names.item[i]+1, 0, screen->h+y-int(scroll),
337                 A_HMIDDLE, A_TOP, 3);
338             y += white_big_text->h+ITEMS_SPACE;
339             break;
340           default:
341             blue_text->drawf(names.item[i], 0, screen->h+y-int(scroll),
342                 A_HMIDDLE, A_TOP, 1);
343             y += blue_text->h+ITEMS_SPACE;
344             break;
345           }
346         }
347
348       flipscreen();
349
350       if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
351         done = 1;
352
353       Uint32 ticks = SDL_GetTicks();
354       scroll += speed * (ticks - lastticks);
355       lastticks = ticks;
356       if(scroll < 0)
357         scroll = 0;
358
359       SDL_Delay(10);
360     }
361   string_list_free(&names);
362
363   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
364   Menu::set_current(main_menu);
365 }
366