4 Super Tux - Screen Functions
7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - April 22, 2000
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);
50 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
54 /* 'Stolen' from the SDL documentation.
55 * Set the pixel at (x, y) to the given value
56 * NOTE: The surface must be locked before calling this!
58 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
60 int bpp = surface->format->BytesPerPixel;
61 /* Here p is the address to the pixel we want to set */
62 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
75 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
77 p[0] = (pixel >> 16) & 0xff;
78 p[1] = (pixel >> 8) & 0xff;
84 p[1] = (pixel >> 8) & 0xff;
85 p[2] = (pixel >> 16) & 0xff;
95 /* Draw a single pixel on the screen. */
96 void drawpixel(int x, int y, Uint32 pixel)
98 /* Lock the screen for direct access to the pixels */
99 if ( SDL_MUSTLOCK(screen) )
101 if ( SDL_LockSurface(screen) < 0 )
103 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
108 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
109 putpixel(screen, x, y, pixel);
111 if ( SDL_MUSTLOCK(screen) )
113 SDL_UnlockSurface(screen);
115 /* Update just the part of the display that we've changed */
116 SDL_UpdateRect(screen, x, y, 1, 1);
119 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
125 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
126 glColor4ub(r, g, b,a);
138 /* Basic unantialiased Bresenham line algorithm */
139 int lg_delta, sh_delta, cycle, lg_step, sh_step;
140 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
144 lg_step = SGN(lg_delta);
145 lg_delta = ABS(lg_delta);
146 sh_step = SGN(sh_delta);
147 sh_delta = ABS(sh_delta);
148 if (sh_delta < lg_delta)
150 cycle = lg_delta >> 1;
153 drawpixel(x1, y1, color);
155 if (cycle > lg_delta)
162 drawpixel(x1, y1, color);
164 cycle = sh_delta >> 1;
167 drawpixel(x1, y1, color);
169 if (cycle > sh_delta)
176 drawpixel(x1, y1, color);
180 /* --- FILL A RECT --- */
182 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
199 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
200 glColor4ub(r, g, b,a);
205 glVertex2f(x+w, y+h);
214 SDL_Surface *temp = NULL;
223 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
224 screen->format->Rmask,
225 screen->format->Gmask,
226 screen->format->Bmask,
227 screen->format->Amask);
235 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
237 SDL_SetAlpha(temp, SDL_SRCALPHA, a);
239 SDL_BlitSurface(temp,0,screen,&rect);
241 SDL_FreeSurface(temp);
244 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
253 /* --- UPDATE SCREEN --- */
255 void updatescreen(void)
257 if(use_gl) /*clearscreen(0,0,0);*/
258 SDL_GL_SwapBuffers();
260 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
263 void flipscreen(void)
266 SDL_GL_SwapBuffers();
271 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
274 SDL_UpdateRect(scr, x, y, w, h);