More -Weffc++ cleanup
[supertux.git] / src / video / gl / gl_texture.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program 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.
8 //
9 //  This program 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.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/gameconfig.hpp"
18 #include "video/gl/gl_texture.hpp"
19
20 namespace {
21
22 inline bool is_power_of_2(int v)
23 {
24   return (v & (v-1)) == 0;
25 }
26
27 inline int next_power_of_two(int val)
28 {
29   int result = 1;
30   while(result < val)
31     result *= 2;
32   return result;
33 }
34
35 } // namespace
36
37 GLTexture::GLTexture(unsigned int width, unsigned int height) :
38   handle(),
39   texture_width(),
40   texture_height(),
41   image_width(),
42   image_height()
43 {
44   assert(is_power_of_2(width));
45   assert(is_power_of_2(height));
46   texture_width  = width;
47   texture_height = height;
48   image_width  = width;
49   image_height = height;
50
51   assert_gl("before creating texture");
52   glGenTextures(1, &handle);
53
54   try {
55     glBindTexture(GL_TEXTURE_2D, handle);
56
57     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width,
58                  texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
59
60     set_texture_params();
61   } catch(...) {
62     glDeleteTextures(1, &handle);
63     throw;
64   }
65 }
66
67 GLTexture::GLTexture(SDL_Surface* image) :
68   handle(),
69   texture_width(),
70   texture_height(),
71   image_width(),
72   image_height()
73 {
74   texture_width = next_power_of_two(image->w);
75   texture_height = next_power_of_two(image->h);
76   image_width = image->w;
77   image_height = image->h;
78
79 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
80   SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
81                                               texture_width, texture_height, 32,
82                                               0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
83 #else
84   SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
85                                               texture_width, texture_height, 32,
86                                               0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
87 #endif
88
89   if(convert == 0) {
90     throw std::runtime_error("Couldn't create texture: out of memory");
91   }
92
93   SDL_SetAlpha(image, 0, 0);
94   SDL_BlitSurface(image, 0, convert, 0);
95
96   assert_gl("before creating texture");
97   glGenTextures(1, &handle);
98
99   try {
100     GLenum sdl_format;
101     if(convert->format->BytesPerPixel == 3)
102       sdl_format = GL_RGB;
103     else if(convert->format->BytesPerPixel == 4)
104       sdl_format = GL_RGBA;
105     else
106       assert(false);
107
108     glBindTexture(GL_TEXTURE_2D, handle);
109     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
110 #ifdef GL_UNPACK_ROW_LENGTH
111     glPixelStorei(GL_UNPACK_ROW_LENGTH, convert->pitch/convert->format->BytesPerPixel);
112 #else
113     /* OpenGL ES doesn't support UNPACK_ROW_LENGTH, let's hope SDL didn't add
114      * padding bytes, otherwise we need some extra code here... */
115     assert(convert->pitch == texture_width * convert->format->BytesPerPixel);
116 #endif
117
118     if(SDL_MUSTLOCK(convert))
119     {
120       SDL_LockSurface(convert);
121     }
122     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width,
123                  texture_height, 0, sdl_format,
124                  GL_UNSIGNED_BYTE, convert->pixels);
125     if(SDL_MUSTLOCK(convert))
126     {
127       SDL_UnlockSurface(convert);
128     }
129
130     assert_gl("creating texture");
131
132     set_texture_params();
133   } catch(...) {
134     glDeleteTextures(1, &handle);
135     SDL_FreeSurface(convert);
136     throw;
137   }
138   SDL_FreeSurface(convert);
139 }
140
141 GLTexture::~GLTexture()
142 {
143   glDeleteTextures(1, &handle);
144 }
145
146 void
147 GLTexture::set_texture_params()
148 {
149   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
150   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
151 #ifdef GL_CLAMP
152   /* OpenGL ES doesn't support it */
153   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
154   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
155 #endif
156
157   assert_gl("set texture params");
158 }
159
160 /* EOF */