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"
20 void (*texture_load) (texture_type* ptexture, const std::string& file, int use_alpha);
21 void (*texture_load_part)(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha);
22 void (*texture_free) (texture_type* ptexture);
23 void (*texture_draw) (texture_type* ptexture, float x, float y, Uint8 alpha, bool update);
24 void (*texture_draw_bg) (texture_type* ptexture, Uint8 alpha, bool update);
25 void (*texture_draw_part)(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update);
28 void texture_setup(void)
31 texture_load = texture_load_sdl;
32 texture_load_part = texture_load_part_sdl;
33 texture_free = texture_free_sdl;
34 texture_draw = texture_draw_sdl;
35 texture_draw_bg = texture_draw_bg_sdl;
36 texture_draw_part = texture_draw_part_sdl;
41 texture_load = texture_load_gl;
42 texture_load_part = texture_load_part_gl;
43 texture_free = texture_free_gl;
44 texture_draw = texture_draw_gl;
45 texture_draw_bg = texture_draw_bg_gl;
46 texture_draw_part = texture_draw_part_gl;
50 texture_load = texture_load_sdl;
51 texture_load_part = texture_load_part_sdl;
52 texture_free = texture_free_sdl;
53 texture_draw = texture_draw_sdl;
54 texture_draw_bg = texture_draw_bg_sdl;
55 texture_draw_part = texture_draw_part_sdl;
61 void texture_load_gl(texture_type* ptexture, const std::string& file, int use_alpha)
63 texture_load_sdl(ptexture,file,use_alpha);
64 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
67 void texture_load_part_gl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha)
69 texture_load_part_sdl(ptexture,file,x,y,w,h,use_alpha);
70 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
73 /* Quick utility function for texture creation */
74 static int power_of_two(int input)
78 while ( value < input ) {
84 void texture_create_gl(SDL_Surface * surf, GLuint * tex)
91 w = power_of_two(surf->w);
92 h = power_of_two(surf->h),
93 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
94 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
95 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
98 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
101 /* Save the alpha blending attributes */
102 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
103 saved_alpha = surf->format->alpha;
104 if ( (saved_flags & SDL_SRCALPHA)
107 SDL_SetAlpha(surf, 0, 0);
110 SDL_BlitSurface(surf, 0, conv, 0);
112 /* Restore the alpha blending attributes */
113 if ( (saved_flags & SDL_SRCALPHA)
116 SDL_SetAlpha(surf, saved_flags, saved_alpha);
119 glGenTextures(1, &*tex);
120 glBindTexture(GL_TEXTURE_2D , *tex);
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
125 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
126 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
127 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
129 SDL_FreeSurface(conv);
132 void texture_free_gl(texture_type* ptexture)
134 SDL_FreeSurface(ptexture->sdl_surface);
135 glDeleteTextures(1, &ptexture->gl_texture);
138 void texture_draw_gl(texture_type* ptexture, float x, float y, Uint8 alpha, bool update)
140 float pw = power_of_two(ptexture->w);
141 float ph = power_of_two(ptexture->h);
143 glEnable(GL_TEXTURE_2D);
145 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
147 glColor4ub(alpha, alpha, alpha, alpha);
149 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
154 glTexCoord2f((float)ptexture->w / pw, 0);
155 glVertex2f((float)ptexture->w+x, y);
156 glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph); glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
157 glTexCoord2f(0, (float)ptexture->h / ph);
158 glVertex2f(x, (float)ptexture->h+y);
161 glDisable(GL_TEXTURE_2D);
164 /* Avoid compiler warnings */
169 void texture_draw_bg_gl(texture_type* ptexture, Uint8 alpha, bool update)
171 float pw = power_of_two(ptexture->w);
172 float ph = power_of_two(ptexture->h);
174 glColor3ub(alpha, alpha, alpha);
176 glEnable(GL_TEXTURE_2D);
177 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
182 glTexCoord2f((float)ptexture->w / pw, 0);
183 glVertex2f(screen->w, 0);
184 glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph);
185 glVertex2f(screen->w, screen->h);
186 glTexCoord2f(0, (float)ptexture->h / ph);
187 glVertex2f(0, screen->h);
190 glDisable(GL_TEXTURE_2D);
192 /* Avoid compiler warnings */
197 void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
199 float pw = power_of_two(ptexture->w);
200 float ph = power_of_two(ptexture->h);
202 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
205 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
207 glColor4ub(alpha, alpha, alpha, alpha);
209 glEnable(GL_TEXTURE_2D);
213 glTexCoord2f(sx / pw, sy / ph);
215 glTexCoord2f((float)(sx + w) / pw, sy / ph);
217 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
218 glVertex2f(w +x, h+y);
219 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
223 glDisable(GL_TEXTURE_2D);
226 /* Avoid compiler warnings */
232 void texture_load_sdl(texture_type* ptexture, const std::string& file, int use_alpha)
236 temp = IMG_Load(file.c_str());
239 st_abort("Can't load", file);
241 if(use_alpha == IGNORE_ALPHA && !use_gl)
242 ptexture->sdl_surface = SDL_DisplayFormat(temp);
244 ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
246 if (ptexture->sdl_surface == NULL)
247 st_abort("Can't covert to display format", file);
249 if (use_alpha == IGNORE_ALPHA && !use_gl)
250 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
252 SDL_FreeSurface(temp);
254 ptexture->w = ptexture->sdl_surface->w;
255 ptexture->h = ptexture->sdl_surface->h;
259 void texture_load_part_sdl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha)
266 temp = IMG_Load(file.c_str());
269 st_abort("Can't load", file);
271 /* Set source rectangle for conv: */
278 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
282 temp->format->Amask);
284 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
285 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
288 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
291 SDL_SetAlpha(temp,0,0);
293 SDL_BlitSurface(temp, &src, conv, NULL);
294 if(use_alpha == IGNORE_ALPHA && !use_gl)
295 ptexture->sdl_surface = SDL_DisplayFormat(conv);
297 ptexture->sdl_surface = SDL_DisplayFormatAlpha(conv);
299 if (ptexture->sdl_surface == NULL)
300 st_abort("Can't covert to display format", file);
302 if (use_alpha == IGNORE_ALPHA && !use_gl)
303 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
305 SDL_FreeSurface(temp);
306 SDL_FreeSurface(conv);
308 ptexture->w = ptexture->sdl_surface->w;
309 ptexture->h = ptexture->sdl_surface->h;
312 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
317 /* Save the alpha blending attributes */
318 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
319 saved_alpha = sdl_surf->format->alpha;
320 if ( (saved_flags & SDL_SRCALPHA)
323 SDL_SetAlpha(sdl_surf, 0, 0);
326 if(use_alpha == IGNORE_ALPHA && !use_gl)
327 ptexture->sdl_surface = SDL_DisplayFormat(sdl_surf);
329 ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
331 /* Restore the alpha blending attributes */
332 if ( (saved_flags & SDL_SRCALPHA)
335 SDL_SetAlpha(ptexture->sdl_surface, saved_flags, saved_alpha);
338 if (ptexture->sdl_surface == NULL)
339 st_abort("Can't covert to display format", "SURFACE");
341 if (use_alpha == IGNORE_ALPHA && !use_gl)
342 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
344 ptexture->w = ptexture->sdl_surface->w;
345 ptexture->h = ptexture->sdl_surface->h;
351 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
356 void texture_draw_sdl(texture_type* ptexture, float x, float y, Uint8 alpha, bool update)
362 dest.w = ptexture->w;
363 dest.h = ptexture->h;
365 if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
368 SDL_SetAlpha(ptexture->sdl_surface ,SDL_SRCALPHA,alpha);
369 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
371 if (update == UPDATE)
372 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
376 void texture_draw_bg_sdl(texture_type* ptexture, Uint8 alpha, bool update)
386 SDL_SetAlpha(ptexture->sdl_surface ,SDL_SRCALPHA,alpha);
387 SDL_SoftStretch(ptexture->sdl_surface, NULL, screen, &dest);
389 if (update == UPDATE)
390 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
393 void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
408 SDL_SetAlpha(ptexture->sdl_surface ,SDL_SRCALPHA,alpha);
410 SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
412 if (update == UPDATE)
413 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
416 void texture_free_sdl(texture_type* ptexture)
418 SDL_FreeSurface(ptexture->sdl_surface);