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);
82 for(float y = 0; y < 480; y += 2)
86 SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, (int)(((float)(top_r-bot_r)/640) * y + top_r), (int)(((float)(top_g-bot_g)/640) * y + top_g), (int)(((float)(top_b-bot_b)/640) * y + top_b)));
88 /* calculates the color for each line, based in the generic equation for functions: y = mx + b */
96 /* 'Stolen' from the SDL documentation.
97 * Set the pixel at (x, y) to the given value
98 * NOTE: The surface must be locked before calling this!
100 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
102 int bpp = surface->format->BytesPerPixel;
103 /* Here p is the address to the pixel we want to set */
104 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
113 *(Uint16 *)p = pixel;
117 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
119 p[0] = (pixel >> 16) & 0xff;
120 p[1] = (pixel >> 8) & 0xff;
126 p[1] = (pixel >> 8) & 0xff;
127 p[2] = (pixel >> 16) & 0xff;
132 *(Uint32 *)p = pixel;
137 /* Draw a single pixel on the screen. */
138 void drawpixel(int x, int y, Uint32 pixel)
140 /* Lock the screen for direct access to the pixels */
141 if ( SDL_MUSTLOCK(screen) )
143 if ( SDL_LockSurface(screen) < 0 )
145 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
150 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
151 putpixel(screen, x, y, pixel);
153 if ( SDL_MUSTLOCK(screen) )
155 SDL_UnlockSurface(screen);
157 /* Update just the part of the display that we've changed */
158 SDL_UpdateRect(screen, x, y, 1, 1);
161 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
167 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
168 glColor4ub(r, g, b,a);
180 /* Basic unantialiased Bresenham line algorithm */
181 int lg_delta, sh_delta, cycle, lg_step, sh_step;
182 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
186 lg_step = SGN(lg_delta);
187 lg_delta = ABS(lg_delta);
188 sh_step = SGN(sh_delta);
189 sh_delta = ABS(sh_delta);
190 if (sh_delta < lg_delta)
192 cycle = lg_delta >> 1;
195 drawpixel(x1, y1, color);
197 if (cycle > lg_delta)
204 drawpixel(x1, y1, color);
206 cycle = sh_delta >> 1;
209 drawpixel(x1, y1, color);
211 if (cycle > sh_delta)
218 drawpixel(x1, y1, color);
225 /* --- FILL A RECT --- */
227 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
244 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
245 glColor4ub(r, g, b,a);
250 glVertex2f(x+w, y+h);
259 SDL_Surface *temp = NULL;
268 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
269 screen->format->Rmask,
270 screen->format->Gmask,
271 screen->format->Bmask,
272 screen->format->Amask);
280 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
282 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
284 SDL_BlitSurface(temp,0,screen,&rect);
286 SDL_FreeSurface(temp);
289 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
298 /* --- UPDATE SCREEN --- */
300 void updatescreen(void)
302 if(use_gl) /*clearscreen(0,0,0);*/
303 SDL_GL_SwapBuffers();
305 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
308 void flipscreen(void)
311 SDL_GL_SwapBuffers();
316 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
319 SDL_UpdateRect(scr, x, y, w, h);