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 void text_load(text_type* ptext, const std::string& file, int kind, int w, int h)
32 else if(kind == TEXT_NUM)
46 ptext->chars = new Surface(file, USE_ALPHA);
48 /* Load shadow font. */
49 conv = SDL_DisplayFormatAlpha(ptext->chars->impl->sdl_surface);
50 pixels = conv->w * conv->h;
51 SDL_LockSurface(conv);
52 for(i = 0; i < pixels; ++i)
54 Uint32 *p = (Uint32 *)conv->pixels + i;
55 *p = *p & conv->format->Amask;
57 SDL_UnlockSurface(conv);
58 SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
59 ptext->shadow_chars = new Surface(conv, USE_ALPHA);
61 SDL_FreeSurface(conv);
64 void text_draw(text_type* ptext,const char* text, int x, int y, int shadowsize, int update)
69 text_draw_chars(ptext, ptext->shadow_chars, text,x+shadowsize,y+shadowsize, update);
71 text_draw_chars(ptext, ptext->chars, text,x,y, update);
75 void text_draw_chars(text_type* ptext, Surface* pchars,const char* text, int x, int y, int update)
84 if(ptext->kind == TEXT_TEXT)
86 for( i = 0, j = 0; i < len; ++i,++j)
88 if( text[i] >= 'A' && text[i] <= 'Z')
89 pchars->draw_part((int)(text[i] - 'A')*w, 0, x+(j*w), y, ptext->w, ptext->h, 255, update);
90 else if( text[i] >= 'a' && text[i] <= 'z')
91 pchars->draw_part((int)(text[i] - 'a')*w, h, x+(j*w), y, ptext->w, ptext->h, 255, update);
92 else if ( text[i] >= '!' && text[i] <= '9')
93 pchars->draw_part((int)(text[i] - '!')*w, h*2, x+(j*w), y, ptext->w, ptext->h, 255, update);
94 else if ( text[i] == '?')
95 pchars->draw_part(25*w, h*2, x+(j*w), y, ptext->w, ptext->h, 255, update);
96 else if ( text[i] == '\n')
103 else if(ptext->kind == TEXT_NUM)
105 for( i = 0, j = 0; i < len; ++i, ++j)
107 if ( text[i] >= '0' && text[i] <= '9')
108 pchars->draw_part((int)(text[i] - '0')*w, 0, x+(j*w), y, w, h, 255, update);
109 else if ( text[i] == '\n')
118 void text_draw_align(text_type* ptext, const char* text, int x, int y,
119 TextHAlign halign, TextVAlign valign, int shadowsize, int update)
126 x += -(strlen(text)*ptext->w);
129 x += -((strlen(text)*ptext->w)/2);
150 text_draw(ptext, text, x, y, shadowsize, update);
154 void text_drawf(text_type* ptext,const char* text, int x, int y, TextHAlign halign, TextVAlign valign, int shadowsize, int update)
158 if(halign == A_RIGHT) /* FIXME: this doesn't work correctly for strings with newlines.*/
159 x += screen->w - (strlen(text)*ptext->w);
160 else if(halign == A_HMIDDLE)
161 x += screen->w/2 - ((strlen(text)*ptext->w)/2);
163 if(valign == A_BOTTOM)
164 y += screen->h - ptext->h;
165 else if(valign == A_VMIDDLE)
166 y += screen->h/2 - ptext->h/2;
168 text_draw(ptext,text,x,y,shadowsize, update);
172 void text_free(text_type* ptext)
174 if(ptext->kind == TEXT_TEXT)
176 else if(ptext->kind == TEXT_NUM)
180 /* --- ERASE TEXT: --- */
182 void erasetext(text_type* ptext,const char * text, int x, int y, Surface * ptexture, int update, int shadowsize)
189 dest.w = strlen(text) * ptext->w + shadowsize;
192 if (dest.w > screen->w)
195 ptexture->draw_part(dest.x,dest.y,dest.x,dest.y,dest.w,dest.h, 255, update);
197 if (update == UPDATE)
198 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
202 /* --- ERASE CENTERED TEXT: --- */
204 void erasecenteredtext(text_type* ptext,const char * text, int y, Surface * ptexture, int update, int shadowsize)
206 erasetext(ptext, text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);