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_setup(void)
23 texture_load = texture_load_sdl;
24 texture_load_part = texture_load_part_sdl;
25 texture_free = texture_free_sdl;
26 texture_draw = texture_draw_sdl;
27 texture_draw_bg = texture_draw_bg_sdl;
28 texture_draw_part = texture_draw_part_sdl;
33 texture_load = texture_load_gl;
34 texture_load_part = texture_load_part_gl;
35 texture_free = texture_free_gl;
36 texture_draw = texture_draw_gl;
37 texture_draw_bg = texture_draw_bg_gl;
38 texture_draw_part = texture_draw_part_gl;
42 texture_load = texture_load_sdl;
43 texture_load_part = texture_load_part_sdl;
44 texture_free = texture_free_sdl;
45 texture_draw = texture_draw_sdl;
46 texture_draw_bg = texture_draw_bg_sdl;
47 texture_draw_part = texture_draw_part_sdl;
53 void texture_load_gl(texture_type* ptexture, char * file, int use_alpha)
55 texture_load_sdl(ptexture,file,use_alpha);
56 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
59 void texture_load_part_gl(texture_type* ptexture, char * file, int x, int y, int w, int h, int use_alpha)
61 texture_load_part_sdl(ptexture,file,x,y,w,h,use_alpha);
62 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
65 /* Quick utility function for texture creation */
66 static int power_of_two(int input)
70 while ( value < input ) {
76 void texture_create_gl(SDL_Surface * surf, GLint * tex)
83 w = power_of_two(surf->w);
84 h = power_of_two(surf->h),
85 conv = SDL_CreateRGBSurface(surf->flags, w, h, surf->format->BitsPerPixel,
86 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
87 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
90 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
93 /* Save the alpha blending attributes */
94 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
95 saved_alpha = surf->format->alpha;
96 if ( (saved_flags & SDL_SRCALPHA)
99 SDL_SetAlpha(surf, 0, 0);
102 SDL_BlitSurface(surf, 0, conv, 0);
104 /* Restore the alpha blending attributes */
105 if ( (saved_flags & SDL_SRCALPHA)
108 SDL_SetAlpha(surf, saved_flags, saved_alpha);
112 glGenTextures(1, &*tex);
114 glBindTexture(GL_TEXTURE_2D , *tex);
115 glEnable(GL_TEXTURE_2D);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
120 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
121 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
122 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
123 SDL_FreeSurface(conv);
126 void texture_free_gl(texture_type* ptexture)
128 SDL_FreeSurface(ptexture->sdl_surface);
129 glDeleteTextures(1, &ptexture->gl_texture);
132 void texture_draw_gl(texture_type* ptexture, float x, float y, int update)
137 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
139 glColor4ub(255, 255, 255,255);
141 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
146 glTexCoord2f((float)ptexture->w / (float)power_of_two(ptexture->w), 0);
147 glVertex2f((float)ptexture->w+x, y);
148 glTexCoord2f((float)ptexture->w / (float)power_of_two(ptexture->w), (float)ptexture->h / (float)power_of_two(ptexture->h)); glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
149 glTexCoord2f(0, (float)ptexture->h / (float)power_of_two(ptexture->h));
150 glVertex2f(x, (float)ptexture->h+y);
156 void texture_draw_bg_gl(texture_type* ptexture, int update)
158 glColor3ub(255, 255, 255);
160 glEnable(GL_TEXTURE_2D);
161 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
166 glTexCoord2f((float)ptexture->w / (float)power_of_two(ptexture->w), 0);
167 glVertex2f(screen->w, 0);
168 glTexCoord2f((float)ptexture->w / (float)power_of_two(ptexture->w), (float)ptexture->h / power_of_two(ptexture->h));
169 glVertex2f(screen->w, screen->h);
170 glTexCoord2f(0, (float)ptexture->h / (float)power_of_two(ptexture->h));
171 glVertex2f(0, screen->h);
175 void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, int update)
178 glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
181 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
183 glColor4ub(255, 255, 255,255);
185 glEnable(GL_TEXTURE_2D);
189 glTexCoord2f(sx / (float)power_of_two(ptexture->w), sy / (float)power_of_two(ptexture->h));
191 glTexCoord2f((float)(sx + w) / (float)power_of_two(ptexture->w), sy / (float)power_of_two(ptexture->h));
193 glTexCoord2f((sx+w) / (float)power_of_two(ptexture->w), (sy+h) / (float)power_of_two(ptexture->h));
194 glVertex2f(w +x, h+y);
195 glTexCoord2f(sx / (float)power_of_two(ptexture->w), (float)(sy+h) / (float)power_of_two(ptexture->h));
204 void texture_load_sdl(texture_type* ptexture, char * file, int use_alpha)
208 temp = IMG_Load(file);
211 st_abort("Can't load", file);
213 ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
215 if (ptexture->sdl_surface == NULL)
216 st_abort("Can't covert to display format", file);
218 if (use_alpha == IGNORE_ALPHA)
219 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
221 SDL_FreeSurface(temp);
223 ptexture->w = ptexture->sdl_surface->w;
224 ptexture->h = ptexture->sdl_surface->h;
228 void texture_load_part_sdl(texture_type* ptexture, char * file, int x, int y, int w, int h, int use_alpha)
235 temp = IMG_Load(file);
238 st_abort("Can't load", file);
240 /* Set source rectangle for conv: */
247 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
251 temp->format->Amask);
253 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
254 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
257 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
260 SDL_SetAlpha(temp,0,0);
262 SDL_BlitSurface(temp, &src, conv, NULL);
263 ptexture->sdl_surface = SDL_DisplayFormatAlpha(conv);
265 if (ptexture->sdl_surface == NULL)
266 st_abort("Can't covert to display format", file);
268 if (use_alpha == IGNORE_ALPHA)
269 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
271 SDL_FreeSurface(temp);
272 SDL_FreeSurface(conv);
274 ptexture->w = ptexture->sdl_surface->w;
275 ptexture->h = ptexture->sdl_surface->h;
278 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
281 /* SDL_Surface * temp;
283 temp = IMG_Load(file);
286 st_abort("Can't load", file);*/
288 ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
290 if (ptexture->sdl_surface == NULL)
291 st_abort("Can't covert to display format", "SURFACE");
293 if (use_alpha == IGNORE_ALPHA)
294 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
296 ptexture->w = ptexture->sdl_surface->w;
297 ptexture->h = ptexture->sdl_surface->h;
303 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
308 void texture_draw_sdl(texture_type* ptexture, float x, float y, int update)
314 dest.w = ptexture->w;
315 dest.h = ptexture->h;
317 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
319 if (update == UPDATE)
320 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
324 void texture_draw_bg_sdl(texture_type* ptexture, int update)
333 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
335 if (update == UPDATE)
336 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
339 void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update)
354 SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
356 if (update == UPDATE)
357 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
360 void texture_free_sdl(texture_type* ptexture)
362 SDL_FreeSurface(ptexture->sdl_surface);