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)
31 else if(kind == TEXT_NUM)
45 texture_load(&ptext->chars, file, USE_ALPHA);
47 /* 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)
82 w = ptext->w; h = ptext->h;
84 if(ptext->kind == TEXT_TEXT)
86 for( i = 0; i < len; ++i)
88 if( text[i] >= 'A' && text[i] <= 'Z')
89 texture_draw_part(pchars, (int)(text[i] - 'A')*w, 0, x+(i*w), y, ptext->w, ptext->h, update);
90 else if( text[i] >= 'a' && text[i] <= 'z')
91 texture_draw_part(pchars, (int)(text[i] - 'a')*w, h, x+(i*w), y, ptext->w, ptext->h, update);
92 else if ( text[i] >= '!' && text[i] <= '9')
93 texture_draw_part(pchars, (int)(text[i] - '!')*w, h*2, x+(i*w), y, ptext->w, ptext->h, update);
94 else if ( text[i] == '?')
95 texture_draw_part(pchars, 25*w, h*2, x+(i*w), y, ptext->w, ptext->h, update);
96 else if ( text[i] == '\n')
102 else if(ptext->kind == TEXT_NUM)
104 for( i = 0; i < len; ++i)
106 if ( text[i] >= '0' && text[i] <= '9')
107 texture_draw_part(pchars, (int)(text[i] - '0')*w, 0, x+(i*w), y, w, h, update);
108 else if ( text[i] == '\n')
116 void text_drawf(text_type* ptext, char* text, int x, int y, int halign, int valign, int shadowsize, int update)
120 if(halign == A_RIGHT)
121 x += screen->w - (strlen(text)*ptext->w);
122 else if(halign == A_HMIDDLE)
123 x += screen->w/2 - ((strlen(text)*ptext->w)/2);
125 if(valign == A_BOTTOM)
126 y += screen->h - ptext->h;
127 else if(valign == A_VMIDDLE)
128 y += screen->h/2 - ptext->h/2;
130 text_draw(ptext,text,x,y,shadowsize,update);
134 void text_free(text_type* ptext)
137 if(ptext->kind == TEXT_TEXT)
138 texture_free(&ptext->chars);
139 else if(ptext->kind == TEXT_NUM)
140 texture_free(&ptext->chars);
143 /* --- ERASE TEXT: --- */
145 void erasetext(text_type* ptext, char * text, int x, int y, texture_type * ptexture, int update, int shadowsize)
152 dest.w = strlen(text) * ptext->w + shadowsize;
155 if (dest.w > screen->w)
158 texture_draw_part(ptexture,dest.x,dest.y,dest.x,dest.y,dest.w,dest.h,update);
160 if (update == UPDATE)
161 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
165 /* --- ERASE CENTERED TEXT: --- */
167 void erasecenteredtext(text_type* ptext, char * text, int y, texture_type * ptexture, int update, int shadowsize)
169 erasetext(ptext, text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);