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>
22 #include <sys/types.h>
32 /* Needed for line calculations */
33 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
34 #define ABS(x) ((x)>0 ? (x) : (-x))
36 /* --- CLEAR SCREEN --- */
38 void clearscreen(int r, int g, int b)
43 glClearColor(r/256, g/256, b/256, 1.0);
44 glClear(GL_COLOR_BUFFER_BIT);
50 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
57 /* 'Stolen' from the SDL documentation.
58 * Set the pixel at (x, y) to the given value
59 * NOTE: The surface must be locked before calling this!
61 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
63 int bpp = surface->format->BytesPerPixel;
64 /* Here p is the address to the pixel we want to set */
65 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
78 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
80 p[0] = (pixel >> 16) & 0xff;
81 p[1] = (pixel >> 8) & 0xff;
87 p[1] = (pixel >> 8) & 0xff;
88 p[2] = (pixel >> 16) & 0xff;
98 /* Draw a single pixel on the screen. */
99 void drawpixel(int x, int y, Uint32 pixel)
101 /* Lock the screen for direct access to the pixels */
102 if ( SDL_MUSTLOCK(screen) )
104 if ( SDL_LockSurface(screen) < 0 )
106 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
111 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
112 putpixel(screen, x, y, pixel);
114 if ( SDL_MUSTLOCK(screen) )
116 SDL_UnlockSurface(screen);
118 /* Update just the part of the display that we've changed */
119 SDL_UpdateRect(screen, x, y, 1, 1);
122 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
128 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
129 glColor4ub(r, g, b,a);
141 /* Basic unantialiased Bresenham line algorithm */
142 int lg_delta, sh_delta, cycle, lg_step, sh_step;
143 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
147 lg_step = SGN(lg_delta);
148 lg_delta = ABS(lg_delta);
149 sh_step = SGN(sh_delta);
150 sh_delta = ABS(sh_delta);
151 if (sh_delta < lg_delta)
153 cycle = lg_delta >> 1;
156 drawpixel(x1, y1, color);
158 if (cycle > lg_delta)
165 drawpixel(x1, y1, color);
167 cycle = sh_delta >> 1;
170 drawpixel(x1, y1, color);
172 if (cycle > sh_delta)
179 drawpixel(x1, y1, color);
186 /* --- FILL A RECT --- */
188 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
205 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
206 glColor4ub(r, g, b,a);
211 glVertex2f(x+w, y+h);
220 SDL_Surface *temp = NULL;
229 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
230 screen->format->Rmask,
231 screen->format->Gmask,
232 screen->format->Bmask,
233 screen->format->Amask);
241 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
243 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
245 SDL_BlitSurface(temp,0,screen,&rect);
247 SDL_FreeSurface(temp);
250 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
259 /* --- UPDATE SCREEN --- */
261 void updatescreen(void)
263 if(use_gl) /*clearscreen(0,0,0);*/
264 SDL_GL_SwapBuffers();
266 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
269 void flipscreen(void)
272 SDL_GL_SwapBuffers();
277 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
280 SDL_UpdateRect(scr, x, y, w, h);