Added a ton of stuff, including the beginnings of a new UI system
[ne.git] / src / backend / texture.c
blob88b20980e2f50bbe8477a31955990c89f866ad88
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 ************************************************************************/
18 #include "texture.h"
19 #include "error.h"
20 #include "video.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <GL/gl.h>
25 #include <GL/glu.h>
26 #include <SDL/SDL_image.h>
28 void t_init()
30 static int init = 0;
31 if (init) return;
32 init = 1;
34 glMatrixMode(GL_TEXTURE);
35 glLoadIdentity();
36 //glScalef(-1,1,1);
39 struct texture *t_create()
41 struct texture *t = (struct texture*)malloc(sizeof(struct texture));
43 t->ready = 0;
44 t->gl_handle = 0;
46 return t;
49 void t_destroy(struct texture *t)
51 t_unload(t);
52 free(t);
55 int t_ready(struct texture *t)
57 return t->ready;
60 void t_load(struct texture *t, const char *filename)
62 t_unload(t);
63 t->ready = 0;
65 SDL_Surface *img = IMG_Load(filename);
67 if (!img)
69 fprintf(stderr, "Texture load error: %s\n", IMG_GetError());
70 return;
73 if (img->format->BytesPerPixel < 2)
75 fprintf(stderr, "Texture load error: %s is not true color\n", filename);
76 return;
79 glGenTextures(1, &t->gl_handle);
80 glBindTexture(GL_TEXTURE_2D, t->gl_handle);
82 t->width = img->w;
83 t->height = img->h;
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);
93 GLuint mode = GL_RGB;
94 if (img->format->BytesPerPixel == 4) mode = GL_RGBA;
96 int mipmap = 0;
98 if (!mipmap)
100 glTexImage2D(GL_TEXTURE_2D, 0, img->format->BytesPerPixel,
101 img->w, img->h, 0, mode, GL_UNSIGNED_BYTE, img->pixels);
103 else
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);
112 t->ready = 1;
115 void t_unload(struct texture *t)
117 if (t_ready(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)
130 t_apply(t);
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;
147 t->u = t->v = 1.0;
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);
157 t->ready = 1;
159 return t;