Only use power-of-two texture on OpenGL-ES, as newer OpenGL standards allow non-power...
[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 #ifdef GL_VERSION_ES_CM_1_0
45   assert(is_power_of_2(width));
46   assert(is_power_of_2(height));
47 #endif
48   texture_width  = width;
49   texture_height = height;
50   image_width  = width;
51   image_height = height;
52
53   assert_gl("before creating texture");
54   glGenTextures(1, &handle);
55
56   try {
57     glBindTexture(GL_TEXTURE_2D, handle);
58
59     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width,
60                  texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
61
62     set_texture_params();
63   } catch(...) {
64     glDeleteTextures(1, &handle);
65     throw;
66   }
67 }
68
69 GLTexture::GLTexture(SDL_Surface* image) :
70   handle(),
71   texture_width(),
72   texture_height(),
73   image_width(),
74   image_height()
75 {
76 #ifdef GL_VERSION_ES_CM_1_0
77   texture_width = next_power_of_two(image->w);
78   texture_height = next_power_of_two(image->h);
79 #else
80   texture_width  = image->w;
81   texture_height = image->h;
82 #endif
83
84   image_width  = image->w;
85   image_height = image->h;
86
87 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
88   SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
89                                               texture_width, texture_height, 32,
90                                               0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
91 #else
92   SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
93                                               texture_width, texture_height, 32,
94                                               0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
95 #endif
96
97   if(convert == 0) {
98     throw std::runtime_error("Couldn't create texture: out of memory");
99   }
100
101   SDL_SetAlpha(image, 0, 0);
102   SDL_BlitSurface(image, 0, convert, 0);
103
104   assert_gl("before creating texture");
105   glGenTextures(1, &handle);
106
107   try {
108     GLenum sdl_format;
109     if(convert->format->BytesPerPixel == 3)
110       sdl_format = GL_RGB;
111     else if(convert->format->BytesPerPixel == 4)
112       sdl_format = GL_RGBA;
113     else
114       assert(false);
115
116     glBindTexture(GL_TEXTURE_2D, handle);
117     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
118 #ifdef GL_UNPACK_ROW_LENGTH
119     glPixelStorei(GL_UNPACK_ROW_LENGTH, convert->pitch/convert->format->BytesPerPixel);
120 #else
121     /* OpenGL ES doesn't support UNPACK_ROW_LENGTH, let's hope SDL didn't add
122      * padding bytes, otherwise we need some extra code here... */
123     assert(convert->pitch == texture_width * convert->format->BytesPerPixel);
124 #endif
125
126     if(SDL_MUSTLOCK(convert))
127     {
128       SDL_LockSurface(convert);
129     }
130     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width,
131                  texture_height, 0, sdl_format,
132                  GL_UNSIGNED_BYTE, convert->pixels);
133     if(SDL_MUSTLOCK(convert))
134     {
135       SDL_UnlockSurface(convert);
136     }
137
138     assert_gl("creating texture");
139
140     set_texture_params();
141   } catch(...) {
142     glDeleteTextures(1, &handle);
143     SDL_FreeSurface(convert);
144     throw;
145   }
146   SDL_FreeSurface(convert);
147 }
148
149 GLTexture::~GLTexture()
150 {
151   glDeleteTextures(1, &handle);
152 }
153
154 void
155 GLTexture::set_texture_params()
156 {
157   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
158   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
159 #ifdef GL_CLAMP
160   /* OpenGL ES doesn't support it */
161   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
162   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
163 #endif
164
165   assert_gl("set texture params");
166 }
167
168 /* EOF */