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 * Set the pixel at (x, y) to the given value
46 * NOTE: The surface must be locked before calling this!
48 void SuperTux::putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
50 int bpp = surface->format->BytesPerPixel;
51 /* Here p is the address to the pixel we want to set */
52 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
65 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
67 p[0] = (pixel >> 16) & 0xff;
68 p[1] = (pixel >> 8) & 0xff;
74 p[1] = (pixel >> 8) & 0xff;
75 p[2] = (pixel >> 16) & 0xff;
85 /* Draw a single pixel on the screen. */
86 void SuperTux::drawpixel(int x, int y, Uint32 pixel)
88 /* Lock the screen for direct access to the pixels */
89 if ( SDL_MUSTLOCK(screen) )
91 if ( SDL_LockSurface(screen) < 0 )
93 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
98 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
99 putpixel(screen, x, y, pixel);
101 if ( SDL_MUSTLOCK(screen) )
103 SDL_UnlockSurface(screen);
105 /* Update just the part of the display that we've changed */
106 SDL_UpdateRect(screen, x, y, 1, 1);
109 /* --- FILL A RECT --- */
111 void SuperTux::fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
128 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
129 glColor4ub(r, g, b,a);
134 glVertex2f(x+w, y+h);
143 SDL_Surface *temp = NULL;
152 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
153 screen->format->Rmask,
154 screen->format->Gmask,
155 screen->format->Bmask,
156 screen->format->Amask);
164 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
166 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
168 SDL_BlitSurface(temp,0,screen,&rect);
170 SDL_FreeSurface(temp);
173 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
181 /* Needed for line calculations */
182 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
183 #define ABS(x) ((x)>0 ? (x) : (-x))
185 void SuperTux::draw_line(float x1, float y1, float x2, float y2, int r, int g, int b, int a)
191 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
192 glColor4ub(r, g, b,a);
203 /* Basic unantialiased Bresenham line algorithm */
204 int lg_delta, sh_delta, cycle, lg_step, sh_step;
205 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
207 lg_delta = (int)(x2 - x1);
208 sh_delta = (int)(y2 - y1);
209 lg_step = SGN(lg_delta);
210 lg_delta = ABS(lg_delta);
211 sh_step = SGN(sh_delta);
212 sh_delta = ABS(sh_delta);
213 if (sh_delta < lg_delta)
215 cycle = lg_delta >> 1;
218 drawpixel((int)x1, (int)y1, color);
220 if (cycle > lg_delta)
227 drawpixel((int)x1, (int)y1, color);
229 cycle = sh_delta >> 1;
232 drawpixel((int)x1, (int)y1, color);
234 if (cycle > sh_delta)
241 drawpixel((int)x1, (int)y1, color);
248 #define LOOP_DELAY 20.0
250 void SuperTux::fadeout(int fade_time)
252 float alpha_inc = 256 / (fade_time / LOOP_DELAY);
258 fillrect(0, 0, screen->w, screen->h, 0,0,0, (int)alpha_inc); // left side
260 DrawingContext context; // ugly...
261 context.do_drawing();
263 SDL_Delay(int(LOOP_DELAY));
266 fillrect(0, 0, screen->w, screen->h, 0, 0, 0, 255);
270 void SuperTux::shrink_fade(const Vector& point, int fade_time)
272 float left_inc = point.x / ((float)fade_time / LOOP_DELAY);
273 float right_inc = (screen->w - point.x) / ((float)fade_time / LOOP_DELAY);
274 float up_inc = point.y / ((float)fade_time / LOOP_DELAY);
275 float down_inc = (screen->h - point.y) / ((float)fade_time / LOOP_DELAY);
277 float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
279 while(left_cor < point.x && right_cor < screen->w - point.x &&
280 up_cor < point.y && down_cor < screen->h - point.y)
282 left_cor += left_inc;
283 right_cor += right_inc;
285 down_cor += down_inc;
287 fillrect(0, 0, left_cor, screen->h, 0,0,0); // left side
288 fillrect(screen->w - right_cor, 0, right_cor, screen->h, 0,0,0); // right side
289 fillrect(0, 0, screen->w, up_cor, 0,0,0); // up side
290 fillrect(0, screen->h - down_cor, screen->w, down_cor+1, 0,0,0); // down side
291 DrawingContext context; // ugly...
292 context.do_drawing();
294 SDL_Delay(int(LOOP_DELAY));