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, bool update);
24 void (*texture_draw_bg) (texture_type* ptexture, bool update);
25 void (*texture_draw_part)(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, 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, 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(255, 255, 255,255);
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);
165 void texture_draw_bg_gl(texture_type* ptexture, bool update)
167 float pw = power_of_two(ptexture->w);
168 float ph = power_of_two(ptexture->h);
170 glColor3ub(255, 255, 255);
172 glEnable(GL_TEXTURE_2D);
173 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
178 glTexCoord2f((float)ptexture->w / pw, 0);
179 glVertex2f(screen->w, 0);
180 glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph);
181 glVertex2f(screen->w, screen->h);
182 glTexCoord2f(0, (float)ptexture->h / ph);
183 glVertex2f(0, screen->h);
186 glDisable(GL_TEXTURE_2D);
189 void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, bool update)
191 float pw = power_of_two(ptexture->w);
192 float ph = power_of_two(ptexture->h);
194 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
197 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
199 glColor4ub(255, 255, 255,255);
201 glEnable(GL_TEXTURE_2D);
205 glTexCoord2f(sx / pw, sy / ph);
207 glTexCoord2f((float)(sx + w) / pw, sy / ph);
209 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
210 glVertex2f(w +x, h+y);
211 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
215 glDisable(GL_TEXTURE_2D);
221 void texture_load_sdl(texture_type* ptexture, const std::string& file, int use_alpha)
225 temp = IMG_Load(file.c_str());
228 st_abort("Can't load", file);
230 if(use_alpha == IGNORE_ALPHA && !use_gl)
231 ptexture->sdl_surface = SDL_DisplayFormat(temp);
233 ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
235 if (ptexture->sdl_surface == NULL)
236 st_abort("Can't covert to display format", file);
238 if (use_alpha == IGNORE_ALPHA && !use_gl)
239 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
241 SDL_FreeSurface(temp);
243 ptexture->w = ptexture->sdl_surface->w;
244 ptexture->h = ptexture->sdl_surface->h;
248 void texture_load_part_sdl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha)
255 temp = IMG_Load(file.c_str());
258 st_abort("Can't load", file);
260 /* Set source rectangle for conv: */
267 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
271 temp->format->Amask);
273 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
274 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
277 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
280 SDL_SetAlpha(temp,0,0);
282 SDL_BlitSurface(temp, &src, conv, NULL);
283 if(use_alpha == IGNORE_ALPHA && !use_gl)
284 ptexture->sdl_surface = SDL_DisplayFormat(conv);
286 ptexture->sdl_surface = SDL_DisplayFormatAlpha(conv);
288 if (ptexture->sdl_surface == NULL)
289 st_abort("Can't covert to display format", file);
291 if (use_alpha == IGNORE_ALPHA && !use_gl)
292 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
294 SDL_FreeSurface(temp);
295 SDL_FreeSurface(conv);
297 ptexture->w = ptexture->sdl_surface->w;
298 ptexture->h = ptexture->sdl_surface->h;
301 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
306 /* Save the alpha blending attributes */
307 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
308 saved_alpha = sdl_surf->format->alpha;
309 if ( (saved_flags & SDL_SRCALPHA)
312 SDL_SetAlpha(sdl_surf, 0, 0);
315 if(use_alpha == IGNORE_ALPHA && !use_gl)
316 ptexture->sdl_surface = SDL_DisplayFormat(sdl_surf);
318 ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
320 /* Restore the alpha blending attributes */
321 if ( (saved_flags & SDL_SRCALPHA)
324 SDL_SetAlpha(ptexture->sdl_surface, saved_flags, saved_alpha);
327 if (ptexture->sdl_surface == NULL)
328 st_abort("Can't covert to display format", "SURFACE");
330 if (use_alpha == IGNORE_ALPHA && !use_gl)
331 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
333 ptexture->w = ptexture->sdl_surface->w;
334 ptexture->h = ptexture->sdl_surface->h;
340 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
345 void texture_draw_sdl(texture_type* ptexture, float x, float y, bool update)
351 dest.w = ptexture->w;
352 dest.h = ptexture->h;
353 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
355 if (update == UPDATE)
356 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
360 void texture_draw_bg_sdl(texture_type* ptexture, bool update)
369 SDL_SoftStretch(ptexture->sdl_surface, NULL, screen, &dest);
371 if (update == UPDATE)
372 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
375 void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, bool update)
390 SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
392 if (update == UPDATE)
393 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
396 void texture_free_sdl(texture_type* ptexture)
398 SDL_FreeSurface(ptexture->sdl_surface);