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 /* --- LOAD AND DISPLAY AN IMAGE --- */
39 void load_and_display_image(char * file)
43 img = load_image(file, IGNORE_ALPHA);
44 SDL_BlitSurface(img, NULL, screen, NULL);
49 /* --- CLEAR SCREEN --- */
51 void clearscreen(int r, int g, int b)
56 glClearColor(r/256, g/256, b/256, 1.0);
57 glClear(GL_COLOR_BUFFER_BIT);
62 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
66 /* 'Stolen' from the SDL documentation.
67 * Set the pixel at (x, y) to the given value
68 * NOTE: The surface must be locked before calling this!
70 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
72 int bpp = surface->format->BytesPerPixel;
73 /* Here p is the address to the pixel we want to set */
74 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
87 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
89 p[0] = (pixel >> 16) & 0xff;
90 p[1] = (pixel >> 8) & 0xff;
96 p[1] = (pixel >> 8) & 0xff;
97 p[2] = (pixel >> 16) & 0xff;
102 *(Uint32 *)p = pixel;
107 void drawpixel(int x, int y, Uint32 pixel)
109 /* Lock the screen for direct access to the pixels */
110 if ( SDL_MUSTLOCK(screen) )
112 if ( SDL_LockSurface(screen) < 0 )
114 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
119 if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
120 putpixel(screen, x, y, pixel);
122 if ( SDL_MUSTLOCK(screen) )
124 SDL_UnlockSurface(screen);
126 /* Update just the part of the display that we've changed */
127 SDL_UpdateRect(screen, x, y, 1, 1);
130 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
136 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
137 glColor4ub(r, g, b,a);
149 /* Basic unantialiased Bresenham line algorithm */
150 int lg_delta, sh_delta, cycle, lg_step, sh_step;
151 Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
155 lg_step = SGN(lg_delta);
156 lg_delta = ABS(lg_delta);
157 sh_step = SGN(sh_delta);
158 sh_delta = ABS(sh_delta);
159 if (sh_delta < lg_delta)
161 cycle = lg_delta >> 1;
164 drawpixel(x1, y1, color);
166 if (cycle > lg_delta)
173 drawpixel(x1, y1, color);
175 cycle = sh_delta >> 1;
178 drawpixel(x1, y1, color);
180 if (cycle > sh_delta)
187 drawpixel(x1, y1, color);
191 /* --- FILL A RECT --- */
193 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 /* --- LOAD AN IMAGE --- */
273 SDL_Surface * load_image(char * file, int use_alpha)
276 if(!faccessible(file))
278 if(!faccessible(st_dir,
281 SDL_Surface * temp, * surf;
283 temp = IMG_Load(file);
286 st_abort("Can't load", file);
288 surf = SDL_DisplayFormatAlpha(temp);
291 st_abort("Can't covert to display format", file);
293 if (use_alpha == IGNORE_ALPHA)
294 SDL_SetAlpha(surf, 0, 0);
296 SDL_FreeSurface(temp);
301 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
304 SDL_UpdateRect(scr, x, y, w, h);