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, char* file, int kind, int w, int h)
33 else if(kind == TEXT_NUM)
47 texture_load(&ptext->chars, file, USE_ALPHA);
49 /* Load shadow font. */
50 conv = SDL_DisplayFormatAlpha(ptext->chars.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 texture_from_sdl_surface(&ptext->shadow_chars,conv,USE_ALPHA);
62 SDL_FreeSurface(conv);
65 void text_draw(text_type* ptext, char* text, int x, int y, int shadowsize, int update)
70 text_draw_chars(ptext,&ptext->shadow_chars, text,x+shadowsize,y+shadowsize, update);
72 text_draw_chars(ptext,&ptext->chars, text,x,y, update);
76 void text_draw_chars(text_type* ptext, texture_type* pchars, char* text, int x, int y, int update)
85 if(ptext->kind == TEXT_TEXT)
87 for( i = 0, j = 0; i < len; ++i,++j)
89 if( text[i] >= 'A' && text[i] <= 'Z')
90 texture_draw_part(pchars, (int)(text[i] - 'A')*w, 0, x+(j*w), y, ptext->w, ptext->h, update);
91 else if( text[i] >= 'a' && text[i] <= 'z')
92 texture_draw_part(pchars, (int)(text[i] - 'a')*w, h, x+(j*w), y, ptext->w, ptext->h, update);
93 else if ( text[i] >= '!' && text[i] <= '9')
94 texture_draw_part(pchars, (int)(text[i] - '!')*w, h*2, x+(j*w), y, ptext->w, ptext->h, update);
95 else if ( text[i] == '?')
96 texture_draw_part(pchars, 25*w, h*2, x+(j*w), y, ptext->w, ptext->h, update);
97 else if ( text[i] == '\n')
104 else if(ptext->kind == TEXT_NUM)
106 for( i = 0, j = 0; i < len; ++i, ++j)
108 if ( text[i] >= '0' && text[i] <= '9')
109 texture_draw_part(pchars, (int)(text[i] - '0')*w, 0, x+(j*w), y, w, h, update);
110 else if ( text[i] == '\n')
119 void text_drawf(text_type* ptext, char* text, int x, int y, int halign, int valign, int shadowsize, int update)
123 if(halign == A_RIGHT) /* FIXME: this doesn't work correctly for strings with newlines.*/
124 x += screen->w - (strlen(text)*ptext->w);
125 else if(halign == A_HMIDDLE)
126 x += screen->w/2 - ((strlen(text)*ptext->w)/2);
128 if(valign == A_BOTTOM)
129 y += screen->h - ptext->h;
130 else if(valign == A_VMIDDLE)
131 y += screen->h/2 - ptext->h/2;
133 text_draw(ptext,text,x,y,shadowsize,update);
137 void text_free(text_type* ptext)
140 if(ptext->kind == TEXT_TEXT)
141 texture_free(&ptext->chars);
142 else if(ptext->kind == TEXT_NUM)
143 texture_free(&ptext->chars);
146 /* --- ERASE TEXT: --- */
148 void erasetext(text_type* ptext, char * text, int x, int y, texture_type * ptexture, int update, int shadowsize)
155 dest.w = strlen(text) * ptext->w + shadowsize;
158 if (dest.w > screen->w)
161 texture_draw_part(ptexture,dest.x,dest.y,dest.x,dest.y,dest.w,dest.h,update);
163 if (update == UPDATE)
164 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
168 /* --- ERASE CENTERED TEXT: --- */
170 void erasecenteredtext(text_type* ptext, char * text, int y, texture_type * ptexture, int update, int shadowsize)
172 erasetext(ptext, text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);