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