initial
[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       ptext->chars = (texture_type*) malloc(sizeof(texture_type) * 79);
29       ptext->shadow_chars = (texture_type*) malloc(sizeof(texture_type) * 79);
30       mx = 26;
31       my = 3;
32     }
33   else if(kind == TEXT_NUM)
34     {
35       ptext->chars = (texture_type*) malloc(sizeof(texture_type) * 10);
36       ptext->shadow_chars = (texture_type*) malloc(sizeof(texture_type) * 10);
37       mx = 10;
38       my = 1;
39     }
40   else
41     {
42       ptext->chars = NULL;
43       ptext->shadow_chars = NULL;
44       mx = 0;
45       my = 0;
46     }
47   ptext->kind = kind;
48   ptext->w = w;
49   ptext->h = h;
50
51   for(y = 0; y < my ; ++y)
52     {
53       for(x = 0; x < mx ; ++x)
54         {
55           texture_load_part(&ptext->chars[y*mx+x],file,x*w,y*h,w,h, USE_ALPHA);
56         }
57     }
58
59   /* Load shadow font. */
60   for(y = 0; y < my ; ++y)
61     {
62       for(x = 0; x < mx ; ++x)
63         {
64           int pixels;
65           int i;
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)
70             {
71               Uint32 *p = (Uint32 *)conv->pixels + i;
72               *p = *p & conv->format->Amask;
73             }
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);
77         }
78     }
79
80 }
81
82 void text_draw(text_type* ptext, char* text, int x, int y, int shadowsize, int update)
83 {
84   if(text != NULL)
85     {
86       if(shadowsize != 0)
87         text_draw_chars(ptext,&ptext->shadow_chars[0], text,x+shadowsize,y+shadowsize, update);
88
89       text_draw_chars(ptext,ptext->chars, text,x,y, update);
90     }
91 }
92
93 void text_draw_chars(text_type* ptext, texture_type* pchars, char* text, int x, int y, int update)
94 {
95   int i,len;
96
97   len = strlen(text);
98
99   if(ptext->kind == TEXT_TEXT)
100     {
101       for( i = 0; i < len; ++i)
102         {
103           if( text[i] >= 'A' && text[i] <= 'Z')
104             {
105               texture_draw(&pchars[(int)(text[i] - 'A')],x+i*ptext->w,y,update);
106             }
107           else if( text[i] >= 'a' && text[i] <= 'z')
108             {
109               texture_draw(&pchars[(int)(text[i] - 'a') + 26],x+i*ptext->w,y,update);
110             }
111           else if ( text[i] >= '!' && text[i] <= '9')
112             {
113               texture_draw(&pchars[(int)(text[i] - '!') + 52],x+i*ptext->w,y,update);
114             }
115           else if ( text[i] == '?')
116             {
117               texture_draw(&pchars[77],x+i*ptext->w,y,update);
118             }
119           else if ( text[i] == '\n')
120             {
121               y += ptext->h + 2;
122             }
123         }
124     }
125   else if(ptext->kind == TEXT_NUM)
126     {
127       for( i = 0; i < len; ++i)
128         {
129           if ( text[i] >= '0' && text[i] <= '9')
130             {
131               texture_draw(&pchars[(int)(text[i] - '0')],x+i*ptext->w,y,update);
132             }
133           else if ( text[i] == '\n')
134             {
135               y += ptext->h + 2;
136             }
137         }
138     }
139 }
140
141 void text_drawf(text_type* ptext, char* text, int x, int y, int halign, int valign, int shadowsize, int update)
142 {
143   if(text != NULL)
144     {
145       if(halign == A_RIGHT)
146         x += screen->w;
147       else if(halign == A_HMIDDLE)
148         x += screen->w/2 - ((strlen(text)*ptext->w)/2);
149
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;
154
155       text_draw(ptext,text,x,y,shadowsize,update);
156     }
157 }
158
159 void text_free(text_type* ptext)
160 {
161   int c;
162   if(ptext->kind == TEXT_TEXT)
163     {
164       for( c = 0; c < 78; ++c)
165         texture_free(&ptext->chars[c]);
166     }
167   else if(ptext->kind == TEXT_NUM)
168     {
169       for( c = 0; c < 10; ++c)
170         texture_free(&ptext->chars[c]);
171     }
172 }