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);
80 Text::draw(const char* text, int x, int y, int shadowsize, int update)
85 draw_chars(shadow_chars, text,x+shadowsize,y+shadowsize, update);
87 draw_chars(chars, text,x,y, update);
92 Text::draw_chars(Surface* pchars,const char* text, int x, int y, int update)
100 if(kind == TEXT_TEXT)
102 for( i = 0, j = 0; i < len; ++i,++j)
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')
119 else if(kind == TEXT_NUM)
121 for( i = 0, j = 0; i < len; ++i, ++j)
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')
135 Text::draw_align(const char* text, int x, int y,
136 TextHAlign halign, TextVAlign valign, int shadowsize, int update)
143 x += -(strlen(text)*w);
146 x += -((strlen(text)*w)/2);
167 draw(text, x, y, shadowsize, update);
172 Text::drawf(const char* text, int x, int y,
173 TextHAlign halign, TextVAlign valign, int shadowsize, int update)
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);
182 if(valign == A_BOTTOM)
184 else if(valign == A_VMIDDLE)
185 y += screen->h/2 - h/2;
187 draw(text,x,y,shadowsize, update);
191 /* --- ERASE TEXT: --- */
194 Text::erasetext(const char * text, int x, int y, Surface * ptexture, int update, int shadowsize)
200 dest.w = strlen(text) * w + shadowsize;
203 if (dest.w > screen->w)
206 ptexture->draw_part(dest.x,dest.y,dest.x,dest.y,dest.w,dest.h, 255, update);
208 if (update == UPDATE)
209 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
213 /* --- ERASE CENTERED TEXT: --- */
216 Text::erasecenteredtext(const char * text, int y, Surface * ptexture, int update, int shadowsize)
218 erasetext(text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);
222 /* --- SCROLL TEXT FUNCTION --- */
225 #define SPEED_INC 0.01
227 #define ITEMS_SPACE 4
229 void display_text_file(const std::string& file, const std::string& surface, float scroll_speed)
231 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
232 display_text_file(file, sur, scroll_speed);
236 void display_text_file(const std::string& file, Surface* surface, float scroll_speed)
245 string_list_type names;
247 string_list_init(&names);
248 sprintf(filename,"%s/%s", datadir.c_str(), file.c_str());
249 if((fi = fopen(filename,"r")) != NULL)
251 while(fgets(temp, sizeof(temp), fi) != NULL)
253 temp[strlen(temp)-1]='\0';
254 string_list_add_item(&names,temp);
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.");
269 speed = scroll_speed / 50;
272 length = names.num_items;
274 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
276 Uint32 lastticks = SDL_GetTicks();
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, screen->h+y-int(scroll),
327 A_HMIDDLE, A_TOP, 1);
328 y += white_small_text->h+ITEMS_SPACE;
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;
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;
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;
350 if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
353 Uint32 ticks = SDL_GetTicks();
354 scroll += speed * (ticks - lastticks);
361 string_list_free(&names);
363 SDL_EnableKeyRepeat(0, 0); // disables key repeating
364 Menu::set_current(main_menu);