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 void texture_draw_gl(texture_type* ptexture, float x, float y, int update)
70 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
72 glColor4ub(255, 255, 255,255);
74 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
79 glTexCoord2f((float)ptexture->w, 0);
80 glVertex2f((float)ptexture->w+x, y);
81 glTexCoord2f((float)ptexture->w, (float)ptexture->h);
82 glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
83 glTexCoord2f(0, (float)ptexture->h);
84 glVertex2f(x, (float)ptexture->h+y);
90 void texture_draw_bg_gl(texture_type* ptexture, int update)
92 glColor3ub(255, 255, 255);
94 glEnable(GL_TEXTURE_RECTANGLE_NV);
95 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
100 glTexCoord2f((float)ptexture->w, 0);
101 glVertex2f(screen->w, 0);
102 glTexCoord2f((float)ptexture->w, (float)ptexture->h);
103 glVertex2f(screen->w, screen->h);
104 glTexCoord2f(0, (float)ptexture->h);
105 glVertex2f(0, screen->h);
109 void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, int update)
112 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
115 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
117 glColor4ub(255, 255, 255,255);
119 glEnable(GL_TEXTURE_RECTANGLE_NV);
125 glTexCoord2f(x+w, y);
127 glTexCoord2f(x+w, y+h);
128 glVertex2f(w+x, h+y);
129 glTexCoord2f(x, y+h);
137 void texture_create_gl(SDL_Surface * surf, GLint * tex)
143 conv = SDL_CreateRGBSurface(surf->flags, surf->w, surf->h, surf->format->BitsPerPixel,
144 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
145 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
148 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
151 /* Save the alpha blending attributes */
152 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
153 saved_alpha = surf->format->alpha;
154 if ( (saved_flags & SDL_SRCALPHA)
157 SDL_SetAlpha(surf, 0, 0);
160 SDL_BlitSurface(surf, 0, conv, 0);
162 /* Restore the alpha blending attributes */
163 if ( (saved_flags & SDL_SRCALPHA)
166 SDL_SetAlpha(surf, saved_flags, saved_alpha);
170 glGenTextures(1, &*tex);
172 glBindTexture(GL_TEXTURE_RECTANGLE_NV , *tex);
173 glEnable(GL_TEXTURE_RECTANGLE_NV);
174 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
175 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
176 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
177 glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB10_A2, conv->w, conv->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
178 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
179 SDL_FreeSurface(conv);
182 void texture_free_gl(texture_type* ptexture)
184 SDL_FreeSurface(ptexture->sdl_surface);
185 glDeleteTextures(1, &ptexture->gl_texture);
189 void texture_load_sdl(texture_type* ptexture, char * file, int use_alpha)
193 temp = IMG_Load(file);
196 st_abort("Can't load", file);
198 ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
200 if (ptexture->sdl_surface == NULL)
201 st_abort("Can't covert to display format", file);
203 if (use_alpha == IGNORE_ALPHA)
204 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
206 SDL_FreeSurface(temp);
208 ptexture->w = ptexture->sdl_surface->w;
209 ptexture->h = ptexture->sdl_surface->h;
213 void texture_load_part_sdl(texture_type* ptexture, char * file, int x, int y, int w, int h, int use_alpha)
220 temp = IMG_Load(file);
223 st_abort("Can't load", file);
225 /* Set source rectangle for conv: */
232 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
236 temp->format->Amask);
238 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
239 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
242 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
245 SDL_SetAlpha(temp,0,0);
247 SDL_BlitSurface(temp, &src, conv, NULL);
248 ptexture->sdl_surface = SDL_DisplayFormatAlpha(conv);
250 if (ptexture->sdl_surface == NULL)
251 st_abort("Can't covert to display format", file);
253 if (use_alpha == IGNORE_ALPHA)
254 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
256 SDL_FreeSurface(temp);
257 SDL_FreeSurface(conv);
259 ptexture->w = ptexture->sdl_surface->w;
260 ptexture->h = ptexture->sdl_surface->h;
263 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
266 /* SDL_Surface * temp;
268 temp = IMG_Load(file);
271 st_abort("Can't load", file);*/
273 ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
275 if (ptexture->sdl_surface == NULL)
276 st_abort("Can't covert to display format", "SURFACE");
278 if (use_alpha == IGNORE_ALPHA)
279 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
281 ptexture->w = ptexture->sdl_surface->w;
282 ptexture->h = ptexture->sdl_surface->h;
288 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
293 void texture_draw_sdl(texture_type* ptexture, float x, float y, int update)
299 dest.w = ptexture->w;
300 dest.h = ptexture->h;
302 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
304 if (update == UPDATE)
305 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
309 void texture_draw_bg_sdl(texture_type* ptexture, int update)
318 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
320 if (update == UPDATE)
321 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
324 void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update)
339 SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
341 if (update == UPDATE)
342 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
345 void texture_free_sdl(texture_type* ptexture)
347 SDL_FreeSurface(ptexture->sdl_surface);