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 /* --- LOAD AND DISPLAY AN IMAGE --- */
35 void load_and_display_image(char * file)
39 img = load_image(file, IGNORE_ALPHA);
40 SDL_BlitSurface(img, NULL, screen, NULL);
45 /* --- CLEAR SCREEN --- */
47 void clearscreen(float r, float g, float b)
52 glClearColor(r/256, g/256, b/256, 1.0);
53 glClear(GL_COLOR_BUFFER_BIT);
57 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
62 /* --- UPDATE SCREEN --- */
64 void updatescreen(void)
66 if(use_gl) /*clearscreen(0,0,0);*/
69 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
80 /* --- LOAD AN IMAGE --- */
82 SDL_Surface * load_image(char * file, int use_alpha)
85 if(!faccessible(file))
87 if(!faccessible(st_dir,
90 SDL_Surface * temp, * surf;
92 temp = IMG_Load(file);
95 st_abort("Can't load", file);
97 surf = SDL_DisplayFormatAlpha(temp);
100 st_abort("Can't covert to display format", file);
102 if (use_alpha == IGNORE_ALPHA)
103 SDL_SetAlpha(surf, 0, 0);
105 SDL_FreeSurface(temp);
110 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
113 SDL_UpdateRect(scr, x, y, w, h);
116 void drawtext(char * text, int x, int y, SDL_Surface * surf, int update, int shadowsize)
118 /* i - helps to keep tracking of the all string length
119 j - helps to keep track of the length of the current line */
124 /* For each letter in the string... */
126 for (i = 0; i < strlen(text); i++)
128 /* Set source rectangle: */
132 if (c >= 'A' && c <= 'Z')
134 /* Capital letter - first row: */
136 src.x = (c - 'A') * 16;
139 else if (c >= 'a' && c <= 'z')
141 /* Lowercase letter - first row: */
143 src.x = (c - 'a') * 16;
146 else if (c >= '!' && c <= '9')
148 /* Punctuation (except '?') or number - third row: */
150 src.x = (c - '!') * 16;
155 /* Question mark - third row, last character: */
160 else if (c == '\n') /* support for multi-lines */
173 /* Draw character: */
177 /* Set destination rectangle for shadow: */
179 dest.x = x + (i * 16) + shadowsize;
180 dest.y = y + shadowsize;
187 SDL_BlitSurface(letters_black, &src, screen, &dest);
190 /* Set destination rectangle for text: */
192 dest.x = x + (i * 16);
200 SDL_BlitSurface(surf, &src, screen, &dest);
203 /* FIXME: Text doesn't work in OpenGL mode, below is experimental code */
212 temp = SDL_CreateRGBSurface(SDL_SWSURFACE, dest.w, dest.h, 32,
213 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
214 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
216 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
218 SDL_BlitSurface(letters_black, &src, temp, NULL);
220 texture_from_sdl_surface(&xyz,temp,IGNORE_ALPHA);
221 texture_draw(&xyz,x + (i * 16) + shadowsize,y + shadowsize, update);
223 / * Set destination rectangle for text: * /
225 dest.x = x + (i * 16);
232 SDL_BlitSurface(surf, &src, temp, NULL);
233 texture_from_sdl_surface(&xyz,temp,IGNORE_ALPHA);
234 SDL_FreeSurface(temp);
235 texture_draw(&xyz,x + (i * 16) + shadowsize,y + shadowsize, update);
236 texture_free(&xyz);*/
243 if (update == UPDATE)
245 dest.w = strlen(text) * 16 + 1;
247 if (dest.w > screen->w)
250 update_rect(screen, x, y, dest.w, 17);
256 /* --- DRAW HORIZONTALLY-CENTERED TEXT: --- */
258 void drawcenteredtext(char * text, int y, SDL_Surface * surf, int update, int shadowsize)
260 drawtext(text, screen->w / 2 - (strlen(text) * 8), y, surf, update, shadowsize);
263 /* --- ERASE TEXT: --- */
265 void erasetext(char * text, int x, int y, SDL_Surface * surf, int update, int shadowsize)
272 dest.w = strlen(text) * 16 + shadowsize;
275 if (dest.w > screen->w)
278 SDL_BlitSurface(surf, &dest, screen, &dest);
280 if (update == UPDATE)
281 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
285 /* --- ERASE CENTERED TEXT: --- */
287 void erasecenteredtext(char * text, int y, SDL_Surface * surf, int update, int shadowsize)
289 erasetext(text, screen->w / 2 - (strlen(text) * 8), y, surf, update, shadowsize);