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] >= ' ' && text[i] <= '/')
105 pchars->draw_part((int)(text[i] - ' ')*w, 0 , x+(j*w), y, w, h, 255, update);
106 else if( text[i] >= '0' && text[i] <= '?')
107 pchars->draw_part((int)(text[i] - '0')*w, h*1, x+(j*w), y, w, h, 255, update);
108 else if ( text[i] >= '@' && text[i] <= 'O')
109 pchars->draw_part((int)(text[i] - '@')*w, h*2, x+(j*w), y, w, h, 255, update);
110 else if ( text[i] >= 'P' && text[i] <= '_')
111 pchars->draw_part((int)(text[i] - 'P')*w, h*3, x+(j*w), y, w, h, 255, update);
112 else if ( text[i] >= '`' && text[i] <= 'o')
113 pchars->draw_part((int)(text[i] - '`')*w, h*4, x+(j*w), y, w, h, 255, update);
114 else if ( text[i] >= 'p' && text[i] <= '~')
115 pchars->draw_part((int)(text[i] - 'p')*w, h*5, x+(j*w), y, w, h, 255, update);
116 else if ( text[i] == '\n')
123 else if(kind == TEXT_NUM)
125 for( i = 0, j = 0; i < len; ++i, ++j)
127 if ( text[i] >= '0' && text[i] <= '9')
128 pchars->draw_part((int)(text[i] - '0')*w, 0, x+(j*w), y, w, h, 255, update);
129 else if ( text[i] == '\n')
139 Text::draw_align(const char* text, int x, int y,
140 TextHAlign halign, TextVAlign valign, int shadowsize, int update)
147 x += -(strlen(text)*w);
150 x += -((strlen(text)*w)/2);
171 draw(text, x, y, shadowsize, update);
176 Text::drawf(const char* text, int x, int y,
177 TextHAlign halign, TextVAlign valign, int shadowsize, int update)
181 if(halign == A_RIGHT) /* FIXME: this doesn't work correctly for strings with newlines.*/
182 x += screen->w - (strlen(text)*w);
183 else if(halign == A_HMIDDLE)
184 x += screen->w/2 - ((strlen(text)*w)/2);
186 if(valign == A_BOTTOM)
188 else if(valign == A_VMIDDLE)
189 y += screen->h/2 - h/2;
191 draw(text,x,y,shadowsize, update);
195 /* --- ERASE TEXT: --- */
198 Text::erasetext(const char * text, int x, int y, Surface * ptexture, int update, int shadowsize)
204 dest.w = strlen(text) * w + shadowsize;
207 if (dest.w > screen->w)
210 ptexture->draw_part(dest.x,dest.y,dest.x,dest.y,dest.w,dest.h, 255, update);
212 if (update == UPDATE)
213 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
217 /* --- ERASE CENTERED TEXT: --- */
220 Text::erasecenteredtext(const char * text, int y, Surface * ptexture, int update, int shadowsize)
222 erasetext(text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);
226 /* --- SCROLL TEXT FUNCTION --- */
229 #define SPEED_INC 0.01
231 #define ITEMS_SPACE 4
233 void display_text_file(const std::string& file, const std::string& surface, float scroll_speed)
235 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
236 display_text_file(file, sur, scroll_speed);
240 void display_text_file(const std::string& file, Surface* surface, float scroll_speed)
249 string_list_type names;
251 string_list_init(&names);
252 sprintf(filename,"%s/%s", datadir.c_str(), file.c_str());
253 if((fi = fopen(filename,"r")) != NULL)
255 while(fgets(temp, sizeof(temp), fi) != NULL)
257 temp[strlen(temp)-1]='\0';
258 string_list_add_item(&names,temp);
264 string_list_add_item(&names,"File was not found!");
265 string_list_add_item(&names,filename);
266 string_list_add_item(&names,"Shame on the guy, who");
267 string_list_add_item(&names,"forgot to include it");
268 string_list_add_item(&names,"in your SuperTux distribution.");
273 speed = scroll_speed / 50;
276 length = names.num_items;
278 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
280 Uint32 lastticks = SDL_GetTicks();
283 /* in case of input, exit */
285 while(SDL_PollEvent(&event))
289 switch(event.key.keysym.sym)
318 else if(speed < -MAX_VEL)
321 /* draw the credits */
325 for(int i = 0; i < length; i++)
327 switch(names.item[i][0])
330 white_small_text->drawf(names.item[i]+1, 0, screen->h+y-int(scroll),
331 A_HMIDDLE, A_TOP, 1);
332 y += white_small_text->h+ITEMS_SPACE;
335 white_text->drawf(names.item[i]+1, 0, screen->h+y-int(scroll),
336 A_HMIDDLE, A_TOP, 1);
337 y += white_text->h+ITEMS_SPACE;
340 white_big_text->drawf(names.item[i]+1, 0, screen->h+y-int(scroll),
341 A_HMIDDLE, A_TOP, 3);
342 y += white_big_text->h+ITEMS_SPACE;
345 blue_text->drawf(names.item[i], 0, screen->h+y-int(scroll),
346 A_HMIDDLE, A_TOP, 1);
347 y += blue_text->h+ITEMS_SPACE;
354 if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
357 Uint32 ticks = SDL_GetTicks();
358 scroll += speed * (ticks - lastticks);
365 string_list_free(&names);
367 SDL_EnableKeyRepeat(0, 0); // disables key repeating
368 Menu::set_current(main_menu);