draw*text <- functions can now specify the size of the shadow :)
[supertux.git] / src / screen.c
1 /*
2   screen.c
3   
4   Super Tux - Screen Functions
5   
6   by Bill Kendrick
7   bill@newbreedsoftware.com
8   http://www.newbreedsoftware.com/supertux/
9   
10   April 11, 2000 - April 22, 2000
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <SDL.h>
19 #include <SDL_image.h>
20
21 #ifdef LINUX
22 #include <pwd.h>
23 #include <sys/types.h>
24 #include <ctype.h>
25 #endif
26
27 #include "defines.h"
28 #include "globals.h"
29 #include "screen.h"
30 #include "setup.h"
31
32
33 /* --- LOAD AND DISPLAY AN IMAGE --- */
34
35 void load_and_display_image(char * file)
36 {
37   SDL_Surface * img;
38   
39   img = load_image(file, IGNORE_ALPHA);
40   SDL_BlitSurface(img, NULL, screen, NULL);
41   SDL_FreeSurface(img);
42 }
43
44
45 /* --- CLEAR SCREEN --- */
46
47 void clearscreen(int r, int g, int b)
48 {
49   SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
50 }
51
52
53 /* --- UPDATE SCREEN --- */
54
55 void updatescreen(void)
56 {
57   SDL_UpdateRect(screen, 0, 0, 640, 480);
58 }
59
60
61 /* --- LOAD AN IMAGE --- */
62
63 SDL_Surface * load_image(char * file, int use_alpha)
64 {
65   SDL_Surface * temp, * surf;
66   
67   temp = IMG_Load(file);
68   if (temp == NULL)
69     st_abort("Can't load", file);
70   
71   surf = SDL_DisplayFormatAlpha(temp);
72
73   if (surf == NULL)
74     st_abort("Can't covert to display format", file);
75
76   if (use_alpha == IGNORE_ALPHA)
77     SDL_SetAlpha(surf, 0, 0);
78   
79   SDL_FreeSurface(temp);
80
81   return(surf);
82 }
83
84
85 /* --- DRAW AN IMAGE ONTO THE SCREEN --- */
86
87 void drawimage(SDL_Surface * surf, int x, int y, int update)
88 {
89   SDL_Rect dest;
90   
91   dest.x = x;
92   dest.y = y;
93   dest.w = surf->w;
94   dest.h = surf->h;
95   
96   SDL_BlitSurface(surf, NULL, screen, &dest);
97   
98   if (update == UPDATE)
99     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
100 }
101
102
103 /* --- DRAW PART OF AN IMAGE ONTO THE SCREEN --- */
104
105 void drawpart(SDL_Surface * surf, int x, int y, int w, int h, int update)
106 {
107   SDL_Rect src, dest;
108   
109   src.x = x;
110   src.y = y;
111   src.w = w;
112   src.h = h;
113
114   dest.x = x;
115   dest.y = y;
116   dest.w = w;
117   dest.h = h;
118   
119   SDL_BlitSurface(surf, &src, screen, &dest);
120   
121   if (update == UPDATE)
122     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
123 }
124
125 /* --- DRAW TEXT ONTO THE SCREEN --- */
126
127 void drawtext(char * text, int x, int y, SDL_Surface * surf, int update, int shadowsize)
128 {
129   int i;
130   char c;
131   SDL_Rect src, dest;
132   
133   /* For each letter in the string... */
134   
135   for (i = 0; i < strlen(text); i++)
136     {
137       /* Set source rectangle: */
138       
139       c = text[i];
140       
141       if (c >= 'A' && c <= 'Z')
142         {
143           /* Capital letter - first row: */
144           
145           src.x = (c - 'A') * 16;
146           src.y = 0;
147         }
148       else if (c >= 'a' && c <= 'z')
149         {
150           /* Lowercase letter - first row: */
151           
152           src.x = (c - 'a') * 16;
153           src.y = 16;
154         }
155       else if (c >= '!' && c <= '9')
156         {
157           /* Punctuation (except '?') or number - third row: */
158           
159           src.x = (c - '!') * 16;
160           src.y = 32;
161         }
162       else if (c == '?')
163         {
164           /* Question mark - third row, last character: */
165           
166           src.x = 400;
167           src.y = 24;
168         }
169       else
170         src.x = -1;
171       
172       src.w = 16;
173       src.h = 16;
174       
175
176       /* Draw character: */
177       
178       if (src.x != -1)
179         {
180           /* Set destination rectangle for shadow: */
181           
182           dest.x = x + (i * 16) + shadowsize;
183           dest.y = y + shadowsize;
184           dest.w = src.w;
185           dest.h = src.h;
186           
187           
188           /* Shadow: */
189           
190           SDL_BlitSurface(letters_black, &src, screen, &dest);
191           
192           
193           /* Set destination rectangle for text: */
194           
195           dest.x = x + (i * 16);
196           dest.y = y;
197           dest.w = src.w;
198           dest.h = src.h;
199           
200           
201           /* Shadow: */
202           
203           SDL_BlitSurface(surf, &src, screen, &dest);
204         }
205     }
206   
207   
208   /* Update */
209   
210   if (update == UPDATE)
211     {
212       dest.w = strlen(text) * 16 + 1;
213       
214       if (dest.w > 640)
215         dest.w = 640;
216       
217       SDL_UpdateRect(screen, x, y, dest.w, 17);
218     }
219     
220 }
221
222
223 /* --- DRAW HORIZONTALLY-CENTERED TEXT: --- */
224
225 void drawcenteredtext(char * text, int y, SDL_Surface * surf, int update, int shadowsize)
226 {
227   drawtext(text, 320 - (strlen(text) * 8), y, surf, update, shadowsize);
228 }
229
230 /* --- ERASE TEXT: --- */
231
232 void erasetext(char * text, int x, int y, SDL_Surface * surf, int update, int shadowsize)
233 {
234   SDL_Rect dest;
235   
236   
237   dest.x = x;
238   dest.y = y;
239   dest.w = strlen(text) * 16 + shadowsize;
240   dest.h = 17;
241   
242   if (dest.w > 640)
243     dest.w = 640;
244   
245   SDL_BlitSurface(surf, &dest, screen, &dest);
246   
247   if (update == UPDATE)
248     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
249 }
250
251
252 /* --- ERASE CENTERED TEXT: --- */
253
254 void erasecenteredtext(char * text, int y, SDL_Surface * surf, int update, int shadowsize)
255 {
256   erasetext(text, 320 - (strlen(text) * 8), y, surf, update, shadowsize);
257 }