fixed, subset info
[supertux.git] / src / text.c
1 //
2 // C Implementation: text
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include "globals.h"
16 #include "defines.h"
17 #include "screen.h"
18 #include "text.h"
19
20 void text_load(text_type* ptext, char* file, int kind, int w, int h)
21 {
22   int x, y;
23   int mx, my;
24   SDL_Surface *conv;
25
26   if(kind == TEXT_TEXT)
27     {
28       mx = 26;
29       my = 3;
30     }
31   else if(kind == TEXT_NUM)
32     {
33       mx = 10;
34       my = 1;
35     }
36   else
37     {
38       mx = 0;
39       my = 0;
40     }
41   ptext->kind = kind;
42   ptext->w = w;
43   ptext->h = h;
44
45 texture_load(&ptext->chars, file, USE_ALPHA);
46
47   /* Load shadow font. */
48           int pixels;
49           int i;
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)
54             {
55               Uint32 *p = (Uint32 *)conv->pixels + i;
56               *p = *p & conv->format->Amask;
57             }
58             SDL_UnlockSurface(conv);
59            SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
60           texture_from_sdl_surface(&ptext->shadow_chars,conv,USE_ALPHA);
61
62 SDL_FreeSurface(conv); 
63 }
64
65 void text_draw(text_type* ptext, char* text, int x, int y, int shadowsize, int update)
66 {
67   if(text != NULL)
68     {
69       if(shadowsize != 0)
70         text_draw_chars(ptext,&ptext->shadow_chars, text,x+shadowsize,y+shadowsize, update);
71
72       text_draw_chars(ptext,&ptext->chars, text,x,y, update);
73     }
74 }
75
76 void text_draw_chars(text_type* ptext, texture_type* pchars, char* text, int x, int y, int update)
77 {
78   int i,j,len;
79 int w, h;
80
81   len = strlen(text);
82   w = ptext->w; h = ptext->h;
83
84   if(ptext->kind == TEXT_TEXT)
85     {
86       for( i = 0, j = 0; i < len; ++i,++j)
87         {
88           if( text[i] >= 'A' && text[i] <= 'Z')
89         texture_draw_part(pchars, (int)(text[i] - 'A')*w, 0, x+(j*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+(j*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+(j*w), y, ptext->w, ptext->h, update);
94           else if ( text[i] == '?')
95         texture_draw_part(pchars, 25*w, h*2, x+(j*w), y, ptext->w, ptext->h, update);
96           else if ( text[i] == '\n')
97             {
98               y += ptext->h + 2;
99               j = 0;
100             }
101         }
102     }
103   else if(ptext->kind == TEXT_NUM)
104     {
105       for( i = 0, j = 0; i < len; ++i, ++j)
106         {
107           if ( text[i] >= '0' && text[i] <= '9')
108         texture_draw_part(pchars, (int)(text[i] - '0')*w, 0, x+(j*w), y, w, h, update);
109           else if ( text[i] == '\n')
110             {
111               y += ptext->h + 2;
112               j = 0;
113             }
114         }
115     }
116 }
117
118 void text_drawf(text_type* ptext, char* text, int x, int y, int halign, int valign, int shadowsize, int update)
119 {
120   if(text != NULL)
121     {
122       if(halign == A_RIGHT)  /* FIXME: this doesn't work correctly for strings with newlines.*/
123         x += screen->w - (strlen(text)*ptext->w);
124       else if(halign == A_HMIDDLE)
125         x += screen->w/2 - ((strlen(text)*ptext->w)/2);
126
127       if(valign == A_BOTTOM)
128         y += screen->h - ptext->h;
129       else if(valign == A_VMIDDLE)
130         y += screen->h/2 - ptext->h/2;
131
132       text_draw(ptext,text,x,y,shadowsize,update);
133     }
134 }
135
136 void text_free(text_type* ptext)
137 {
138   int c;
139   if(ptext->kind == TEXT_TEXT)
140         texture_free(&ptext->chars);
141   else if(ptext->kind == TEXT_NUM)
142         texture_free(&ptext->chars);
143 }
144
145 /* --- ERASE TEXT: --- */
146
147 void erasetext(text_type* ptext, char * text, int x, int y, texture_type * ptexture, int update, int shadowsize)
148 {
149   SDL_Rect dest;
150
151
152   dest.x = x;
153   dest.y = y;
154   dest.w = strlen(text) * ptext->w + shadowsize;
155   dest.h = ptext->h;
156
157   if (dest.w > screen->w)
158     dest.w = screen->w;
159
160   texture_draw_part(ptexture,dest.x,dest.y,dest.x,dest.y,dest.w,dest.h,update);
161
162   if (update == UPDATE)
163     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
164 }
165
166
167 /* --- ERASE CENTERED TEXT: --- */
168
169 void erasecenteredtext(text_type* ptext, char * text, int y, texture_type * ptexture, int update, int shadowsize)
170 {
171   erasetext(ptext, text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);
172 }