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/SDL_image.h>
20 void (*texture_load) (texture_type* ptexture, char * file, int use_alpha);
21 void (*texture_load_part) (texture_type* ptexture, char * 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, int update);
24 void (*texture_draw_bg) (texture_type* ptexture, int update);
25 void (*texture_draw_part) (texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int 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, char * 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, char * 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(surf->flags, 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);
120 glGenTextures(1, &*tex);
122 glBindTexture(GL_TEXTURE_2D , *tex);
123 glEnable(GL_TEXTURE_2D);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
128 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
129 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
130 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
132 SDL_FreeSurface(conv);
135 void texture_free_gl(texture_type* ptexture)
137 SDL_FreeSurface(ptexture->sdl_surface);
138 glDeleteTextures(1, &ptexture->gl_texture);
141 void texture_draw_gl(texture_type* ptexture, float x, float y, int update)
143 float pw = power_of_two(ptexture->w);
144 float ph = power_of_two(ptexture->h);
146 glEnable(GL_TEXTURE_2D);
148 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
150 glColor4ub(255, 255, 255,255);
152 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
157 glTexCoord2f((float)ptexture->w / pw, 0);
158 glVertex2f((float)ptexture->w+x, y);
159 glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph); glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
160 glTexCoord2f(0, (float)ptexture->h / ph);
161 glVertex2f(x, (float)ptexture->h+y);
164 glDisable(GL_TEXTURE_2D);
168 void texture_draw_bg_gl(texture_type* ptexture, int update)
170 float pw = power_of_two(ptexture->w);
171 float ph = power_of_two(ptexture->h);
173 glColor3ub(255, 255, 255);
175 glEnable(GL_TEXTURE_2D);
176 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
181 glTexCoord2f((float)ptexture->w / pw, 0);
182 glVertex2f(screen->w, 0);
183 glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph);
184 glVertex2f(screen->w, screen->h);
185 glTexCoord2f(0, (float)ptexture->h / ph);
186 glVertex2f(0, screen->h);
189 glDisable(GL_TEXTURE_2D);
192 void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, int update)
194 float pw = power_of_two(ptexture->w);
195 float ph = power_of_two(ptexture->h);
197 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
200 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
202 glColor4ub(255, 255, 255,255);
204 glEnable(GL_TEXTURE_2D);
208 glTexCoord2f(sx / pw, sy / ph);
210 glTexCoord2f((float)(sx + w) / pw, sy / ph);
212 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
213 glVertex2f(w +x, h+y);
214 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
218 glDisable(GL_TEXTURE_2D);
224 void texture_load_sdl(texture_type* ptexture, char * file, int use_alpha)
228 temp = IMG_Load(file);
231 st_abort("Can't load", file);
233 if(use_alpha == IGNORE_ALPHA)
234 ptexture->sdl_surface = SDL_DisplayFormat(temp);
236 ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
238 if (ptexture->sdl_surface == NULL)
239 st_abort("Can't covert to display format", file);
241 if (use_alpha == IGNORE_ALPHA)
242 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
244 SDL_FreeSurface(temp);
246 ptexture->w = ptexture->sdl_surface->w;
247 ptexture->h = ptexture->sdl_surface->h;
251 void texture_load_part_sdl(texture_type* ptexture, char * file, int x, int y, int w, int h, int use_alpha)
258 temp = IMG_Load(file);
261 st_abort("Can't load", file);
263 /* Set source rectangle for conv: */
270 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
274 temp->format->Amask);
276 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
277 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
280 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
283 SDL_SetAlpha(temp,0,0);
285 SDL_BlitSurface(temp, &src, conv, NULL);
286 if(use_alpha == IGNORE_ALPHA)
287 ptexture->sdl_surface = SDL_DisplayFormat(conv);
289 ptexture->sdl_surface = SDL_DisplayFormatAlpha(conv);
291 if (ptexture->sdl_surface == NULL)
292 st_abort("Can't covert to display format", file);
294 if (use_alpha == IGNORE_ALPHA)
295 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
297 SDL_FreeSurface(temp);
298 SDL_FreeSurface(conv);
300 ptexture->w = ptexture->sdl_surface->w;
301 ptexture->h = ptexture->sdl_surface->h;
304 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
309 /* Save the alpha blending attributes */
310 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
311 saved_alpha = sdl_surf->format->alpha;
312 if ( (saved_flags & SDL_SRCALPHA)
315 SDL_SetAlpha(sdl_surf, 0, 0);
318 if(use_alpha == IGNORE_ALPHA)
319 ptexture->sdl_surface = SDL_DisplayFormat(sdl_surf);
321 ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
323 /* Restore the alpha blending attributes */
324 if ( (saved_flags & SDL_SRCALPHA)
327 SDL_SetAlpha(ptexture->sdl_surface, saved_flags, saved_alpha);
330 if (ptexture->sdl_surface == NULL)
331 st_abort("Can't covert to display format", "SURFACE");
333 if (use_alpha == IGNORE_ALPHA)
334 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
336 ptexture->w = ptexture->sdl_surface->w;
337 ptexture->h = ptexture->sdl_surface->h;
343 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
348 void texture_draw_sdl(texture_type* ptexture, float x, float y, int update)
355 dest.w = ptexture->w;
356 dest.h = ptexture->h;
357 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
359 if (update == UPDATE)
360 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
364 void texture_draw_bg_sdl(texture_type* ptexture, int update)
373 SDL_SoftStretch(ptexture->sdl_surface, NULL, screen, &dest);
375 if (update == UPDATE)
376 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
379 void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update)
394 SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
396 if (update == UPDATE)
397 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
400 void texture_free_sdl(texture_type* ptexture)
402 SDL_FreeSurface(ptexture->sdl_surface);