2 // C Implementation: text
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
20 Text::Text(const std::string& file, int kind_, int w_, int h_)
36 else if(kind == TEXT_NUM)
47 chars = new Surface(file, USE_ALPHA);
50 conv = SDL_DisplayFormatAlpha(chars->impl->sdl_surface);
51 pixels = conv->w * conv->h;
52 SDL_LockSurface(conv);
53 for(i = 0; i < pixels; ++i)
55 Uint32 *p = (Uint32 *)conv->pixels + i;
56 *p = *p & conv->format->Amask;
58 SDL_UnlockSurface(conv);
59 SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
60 shadow_chars = new Surface(conv, USE_ALPHA);
62 SDL_FreeSurface(conv);
66 Text::draw(const char* text, int x, int y, int shadowsize, int update)
71 draw_chars(shadow_chars, text,x+shadowsize,y+shadowsize, update);
73 draw_chars(chars, text,x,y, update);
78 Text::draw_chars(Surface* pchars,const char* text, int x, int y, int update)
88 for( i = 0, j = 0; i < len; ++i,++j)
90 if( text[i] >= 'A' && text[i] <= 'Z')
91 pchars->draw_part((int)(text[i] - 'A')*w, 0, x+(j*w), y, w, h, 255, update);
92 else if( text[i] >= 'a' && text[i] <= 'z')
93 pchars->draw_part((int)(text[i] - 'a')*w, h, x+(j*w), y, w, h, 255, update);
94 else if ( text[i] >= '!' && text[i] <= '9')
95 pchars->draw_part((int)(text[i] - '!')*w, h*2, x+(j*w), y, w, h, 255, update);
96 else if ( text[i] == '?')
97 pchars->draw_part(25*w, h*2, x+(j*w), y, w, h, 255, update);
98 else if ( text[i] == '\n')
105 else if(kind == TEXT_NUM)
107 for( i = 0, j = 0; i < len; ++i, ++j)
109 if ( text[i] >= '0' && text[i] <= '9')
110 pchars->draw_part((int)(text[i] - '0')*w, 0, x+(j*w), y, w, h, 255, update);
111 else if ( text[i] == '\n')
121 Text::draw_align(const char* text, int x, int y,
122 TextHAlign halign, TextVAlign valign, int shadowsize, int update)
129 x += -(strlen(text)*w);
132 x += -((strlen(text)*w)/2);
153 draw(text, x, y, shadowsize, update);
158 Text::drawf(const char* text, int x, int y,
159 TextHAlign halign, TextVAlign valign, int shadowsize, int update)
163 if(halign == A_RIGHT) /* FIXME: this doesn't work correctly for strings with newlines.*/
164 x += screen->w - (strlen(text)*w);
165 else if(halign == A_HMIDDLE)
166 x += screen->w/2 - ((strlen(text)*w)/2);
168 if(valign == A_BOTTOM)
170 else if(valign == A_VMIDDLE)
171 y += screen->h/2 - h/2;
173 draw(text,x,y,shadowsize, update);
179 if(kind == TEXT_TEXT)
181 else if(kind == TEXT_NUM)
185 /* --- ERASE TEXT: --- */
188 Text::erasetext(const char * text, int x, int y, Surface * ptexture, int update, int shadowsize)
194 dest.w = strlen(text) * w + shadowsize;
197 if (dest.w > screen->w)
200 ptexture->draw_part(dest.x,dest.y,dest.x,dest.y,dest.w,dest.h, 255, update);
202 if (update == UPDATE)
203 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
207 /* --- ERASE CENTERED TEXT: --- */
210 Text::erasecenteredtext(const char * text, int y, Surface * ptexture, int update, int shadowsize)
212 erasetext(text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);