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