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_free = texture_free_sdl;
25 texture_draw = texture_draw_sdl;
26 texture_draw_bg = texture_draw_bg_sdl;
27 texture_draw_part = texture_draw_part_sdl;
31 texture_load = texture_load_gl;
32 texture_free = texture_free_gl;
33 texture_draw = texture_draw_gl;
34 texture_draw_bg = texture_draw_bg_gl;
35 texture_draw_part = texture_draw_part_gl;
39 texture_load = texture_load_sdl;
40 texture_free = texture_free_sdl;
41 texture_draw = texture_draw_sdl;
42 texture_draw_bg = texture_draw_bg_sdl;
43 texture_draw_part = texture_draw_part_sdl;
49 void texture_load_gl(texture_type* ptexture, char * file, int use_alpha)
51 texture_load_sdl(ptexture,file,use_alpha);
52 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
55 void texture_draw_gl(texture_type* ptexture, float x, float y, int update)
57 glColor4ub(255, 255, 255,255);
58 glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
60 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
65 glTexCoord2f((float)ptexture->w, 0);
66 glVertex2f((float)ptexture->w+x, y);
67 glTexCoord2f((float)ptexture->w, (float)ptexture->h);
68 glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
69 glTexCoord2f(0, (float)ptexture->h);
70 glVertex2f(x, (float)ptexture->h+y);
74 void texture_draw_bg_gl(texture_type* ptexture, int update)
76 //glColor3ub(255, 255, 255);
78 glEnable(GL_TEXTURE_RECTANGLE_NV);
79 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
82 glTexCoord2f(0, 0); glVertex2f(0, 0);
83 glTexCoord2f((float)ptexture->w, 0); glVertex2f(screen->w, 0);
84 glTexCoord2f((float)ptexture->w, (float)ptexture->h); glVertex2f(screen->w, screen->h);
85 glTexCoord2f(0, (float)ptexture->h); glVertex2f(0, screen->h);
89 void texture_draw_part_gl(texture_type* ptexture, float x, float y, float w, float h, int update)
91 glColor3ub(255, 255, 255);
93 glEnable(GL_TEXTURE_RECTANGLE_NV);
94 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
101 glTexCoord2f(x+w, y+h);
102 glVertex2f(w+x, h+y);
103 glTexCoord2f(x, y+h);
108 void texture_create_gl(SDL_Surface * surf, GLint * tex)
111 conv = SDL_CreateRGBSurface(SDL_SWSURFACE , surf->w, surf->h, 32,
112 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
113 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
115 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
117 SDL_BlitSurface(surf, 0, conv, 0);
118 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
119 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
121 glGenTextures(1, &*tex);
123 glBindTexture(GL_TEXTURE_RECTANGLE_NV , *tex);
124 glEnable(GL_TEXTURE_RECTANGLE_NV);
125 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
126 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
128 glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, 3, conv->w, conv->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
129 //glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_NV, 0, 0, 0, 0, 0, conv->w, conv->h);
130 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
131 SDL_FreeSurface(conv);
134 void texture_free_gl(texture_type* ptexture)
136 SDL_FreeSurface(ptexture->sdl_surface);
137 glDeleteTextures(1, &ptexture->gl_texture);
141 void texture_load_sdl(texture_type* ptexture, char * file, int use_alpha)
145 temp = IMG_Load(file);
148 st_abort("Can't load", file);
150 ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
152 if (ptexture->sdl_surface == NULL)
153 st_abort("Can't covert to display format", file);
155 if (use_alpha == IGNORE_ALPHA)
156 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
158 SDL_FreeSurface(temp);
160 ptexture->w = ptexture->sdl_surface->w;
161 ptexture->h = ptexture->sdl_surface->h;
165 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
168 /* SDL_Surface * temp;
170 temp = IMG_Load(file);
173 st_abort("Can't load", file);*/
175 ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
177 if (ptexture->sdl_surface == NULL)
178 st_abort("Can't covert to display format", "SURFACE");
180 if (use_alpha == IGNORE_ALPHA)
181 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
183 ptexture->w = ptexture->sdl_surface->w;
184 ptexture->h = ptexture->sdl_surface->h;
189 texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
194 void texture_draw_sdl(texture_type* ptexture, float x, float y, int update)
200 dest.w = ptexture->w;
201 dest.h = ptexture->h;
203 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
205 if (update == UPDATE)
206 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
210 void texture_draw_bg_sdl(texture_type* ptexture, int update)
219 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
221 if (update == UPDATE)
222 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
225 void texture_draw_part_sdl(texture_type* ptexture, float x, float y, float w, float h, int update)
240 SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
242 if (update == UPDATE)
243 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
246 void texture_free_sdl(texture_type* ptexture)
248 SDL_FreeSurface(ptexture->sdl_surface);