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>
40 /* Needed for line calculations */
41 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
42 #define ABS(x) ((x)>0 ? (x) : (-x))
44 /* --- CLEAR SCREEN --- */
46 void clearscreen(int r, int g, int b)
51 glClearColor(r/256, g/256, b/256, 1.0);
52 glClear(GL_COLOR_BUFFER_BIT);
58 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
65 /* --- DRAWS A VERTICAL GRADIENT --- */
67 void drawgradient(Color top_clr, Color bot_clr)
73 glColor3ub(top_clr.red, top_clr.green, top_clr.blue);
75 glVertex2f(screen->w, 0);
76 glColor3ub(bot_clr.red, bot_clr.green, bot_clr.blue);
77 glVertex2f(screen->w, screen->h);
78 glVertex2f(0, screen->h);
85 for(float y = 0; y < screen->h; y += 2)
86 fillrect(0, (int)y, screen->w, 2,
87 (int)(((float)(top_clr.red-bot_clr.red)/(0-screen->h)) * y + top_clr.red),
88 (int)(((float)(top_clr.green-bot_clr.green)/(0-screen->h)) * y + top_clr.green),
89 (int)(((float)(top_clr.blue-bot_clr.blue)/(0-screen->h)) * y + top_clr.blue),
91 /* calculates the color for each line, based in the generic equation for functions: y = mx + b */
101 /** Fades the given surface into a black one. If fade_out is true, it will fade out, else
104 void fade(Surface *surface, int seconds, bool fade_out);
106 void fade(const std::string& surface, int seconds, bool fade_out)
108 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
109 fade(sur, seconds, fade_out);
113 void fade(Surface *surface, int seconds, bool fade_out)
121 int cur_time, old_time;
122 cur_time = SDL_GetTicks();
124 while(alpha >= 0 && alpha < 256)
126 surface->draw(0,0,(int)alpha);
130 cur_time = SDL_GetTicks();
132 /* Calculate the next alpha value */
133 float calc = (float) ((cur_time - old_time) / seconds);
141 /** This fade shrinks to the given point */
143 #define LOOP_DELAY 20
144 void shrink_fade(Point point, int fade_time)
146 float left_inc = (float)point.x / ((float)fade_time / LOOP_DELAY);
147 float right_inc = ((float)screen->w - point.x) / ((float)fade_time / LOOP_DELAY);
148 float up_inc = (float)point.y / ((float)fade_time / LOOP_DELAY);
149 float down_inc = ((float)screen->h - point.y) / ((float)fade_time / LOOP_DELAY);
151 float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
153 while(left_cor < screen->w - point.x && right_cor < screen->w - point.x &&
154 up_cor < screen->h - point.y && down_cor < screen->h - point.y)
156 left_cor += left_inc;
157 right_cor += right_inc;
159 down_cor += down_inc;
161 fillrect(0, 0, left_cor, screen->h, 0,0,0); // left side
162 fillrect(screen->w - right_cor, 0, right_cor, screen->h, 0,0,0); // right side
163 fillrect(0, 0, screen->w, up_cor, 0,0,0); // up side
164 fillrect(0, screen->h - down_cor, screen->w, down_cor, 0,0,0); // down side
167 SDL_Delay(LOOP_DELAY);
171 /* 'Stolen' from the SDL documentation.
172 * Set the pixel at (x, y) to the given value
173 * NOTE: The surface must be locked before calling this!
175 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
177 int bpp = surface->format->BytesPerPixel;
178 /* Here p is the address to the pixel we want to set */
179 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
188 *(Uint16 *)p = pixel;
192 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
194 p[0] = (pixel >> 16) & 0xff;
195 p[1] = (pixel >> 8) & 0xff;
201 p[1] = (pixel >> 8) & 0xff;
202 p[2] = (pixel >> 16) & 0xff;
207 *(Uint32 *)p = pixel;
212 /* Draw a single pixel on the screen. */
213 void drawpixel(int x, int y, Uint32 pixel)
215 /* Lock the screen for direct access to the pixels */
216 if ( SDL_MUSTLOCK(screen) )
218 if ( SDL_LockSurface(screen) < 0 )
220 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
225 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
226 putpixel(screen, x, y, pixel);
228 if ( SDL_MUSTLOCK(screen) )
230 SDL_UnlockSurface(screen);
232 /* Update just the part of the display that we've changed */
233 SDL_UpdateRect(screen, x, y, 1, 1);
236 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
242 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
243 glColor4ub(r, g, b,a);
255 /* Basic unantialiased Bresenham line algorithm */
256 int lg_delta, sh_delta, cycle, lg_step, sh_step;
257 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
261 lg_step = SGN(lg_delta);
262 lg_delta = ABS(lg_delta);
263 sh_step = SGN(sh_delta);
264 sh_delta = ABS(sh_delta);
265 if (sh_delta < lg_delta)
267 cycle = lg_delta >> 1;
270 drawpixel(x1, y1, color);
272 if (cycle > lg_delta)
279 drawpixel(x1, y1, color);
281 cycle = sh_delta >> 1;
284 drawpixel(x1, y1, color);
286 if (cycle > sh_delta)
293 drawpixel(x1, y1, color);
300 /* --- FILL A RECT --- */
302 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
319 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
320 glColor4ub(r, g, b,a);
325 glVertex2f(x+w, y+h);
334 SDL_Surface *temp = NULL;
343 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
344 screen->format->Rmask,
345 screen->format->Gmask,
346 screen->format->Bmask,
347 screen->format->Amask);
355 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
357 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
359 SDL_BlitSurface(temp,0,screen,&rect);
361 SDL_FreeSurface(temp);
364 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
373 /* --- UPDATE SCREEN --- */
375 void updatescreen(void)
377 if(use_gl) /*clearscreen(0,0,0);*/
378 SDL_GL_SwapBuffers();
380 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
383 void flipscreen(void)
386 SDL_GL_SwapBuffers();
393 clearscreen(0, 0, 0);
394 white_text->draw_align("Loading...", screen->w/2, screen->h/2, A_HMIDDLE, A_TOP);
398 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
401 SDL_UpdateRect(scr, x, y, w, h);