4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
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.
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.
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
28 Text::Text(const std::string& file, int kind_, int w_, int h_)
44 else if(kind == TEXT_NUM)
55 chars = new Surface(file, USE_ALPHA);
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)
63 Uint32 *p = (Uint32 *)conv->pixels + i;
64 *p = *p & conv->format->Amask;
66 SDL_UnlockSurface(conv);
67 SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
68 shadow_chars = new Surface(conv, USE_ALPHA);
70 SDL_FreeSurface(conv);
74 Text::draw(const char* text, int x, int y, int shadowsize, int update)
79 draw_chars(shadow_chars, text,x+shadowsize,y+shadowsize, update);
81 draw_chars(chars, text,x,y, update);
86 Text::draw_chars(Surface* pchars,const char* text, int x, int y, int update)
96 for( i = 0, j = 0; i < len; ++i,++j)
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')
113 else if(kind == TEXT_NUM)
115 for( i = 0, j = 0; i < len; ++i, ++j)
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')
129 Text::draw_align(const char* text, int x, int y,
130 TextHAlign halign, TextVAlign valign, int shadowsize, int update)
137 x += -(strlen(text)*w);
140 x += -((strlen(text)*w)/2);
161 draw(text, x, y, shadowsize, update);
166 Text::drawf(const char* text, int x, int y,
167 TextHAlign halign, TextVAlign valign, int shadowsize, int update)
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);
176 if(valign == A_BOTTOM)
178 else if(valign == A_VMIDDLE)
179 y += screen->h/2 - h/2;
181 draw(text,x,y,shadowsize, update);
187 if(kind == TEXT_TEXT)
189 else if(kind == TEXT_NUM)
193 /* --- ERASE TEXT: --- */
196 Text::erasetext(const char * text, int x, int y, Surface * ptexture, int update, int shadowsize)
202 dest.w = strlen(text) * w + shadowsize;
205 if (dest.w > screen->w)
208 ptexture->draw_part(dest.x,dest.y,dest.x,dest.y,dest.w,dest.h, 255, update);
210 if (update == UPDATE)
211 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
215 /* --- ERASE CENTERED TEXT: --- */
218 Text::erasecenteredtext(const char * text, int y, Surface * ptexture, int update, int shadowsize)
220 erasetext(text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);
224 /* --- SCROLL TEXT FUNCTION --- */
229 #define ITEMS_SPACE 4
231 void display_text_file(char *file, char* surface)
233 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
234 display_text_file(file, sur);
238 void display_text_file(char *file, Surface* surface)
246 string_list_type names;
248 string_list_init(&names);
249 sprintf(filename,"%s/%s", datadir.c_str(), file);
250 if((fi = fopen(filename,"r")) != NULL)
252 while(fgets(temp, sizeof(temp), fi) != NULL)
254 temp[strlen(temp)-1]='\0';
255 string_list_add_item(&names,temp);
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.");
273 length = names.num_items;
275 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
279 /* in case of input, exit */
281 while(SDL_PollEvent(&event))
285 switch(event.key.keysym.sym)
314 else if(speed < -MAX_VEL)
317 /* draw the credits */
321 for(int i = 0; i < length; i++)
323 switch(names.item[i][0])
326 white_small_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
327 y += white_small_text->h+ITEMS_SPACE;
330 white_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
331 y += white_text->h+ITEMS_SPACE;
334 white_big_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 3);
335 y += white_big_text->h+ITEMS_SPACE;
338 blue_text->drawf(names.item[i], 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
339 y += blue_text->h+ITEMS_SPACE;
346 if(60+screen->h+y-scroll < 0 && 20+60+screen->h+y-scroll < 0)
355 string_list_free(&names);
357 SDL_EnableKeyRepeat(0, 0); // disables key repeating
358 Menu::set_current(main_menu);