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(Color top_clr, Color bot_clr)
65 glColor3ub(top_clr.red, top_clr.green, top_clr.blue);
68 glColor3ub(bot_clr.red, bot_clr.green, bot_clr.blue);
77 for(float y = 0; y < 480; y += 2)
78 fillrect(0, (int)y, 640, 2,
79 (int)(((float)(top_clr.red-bot_clr.red)/(0-480)) * y + top_clr.red),
80 (int)(((float)(top_clr.green-bot_clr.green)/(0-480)) * y + top_clr.green),
81 (int)(((float)(top_clr.blue-bot_clr.blue)/(0-480)) * y + top_clr.blue), 255);
82 /* calculates the color for each line, based in the generic equation for functions: y = mx + b */
90 /* 'Stolen' from the SDL documentation.
91 * Set the pixel at (x, y) to the given value
92 * NOTE: The surface must be locked before calling this!
94 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
96 int bpp = surface->format->BytesPerPixel;
97 /* Here p is the address to the pixel we want to set */
98 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
107 *(Uint16 *)p = pixel;
111 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
113 p[0] = (pixel >> 16) & 0xff;
114 p[1] = (pixel >> 8) & 0xff;
120 p[1] = (pixel >> 8) & 0xff;
121 p[2] = (pixel >> 16) & 0xff;
126 *(Uint32 *)p = pixel;
131 /* Draw a single pixel on the screen. */
132 void drawpixel(int x, int y, Uint32 pixel)
134 /* Lock the screen for direct access to the pixels */
135 if ( SDL_MUSTLOCK(screen) )
137 if ( SDL_LockSurface(screen) < 0 )
139 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
144 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
145 putpixel(screen, x, y, pixel);
147 if ( SDL_MUSTLOCK(screen) )
149 SDL_UnlockSurface(screen);
151 /* Update just the part of the display that we've changed */
152 SDL_UpdateRect(screen, x, y, 1, 1);
155 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
161 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
162 glColor4ub(r, g, b,a);
174 /* Basic unantialiased Bresenham line algorithm */
175 int lg_delta, sh_delta, cycle, lg_step, sh_step;
176 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
180 lg_step = SGN(lg_delta);
181 lg_delta = ABS(lg_delta);
182 sh_step = SGN(sh_delta);
183 sh_delta = ABS(sh_delta);
184 if (sh_delta < lg_delta)
186 cycle = lg_delta >> 1;
189 drawpixel(x1, y1, color);
191 if (cycle > lg_delta)
198 drawpixel(x1, y1, color);
200 cycle = sh_delta >> 1;
203 drawpixel(x1, y1, color);
205 if (cycle > sh_delta)
212 drawpixel(x1, y1, color);
219 /* --- FILL A RECT --- */
221 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
238 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
239 glColor4ub(r, g, b,a);
244 glVertex2f(x+w, y+h);
253 SDL_Surface *temp = NULL;
262 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
263 screen->format->Rmask,
264 screen->format->Gmask,
265 screen->format->Bmask,
266 screen->format->Amask);
274 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
276 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
278 SDL_BlitSurface(temp,0,screen,&rect);
280 SDL_FreeSurface(temp);
283 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
292 /* --- UPDATE SCREEN --- */
294 void updatescreen(void)
296 if(use_gl) /*clearscreen(0,0,0);*/
297 SDL_GL_SwapBuffers();
299 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
302 void flipscreen(void)
305 SDL_GL_SwapBuffers();
310 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
313 SDL_UpdateRect(scr, x, y, w, h);