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 /* --- DRAWS A VERTICAL GRADIENT --- */
59 void drawgradient(int top_r, int top_g, int top_b, int bot_r, int bot_g, int bot_b)
65 glColor3ub(top_r, top_g, top_b);
68 glColor3ub(bot_r, bot_g, bot_b);
77 for(float y = 0; y < 480; y += 2)
78 fillrect(0, (int)y, 640, 2, (int)(((float)(top_r-bot_r)/640) * y + top_r),
79 (int)(((float)(top_g-bot_g)/640) * y + top_g),
80 (int)(((float)(top_b-bot_b)/640) * y + top_b), 255);
81 /* calculates the color for each line, based in the generic equation for functions: y = mx + b */
89 /* 'Stolen' from the SDL documentation.
90 * Set the pixel at (x, y) to the given value
91 * NOTE: The surface must be locked before calling this!
93 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
95 int bpp = surface->format->BytesPerPixel;
96 /* Here p is the address to the pixel we want to set */
97 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
106 *(Uint16 *)p = pixel;
110 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
112 p[0] = (pixel >> 16) & 0xff;
113 p[1] = (pixel >> 8) & 0xff;
119 p[1] = (pixel >> 8) & 0xff;
120 p[2] = (pixel >> 16) & 0xff;
125 *(Uint32 *)p = pixel;
130 /* Draw a single pixel on the screen. */
131 void drawpixel(int x, int y, Uint32 pixel)
133 /* Lock the screen for direct access to the pixels */
134 if ( SDL_MUSTLOCK(screen) )
136 if ( SDL_LockSurface(screen) < 0 )
138 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
143 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
144 putpixel(screen, x, y, pixel);
146 if ( SDL_MUSTLOCK(screen) )
148 SDL_UnlockSurface(screen);
150 /* Update just the part of the display that we've changed */
151 SDL_UpdateRect(screen, x, y, 1, 1);
154 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
160 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
161 glColor4ub(r, g, b,a);
173 /* Basic unantialiased Bresenham line algorithm */
174 int lg_delta, sh_delta, cycle, lg_step, sh_step;
175 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
179 lg_step = SGN(lg_delta);
180 lg_delta = ABS(lg_delta);
181 sh_step = SGN(sh_delta);
182 sh_delta = ABS(sh_delta);
183 if (sh_delta < lg_delta)
185 cycle = lg_delta >> 1;
188 drawpixel(x1, y1, color);
190 if (cycle > lg_delta)
197 drawpixel(x1, y1, color);
199 cycle = sh_delta >> 1;
202 drawpixel(x1, y1, color);
204 if (cycle > sh_delta)
211 drawpixel(x1, y1, color);
218 /* --- FILL A RECT --- */
220 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
237 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
238 glColor4ub(r, g, b,a);
243 glVertex2f(x+w, y+h);
252 SDL_Surface *temp = NULL;
261 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
262 screen->format->Rmask,
263 screen->format->Gmask,
264 screen->format->Bmask,
265 screen->format->Amask);
273 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
275 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
277 SDL_BlitSurface(temp,0,screen,&rect);
279 SDL_FreeSurface(temp);
282 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
291 /* --- UPDATE SCREEN --- */
293 void updatescreen(void)
295 if(use_gl) /*clearscreen(0,0,0);*/
296 SDL_GL_SwapBuffers();
298 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
301 void flipscreen(void)
304 SDL_GL_SwapBuffers();
309 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
312 SDL_UpdateRect(scr, x, y, w, h);