huge CVS merge, see ChangeLog for details.
[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(int r, int g, int 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
58     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
59
60 }
61
62 /* --- FILL A RECT --- */
63
64 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
65 {
66 #ifndef NOOPENGL
67   if(use_gl)
68     {
69       glEnable(GL_BLEND);
70       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
71       glColor4ub(r, g, b,a);
72       
73       glBegin(GL_POLYGON);
74       glVertex2f(x, y);
75       glVertex2f(x+w, y);
76       glVertex2f(x+w, y+h);
77       glVertex2f(x, y+h);
78       glEnd();
79       glDisable(GL_BLEND);
80     }
81   else
82     {
83 #endif
84       SDL_Rect src, rect;
85       SDL_Surface *temp = NULL;
86       
87       rect.x = (int)x;
88       rect.y = (int)y;
89       rect.w = (int)w;
90       rect.h = (int)h;
91       
92       if(a != 255)
93       {
94       temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
95                                   screen->format->Rmask,
96                               screen->format->Gmask,
97                               screen->format->Bmask,
98                               screen->format->Amask);
99
100
101       src.x = 0;
102       src.y = 0;
103       src.w = rect.w;
104       src.h = rect.h;
105
106       SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
107       
108       SDL_SetAlpha(temp, SDL_SRCALPHA, a);
109       
110       SDL_BlitSurface(temp,0,screen,&rect);
111       
112       SDL_FreeSurface(temp);
113       }
114       else
115       SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
116       
117 #ifndef NOOPENGL
118
119     }
120 #endif
121 }
122
123
124 /* --- UPDATE SCREEN --- */
125
126 void updatescreen(void)
127 {
128   if(use_gl)  /*clearscreen(0,0,0);*/
129     SDL_GL_SwapBuffers();
130   else
131     SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
132 }
133
134 void flipscreen(void)
135 {
136   if(use_gl)
137     SDL_GL_SwapBuffers();
138   else
139     SDL_Flip(screen);
140 }
141
142 /* --- LOAD AN IMAGE --- */
143
144 SDL_Surface * load_image(char * file, int use_alpha)
145 {
146   /*
147   if(!faccessible(file))
148   {
149   if(!faccessible(st_dir,
150   */
151
152   SDL_Surface * temp, * surf;
153
154   temp = IMG_Load(file);
155
156   if (temp == NULL)
157     st_abort("Can't load", file);
158
159   surf = SDL_DisplayFormatAlpha(temp);
160
161   if (surf == NULL)
162     st_abort("Can't covert to display format", file);
163
164   if (use_alpha == IGNORE_ALPHA)
165     SDL_SetAlpha(surf, 0, 0);
166
167   SDL_FreeSurface(temp);
168
169   return(surf);
170 }
171
172 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
173 {
174   if(!use_gl)
175     SDL_UpdateRect(scr, x, y, w, h);
176 }
177
178
179 /* --- ERASE TEXT: --- */
180
181 void erasetext(char * text, int x, int y, texture_type * ptexture, int update, int shadowsize)
182 {
183   SDL_Rect dest;
184
185
186   dest.x = x;
187   dest.y = y;
188   dest.w = strlen(text) * 16 + shadowsize;
189   dest.h = 17;
190
191   if (dest.w > screen->w)
192     dest.w = screen->w;
193
194   texture_draw_part(ptexture,dest.x,dest.y,dest.x,dest.y,dest.w,dest.h,update);
195
196   if (update == UPDATE)
197     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
198 }
199
200
201 /* --- ERASE CENTERED TEXT: --- */
202
203 void erasecenteredtext(char * text, int y, texture_type * ptexture, int update, int shadowsize)
204 {
205   erasetext(text, screen->w / 2 - (strlen(text) * 8), y, ptexture, update, shadowsize);
206 }