]>
Dogcows Code - chaz/yoink/blob - src/texture.cc
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
32 #include <boost/bind.hpp>
35 #include <SDL/SDL_image.h>
37 #include "dispatcher.hh"
52 glDeleteTextures(1, &object
);
57 void contextRecreated(const notification
& note
)
64 texture_impl(const std::string
& filePath
, bool keepInMemory
)
65 : object(0), imageData(0), imagePath(filePath
), keepData(keepInMemory
)
67 dispatcher::instance().addHandler("video.context_recreated",
68 boost::bind(&texture_impl::contextRecreated
, this, _1
),
74 dispatcher::instance().removeHandler(this);
78 SDL_FreeSurface(imageData
);
84 static int powerOfTwo(int input
)
95 // Adapted from some public domain code. This code is common enough that it
96 // really should be included in SDL_image...
97 static SDL_Surface
* prepareImageForGL(SDL_Surface
* surface
)
99 /* Use the surface width and height expanded to powers of 2 */
100 int w
= powerOfTwo(surface
->w
);
101 int h
= powerOfTwo(surface
->h
);
103 SDL_Surface
* image
= SDL_CreateRGBSurface
108 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
126 // Save the alpha blending attributes.
127 Uint32 savedFlags
= surface
->flags
&(SDL_SRCALPHA
|SDL_RLEACCELOK
);
128 Uint8 savedAlpha
= surface
->format
->alpha
;
129 if (savedFlags
& SDL_SRCALPHA
)
131 SDL_SetAlpha(surface
, 0, 0);
134 SDL_Rect srcArea
, destArea
;
135 /* Copy the surface into the GL texture image */
136 srcArea
.x
= 0; destArea
.x
= 0;
137 /* Copy it in at the bottom, because we're going to flip
138 this image upside-down in a moment
140 srcArea
.y
= 0; destArea
.y
= h
- surface
->h
;
141 srcArea
.w
= surface
->w
;
142 srcArea
.h
= surface
->h
;
143 SDL_BlitSurface(surface
, &srcArea
, image
, &destArea
);
145 /* Restore the alpha blending attributes */
146 if (savedFlags
& SDL_SRCALPHA
)
148 SDL_SetAlpha(surface
, savedFlags
, savedAlpha
);
151 /* Turn the image upside-down, because OpenGL textures
152 start at the bottom-left, instead of the top-left
154 Uint8 line
[image
->pitch
];
156 /* These two make the following more readable */
157 Uint8
*pixels
= static_cast<Uint8
*>(image
->pixels
);
158 Uint16 pitch
= image
->pitch
;
160 int yend
= image
->h
- 1;
162 // TODO: consider if this lock is legal/appropriate
163 if (SDL_MUSTLOCK(image
)) { SDL_LockSurface(image
); }
164 while (ybegin
< yend
)
166 memcpy(line
, pixels
+ pitch
*ybegin
, pitch
);
167 memcpy(pixels
+ pitch
*ybegin
, pixels
+ pitch
*yend
, pitch
);
168 memcpy(pixels
+ pitch
*yend
, line
, pitch
);
172 if (SDL_MUSTLOCK(image
)) { SDL_UnlockSurface(image
); }
180 SDL_Surface
* surface
;
182 surface
= IMG_Load(imagePath
.c_str());
186 throw std::runtime_error("could not load image data from file");
189 imageData
= prepareImageForGL(surface
);
190 SDL_FreeSurface(surface
);
194 throw std::runtime_error("error in preparing image data for GL");
197 if (imageData
->format
->BytesPerPixel
== 3)
201 else if (imageData
->format
->BytesPerPixel
== 4)
207 SDL_FreeSurface(imageData
);
208 throw std::runtime_error("image must be 24 or 32 bpp");
211 width
= imageData
->w
;
212 height
= imageData
->h
;
229 glGenTextures(1, &object
);
231 glBindTexture(GL_TEXTURE_2D
, object
);
246 // These default filters can be changed later...
247 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
248 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
252 SDL_FreeSurface(imageData
);
263 std::string imagePath
;
265 SDL_Surface
* imageData
;
269 texture::texture(const std::string
& filePath
, bool keepInMemory
)
270 : impl(new texture_impl(filePath
, keepInMemory
)) {}
273 const std::string
& texture::filePath()
275 return impl
->imagePath
;
281 glBindTexture(GL_TEXTURE_2D
, object());
284 GLuint
texture::object()
295 unsigned texture::width()
300 unsigned texture::height()
This page took 0.048523 seconds and 5 git commands to generate.