2 // C Implementation: texture
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
14 #include "SDL_image.h"
22 void (*texture_load) (texture_type* ptexture, const std::string& file, int use_alpha);
23 void (*texture_load_part)(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha);
24 void (*texture_free) (texture_type* ptexture);
25 void (*texture_draw) (texture_type* ptexture, float x, float y, Uint8 alpha, bool update);
26 void (*texture_draw_bg) (texture_type* ptexture, Uint8 alpha, bool update);
27 void (*texture_draw_part)(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update);
30 void texture_setup(void)
33 texture_load = texture_load_sdl;
34 texture_load_part = texture_load_part_sdl;
35 texture_free = texture_free_sdl;
36 texture_draw = texture_draw_sdl;
37 texture_draw_bg = texture_draw_bg_sdl;
38 texture_draw_part = texture_draw_part_sdl;
43 texture_load = texture_load_gl;
44 texture_load_part = texture_load_part_gl;
45 texture_free = texture_free_gl;
46 texture_draw = texture_draw_gl;
47 texture_draw_bg = texture_draw_bg_gl;
48 texture_draw_part = texture_draw_part_gl;
52 texture_load = texture_load_sdl;
53 texture_load_part = texture_load_part_sdl;
54 texture_free = texture_free_sdl;
55 texture_draw = texture_draw_sdl;
56 texture_draw_bg = texture_draw_bg_sdl;
57 texture_draw_part = texture_draw_part_sdl;
63 void texture_load_gl(texture_type* ptexture, const std::string& file, int use_alpha)
65 texture_load_sdl(ptexture,file,use_alpha);
66 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
69 void texture_load_part_gl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha)
71 texture_load_part_sdl(ptexture,file,x,y,w,h,use_alpha);
72 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
75 /* Quick utility function for texture creation */
76 static int power_of_two(int input)
80 while ( value < input ) {
86 void texture_create_gl(SDL_Surface * surf, GLuint * tex)
93 w = power_of_two(surf->w);
94 h = power_of_two(surf->h),
95 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
96 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
97 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
100 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
103 /* Save the alpha blending attributes */
104 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
105 saved_alpha = surf->format->alpha;
106 if ( (saved_flags & SDL_SRCALPHA)
109 SDL_SetAlpha(surf, 0, 0);
112 SDL_BlitSurface(surf, 0, conv, 0);
114 /* Restore the alpha blending attributes */
115 if ( (saved_flags & SDL_SRCALPHA)
118 SDL_SetAlpha(surf, saved_flags, saved_alpha);
121 glGenTextures(1, &*tex);
122 glBindTexture(GL_TEXTURE_2D , *tex);
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
127 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
128 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
129 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
131 SDL_FreeSurface(conv);
134 void texture_free_gl(texture_type* ptexture)
136 SDL_FreeSurface(ptexture->sdl_surface);
137 glDeleteTextures(1, &ptexture->gl_texture);
140 void texture_draw_gl(texture_type* ptexture, float x, float y, Uint8 alpha, bool update)
142 if(ptexture->gl_texture == NO_TEXTURE)
143 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
145 float pw = power_of_two(ptexture->w);
146 float ph = power_of_two(ptexture->h);
148 glEnable(GL_TEXTURE_2D);
150 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
152 glColor4ub(alpha, alpha, alpha, alpha);
154 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
159 glTexCoord2f((float)ptexture->w / pw, 0);
160 glVertex2f((float)ptexture->w+x, y);
161 glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph); glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
162 glTexCoord2f(0, (float)ptexture->h / ph);
163 glVertex2f(x, (float)ptexture->h+y);
166 glDisable(GL_TEXTURE_2D);
169 /* Avoid compiler warnings */
174 void texture_draw_bg_gl(texture_type* ptexture, Uint8 alpha, bool update)
176 if(ptexture->gl_texture == NO_TEXTURE)
177 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
179 float pw = power_of_two(ptexture->w);
180 float ph = power_of_two(ptexture->h);
182 glColor3ub(alpha, alpha, alpha);
184 glEnable(GL_TEXTURE_2D);
185 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
190 glTexCoord2f((float)ptexture->w / pw, 0);
191 glVertex2f(screen->w, 0);
192 glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph);
193 glVertex2f(screen->w, screen->h);
194 glTexCoord2f(0, (float)ptexture->h / ph);
195 glVertex2f(0, screen->h);
198 glDisable(GL_TEXTURE_2D);
200 /* Avoid compiler warnings */
205 void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
207 if(ptexture->gl_texture == NO_TEXTURE)
208 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
210 float pw = power_of_two(ptexture->w);
211 float ph = power_of_two(ptexture->h);
213 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
216 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
218 glColor4ub(alpha, alpha, alpha, alpha);
220 glEnable(GL_TEXTURE_2D);
224 glTexCoord2f(sx / pw, sy / ph);
226 glTexCoord2f((float)(sx + w) / pw, sy / ph);
228 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
229 glVertex2f(w +x, h+y);
230 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
234 glDisable(GL_TEXTURE_2D);
237 /* Avoid compiler warnings */
243 void texture_load_sdl(texture_type* ptexture, const std::string& file, int use_alpha)
247 temp = IMG_Load(file.c_str());
250 st_abort("Can't load", file);
252 if(use_alpha == IGNORE_ALPHA && !use_gl)
253 ptexture->sdl_surface = SDL_DisplayFormat(temp);
255 ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
257 if (ptexture->sdl_surface == NULL)
258 st_abort("Can't covert to display format", file);
260 if (use_alpha == IGNORE_ALPHA && !use_gl)
261 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
263 SDL_FreeSurface(temp);
265 ptexture->w = ptexture->sdl_surface->w;
266 ptexture->h = ptexture->sdl_surface->h;
268 ptexture->gl_texture = NO_TEXTURE;
271 void texture_load_part_sdl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha)
278 temp = IMG_Load(file.c_str());
281 st_abort("Can't load", file);
283 /* Set source rectangle for conv: */
290 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
294 temp->format->Amask);
296 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
297 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
300 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
303 SDL_SetAlpha(temp,0,0);
305 SDL_BlitSurface(temp, &src, conv, NULL);
306 if(use_alpha == IGNORE_ALPHA && !use_gl)
307 ptexture->sdl_surface = SDL_DisplayFormat(conv);
309 ptexture->sdl_surface = SDL_DisplayFormatAlpha(conv);
311 if (ptexture->sdl_surface == NULL)
312 st_abort("Can't covert to display format", file);
314 if (use_alpha == IGNORE_ALPHA && !use_gl)
315 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
317 SDL_FreeSurface(temp);
318 SDL_FreeSurface(conv);
320 ptexture->w = ptexture->sdl_surface->w;
321 ptexture->h = ptexture->sdl_surface->h;
324 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
329 /* Save the alpha blending attributes */
330 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
331 saved_alpha = sdl_surf->format->alpha;
332 if ( (saved_flags & SDL_SRCALPHA)
335 SDL_SetAlpha(sdl_surf, 0, 0);
338 if(use_alpha == IGNORE_ALPHA && !use_gl)
339 ptexture->sdl_surface = SDL_DisplayFormat(sdl_surf);
341 ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
343 /* Restore the alpha blending attributes */
344 if ( (saved_flags & SDL_SRCALPHA)
347 SDL_SetAlpha(ptexture->sdl_surface, saved_flags, saved_alpha);
350 if (ptexture->sdl_surface == NULL)
351 st_abort("Can't covert to display format", "SURFACE");
353 if (use_alpha == IGNORE_ALPHA && !use_gl)
354 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
356 ptexture->w = ptexture->sdl_surface->w;
357 ptexture->h = ptexture->sdl_surface->h;
363 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
368 void texture_draw_sdl(texture_type* ptexture, float x, float y, Uint8 alpha, bool update)
374 dest.w = ptexture->w;
375 dest.h = ptexture->h;
377 if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
380 SDL_SetAlpha(ptexture->sdl_surface ,SDL_SRCALPHA,alpha);
381 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
383 if (update == UPDATE)
384 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
388 void texture_draw_bg_sdl(texture_type* ptexture, Uint8 alpha, bool update)
398 SDL_SetAlpha(ptexture->sdl_surface ,SDL_SRCALPHA,alpha);
399 SDL_SoftStretch(ptexture->sdl_surface, NULL, screen, &dest);
401 if (update == UPDATE)
402 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
405 void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
420 SDL_SetAlpha(ptexture->sdl_surface ,SDL_SRCALPHA,alpha);
422 SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
424 if (update == UPDATE)
425 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
428 void texture_free_sdl(texture_type* ptexture)
430 SDL_FreeSurface(ptexture->sdl_surface);
432 if(ptexture->gl_texture != NO_TEXTURE)
433 glDeleteTextures(1, &ptexture->gl_texture);