- fixed miss align in scrolling text
[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 void
74 Text::draw(const  char* text, int x, int y, int shadowsize, int update)
75 {
76   if(text != NULL)
77     {
78       if(shadowsize != 0)
79         draw_chars(shadow_chars, text,x+shadowsize,y+shadowsize, update);
80
81       draw_chars(chars, text,x,y, update);
82     }
83 }
84
85 void
86 Text::draw_chars(Surface* pchars,const  char* text, int x, int y, int update)
87 {
88   int i,j,len;
89
90   len = strlen(text);
91   int w = this->w;
92   int h = this->h;
93
94   if(kind == TEXT_TEXT)
95     {
96       for( i = 0, j = 0; i < len; ++i,++j)
97         {
98           if( text[i] >= 'A' && text[i] <= 'Z')
99             pchars->draw_part((int)(text[i] - 'A')*w, 0, x+(j*w), y, w, h, 255,  update);
100           else if( text[i] >= 'a' && text[i] <= 'z')
101             pchars->draw_part((int)(text[i] - 'a')*w, h, x+(j*w), y, w, h, 255,  update);
102           else if ( text[i] >= '!' && text[i] <= '9')
103             pchars->draw_part((int)(text[i] - '!')*w, h*2, x+(j*w), y, w, h, 255,  update);
104           else if ( text[i] == '?')
105             pchars->draw_part(25*w, h*2, x+(j*w), y, w, h, 255,  update);
106           else if ( text[i] == '\n')
107             {
108               y += h + 2;
109               j = 0;
110             }
111         }
112     }
113   else if(kind == TEXT_NUM)
114     {
115       for( i = 0, j = 0; i < len; ++i, ++j)
116         {
117           if ( text[i] >= '0' && text[i] <= '9')
118             pchars->draw_part((int)(text[i] - '0')*w, 0, x+(j*w), y, w, h, 255, update);
119           else if ( text[i] == '\n')
120             {
121               y += h + 2;
122               j = 0;
123             }
124         }
125     }
126 }
127
128 void
129 Text::draw_align(const char* text, int x, int y,
130                       TextHAlign halign, TextVAlign valign, int shadowsize, int update)
131 {
132   if(text != NULL)
133     {
134       switch (halign)
135         {
136         case A_RIGHT:
137           x += -(strlen(text)*w);
138           break;
139         case A_HMIDDLE:
140           x += -((strlen(text)*w)/2);
141           break;
142         case A_LEFT:
143           // default
144           break;
145         }
146
147       switch (valign)
148         {
149         case A_BOTTOM:
150           y -= h;
151           break;
152           
153         case A_VMIDDLE:
154           y -= h/2;
155
156         case A_TOP:
157           // default
158           break;
159         }
160
161       draw(text, x, y, shadowsize, update);
162     }
163 }
164
165 void
166 Text::drawf(const  char* text, int x, int y,
167                  TextHAlign halign, TextVAlign valign, int shadowsize, int update)
168 {
169   if(text != NULL)
170     {
171       if(halign == A_RIGHT)  /* FIXME: this doesn't work correctly for strings with newlines.*/
172         x += screen->w - (strlen(text)*w);
173       else if(halign == A_HMIDDLE)
174         x += screen->w/2 - ((strlen(text)*w)/2);
175
176       if(valign == A_BOTTOM)
177         y += screen->h - h;
178       else if(valign == A_VMIDDLE)
179         y += screen->h/2 - h/2;
180
181       draw(text,x,y,shadowsize, update);
182     }
183 }
184
185 Text::~Text()
186 {
187   if(kind == TEXT_TEXT)
188     delete chars;
189   else if(kind == TEXT_NUM)
190     delete chars;
191 }
192
193 /* --- ERASE TEXT: --- */
194
195 void
196 Text::erasetext(const  char * text, int x, int y, Surface * ptexture, int update, int shadowsize)
197 {
198   SDL_Rect dest;
199
200   dest.x = x;
201   dest.y = y;
202   dest.w = strlen(text) * w + shadowsize;
203   dest.h = h;
204
205   if (dest.w > screen->w)
206     dest.w = screen->w;
207
208   ptexture->draw_part(dest.x,dest.y,dest.x,dest.y,dest.w,dest.h, 255, update);
209
210   if (update == UPDATE)
211     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
212 }
213
214
215 /* --- ERASE CENTERED TEXT: --- */
216
217 void
218 Text::erasecenteredtext(const  char * text, int y, Surface * ptexture, int update, int shadowsize)
219 {
220   erasetext(text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);
221 }
222
223
224 /* --- SCROLL TEXT FUNCTION --- */
225
226 #define MAX_VEL 10
227 #define SPEED   1
228 #define SCROLL  60
229 #define ITEMS_SPACE 4
230
231 void display_text_file(char *file, char* surface)
232 {
233 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
234 display_text_file(file, sur);
235 delete sur;
236 }
237
238 void display_text_file(char *file, Surface* surface)
239 {
240   int done;
241   int scroll, speed;
242   int y;
243   int length;
244   FILE* fi;
245   char temp[1024];
246   string_list_type names;
247   char filename[1024];
248   string_list_init(&names);
249   sprintf(filename,"%s/%s", datadir.c_str(), file);
250   if((fi = fopen(filename,"r")) != NULL)
251     {
252       while(fgets(temp, sizeof(temp), fi) != NULL)
253         {
254           temp[strlen(temp)-1]='\0';
255           string_list_add_item(&names,temp);
256         }
257       fclose(fi);
258     }
259   else
260     {
261       string_list_add_item(&names,"File was not found!");
262       string_list_add_item(&names,filename);
263       string_list_add_item(&names,"Shame on the guy, who");
264       string_list_add_item(&names,"forgot to include it");
265       string_list_add_item(&names,"in your SuperTux distribution.");
266     }
267
268
269   scroll = 0;
270   speed = 2;
271   done = 0;
272
273   length = names.num_items;
274
275   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
276
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;
289                 break;
290               case SDLK_DOWN:
291                 speed += SPEED;
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
319       surface->draw_bg();
320
321       if (strcmp(file, "CREDITS") == 0)
322         white_big_text->drawf("- SuperTux " VERSION " -", 
323                               0, screen->h-scroll, A_HMIDDLE, A_TOP, 2);
324
325       y = 0;
326       for(int i = 0; i < length; i++)
327         {
328         switch(names.item[i][0])
329           {
330           case ' ':
331             white_small_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
332             y += white_small_text->h+ITEMS_SPACE;
333             break;
334           case '        ':
335             white_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
336             y += white_text->h+ITEMS_SPACE;
337             break;
338           case '-':
339             white_big_text->drawf(names.item[i], 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 3);
340             y += white_big_text->h+ITEMS_SPACE;
341             break;
342           default:
343             blue_text->drawf(names.item[i], 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
344             y += blue_text->h+ITEMS_SPACE;
345             break;
346           }
347         }
348
349       flipscreen();
350
351       if(60+screen->h+y-scroll < 0 && 20+60+screen->h+y-scroll < 0)
352         done = 1;
353
354       scroll += speed;
355       if(scroll < 0)
356         scroll = 0;
357
358       SDL_Delay(35);
359     }
360   string_list_free(&names);
361
362   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
363   Menu::set_current(main_menu);
364 }
365