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)
51 glClearColor(r/256, g/256, b/256, 1.0);
52 glClear(GL_COLOR_BUFFER_BIT);
55 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
60 /* --- UPDATE SCREEN --- */
62 void updatescreen(void)
64 if(use_gl) /*clearscreen(0,0,0);*/
67 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
78 /* --- LOAD AN IMAGE --- */
80 SDL_Surface * load_image(char * file, int use_alpha)
83 if(!faccessible(file))
85 if(!faccessible(st_dir,
88 SDL_Surface * temp, * surf;
90 temp = IMG_Load(file);
93 st_abort("Can't load", file);
95 surf = SDL_DisplayFormatAlpha(temp);
98 st_abort("Can't covert to display format", file);
100 if (use_alpha == IGNORE_ALPHA)
101 SDL_SetAlpha(surf, 0, 0);
103 SDL_FreeSurface(temp);
108 void create_gl_texture(SDL_Surface * surf, GLint * tex)
111 conv = SDL_CreateRGBSurface(SDL_SWSURFACE , surf->w, surf->h, 32,
112 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
113 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
115 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
117 SDL_BlitSurface(surf, 0, conv, 0);
118 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
119 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
121 glGenTextures(1, &*tex);
123 glBindTexture(GL_TEXTURE_RECTANGLE_NV , *tex);
124 glEnable(GL_TEXTURE_RECTANGLE_NV);
125 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
126 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
128 glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, 3, conv->w, conv->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
129 //glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_NV, 0, 0, 0, 0, 0, conv->w, conv->h);
130 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
131 SDL_FreeSurface(conv);
134 /* --- DRAW AN IMAGE ONTO THE SCREEN --- */
136 void drawimage(SDL_Surface * surf, float x, float y, int update)
141 create_gl_texture(surf,&gl_tex);
142 glColor4ub(255, 255, 255,255);
143 glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
145 glBindTexture(GL_TEXTURE_RECTANGLE_NV, gl_tex);
148 glTexCoord2f(0, 0); glVertex2f(x, y);
149 glTexCoord2f((float)surf->w, 0); glVertex2f((float)surf->w+x, y);
150 glTexCoord2f((float)surf->w, (float)surf->h); glVertex2f((float)surf->w+x, (float)surf->h+y);
151 glTexCoord2f(0, (float)surf->h); glVertex2f(x, (float)surf->h+y);
153 glDeleteTextures(1, &gl_tex);
164 SDL_BlitSurface(surf, NULL, screen, &dest);
166 if (update == UPDATE)
167 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
172 drawbgimage(SDL_Surface * surf, int update)
177 create_gl_texture(surf,&gl_tex);
178 //glColor3ub(255, 255, 255);
180 glEnable(GL_TEXTURE_RECTANGLE_NV);
181 glBindTexture(GL_TEXTURE_RECTANGLE_NV, gl_tex);
184 glTexCoord2f(0, 0); glVertex2f(0, 0);
185 glTexCoord2f((float)surf->w, 0); glVertex2f(screen->w, 0);
186 glTexCoord2f((float)surf->w, (float)surf->h); glVertex2f(screen->w, screen->h);
187 glTexCoord2f(0, (float)surf->h); glVertex2f(0, screen->h);
189 glDeleteTextures(1, &gl_tex);
201 SDL_BlitSurface(surf, NULL, screen, &dest);
203 if (update == UPDATE)
204 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
208 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
211 SDL_UpdateRect(scr, x, y, w, h);
215 /* --- DRAW PART OF AN IMAGE ONTO THE SCREEN --- */
217 void drawpart(SDL_Surface * surf, float x, float y, float w, float h, int update)
222 create_gl_texture(surf,&gl_tex);
223 glColor3ub(255, 255, 255);
225 glEnable(GL_TEXTURE_RECTANGLE_NV);
226 glBindTexture(GL_TEXTURE_RECTANGLE_NV, gl_tex);
229 glTexCoord2f(x, y); glVertex2f(x, y);
230 glTexCoord2f(x+w, y); glVertex2f(w+x, y);
231 glTexCoord2f(x+w, y+h); glVertex2f(w+x, h+y);
232 glTexCoord2f(x, y+h); glVertex2f(x, h+y);
234 glDeleteTextures(1, &gl_tex);
251 SDL_BlitSurface(surf, &src, screen, &dest);
253 if (update == UPDATE)
254 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
258 /* --- DRAW TEXT ONTO THE SCREEN --- */
260 void drawtext(char * text, int x, int y, SDL_Surface * surf, int update, int shadowsize)
262 /* i - helps to keep tracking of the all string length
263 j - helps to keep track of the length of the current line */
268 /* For each letter in the string... */
270 for (i = 0; i < strlen(text); i++)
272 /* Set source rectangle: */
276 if (c >= 'A' && c <= 'Z')
278 /* Capital letter - first row: */
280 src.x = (c - 'A') * 16;
283 else if (c >= 'a' && c <= 'z')
285 /* Lowercase letter - first row: */
287 src.x = (c - 'a') * 16;
290 else if (c >= '!' && c <= '9')
292 /* Punctuation (except '?') or number - third row: */
294 src.x = (c - '!') * 16;
299 /* Question mark - third row, last character: */
304 else if (c == '\n') /* support for multi-lines */
317 /* Draw character: */
321 /* Set destination rectangle for shadow: */
323 dest.x = x + (i * 16) + shadowsize;
324 dest.y = y + shadowsize;
331 SDL_BlitSurface(letters_black, &src, screen, &dest);
334 /* Set destination rectangle for text: */
336 dest.x = x + (i * 16);
344 SDL_BlitSurface(surf, &src, screen, &dest);
347 /* FIXME: Text doesn't work in OpenGL mode, below is experimental code */
356 temp = SDL_CreateRGBSurface(SDL_SWSURFACE, dest.w, dest.h, 32,
357 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
358 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
360 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
362 SDL_BlitSurface(letters_black, &src, temp, NULL);
364 texture_from_sdl_surface(&xyz,temp,IGNORE_ALPHA);
365 texture_draw(&xyz,x + (i * 16) + shadowsize,y + shadowsize, update);
367 / * Set destination rectangle for text: * /
369 dest.x = x + (i * 16);
376 SDL_BlitSurface(surf, &src, temp, NULL);
377 texture_from_sdl_surface(&xyz,temp,IGNORE_ALPHA);
378 SDL_FreeSurface(temp);
379 texture_draw(&xyz,x + (i * 16) + shadowsize,y + shadowsize, update);
380 texture_free(&xyz);*/
387 if (update == UPDATE)
389 dest.w = strlen(text) * 16 + 1;
391 if (dest.w > screen->w)
394 update_rect(screen, x, y, dest.w, 17);
400 /* --- DRAW HORIZONTALLY-CENTERED TEXT: --- */
402 void drawcenteredtext(char * text, int y, SDL_Surface * surf, int update, int shadowsize)
404 drawtext(text, screen->w / 2 - (strlen(text) * 8), y, surf, update, shadowsize);
407 /* --- ERASE TEXT: --- */
409 void erasetext(char * text, int x, int y, SDL_Surface * surf, int update, int shadowsize)
416 dest.w = strlen(text) * 16 + shadowsize;
419 if (dest.w > screen->w)
422 SDL_BlitSurface(surf, &dest, screen, &dest);
424 if (update == UPDATE)
425 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
429 /* --- ERASE CENTERED TEXT: --- */
431 void erasecenteredtext(char * text, int y, SDL_Surface * surf, int update, int shadowsize)
433 erasetext(text, screen->w / 2 - (strlen(text) * 8), y, surf, update, shadowsize);