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 "app/globals.h"
37 #include "video/screen.h"
38 #include "video/drawing_context.h"
39 #include "special/base.h"
41 /* 'Stolen' from the SDL documentation.
42 * Set the pixel at (x, y) to the given value
43 * NOTE: The surface must be locked before calling this!
45 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
47 int bpp = surface->format->BytesPerPixel;
48 /* Here p is the address to the pixel we want to set */
49 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
62 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
64 p[0] = (pixel >> 16) & 0xff;
65 p[1] = (pixel >> 8) & 0xff;
71 p[1] = (pixel >> 8) & 0xff;
72 p[2] = (pixel >> 16) & 0xff;
82 /* Draw a single pixel on the screen. */
83 void drawpixel(int x, int y, Uint32 pixel)
85 /* Lock the screen for direct access to the pixels */
86 if ( SDL_MUSTLOCK(screen) )
88 if ( SDL_LockSurface(screen) < 0 )
90 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
95 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
96 putpixel(screen, x, y, pixel);
98 if ( SDL_MUSTLOCK(screen) )
100 SDL_UnlockSurface(screen);
102 /* Update just the part of the display that we've changed */
103 SDL_UpdateRect(screen, x, y, 1, 1);
106 /* --- FILL A RECT --- */
108 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
125 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
126 glColor4ub(r, g, b,a);
131 glVertex2f(x+w, y+h);
140 SDL_Surface *temp = NULL;
149 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
150 screen->format->Rmask,
151 screen->format->Gmask,
152 screen->format->Bmask,
153 screen->format->Amask);
161 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
163 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
165 SDL_BlitSurface(temp,0,screen,&rect);
167 SDL_FreeSurface(temp);
170 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
178 /* Needed for line calculations */
179 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
180 #define ABS(x) ((x)>0 ? (x) : (-x))
183 draw_line(float x1, float y1, float x2, float y2, int r, int g, int b, int a)
189 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
190 glColor4ub(r, g, b,a);
201 /* Basic unantialiased Bresenham line algorithm */
202 int lg_delta, sh_delta, cycle, lg_step, sh_step;
203 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
205 lg_delta = (int)(x2 - x1);
206 sh_delta = (int)(y2 - y1);
207 lg_step = SGN(lg_delta);
208 lg_delta = ABS(lg_delta);
209 sh_step = SGN(sh_delta);
210 sh_delta = ABS(sh_delta);
211 if (sh_delta < lg_delta)
213 cycle = lg_delta >> 1;
216 drawpixel((int)x1, (int)y1, color);
218 if (cycle > lg_delta)
225 drawpixel((int)x1, (int)y1, color);
227 cycle = sh_delta >> 1;
230 drawpixel((int)x1, (int)y1, color);
232 if (cycle > sh_delta)
239 drawpixel((int)x1, (int)y1, color);
246 #define LOOP_DELAY 20.0
248 void fadeout(int fade_time)
250 float alpha_inc = 256 / (fade_time / LOOP_DELAY);
256 fillrect(0, 0, screen->w, screen->h, 0,0,0, (int)alpha_inc); // left side
258 DrawingContext context; // ugly...
259 context.do_drawing();
261 SDL_Delay(int(LOOP_DELAY));
264 fillrect(0, 0, screen->w, screen->h, 0, 0, 0, 255);
266 DrawingContext context;
267 context.draw_text_center(white_text, "Loading...",
268 Vector(0, screen->h/2), LAYER_FOREGROUND1);
269 context.do_drawing();
272 void shrink_fade(const Vector& point, int fade_time)
274 float left_inc = point.x / ((float)fade_time / LOOP_DELAY);
275 float right_inc = (screen->w - point.x) / ((float)fade_time / LOOP_DELAY);
276 float up_inc = point.y / ((float)fade_time / LOOP_DELAY);
277 float down_inc = (screen->h - point.y) / ((float)fade_time / LOOP_DELAY);
279 float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
281 while(left_cor < point.x && right_cor < screen->w - point.x &&
282 up_cor < point.y && down_cor < screen->h - point.y)
284 left_cor += left_inc;
285 right_cor += right_inc;
287 down_cor += down_inc;
289 fillrect(0, 0, left_cor, screen->h, 0,0,0); // left side
290 fillrect(screen->w - right_cor, 0, right_cor, screen->h, 0,0,0); // right side
291 fillrect(0, 0, screen->w, up_cor, 0,0,0); // up side
292 fillrect(0, screen->h - down_cor, screen->w, down_cor+1, 0,0,0); // down side
293 DrawingContext context; // ugly...
294 context.do_drawing();
296 SDL_Delay(int(LOOP_DELAY));