1 // $Id: gl_texture.cpp 4063 2006-07-21 21:05:23Z anmaster $
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "gl_texture.hpp"
25 #include "gameconfig.hpp"
34 inline bool is_power_of_2(int v)
36 return (v & (v-1)) == 0;
39 inline int next_power_of_two(int val)
50 Texture::Texture(unsigned int width, unsigned int height)
52 assert(is_power_of_2(width));
53 assert(is_power_of_2(height));
54 texture_width = width;
55 texture_height = height;
57 image_height = height;
59 assert_gl("before creating texture");
60 glGenTextures(1, &handle);
63 glBindTexture(GL_TEXTURE_2D, handle);
65 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width,
66 texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
70 glDeleteTextures(1, &handle);
75 Texture::Texture(SDL_Surface* image)
77 texture_width = next_power_of_two(image->w);
78 texture_height = next_power_of_two(image->h);
79 image_width = image->w;
80 image_height = image->h;
82 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
83 SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
84 texture_width, texture_height, 32,
85 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
87 SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
88 texture_width, texture_height, 32,
89 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
93 throw std::runtime_error("Couldn't create texture: out of memory");
96 SDL_SetAlpha(image, 0, 0);
97 SDL_BlitSurface(image, 0, convert, 0);
99 assert_gl("before creating texture");
100 glGenTextures(1, &handle);
104 if(convert->format->BytesPerPixel == 3)
106 else if(convert->format->BytesPerPixel == 4)
107 sdl_format = GL_RGBA;
111 glBindTexture(GL_TEXTURE_2D, handle);
112 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
113 glPixelStorei(GL_UNPACK_ROW_LENGTH, convert->pitch/convert->format->BytesPerPixel);
114 if(SDL_MUSTLOCK(convert))
116 SDL_LockSurface(convert);
118 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width,
119 texture_height, 0, sdl_format,
120 GL_UNSIGNED_BYTE, convert->pixels);
121 if(SDL_MUSTLOCK(convert))
123 SDL_UnlockSurface(convert);
126 assert_gl("creating texture");
128 set_texture_params();
130 glDeleteTextures(1, &handle);
131 SDL_FreeSurface(convert);
134 SDL_FreeSurface(convert);
139 glDeleteTextures(1, &handle);
143 Texture::set_texture_params()
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
148 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
150 assert_gl("set texture params");