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.
27 #include <SDL_image.h>
30 #include <sys/types.h>
38 #include "drawing_context.h"
41 /* Needed for line calculations */
42 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
43 #define ABS(x) ((x)>0 ? (x) : (-x))
47 /** Fades the given surface into a black one. If fade_out is true, it will fade out, else
51 void fade(Surface *surface, int seconds, bool fade_out);
53 void fade(const std::string& surface, int seconds, bool fade_out)
55 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
56 fade(sur, seconds, fade_out);
60 void fade(Surface *surface, int seconds, bool fade_out)
68 int cur_time, old_time;
69 cur_time = SDL_GetTicks();
71 while(alpha >= 0 && alpha < 256)
73 surface->draw(0,0,(int)alpha);
77 cur_time = SDL_GetTicks();
79 /* Calculate the next alpha value */
80 float calc = (float) ((cur_time - old_time) / seconds);
89 /* 'Stolen' from the SDL documentation.
90 * Set the pixel at (x, y) to the given value
91 * NOTE: The surface must be locked before calling this!
93 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
95 int bpp = surface->format->BytesPerPixel;
96 /* Here p is the address to the pixel we want to set */
97 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
106 *(Uint16 *)p = pixel;
110 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
112 p[0] = (pixel >> 16) & 0xff;
113 p[1] = (pixel >> 8) & 0xff;
119 p[1] = (pixel >> 8) & 0xff;
120 p[2] = (pixel >> 16) & 0xff;
125 *(Uint32 *)p = pixel;
130 /* Draw a single pixel on the screen. */
131 void drawpixel(int x, int y, Uint32 pixel)
133 /* Lock the screen for direct access to the pixels */
134 if ( SDL_MUSTLOCK(screen) )
136 if ( SDL_LockSurface(screen) < 0 )
138 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
143 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
144 putpixel(screen, x, y, pixel);
146 if ( SDL_MUSTLOCK(screen) )
148 SDL_UnlockSurface(screen);
150 /* Update just the part of the display that we've changed */
151 SDL_UpdateRect(screen, x, y, 1, 1);
154 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
160 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
161 glColor4ub(r, g, b,a);
173 /* Basic unantialiased Bresenham line algorithm */
174 int lg_delta, sh_delta, cycle, lg_step, sh_step;
175 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
179 lg_step = SGN(lg_delta);
180 lg_delta = ABS(lg_delta);
181 sh_step = SGN(sh_delta);
182 sh_delta = ABS(sh_delta);
183 if (sh_delta < lg_delta)
185 cycle = lg_delta >> 1;
188 drawpixel(x1, y1, color);
190 if (cycle > lg_delta)
197 drawpixel(x1, y1, color);
199 cycle = sh_delta >> 1;
202 drawpixel(x1, y1, color);
204 if (cycle > sh_delta)
211 drawpixel(x1, y1, color);
218 /* --- FILL A RECT --- */
220 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
237 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
238 glColor4ub(r, g, b,a);
243 glVertex2f(x+w, y+h);
252 SDL_Surface *temp = NULL;
261 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
262 screen->format->Rmask,
263 screen->format->Gmask,
264 screen->format->Bmask,
265 screen->format->Amask);
273 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
275 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
277 SDL_BlitSurface(temp,0,screen,&rect);
279 SDL_FreeSurface(temp);
282 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
293 fillrect(0, 0, screen->w, screen->h, 0, 0, 0, 255);
295 DrawingContext context;
296 context.draw_text_center(white_text, "Loading...",
297 Vector(0, screen->h/2), LAYER_FOREGROUND1);
298 context.do_drawing();
301 #define LOOP_DELAY 20.0
302 void shrink_fade(const Vector& point, float fade_time)
304 float left_inc = point.x / (fade_time / LOOP_DELAY);
305 float right_inc = (screen->w - point.x) / (fade_time / LOOP_DELAY);
306 float up_inc = point.y / (fade_time / LOOP_DELAY);
307 float down_inc = (screen->h - point.y) / (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->w - point.x &&
312 up_cor < point.y && down_cor < screen->h - point.y)
314 left_cor += left_inc;
315 right_cor += right_inc;
317 down_cor += down_inc;
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();
326 SDL_Delay(int(LOOP_DELAY));