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>
15 #include <SDL/SDL_opengl.h>
21 void texture_load(texture_type* ptexture, char * file, int use_alpha)
25 temp = IMG_Load(file);
28 st_abort("Can't load", file);
30 ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
32 if (ptexture->sdl_surface == NULL)
33 st_abort("Can't covert to display format", file);
35 if (use_alpha == IGNORE_ALPHA)
36 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
38 SDL_FreeSurface(temp);
40 ptexture->w = ptexture->sdl_surface->w;
41 ptexture->h = ptexture->sdl_surface->h;
45 create_gl_texture(ptexture->sdl_surface,&ptexture->gl_texture);
49 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
51 /* SDL_Surface * temp;
53 temp = IMG_Load(file);
56 st_abort("Can't load", file);*/
58 ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
60 if (ptexture->sdl_surface == NULL)
61 st_abort("Can't covert to display format", "SURFACE");
63 if (use_alpha == IGNORE_ALPHA)
64 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
66 ptexture->w = ptexture->sdl_surface->w;
67 ptexture->h = ptexture->sdl_surface->h;
71 create_gl_texture(ptexture->sdl_surface,&ptexture->gl_texture);
75 void texture_draw(texture_type* ptexture, float x, float y, int update)
79 glColor4ub(255, 255, 255,255);
80 glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
82 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
87 glTexCoord2f((float)ptexture->w, 0);
88 glVertex2f((float)ptexture->w+x, y);
89 glTexCoord2f((float)ptexture->w, (float)ptexture->h);
90 glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
91 glTexCoord2f(0, (float)ptexture->h);
92 glVertex2f(x, (float)ptexture->h+y);
101 dest.w = ptexture->w;
102 dest.h = ptexture->h;
104 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
106 if (update == UPDATE)
107 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
111 void texture_draw_bg(texture_type* ptexture, int update)
115 //glColor3ub(255, 255, 255);
117 glEnable(GL_TEXTURE_RECTANGLE_NV);
118 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
121 glTexCoord2f(0, 0); glVertex2f(0, 0);
122 glTexCoord2f((float)ptexture->w, 0); glVertex2f(screen->w, 0);
123 glTexCoord2f((float)ptexture->w, (float)ptexture->h); glVertex2f(screen->w, screen->h);
124 glTexCoord2f(0, (float)ptexture->h); glVertex2f(0, screen->h);
137 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
139 if (update == UPDATE)
140 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
144 void texture_draw_part(texture_type* ptexture, float x, float y, float w, float h, int update)
148 glColor3ub(255, 255, 255);
150 glEnable(GL_TEXTURE_RECTANGLE_NV);
151 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
156 glTexCoord2f(x+w, y);
158 glTexCoord2f(x+w, y+h);
159 glVertex2f(w+x, h+y);
160 glTexCoord2f(x, y+h);
179 SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
181 if (update == UPDATE)
182 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
186 void texture_free(texture_type* ptexture)
188 SDL_FreeSurface(ptexture->sdl_surface);
190 glDeleteTextures(1, &ptexture->gl_texture);