1 /************************************************************************
2 This file is part of NE.
4 NE is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 NE is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with NE. If not, see <http://www.gnu.org/licenses/>.
16 ************************************************************************/
26 #include <SDL/SDL_image.h>
34 glMatrixMode(GL_TEXTURE
);
39 struct texture
*t_create()
41 struct texture
*t
= (struct texture
*)malloc(sizeof(struct texture
));
49 void t_destroy(struct texture
*t
)
55 int t_ready(struct texture
*t
)
60 void t_load(struct texture
*t
, const char *filename
)
65 SDL_Surface
*img
= IMG_Load(filename
);
69 fprintf(stderr
, "Texture load error: %s\n", IMG_GetError());
73 if (img
->format
->BytesPerPixel
< 2)
75 fprintf(stderr
, "Texture load error: %s is not true color\n", filename
);
79 glGenTextures(1, &t
->gl_handle
);
80 glBindTexture(GL_TEXTURE_2D
, t
->gl_handle
);
85 t
->u
= t
->v
= 1.0; // TODO: change this when textures are scaled to pow2
87 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
88 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
90 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
91 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
94 if (img
->format
->BytesPerPixel
== 4) mode
= GL_RGBA
;
100 glTexImage2D(GL_TEXTURE_2D
, 0, img
->format
->BytesPerPixel
,
101 img
->w
, img
->h
, 0, mode
, GL_UNSIGNED_BYTE
, img
->pixels
);
105 glTexParameterf( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
106 gluBuild2DMipmaps(GL_TEXTURE_2D
, img
->format
->BytesPerPixel
, img
->w
, img
->h
,
107 mode
, GL_UNSIGNED_BYTE
, img
->pixels
);
110 SDL_FreeSurface(img
);
115 void t_unload(struct texture
*t
)
119 glDeleteTextures(1, &t
->gl_handle
);
123 void t_apply(struct texture
*t
)
125 glBindTexture(GL_TEXTURE_2D
, t
->gl_handle
);
128 void t_set(struct texture
*t
, GLuint name
, GLuint value
)
131 glTexParameteri(GL_TEXTURE_2D
,name
,value
);
134 struct texture
*t_screenshot()
136 struct texture
*t
= t_create();
138 glGenTextures(1, &t
->gl_handle
);
139 glBindTexture(GL_TEXTURE_2D
, t
->gl_handle
);
141 const struct video_info
* vi
= v_info();
143 // TODO: figure out what old cards can't render non pow2 textures...
144 t
->width
= vi
->width
;
145 t
->height
= vi
->height
;
149 glCopyTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, 0, 0, t
->width
, t
->height
, 0);
151 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
152 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
154 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
155 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);