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.
26 #include <SDL_image.h>
29 #include <sys/types.h>
39 /* Needed for line calculations */
40 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
41 #define ABS(x) ((x)>0 ? (x) : (-x))
43 /* --- CLEAR SCREEN --- */
45 void clearscreen(int r, int g, int b)
50 glClearColor(r/256, g/256, b/256, 1.0);
51 glClear(GL_COLOR_BUFFER_BIT);
57 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
64 /* --- DRAWS A VERTICAL GRADIENT --- */
66 void drawgradient(Color top_clr, Color bot_clr)
72 glColor3ub(top_clr.red, top_clr.green, top_clr.blue);
75 glColor3ub(bot_clr.red, bot_clr.green, bot_clr.blue);
84 for(float y = 0; y < 480; y += 2)
85 fillrect(0, (int)y, 640, 2,
86 (int)(((float)(top_clr.red-bot_clr.red)/(0-480)) * y + top_clr.red),
87 (int)(((float)(top_clr.green-bot_clr.green)/(0-480)) * y + top_clr.green),
88 (int)(((float)(top_clr.blue-bot_clr.blue)/(0-480)) * y + top_clr.blue), 255);
89 /* calculates the color for each line, based in the generic equation for functions: y = mx + b */
97 /* 'Stolen' from the SDL documentation.
98 * Set the pixel at (x, y) to the given value
99 * NOTE: The surface must be locked before calling this!
101 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
103 int bpp = surface->format->BytesPerPixel;
104 /* Here p is the address to the pixel we want to set */
105 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
114 *(Uint16 *)p = pixel;
118 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
120 p[0] = (pixel >> 16) & 0xff;
121 p[1] = (pixel >> 8) & 0xff;
127 p[1] = (pixel >> 8) & 0xff;
128 p[2] = (pixel >> 16) & 0xff;
133 *(Uint32 *)p = pixel;
138 /* Draw a single pixel on the screen. */
139 void drawpixel(int x, int y, Uint32 pixel)
141 /* Lock the screen for direct access to the pixels */
142 if ( SDL_MUSTLOCK(screen) )
144 if ( SDL_LockSurface(screen) < 0 )
146 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
151 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
152 putpixel(screen, x, y, pixel);
154 if ( SDL_MUSTLOCK(screen) )
156 SDL_UnlockSurface(screen);
158 /* Update just the part of the display that we've changed */
159 SDL_UpdateRect(screen, x, y, 1, 1);
162 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
168 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
169 glColor4ub(r, g, b,a);
181 /* Basic unantialiased Bresenham line algorithm */
182 int lg_delta, sh_delta, cycle, lg_step, sh_step;
183 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
187 lg_step = SGN(lg_delta);
188 lg_delta = ABS(lg_delta);
189 sh_step = SGN(sh_delta);
190 sh_delta = ABS(sh_delta);
191 if (sh_delta < lg_delta)
193 cycle = lg_delta >> 1;
196 drawpixel(x1, y1, color);
198 if (cycle > lg_delta)
205 drawpixel(x1, y1, color);
207 cycle = sh_delta >> 1;
210 drawpixel(x1, y1, color);
212 if (cycle > sh_delta)
219 drawpixel(x1, y1, color);
226 /* --- FILL A RECT --- */
228 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
245 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
246 glColor4ub(r, g, b,a);
251 glVertex2f(x+w, y+h);
260 SDL_Surface *temp = NULL;
269 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
270 screen->format->Rmask,
271 screen->format->Gmask,
272 screen->format->Bmask,
273 screen->format->Amask);
281 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
283 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
285 SDL_BlitSurface(temp,0,screen,&rect);
287 SDL_FreeSurface(temp);
290 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
299 /* --- UPDATE SCREEN --- */
301 void updatescreen(void)
303 if(use_gl) /*clearscreen(0,0,0);*/
304 SDL_GL_SwapBuffers();
306 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
309 void flipscreen(void)
312 SDL_GL_SwapBuffers();
317 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
320 SDL_UpdateRect(scr, x, y, w, h);