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->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);