4b3197a75db6595f81a5490a831d514a12b575fa
[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,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; i < len; ++i)
87         {
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')
97             {
98               y += ptext->h + 2;
99             }
100         }
101     }
102   else if(ptext->kind == TEXT_NUM)
103     {
104       for( i = 0; i < len; ++i)
105         {
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')
109             {
110               y += ptext->h + 2;
111             }
112         }
113     }
114 }
115
116 void text_drawf(text_type* ptext, char* text, int x, int y, int halign, int valign, int shadowsize, int update)
117 {
118   if(text != NULL)
119     {
120       if(halign == A_RIGHT)
121         x += screen->w;
122       else if(halign == A_HMIDDLE)
123         x += screen->w/2 - ((strlen(text)*ptext->w)/2);
124
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;
129
130       text_draw(ptext,text,x,y,shadowsize,update);
131     }
132 }
133
134 void text_free(text_type* ptext)
135 {
136   int c;
137   if(ptext->kind == TEXT_TEXT)
138         texture_free(&ptext->chars);
139   else if(ptext->kind == TEXT_NUM)
140         texture_free(&ptext->chars);
141 }
142
143 /* --- ERASE TEXT: --- */
144
145 void erasetext(text_type* ptext, char * text, int x, int y, texture_type * ptexture, int update, int shadowsize)
146 {
147   SDL_Rect dest;
148
149
150   dest.x = x;
151   dest.y = y;
152   dest.w = strlen(text) * ptext->w + shadowsize;
153   dest.h = ptext->h;
154
155   if (dest.w > screen->w)
156     dest.w = screen->w;
157
158   texture_draw_part(ptexture,dest.x,dest.y,dest.x,dest.y,dest.w,dest.h,update);
159
160   if (update == UPDATE)
161     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
162 }
163
164
165 /* --- ERASE CENTERED TEXT: --- */
166
167 void erasecenteredtext(text_type* ptext, char * text, int y, texture_type * ptexture, int update, int shadowsize)
168 {
169   erasetext(ptext, text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);
170 }