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