incoporated patches from Duong-Khang (stereo sound!) and Ricardo Cruz <rick2@aeiou...
[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 SHADED TEXT ONTO THE SCREEN --- */
126
127 void drawshadedtext(char * text, int x, int y, SDL_Surface * surf, int update)
128 {
129  if(surf != letters_black)
130  {
131  drawtext(text, x+1, y+1, letters_black, update);
132  drawtext(text, x, y, surf, update);
133  }
134  else
135  drawtext(text, x, y, surf, update);
136  
137 }
138
139 /* --- DRAW TEXT ONTO THE SCREEN --- */
140
141 void drawtext(char * text, int x, int y, SDL_Surface * surf, int update)
142 {
143   int i;
144   char c;
145   SDL_Rect src, dest;
146   
147   /* For each letter in the string... */
148   
149   for (i = 0; i < strlen(text); i++)
150     {
151       /* Set source rectangle: */
152       
153       c = text[i];
154       
155       if (c >= 'A' && c <= 'Z')
156         {
157           /* Capital letter - first row: */
158           
159           src.x = (c - 'A') * 16;
160           src.y = 0;
161         }
162       else if (c >= 'a' && c <= 'z')
163         {
164           /* Lowercase letter - first row: */
165           
166           src.x = (c - 'a') * 16;
167           src.y = 16;
168         }
169       else if (c >= '!' && c <= '9')
170         {
171           /* Punctuation (except '?') or number - third row: */
172           
173           src.x = (c - '!') * 16;
174           src.y = 32;
175         }
176       else if (c == '?')
177         {
178           /* Question mark - third row, last character: */
179           
180           src.x = 400;
181           src.y = 24;
182         }
183       else
184         src.x = -1;
185       
186       src.w = 16;
187       src.h = 16;
188       
189
190       /* Draw character: */
191       
192       if (src.x != -1)
193         {
194           /* Set destination rectangle for shadow: */
195           
196           dest.x = x + (i * 16) + 1;
197           dest.y = y + 1;
198           dest.w = src.w;
199           dest.h = src.h;
200           
201           
202           /* Shadow: */
203           
204           SDL_BlitSurface(letters_black, &src, screen, &dest);
205           
206           
207           /* Set destination rectangle for text: */
208           
209           dest.x = x + (i * 16);
210           dest.y = y;
211           dest.w = src.w;
212           dest.h = src.h;
213           
214           
215           /* Shadow: */
216           
217           SDL_BlitSurface(surf, &src, screen, &dest);
218         }
219     }
220   
221   
222   /* Update */
223   
224   if (update == UPDATE)
225     {
226       dest.w = strlen(text) * 16 + 1;
227       
228       if (dest.w > 640)
229         dest.w = 640;
230       
231       SDL_UpdateRect(screen, x, y, dest.w, 17);
232     }
233     
234 }
235
236
237 /* --- DRAW HORIZONTALLY-CENTERED TEXT: --- */
238
239 void drawcenteredtext(char * text, int y, SDL_Surface * surf, int update)
240 {
241   drawtext(text, 320 - (strlen(text) * 8), y, surf, update);
242 }
243
244 /* --- DRAW SHADED HORIZONTALLY-CENTERED TEXT: --- */
245
246 void drawshadedcenteredtext(char * text, int y, SDL_Surface * surf, int update)
247 {
248   drawshadedtext(text, 320 - (strlen(text) * 8), y, surf, update);
249 }
250
251 /* --- ERASE TEXT: --- */
252
253 void erasetext(char * text, int x, int y, SDL_Surface * surf, int update)
254 {
255   SDL_Rect dest;
256   
257   
258   dest.x = x;
259   dest.y = y;
260   dest.w = strlen(text) * 16 + 1;
261   dest.h = 17;
262   
263   if (dest.w > 640)
264     dest.w = 640;
265   
266   SDL_BlitSurface(surf, &dest, screen, &dest);
267   
268   if (update == UPDATE)
269     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
270 }
271
272
273 /* --- ERASE CENTERED TEXT: --- */
274
275 void erasecenteredtext(char * text, int y, SDL_Surface * surf, int update)
276 {
277   erasetext(text, 320 - (strlen(text) * 8), y, surf, update);
278 }