cleanup and fixes to openal code
[supertux.git] / src / video / screen.cpp
1 //  $Id: screen.cpp 2334 2005-04-04 16:26:14Z grumbel $
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #include <iostream>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <cstring>
25 #include <cerrno>
26 #include <assert.h>
27
28 #include <unistd.h>
29
30 #include <SDL.h>
31 #include <SDL_image.h>
32
33 #ifndef WIN32
34 #include <sys/types.h>
35 #include <ctype.h>
36 #endif
37
38 #include "gameconfig.hpp"
39 #include "screen.hpp"
40 #include "main.hpp"
41 #include "video/drawing_context.hpp"
42 #include "audio/sound_manager.hpp"
43 #include "math/vector.hpp"
44
45 static const float LOOP_DELAY = 20.0;
46 extern SDL_Surface* screen;
47
48 /* 'Stolen' from the SDL documentation.
49  * Return the pixel value at (x, y)
50  * NOTE: The surface must be locked before calling this!
51  */
52 Uint32 getpixel(SDL_Surface *surface, int x, int y)
53 {
54     int bpp = surface->format->BytesPerPixel;
55     /* Here p is the address to the pixel we want to retrieve */
56     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
57
58     switch(bpp) {
59     case 1:
60         return *p;
61
62     case 2:
63         return *(Uint16 *)p;
64
65     case 3:
66         if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
67             return p[0] << 16 | p[1] << 8 | p[2];
68         else
69             return p[0] | p[1] << 8 | p[2] << 16;
70
71     case 4:
72         return *(Uint32 *)p;
73
74     default:
75         return 0;       /* shouldn't happen, but avoids warnings */
76     }
77 }
78
79 /* 'Stolen' from the SDL documentation.
80  * Set the pixel at (x, y) to the given value
81  * NOTE: The surface must be locked before calling this!
82  */
83 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
84 {
85   int bpp = surface->format->BytesPerPixel;
86   /* Here p is the address to the pixel we want to set */
87   Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
88
89   switch(bpp) {
90     case 1:
91       *p = pixel;
92       break;
93
94     case 2:
95       *(Uint16 *)p = pixel;
96       break;
97
98     case 3:
99       if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
100         {
101           p[0] = (pixel >> 16) & 0xff;
102           p[1] = (pixel >> 8) & 0xff;
103           p[2] = pixel & 0xff;
104         }
105       else
106         {
107           p[0] = pixel & 0xff;
108           p[1] = (pixel >> 8) & 0xff;
109           p[2] = (pixel >> 16) & 0xff;
110         }
111       break;
112
113     case 4:
114       *(Uint32 *)p = pixel;
115       break;
116
117     default:
118       assert(false);
119   }
120 }
121
122 /* Draw a single pixel on the screen. */
123 void drawpixel(int x, int y, Uint32 pixel)
124 {
125   /* Lock the screen for direct access to the pixels */
126   if ( SDL_MUSTLOCK(screen) )
127     {
128       if ( SDL_LockSurface(screen) < 0 )
129         {
130           fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
131           return;
132         }
133     }
134
135   if(!(x < 0 || y < 0 || x > SCREEN_WIDTH || y > SCREEN_HEIGHT))
136     putpixel(screen, x, y, pixel);
137
138   if ( SDL_MUSTLOCK(screen) )
139     {
140       SDL_UnlockSurface(screen);
141     }
142   /* Update just the part of the display that we've changed */
143   SDL_UpdateRect(screen, x, y, 1, 1);
144 }
145
146 /* --- FILL A RECT --- */
147
148 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
149 {
150   if(w < 0) {
151     x += w;
152     w = -w;
153   }
154   if(h < 0) {
155     y += h;
156     h = -h;
157   }
158
159   if(config->use_gl) {
160     glEnable(GL_BLEND);
161     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
162     glColor4ub(r, g, b,a);
163     
164     glBegin(GL_POLYGON);
165     glVertex2f(x, y);
166     glVertex2f(x+w, y);
167     glVertex2f(x+w, y+h);
168     glVertex2f(x, y+h);
169     glEnd();
170     glDisable(GL_BLEND);
171   } else {
172     SDL_Rect src, rect;
173     SDL_Surface *temp = NULL;
174
175     rect.x = (int)x;
176     rect.y = (int)y;
177     rect.w = (int)w;
178     rect.h = (int)h;
179     
180     if(a != 255) {
181       temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
182                                   screen->format->Rmask,
183                                   screen->format->Gmask,
184                                   screen->format->Bmask,
185                                   screen->format->Amask);
186       
187       
188       src.x = 0;
189       src.y = 0;
190       src.w = rect.w;
191       src.h = rect.h;
192       
193       SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
194       SDL_SetAlpha(temp, SDL_SRCALPHA, a);
195       SDL_BlitSurface(temp,0,screen,&rect);
196       SDL_FreeSurface(temp);
197     } else {
198       SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
199     }
200   }
201 }
202
203 /* Needed for line calculations */
204 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
205 #define ABS(x) ((x)>0 ? (x) : (-x))
206
207 void draw_line(float x1, float y1, float x2, float y2,
208                          int r, int g, int b, int a)
209 {
210   if(config->use_gl) {
211     glEnable(GL_BLEND);
212     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
213     glColor4ub(r, g, b,a);
214     
215     glBegin(GL_LINES);
216     glVertex2f(x1, y1);
217     glVertex2f(x2, y2);
218     glEnd();
219     glDisable(GL_BLEND);
220   } else {
221     /* Basic unantialiased Bresenham line algorithm */
222     int lg_delta, sh_delta, cycle, lg_step, sh_step;
223     Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
224     
225     lg_delta = (int)(x2 - x1);
226     sh_delta = (int)(y2 - y1);
227     lg_step = SGN(lg_delta);
228     lg_delta = ABS(lg_delta);
229     sh_step = SGN(sh_delta);
230     sh_delta = ABS(sh_delta);
231     if (sh_delta < lg_delta) {
232       cycle = lg_delta >> 1;
233       while (x1 != x2) {
234         drawpixel((int)x1, (int)y1, color);
235         cycle += sh_delta;
236         if (cycle > lg_delta) {
237           cycle -= lg_delta;
238           y1 += sh_step;
239         }
240         x1 += lg_step;
241       }
242       drawpixel((int)x1, (int)y1, color);
243     }
244     cycle = sh_delta >> 1;
245     while (y1 != y2) {
246       drawpixel((int)x1, (int)y1, color);
247       cycle += lg_delta;
248       if (cycle > sh_delta) {
249         cycle -= sh_delta;
250         x1 += lg_step;
251       }
252       y1 += sh_step;
253     }
254     drawpixel((int)x1, (int)y1, color);
255   }
256 }
257
258
259 void fadeout(int fade_time)
260 {
261   float alpha_inc  = 256 / (fade_time / LOOP_DELAY);
262   float alpha = 256;
263
264   while(alpha > 0) {
265     alpha -= alpha_inc;
266     fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0,0,0, (int)alpha_inc);  // left side
267                                                    
268     DrawingContext context; // ugly...
269     context.do_drawing();
270     sound_manager->update();
271     
272     SDL_Delay(int(LOOP_DELAY));
273   }
274
275   fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 255);
276 }
277
278 void shrink_fade(const Vector& point, int fade_time)
279 {
280   float left_inc  = point.x / ((float)fade_time / LOOP_DELAY);
281   float right_inc = (SCREEN_WIDTH - point.x) / ((float)fade_time / LOOP_DELAY);
282   float up_inc    = point.y / ((float)fade_time / LOOP_DELAY);
283   float down_inc  = (SCREEN_HEIGHT - point.y) / ((float)fade_time / LOOP_DELAY);
284                                                                                 
285   float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
286                                                                                 
287   while(left_cor < point.x && right_cor < SCREEN_WIDTH - point.x &&
288       up_cor < point.y && down_cor < SCREEN_HEIGHT - point.y) {
289     left_cor  += left_inc;
290     right_cor += right_inc;
291     up_cor    += up_inc;
292     down_cor  += down_inc;
293                                                                                 
294     fillrect(0, 0, left_cor, SCREEN_HEIGHT, 0,0,0);  // left side
295     fillrect(SCREEN_WIDTH - right_cor, 0, right_cor, SCREEN_HEIGHT, 0,0,0);  // right side
296     fillrect(0, 0, SCREEN_WIDTH, up_cor, 0,0,0);  // up side
297     fillrect(0, SCREEN_HEIGHT - down_cor, SCREEN_WIDTH, down_cor+1, 0,0,0);  // down side                                                                                
298     DrawingContext context; // ugly...
299     context.do_drawing();
300   
301     sound_manager->update();
302     SDL_Delay(int(LOOP_DELAY));
303   }
304 }