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.
22 #include "texture.hpp"
28 static inline bool is_power_of_2(int v)
30 return (v & (v-1)) == 0;
33 Texture::Texture(unsigned int w, unsigned int h, GLenum glformat)
35 assert(is_power_of_2(w));
36 assert(is_power_of_2(h));
41 assert_gl("before creating texture");
42 glGenTextures(1, &handle);
45 glBindTexture(GL_TEXTURE_2D, handle);
47 glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, GL_RGBA,
52 glDeleteTextures(1, &handle);
57 Texture::Texture(SDL_Surface* image, GLenum glformat)
59 const SDL_PixelFormat* format = image->format;
60 if(!is_power_of_2(image->w) || !is_power_of_2(image->h))
61 throw std::runtime_error("image has no power of 2 size");
62 if(format->BitsPerPixel != 24 && format->BitsPerPixel != 32)
63 throw std::runtime_error("image has no 24 or 32 bit color depth");
65 this->width = image->w;
66 this->height = image->h;
68 assert_gl("before creating texture");
69 glGenTextures(1, &handle);
73 if(format->BytesPerPixel == 3)
75 else if(format->BytesPerPixel == 4)
80 glBindTexture(GL_TEXTURE_2D, handle);
81 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
82 glPixelStorei(GL_UNPACK_ROW_LENGTH, image->pitch/format->BytesPerPixel);
83 glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, sdl_format,
84 GL_UNSIGNED_BYTE, image->pixels);
86 assert_gl("creating texture");
90 glDeleteTextures(1, &handle);
97 glDeleteTextures(1, &handle);
101 Texture::set_texture_params()
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
105 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
108 assert_gl("set texture params");