3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
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.
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.
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.
31 #include "SDL_image.h"
34 #include <sys/types.h>
39 #include "app/globals.h"
40 #include "video/drawing_context.h"
41 #include "math/vector.h"
43 using namespace SuperTux;
45 /* 'Stolen' from the SDL documentation.
46 * Return the pixel value at (x, y)
47 * NOTE: The surface must be locked before calling this!
49 Uint32 SuperTux::getpixel(SDL_Surface *surface, int x, int y)
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;
63 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
64 return p[0] << 16 | p[1] << 8 | p[2];
66 return p[0] | p[1] << 8 | p[2] << 16;
72 return 0; /* shouldn't happen, but avoids warnings */
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!
80 void SuperTux::putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
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;
97 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
99 p[0] = (pixel >> 16) & 0xff;
100 p[1] = (pixel >> 8) & 0xff;
106 p[1] = (pixel >> 8) & 0xff;
107 p[2] = (pixel >> 16) & 0xff;
112 *(Uint32 *)p = pixel;
117 /* Draw a single pixel on the screen. */
118 void SuperTux::drawpixel(int x, int y, Uint32 pixel)
120 /* Lock the screen for direct access to the pixels */
121 if ( SDL_MUSTLOCK(screen) )
123 if ( SDL_LockSurface(screen) < 0 )
125 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
130 if(!(x < 0 || y < 0 || x > SCREEN_WIDTH || y > SCREEN_HEIGHT))
131 putpixel(screen, x, y, pixel);
133 if ( SDL_MUSTLOCK(screen) )
135 SDL_UnlockSurface(screen);
137 /* Update just the part of the display that we've changed */
138 SDL_UpdateRect(screen, x, y, 1, 1);
141 /* --- FILL A RECT --- */
143 void SuperTux::fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
160 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
161 glColor4ub(r, g, b,a);
166 glVertex2f(x+w, y+h);
175 SDL_Surface *temp = NULL;
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);
196 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
198 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
200 SDL_BlitSurface(temp,0,screen,&rect);
202 SDL_FreeSurface(temp);
205 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
213 /* Needed for line calculations */
214 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
215 #define ABS(x) ((x)>0 ? (x) : (-x))
217 void SuperTux::draw_line(float x1, float y1, float x2, float y2, int r, int g, int b, int a)
223 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
224 glColor4ub(r, g, b,a);
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);
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)
247 cycle = lg_delta >> 1;
250 drawpixel((int)x1, (int)y1, color);
252 if (cycle > lg_delta)
259 drawpixel((int)x1, (int)y1, color);
261 cycle = sh_delta >> 1;
264 drawpixel((int)x1, (int)y1, color);
266 if (cycle > sh_delta)
273 drawpixel((int)x1, (int)y1, color);
280 #define LOOP_DELAY 20.0
282 void SuperTux::fadeout(int fade_time)
284 float alpha_inc = 256 / (fade_time / LOOP_DELAY);
290 fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0,0,0, (int)alpha_inc); // left side
292 DrawingContext context; // ugly...
293 context.do_drawing();
295 SDL_Delay(int(LOOP_DELAY));
298 fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 255);
302 void SuperTux::shrink_fade(const Vector& point, int fade_time)
304 float left_inc = point.x / ((float)fade_time / LOOP_DELAY);
305 float right_inc = (SCREEN_WIDTH - point.x) / ((float)fade_time / LOOP_DELAY);
306 float up_inc = point.y / ((float)fade_time / LOOP_DELAY);
307 float down_inc = (SCREEN_HEIGHT - point.y) / ((float)fade_time / LOOP_DELAY);
309 float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
311 while(left_cor < point.x && right_cor < SCREEN_WIDTH - point.x &&
312 up_cor < point.y && down_cor < SCREEN_HEIGHT - point.y)
314 left_cor += left_inc;
315 right_cor += right_inc;
317 down_cor += down_inc;
319 fillrect(0, 0, left_cor, SCREEN_HEIGHT, 0,0,0); // left side
320 fillrect(SCREEN_WIDTH - right_cor, 0, right_cor, SCREEN_HEIGHT, 0,0,0); // right side
321 fillrect(0, 0, SCREEN_WIDTH, up_cor, 0,0,0); // up side
322 fillrect(0, SCREEN_HEIGHT - down_cor, SCREEN_WIDTH, down_cor+1, 0,0,0); // down side
323 DrawingContext context; // ugly...
324 context.do_drawing();
326 SDL_Delay(int(LOOP_DELAY));