Fixed more inappropriate variable decl's.
[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 #include "type.h"
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(float r, float g, float b)
48 {
49 #ifndef NOOPENGL
50   if(use_gl)
51   {
52   glClearColor(r/256, g/256, b/256, 1.0);
53   glClear(GL_COLOR_BUFFER_BIT);
54   }
55   else
56 #endif
57   SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
58  
59 }
60
61 /* --- FILL A RECT --- */
62
63 void fillrect(float x, float y, float w, float h, float r, float g, float b)
64 {
65 #ifndef NOOPENGL
66 if(use_gl)
67         {
68         glBegin(GL_QUADS);
69                 glColor3ub(r, g, b);
70                 glVertex2i(x, y);
71                 glVertex2i(x+w, y);
72                 glVertex2i(x+w, y+h);
73                 glVertex2i(x, y+h);
74         glEnd();
75         }
76 else
77         {
78 #endif
79         SDL_Rect rect;
80         rect.x = x;
81         rect.y = y;
82         rect.w = w;
83         rect.h = h;
84
85         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
86 #ifndef NOOPENGL
87         }
88 #endif
89 }
90
91
92 /* --- UPDATE SCREEN --- */
93
94 void updatescreen(void)
95 {
96   if(use_gl)  /*clearscreen(0,0,0);*/
97   SDL_GL_SwapBuffers();
98   else
99     SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
100 }
101
102 void flipscreen(void)
103 {
104 if(use_gl)
105 SDL_GL_SwapBuffers();
106 else
107 SDL_Flip(screen);
108 }
109
110 /* --- LOAD AN IMAGE --- */
111
112 SDL_Surface * load_image(char * file, int use_alpha)
113 {
114 /*
115 if(!faccessible(file))
116 {
117 if(!faccessible(st_dir,
118 */
119
120   SDL_Surface * temp, * surf;
121   
122   temp = IMG_Load(file);
123
124   if (temp == NULL)
125     st_abort("Can't load", file);
126     
127   surf = SDL_DisplayFormatAlpha(temp);
128
129   if (surf == NULL)
130     st_abort("Can't covert to display format", file);
131     
132   if (use_alpha == IGNORE_ALPHA)
133     SDL_SetAlpha(surf, 0, 0);
134   
135   SDL_FreeSurface(temp);
136
137   return(surf);
138 }
139
140 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
141 {
142 if(!use_gl)
143 SDL_UpdateRect(scr, x, y, w, h);
144 }
145
146
147 /* --- ERASE TEXT: --- */
148
149 void erasetext(char * text, int x, int y, texture_type * ptexture, int update, int shadowsize)
150 {
151   SDL_Rect dest;
152   
153   
154   dest.x = x;
155   dest.y = y;
156   dest.w = strlen(text) * 16 + shadowsize;
157   dest.h = 17;
158   
159   if (dest.w > screen->w)
160     dest.w = screen->w;
161   
162   texture_draw_part(ptexture,dest.x,dest.y,dest.x,dest.y,dest.w,dest.h,update);
163   
164   if (update == UPDATE)
165     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
166 }
167
168
169 /* --- ERASE CENTERED TEXT: --- */
170
171 void erasecenteredtext(char * text, int y, texture_type * ptexture, int update, int shadowsize)
172 {
173   erasetext(text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);
174 }