1 // $Id: screen.cpp 2334 2005-04-04 16:26:14Z grumbel $
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.
31 #include <SDL_image.h>
34 #include <sys/types.h>
38 #include "gameconfig.h"
40 #include "app/globals.h"
41 #include "video/drawing_context.h"
42 #include "math/vector.h"
44 static const float LOOP_DELAY = 20.0;
45 extern SDL_Surface* screen;
47 using namespace SuperTux;
49 /* 'Stolen' from the SDL documentation.
50 * Return the pixel value at (x, y)
51 * NOTE: The surface must be locked before calling this!
53 Uint32 getpixel(SDL_Surface *surface, int x, int y)
55 int bpp = surface->format->BytesPerPixel;
56 /* Here p is the address to the pixel we want to retrieve */
57 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
67 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
68 return p[0] << 16 | p[1] << 8 | p[2];
70 return p[0] | p[1] << 8 | p[2] << 16;
76 return 0; /* shouldn't happen, but avoids warnings */
80 /* 'Stolen' from the SDL documentation.
81 * Set the pixel at (x, y) to the given value
82 * NOTE: The surface must be locked before calling this!
84 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
86 int bpp = surface->format->BytesPerPixel;
87 /* Here p is the address to the pixel we want to set */
88 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
100 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
102 p[0] = (pixel >> 16) & 0xff;
103 p[1] = (pixel >> 8) & 0xff;
109 p[1] = (pixel >> 8) & 0xff;
110 p[2] = (pixel >> 16) & 0xff;
115 *(Uint32 *)p = pixel;
123 /* Draw a single pixel on the screen. */
124 void drawpixel(int x, int y, Uint32 pixel)
126 /* Lock the screen for direct access to the pixels */
127 if ( SDL_MUSTLOCK(screen) )
129 if ( SDL_LockSurface(screen) < 0 )
131 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
136 if(!(x < 0 || y < 0 || x > SCREEN_WIDTH || y > SCREEN_HEIGHT))
137 putpixel(screen, x, y, pixel);
139 if ( SDL_MUSTLOCK(screen) )
141 SDL_UnlockSurface(screen);
143 /* Update just the part of the display that we've changed */
144 SDL_UpdateRect(screen, x, y, 1, 1);
147 /* --- FILL A RECT --- */
149 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
162 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
163 glColor4ub(r, g, b,a);
168 glVertex2f(x+w, y+h);
174 SDL_Surface *temp = NULL;
182 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
183 screen->format->Rmask,
184 screen->format->Gmask,
185 screen->format->Bmask,
186 screen->format->Amask);
194 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
195 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
196 SDL_BlitSurface(temp,0,screen,&rect);
197 SDL_FreeSurface(temp);
199 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
204 /* Needed for line calculations */
205 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
206 #define ABS(x) ((x)>0 ? (x) : (-x))
208 void draw_line(float x1, float y1, float x2, float y2,
209 int r, int g, int b, int a)
213 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
214 glColor4ub(r, g, b,a);
222 /* Basic unantialiased Bresenham line algorithm */
223 int lg_delta, sh_delta, cycle, lg_step, sh_step;
224 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
226 lg_delta = (int)(x2 - x1);
227 sh_delta = (int)(y2 - y1);
228 lg_step = SGN(lg_delta);
229 lg_delta = ABS(lg_delta);
230 sh_step = SGN(sh_delta);
231 sh_delta = ABS(sh_delta);
232 if (sh_delta < lg_delta) {
233 cycle = lg_delta >> 1;
235 drawpixel((int)x1, (int)y1, color);
237 if (cycle > lg_delta) {
243 drawpixel((int)x1, (int)y1, color);
245 cycle = sh_delta >> 1;
247 drawpixel((int)x1, (int)y1, color);
249 if (cycle > sh_delta) {
255 drawpixel((int)x1, (int)y1, color);
260 void fadeout(int fade_time)
262 float alpha_inc = 256 / (fade_time / LOOP_DELAY);
267 fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0,0,0, (int)alpha_inc); // left side
269 DrawingContext context; // ugly...
270 context.do_drawing();
272 SDL_Delay(int(LOOP_DELAY));
275 fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 255);
278 void shrink_fade(const Vector& point, int fade_time)
280 float left_inc = point.x / ((float)fade_time / LOOP_DELAY);
281 float right_inc = (SCREEN_WIDTH - point.x) / ((float)fade_time / LOOP_DELAY);
282 float up_inc = point.y / ((float)fade_time / LOOP_DELAY);
283 float down_inc = (SCREEN_HEIGHT - point.y) / ((float)fade_time / LOOP_DELAY);
285 float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
287 while(left_cor < point.x && right_cor < SCREEN_WIDTH - point.x &&
288 up_cor < point.y && down_cor < SCREEN_HEIGHT - point.y) {
289 left_cor += left_inc;
290 right_cor += right_inc;
292 down_cor += down_inc;
294 fillrect(0, 0, left_cor, SCREEN_HEIGHT, 0,0,0); // left side
295 fillrect(SCREEN_WIDTH - right_cor, 0, right_cor, SCREEN_HEIGHT, 0,0,0); // right side
296 fillrect(0, 0, SCREEN_WIDTH, up_cor, 0,0,0); // up side
297 fillrect(0, SCREEN_HEIGHT - down_cor, SCREEN_WIDTH, down_cor+1, 0,0,0); // down side
298 DrawingContext context; // ugly...
299 context.do_drawing();
301 SDL_Delay(int(LOOP_DELAY));