|
From: John S. <jst...@us...> - 2009-12-23 05:59:33
|
Module: performous
Branch: master
Commit: e434b681c86d996b989adcc7df6321393178b754
Author: John Stumpo <st...@js...>
Date: Wed Dec 23 00:17:57 2009 -0500
Blit the surface returned by SDL_image onto an RGBA surface so we know it is in the right format to be loaded into a texture.
---
game/surface.cc | 20 ++++++++++++++------
1 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/game/surface.cc b/game/surface.cc
index aa09180..476c273 100644
--- a/game/surface.cc
+++ b/game/surface.cc
@@ -97,14 +97,22 @@ template <typename T> void loader(T& target, std::string filename, bool autocrop
if (imgsurf == NULL) {
throw std::runtime_error("Unable to load " + filename + ": " + IMG_GetError());
}
- // TODO: blit to a new RGBA surface to ensure it actually *is* RGBA
- try {
- target.load(imgsurf->w, imgsurf->h, pix::CHAR_RGBA, reinterpret_cast<const unsigned char*>(imgsurf->pixels));
- SDL_FreeSurface(imgsurf);
- } catch (...) {
+ // Copy the image to an RGBA surface to ensure that it is indeed RGBA.
+ SDL_Surface* rgbasurf = SDL_CreateRGBSurface(SDL_SWSURFACE, imgsurf->w, imgsurf->h, 32,
+#if SDL_BYTEORDER == SDL_BIGENDIAN
+ 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
+#else
+ 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
+#endif
+ );
+ if (rgbasurf == NULL) {
SDL_FreeSurface(imgsurf);
- throw;
+ throw std::runtime_error(std::string("Unable to allocate image surface: ") + IMG_GetError());
}
+ SDL_BlitSurface(imgsurf, NULL, rgbasurf, NULL);
+ SDL_FreeSurface(imgsurf);
+ target.load(rgbasurf->w, rgbasurf->h, pix::CHAR_RGBA, reinterpret_cast<const unsigned char*>(rgbasurf->pixels));
+ SDL_FreeSurface(rgbasurf);
#else
Magick::Image image;
Magick::Blob blob;
|