4 Super Tux - Screen Functions
7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - March 15, 2004
19 #include <SDL_image.h>
23 #include <sys/types.h>
33 /* Needed for line calculations */
34 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
35 #define ABS(x) ((x)>0 ? (x) : (-x))
37 /* --- CLEAR SCREEN --- */
39 void clearscreen(int r, int g, int b)
44 glClearColor(r/256, g/256, b/256, 1.0);
45 glClear(GL_COLOR_BUFFER_BIT);
51 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
58 /* 'Stolen' from the SDL documentation.
59 * Set the pixel at (x, y) to the given value
60 * NOTE: The surface must be locked before calling this!
62 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
64 int bpp = surface->format->BytesPerPixel;
65 /* Here p is the address to the pixel we want to set */
66 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
79 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
81 p[0] = (pixel >> 16) & 0xff;
82 p[1] = (pixel >> 8) & 0xff;
88 p[1] = (pixel >> 8) & 0xff;
89 p[2] = (pixel >> 16) & 0xff;
99 /* Draw a single pixel on the screen. */
100 void drawpixel(int x, int y, Uint32 pixel)
102 /* Lock the screen for direct access to the pixels */
103 if ( SDL_MUSTLOCK(screen) )
105 if ( SDL_LockSurface(screen) < 0 )
107 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
112 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
113 putpixel(screen, x, y, pixel);
115 if ( SDL_MUSTLOCK(screen) )
117 SDL_UnlockSurface(screen);
119 /* Update just the part of the display that we've changed */
120 SDL_UpdateRect(screen, x, y, 1, 1);
123 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
129 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
130 glColor4ub(r, g, b,a);
142 /* Basic unantialiased Bresenham line algorithm */
143 int lg_delta, sh_delta, cycle, lg_step, sh_step;
144 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
148 lg_step = SGN(lg_delta);
149 lg_delta = ABS(lg_delta);
150 sh_step = SGN(sh_delta);
151 sh_delta = ABS(sh_delta);
152 if (sh_delta < lg_delta)
154 cycle = lg_delta >> 1;
157 drawpixel(x1, y1, color);
159 if (cycle > lg_delta)
166 drawpixel(x1, y1, color);
168 cycle = sh_delta >> 1;
171 drawpixel(x1, y1, color);
173 if (cycle > sh_delta)
180 drawpixel(x1, y1, color);
187 /* --- FILL A RECT --- */
189 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
206 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
207 glColor4ub(r, g, b,a);
212 glVertex2f(x+w, y+h);
221 SDL_Surface *temp = NULL;
230 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
231 screen->format->Rmask,
232 screen->format->Gmask,
233 screen->format->Bmask,
234 screen->format->Amask);
242 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
244 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
246 SDL_BlitSurface(temp,0,screen,&rect);
248 SDL_FreeSurface(temp);
251 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
260 /* --- UPDATE SCREEN --- */
262 void updatescreen(void)
264 if(use_gl) /*clearscreen(0,0,0);*/
265 SDL_GL_SwapBuffers();
267 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
270 void flipscreen(void)
273 SDL_GL_SwapBuffers();
278 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
281 SDL_UpdateRect(scr, x, y, w, h);