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)
79 while ( value < input ) {
85 void texture_create_gl(SDL_Surface * surf, GLuint * tex)
92 w = power_of_two(surf->w);
93 h = power_of_two(surf->h),
94 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
95 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
96 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
99 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
102 /* Save the alpha blending attributes */
103 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
104 saved_alpha = surf->format->alpha;
105 if ( (saved_flags & SDL_SRCALPHA)
108 SDL_SetAlpha(surf, 0, 0);
111 SDL_BlitSurface(surf, 0, conv, 0);
113 /* Restore the alpha blending attributes */
114 if ( (saved_flags & SDL_SRCALPHA)
117 SDL_SetAlpha(surf, saved_flags, saved_alpha);
120 glGenTextures(1, &*tex);
121 glBindTexture(GL_TEXTURE_2D , *tex);
122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
126 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
127 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
128 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
130 SDL_FreeSurface(conv);
133 void texture_free_gl(texture_type* ptexture)
135 SDL_FreeSurface(ptexture->sdl_surface);
136 glDeleteTextures(1, &ptexture->gl_texture);
139 void texture_draw_gl(texture_type* ptexture, float x, float y, bool update)
141 float pw = power_of_two(ptexture->w);
142 float ph = power_of_two(ptexture->h);
144 glEnable(GL_TEXTURE_2D);
146 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
148 glColor4ub(255, 255, 255,255);
150 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
155 glTexCoord2f((float)ptexture->w / pw, 0);
156 glVertex2f((float)ptexture->w+x, y);
157 glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph); glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
158 glTexCoord2f(0, (float)ptexture->h / ph);
159 glVertex2f(x, (float)ptexture->h+y);
162 glDisable(GL_TEXTURE_2D);
166 void texture_draw_bg_gl(texture_type* ptexture, bool update)
168 float pw = power_of_two(ptexture->w);
169 float ph = power_of_two(ptexture->h);
171 glColor3ub(255, 255, 255);
173 glEnable(GL_TEXTURE_2D);
174 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
179 glTexCoord2f((float)ptexture->w / pw, 0);
180 glVertex2f(screen->w, 0);
181 glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph);
182 glVertex2f(screen->w, screen->h);
183 glTexCoord2f(0, (float)ptexture->h / ph);
184 glVertex2f(0, screen->h);
187 glDisable(GL_TEXTURE_2D);
190 void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, bool update)
192 float pw = power_of_two(ptexture->w);
193 float ph = power_of_two(ptexture->h);
195 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
198 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
200 glColor4ub(255, 255, 255,255);
202 glEnable(GL_TEXTURE_2D);
206 glTexCoord2f(sx / pw, sy / ph);
208 glTexCoord2f((float)(sx + w) / pw, sy / ph);
210 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
211 glVertex2f(w +x, h+y);
212 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
216 glDisable(GL_TEXTURE_2D);
222 void texture_load_sdl(texture_type* ptexture, const std::string& file, int use_alpha)
226 temp = IMG_Load(file.c_str());
229 st_abort("Can't load", file);
231 if(use_alpha == IGNORE_ALPHA && !use_gl)
232 ptexture->sdl_surface = SDL_DisplayFormat(temp);
234 ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
236 if (ptexture->sdl_surface == NULL)
237 st_abort("Can't covert to display format", file);
239 if (use_alpha == IGNORE_ALPHA && !use_gl)
240 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
242 SDL_FreeSurface(temp);
244 ptexture->w = ptexture->sdl_surface->w;
245 ptexture->h = ptexture->sdl_surface->h;
249 void texture_load_part_sdl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha)
256 temp = IMG_Load(file.c_str());
259 st_abort("Can't load", file);
261 /* Set source rectangle for conv: */
268 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
272 temp->format->Amask);
274 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
275 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
278 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
281 SDL_SetAlpha(temp,0,0);
283 SDL_BlitSurface(temp, &src, conv, NULL);
284 if(use_alpha == IGNORE_ALPHA && !use_gl)
285 ptexture->sdl_surface = SDL_DisplayFormat(conv);
287 ptexture->sdl_surface = SDL_DisplayFormatAlpha(conv);
289 if (ptexture->sdl_surface == NULL)
290 st_abort("Can't covert to display format", file);
292 if (use_alpha == IGNORE_ALPHA && !use_gl)
293 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
295 SDL_FreeSurface(temp);
296 SDL_FreeSurface(conv);
298 ptexture->w = ptexture->sdl_surface->w;
299 ptexture->h = ptexture->sdl_surface->h;
302 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
307 /* Save the alpha blending attributes */
308 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
309 saved_alpha = sdl_surf->format->alpha;
310 if ( (saved_flags & SDL_SRCALPHA)
313 SDL_SetAlpha(sdl_surf, 0, 0);
316 if(use_alpha == IGNORE_ALPHA && !use_gl)
317 ptexture->sdl_surface = SDL_DisplayFormat(sdl_surf);
319 ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
321 /* Restore the alpha blending attributes */
322 if ( (saved_flags & SDL_SRCALPHA)
325 SDL_SetAlpha(ptexture->sdl_surface, saved_flags, saved_alpha);
328 if (ptexture->sdl_surface == NULL)
329 st_abort("Can't covert to display format", "SURFACE");
331 if (use_alpha == IGNORE_ALPHA && !use_gl)
332 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
334 ptexture->w = ptexture->sdl_surface->w;
335 ptexture->h = ptexture->sdl_surface->h;
341 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
346 void texture_draw_sdl(texture_type* ptexture, float x, float y, bool update)
353 dest.w = ptexture->w;
354 dest.h = ptexture->h;
355 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
357 if (update == UPDATE)
358 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
362 void texture_draw_bg_sdl(texture_type* ptexture, bool update)
371 SDL_SoftStretch(ptexture->sdl_surface, NULL, screen, &dest);
373 if (update == UPDATE)
374 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
377 void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, bool update)
392 SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
394 if (update == UPDATE)
395 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
398 void texture_free_sdl(texture_type* ptexture)
400 SDL_FreeSurface(ptexture->sdl_surface);