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.
29 #include "SDL_image.h"
32 #include <sys/types.h>
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"
42 using namespace SuperTux;
44 /* 'Stolen' from the SDL documentation.
45 * Return the pixel value at (x, y)
46 * NOTE: The surface must be locked before calling this!
48 Uint32 SuperTux::getpixel(SDL_Surface *surface, int x, int y)
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;
62 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
63 return p[0] << 16 | p[1] << 8 | p[2];
65 return p[0] | p[1] << 8 | p[2] << 16;
71 return 0; /* shouldn't happen, but avoids warnings */
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!
79 void SuperTux::putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
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;
96 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
98 p[0] = (pixel >> 16) & 0xff;
99 p[1] = (pixel >> 8) & 0xff;
105 p[1] = (pixel >> 8) & 0xff;
106 p[2] = (pixel >> 16) & 0xff;
111 *(Uint32 *)p = pixel;
116 /* Draw a single pixel on the screen. */
117 void SuperTux::drawpixel(int x, int y, Uint32 pixel)
119 /* Lock the screen for direct access to the pixels */
120 if ( SDL_MUSTLOCK(screen) )
122 if ( SDL_LockSurface(screen) < 0 )
124 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
129 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
130 putpixel(screen, x, y, pixel);
132 if ( SDL_MUSTLOCK(screen) )
134 SDL_UnlockSurface(screen);
136 /* Update just the part of the display that we've changed */
137 SDL_UpdateRect(screen, x, y, 1, 1);
140 /* --- FILL A RECT --- */
142 void SuperTux::fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
159 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
160 glColor4ub(r, g, b,a);
165 glVertex2f(x+w, y+h);
174 SDL_Surface *temp = NULL;
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);
195 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
197 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
199 SDL_BlitSurface(temp,0,screen,&rect);
201 SDL_FreeSurface(temp);
204 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
212 /* Needed for line calculations */
213 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
214 #define ABS(x) ((x)>0 ? (x) : (-x))
216 void SuperTux::draw_line(float x1, float y1, float x2, float y2, int r, int g, int b, int a)
222 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
223 glColor4ub(r, g, b,a);
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);
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)
246 cycle = lg_delta >> 1;
249 drawpixel((int)x1, (int)y1, color);
251 if (cycle > lg_delta)
258 drawpixel((int)x1, (int)y1, color);
260 cycle = sh_delta >> 1;
263 drawpixel((int)x1, (int)y1, color);
265 if (cycle > sh_delta)
272 drawpixel((int)x1, (int)y1, color);
279 #define LOOP_DELAY 20.0
281 void SuperTux::fadeout(int fade_time)
283 float alpha_inc = 256 / (fade_time / LOOP_DELAY);
289 fillrect(0, 0, screen->w, screen->h, 0,0,0, (int)alpha_inc); // left side
291 DrawingContext context; // ugly...
292 context.do_drawing();
294 SDL_Delay(int(LOOP_DELAY));
297 fillrect(0, 0, screen->w, screen->h, 0, 0, 0, 255);
301 void SuperTux::shrink_fade(const Vector& point, int fade_time)
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);
308 float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
310 while(left_cor < point.x && right_cor < screen->w - point.x &&
311 up_cor < point.y && down_cor < screen->h - point.y)
313 left_cor += left_inc;
314 right_cor += right_inc;
316 down_cor += down_inc;
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();
325 SDL_Delay(int(LOOP_DELAY));