3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 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.
21 #include "texture.hpp"
27 static inline bool is_power_of_2(int v)
29 return (v & (v-1)) == 0;
32 Texture::Texture(unsigned int w, unsigned int h, GLenum glformat)
34 assert(is_power_of_2(w));
35 assert(is_power_of_2(h));
40 assert_gl("before creating texture");
41 glGenTextures(1, &handle);
44 glBindTexture(GL_TEXTURE_2D, handle);
46 glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, GL_RGBA,
51 glDeleteTextures(1, &handle);
56 Texture::Texture(SDL_Surface* image, GLenum glformat)
58 const SDL_PixelFormat* format = image->format;
59 if(!is_power_of_2(image->w) || !is_power_of_2(image->h))
60 throw std::runtime_error("image has no power of 2 size");
61 if(format->BitsPerPixel != 24 && format->BitsPerPixel != 32)
62 throw std::runtime_error("image has no 24 or 32 bit color depth");
64 this->width = image->w;
65 this->height = image->h;
67 assert_gl("before creating texture");
68 glGenTextures(1, &handle);
72 if(format->BytesPerPixel == 3)
74 else if(format->BytesPerPixel == 4)
79 glBindTexture(GL_TEXTURE_2D, handle);
80 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
81 glPixelStorei(GL_UNPACK_ROW_LENGTH, image->pitch/format->BytesPerPixel);
82 glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, sdl_format,
83 GL_UNSIGNED_BYTE, image->pixels);
85 assert_gl("creating texture");
89 glDeleteTextures(1, &handle);
96 glDeleteTextures(1, &handle);
100 Texture::set_texture_params()
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
105 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
107 assert_gl("set texture params");