Apply filter mask.
[supertux.git] / lib / video / screen.cpp
1 //  $Id$
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
20 #include <iostream>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24 #include <cerrno>
25
26 #include <unistd.h>
27
28 #include "SDL.h"
29 #include "SDL_image.h"
30
31 #ifndef WIN32
32 #include <sys/types.h>
33 #include <ctype.h>
34 #endif
35
36 #include "../video/screen.h"
37 #include "../app/globals.h"
38 #include "../video/drawing_context.h"
39 #include "../special/base.h"
40 #include "../math/vector.h"
41
42 using namespace SuperTux;
43
44 /* 'Stolen' from the SDL documentation.
45  * Return the pixel value at (x, y)
46  * NOTE: The surface must be locked before calling this!
47  */
48 Uint32 SuperTux::getpixel(SDL_Surface *surface, int x, int y)
49 {
50     int bpp = surface->format->BytesPerPixel;
51     /* Here p is the address to the pixel we want to retrieve */
52     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
53
54     switch(bpp) {
55     case 1:
56         return *p;
57
58     case 2:
59         return *(Uint16 *)p;
60
61     case 3:
62         if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
63             return p[0] << 16 | p[1] << 8 | p[2];
64         else
65             return p[0] | p[1] << 8 | p[2] << 16;
66
67     case 4:
68         return *(Uint32 *)p;
69
70     default:
71         return 0;       /* shouldn't happen, but avoids warnings */
72     }
73 }
74
75 /* 'Stolen' from the SDL documentation.
76  * Set the pixel at (x, y) to the given value
77  * NOTE: The surface must be locked before calling this!
78  */
79 void SuperTux::putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
80 {
81   int bpp = surface->format->BytesPerPixel;
82   /* Here p is the address to the pixel we want to set */
83   Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
84
85   switch(bpp)
86     {
87     case 1:
88       *p = pixel;
89       break;
90
91     case 2:
92       *(Uint16 *)p = pixel;
93       break;
94
95     case 3:
96       if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
97         {
98           p[0] = (pixel >> 16) & 0xff;
99           p[1] = (pixel >> 8) & 0xff;
100           p[2] = pixel & 0xff;
101         }
102       else
103         {
104           p[0] = pixel & 0xff;
105           p[1] = (pixel >> 8) & 0xff;
106           p[2] = (pixel >> 16) & 0xff;
107         }
108       break;
109
110     case 4:
111       *(Uint32 *)p = pixel;
112       break;
113     }
114 }
115
116 /* Draw a single pixel on the screen. */
117 void SuperTux::drawpixel(int x, int y, Uint32 pixel)
118 {
119   /* Lock the screen for direct access to the pixels */
120   if ( SDL_MUSTLOCK(screen) )
121     {
122       if ( SDL_LockSurface(screen) < 0 )
123         {
124           fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
125           return;
126         }
127     }
128
129   if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
130     putpixel(screen, x, y, pixel);
131
132   if ( SDL_MUSTLOCK(screen) )
133     {
134       SDL_UnlockSurface(screen);
135     }
136   /* Update just the part of the display that we've changed */
137   SDL_UpdateRect(screen, x, y, 1, 1);
138 }
139
140 /* --- FILL A RECT --- */
141
142 void SuperTux::fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
143 {
144 if(w < 0)
145         {
146         x += w;
147         w = -w;
148         }
149 if(h < 0)
150         {
151         y += h;
152         h = -h;
153         }
154
155 #ifndef NOOPENGL
156   if(use_gl)
157     {
158       glEnable(GL_BLEND);
159       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
160       glColor4ub(r, g, b,a);
161
162       glBegin(GL_POLYGON);
163       glVertex2f(x, y);
164       glVertex2f(x+w, y);
165       glVertex2f(x+w, y+h);
166       glVertex2f(x, y+h);
167       glEnd();
168       glDisable(GL_BLEND);
169     }
170   else
171     {
172 #endif
173       SDL_Rect src, rect;
174       SDL_Surface *temp = NULL;
175
176       rect.x = (int)x;
177       rect.y = (int)y;
178       rect.w = (int)w;
179       rect.h = (int)h;
180
181       if(a != 255)
182         {
183           temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
184                                       screen->format->Rmask,
185                                       screen->format->Gmask,
186                                       screen->format->Bmask,
187                                       screen->format->Amask);
188
189
190           src.x = 0;
191           src.y = 0;
192           src.w = rect.w;
193           src.h = rect.h;
194
195           SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
196
197           SDL_SetAlpha(temp, SDL_SRCALPHA, a);
198
199           SDL_BlitSurface(temp,0,screen,&rect);
200
201           SDL_FreeSurface(temp);
202         }
203       else
204         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
205
206 #ifndef NOOPENGL
207
208     }
209 #endif
210 }
211
212 /* Needed for line calculations */
213 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
214 #define ABS(x) ((x)>0 ? (x) : (-x))
215
216 void SuperTux::draw_line(float x1, float y1, float x2, float y2, int r, int g, int b, int a)
217 {
218 #ifndef NOOPENGL
219   if(use_gl)
220     {
221       glEnable(GL_BLEND);
222       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
223       glColor4ub(r, g, b,a);
224
225       glBegin(GL_LINES);
226       glVertex2f(x1, y1);
227       glVertex2f(x2, y2);
228       glEnd();
229       glDisable(GL_BLEND);
230     }
231   else
232     {
233 #endif
234       /* Basic unantialiased Bresenham line algorithm */
235       int lg_delta, sh_delta, cycle, lg_step, sh_step;
236       Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
237
238       lg_delta = (int)(x2 - x1);
239       sh_delta = (int)(y2 - y1);
240       lg_step = SGN(lg_delta);
241       lg_delta = ABS(lg_delta);
242       sh_step = SGN(sh_delta);
243       sh_delta = ABS(sh_delta);
244       if (sh_delta < lg_delta)
245         {
246           cycle = lg_delta >> 1;
247           while (x1 != x2)
248             {
249               drawpixel((int)x1, (int)y1, color);
250               cycle += sh_delta;
251               if (cycle > lg_delta)
252                 {
253                   cycle -= lg_delta;
254                   y1 += sh_step;
255                 }
256               x1 += lg_step;
257             }
258           drawpixel((int)x1, (int)y1, color);
259         }
260       cycle = sh_delta >> 1;
261       while (y1 != y2)
262         {
263           drawpixel((int)x1, (int)y1, color);
264           cycle += lg_delta;
265           if (cycle > sh_delta)
266             {
267               cycle -= sh_delta;
268               x1 += lg_step;
269             }
270           y1 += sh_step;
271         }
272       drawpixel((int)x1, (int)y1, color);
273 #ifndef NOOPENGL
274
275     }
276 #endif
277 }
278
279 #define LOOP_DELAY 20.0
280
281 void SuperTux::fadeout(int fade_time)
282 {
283   float alpha_inc  = 256 / (fade_time / LOOP_DELAY);
284   float alpha = 256;
285
286   while(alpha > 0)
287     {
288     alpha -= alpha_inc;
289     fillrect(0, 0, screen->w, screen->h, 0,0,0, (int)alpha_inc);  // left side
290                                                    
291     DrawingContext context; // ugly...
292     context.do_drawing();
293
294     SDL_Delay(int(LOOP_DELAY));
295     }
296
297   fillrect(0, 0, screen->w, screen->h, 0, 0, 0, 255);
298
299 }
300
301 void SuperTux::shrink_fade(const Vector& point, int fade_time)
302 {
303   float left_inc  = point.x / ((float)fade_time / LOOP_DELAY);
304   float right_inc = (screen->w - point.x) / ((float)fade_time / LOOP_DELAY);
305   float up_inc    = point.y / ((float)fade_time / LOOP_DELAY);
306   float down_inc  = (screen->h - point.y) / ((float)fade_time / LOOP_DELAY);
307                                                                                 
308   float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
309                                                                                 
310   while(left_cor < point.x && right_cor < screen->w - point.x &&
311       up_cor < point.y && down_cor < screen->h - point.y)
312   {
313     left_cor  += left_inc;
314     right_cor += right_inc;
315     up_cor    += up_inc;
316     down_cor  += down_inc;
317                                                                                 
318     fillrect(0, 0, left_cor, screen->h, 0,0,0);  // left side
319     fillrect(screen->w - right_cor, 0, right_cor, screen->h, 0,0,0);  // right side
320     fillrect(0, 0, screen->w, up_cor, 0,0,0);  // up side
321     fillrect(0, screen->h - down_cor, screen->w, down_cor+1, 0,0,0);  // down side                                                                                
322     DrawingContext context; // ugly...
323     context.do_drawing();
324     
325     SDL_Delay(int(LOOP_DELAY));
326   }
327 }
328