fixed C99 compilation
[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   int pixels;
26   int i;
27   
28   if(kind == TEXT_TEXT)
29     {
30       mx = 26;
31       my = 3;
32     }
33   else if(kind == TEXT_NUM)
34     {
35       mx = 10;
36       my = 1;
37     }
38   else
39     {
40       mx = 0;
41       my = 0;
42     }
43   ptext->kind = kind;
44   ptext->w = w;
45   ptext->h = h;
46
47   texture_load(&ptext->chars, file, USE_ALPHA);
48
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)
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;
83   h = ptext->h;
84
85   if(ptext->kind == TEXT_TEXT)
86     {
87       for( i = 0, j = 0; i < len; ++i,++j)
88         {
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')
98             {
99               y += ptext->h + 2;
100               j = 0;
101             }
102         }
103     }
104   else if(ptext->kind == TEXT_NUM)
105     {
106       for( i = 0, j = 0; i < len; ++i, ++j)
107         {
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')
111             {
112               y += ptext->h + 2;
113               j = 0;
114             }
115         }
116     }
117 }
118
119 void text_drawf(text_type* ptext, char* text, int x, int y, int halign, int valign, int shadowsize, int update)
120 {
121   if(text != NULL)
122     {
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);
127
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;
132
133       text_draw(ptext,text,x,y,shadowsize,update);
134     }
135 }
136
137 void text_free(text_type* ptext)
138 {
139   int c;
140   if(ptext->kind == TEXT_TEXT)
141     texture_free(&ptext->chars);
142   else if(ptext->kind == TEXT_NUM)
143     texture_free(&ptext->chars);
144 }
145
146 /* --- ERASE TEXT: --- */
147
148 void erasetext(text_type* ptext, char * text, int x, int y, texture_type * ptexture, int update, int shadowsize)
149 {
150   SDL_Rect dest;
151
152
153   dest.x = x;
154   dest.y = y;
155   dest.w = strlen(text) * ptext->w + shadowsize;
156   dest.h = ptext->h;
157
158   if (dest.w > screen->w)
159     dest.w = screen->w;
160
161   texture_draw_part(ptexture,dest.x,dest.y,dest.x,dest.y,dest.w,dest.h,update);
162
163   if (update == UPDATE)
164     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
165 }
166
167
168 /* --- ERASE CENTERED TEXT: --- */
169
170 void erasecenteredtext(text_type* ptext, char * text, int y, texture_type * ptexture, int update, int shadowsize)
171 {
172   erasetext(ptext, text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);
173 }