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)
28 ptext->chars = (texture_type*) malloc(sizeof(texture_type) * 79);
29 ptext->shadow_chars = (texture_type*) malloc(sizeof(texture_type) * 79);
33 else if(kind == TEXT_NUM)
35 ptext->chars = (texture_type*) malloc(sizeof(texture_type) * 10);
36 ptext->shadow_chars = (texture_type*) malloc(sizeof(texture_type) * 10);
43 ptext->shadow_chars = NULL;
51 for(y = 0; y < my ; ++y)
53 for(x = 0; x < mx ; ++x)
55 texture_load_part(&ptext->chars[y*mx+x],file,x*w,y*h,w,h, USE_ALPHA);
59 /* Load shadow font. */
60 for(y = 0; y < my ; ++y)
62 for(x = 0; x < mx ; ++x)
66 conv = SDL_DisplayFormatAlpha(ptext->chars[y*mx+x].sdl_surface);
67 pixels = conv->w * conv->h;
68 SDL_LockSurface(conv);
69 for(i = 0; i < pixels; ++i)
71 Uint32 *p = (Uint32 *)conv->pixels + i;
72 *p = *p & conv->format->Amask;
74 SDL_UnlockSurface(conv);
75 SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
76 texture_from_sdl_surface(&ptext->shadow_chars[y*mx+x],conv,USE_ALPHA);
82 void text_draw(text_type* ptext, char* text, int x, int y, int shadowsize, int update)
87 text_draw_chars(ptext,&ptext->shadow_chars[0], text,x+shadowsize,y+shadowsize, update);
89 text_draw_chars(ptext,ptext->chars, text,x,y, update);
93 void text_draw_chars(text_type* ptext, texture_type* pchars, char* text, int x, int y, int update)
99 if(ptext->kind == TEXT_TEXT)
101 for( i = 0; i < len; ++i)
103 if( text[i] >= 'A' && text[i] <= 'Z')
105 texture_draw(&pchars[(int)(text[i] - 'A')],x+i*ptext->w,y,update);
107 else if( text[i] >= 'a' && text[i] <= 'z')
109 texture_draw(&pchars[(int)(text[i] - 'a') + 26],x+i*ptext->w,y,update);
111 else if ( text[i] >= '!' && text[i] <= '9')
113 texture_draw(&pchars[(int)(text[i] - '!') + 52],x+i*ptext->w,y,update);
115 else if ( text[i] == '?')
117 texture_draw(&pchars[77],x+i*ptext->w,y,update);
119 else if ( text[i] == '\n')
125 else if(ptext->kind == TEXT_NUM)
127 for( i = 0; i < len; ++i)
129 if ( text[i] >= '0' && text[i] <= '9')
131 texture_draw(&pchars[(int)(text[i] - '0')],x+i*ptext->w,y,update);
133 else if ( text[i] == '\n')
141 void text_drawf(text_type* ptext, char* text, int x, int y, int halign, int valign, int shadowsize, int update)
145 if(halign == A_RIGHT)
147 else if(halign == A_HMIDDLE)
148 x += screen->w/2 - ((strlen(text)*ptext->w)/2);
150 if(valign == A_BOTTOM)
151 y += screen->h - ptext->h;
152 else if(valign == A_VMIDDLE)
153 y += screen->h/2 - ptext->h/2;
155 text_draw(ptext,text,x,y,shadowsize,update);
159 void text_free(text_type* ptext)
162 if(ptext->kind == TEXT_TEXT)
164 for( c = 0; c < 78; ++c)
165 texture_free(&ptext->chars[c]);
167 else if(ptext->kind == TEXT_NUM)
169 for( c = 0; c < 10; ++c)
170 texture_free(&ptext->chars[c]);