From 2dd7568047c7b0af8a7c8a656f1bb6708fb856c6 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Tue, 20 Mar 2007 23:26:28 +0000 Subject: Reduced amount of useless logging, calculate nearest power of two in a function and only do the alpha check in software mode. --- src/resources/image.cpp | 81 +++++++++++++++++++++++++++---------------------- src/resources/image.h | 6 ++++ 2 files changed, 50 insertions(+), 37 deletions(-) (limited to 'src/resources') diff --git a/src/resources/image.cpp b/src/resources/image.cpp index a7c81574..e2220088 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -80,7 +80,7 @@ Image* Image::load(void *buffer, unsigned int bufferSize, tmpImage = IMG_Load_RW(rw, 1); } - if (tmpImage == NULL) { + if (!tmpImage) { logger->log("Error, image load failed: %s", IMG_GetError()); return NULL; } @@ -99,49 +99,28 @@ Image* Image::load(void *buffer, unsigned int bufferSize, amask = 0xff000000; #endif - bool hasAlpha = false; - - // Figure out whether the image uses its alpha layer - for (int i = 0; i < tmpImage->w * tmpImage->h; ++i) - { - Uint8 r, g, b, a; - SDL_GetRGBA( - ((Uint32*) tmpImage->pixels)[i], - tmpImage->format, - &r, &g, &b, &a); - - if (a != 255) - { - hasAlpha = true; - break; - } - } - - if (hasAlpha) { - SDL_SetAlpha(tmpImage, SDL_SRCALPHA, SDL_ALPHA_OPAQUE); - } - #ifdef USE_OPENGL if (mUseOpenGL) { int width = tmpImage->w; int height = tmpImage->h; - int realWidth = 1, realHeight = 1; + int realWidth = powerOfTwo(width); + int realHeight = powerOfTwo(height); - while (realWidth < width && realWidth < 1024) { - realWidth *= 2; - } - - while (realHeight < height && realHeight < 1024) { - realHeight *= 2; + if (realWidth < width || realHeight < height) + { + logger->log("Warning: image too large, cropping to %dx%d texture!", + tmpImage->w, tmpImage->h); } + // Make sure the alpha channel is not used, but copied to destination SDL_SetAlpha(tmpImage, 0, SDL_ALPHA_OPAQUE); + SDL_Surface *oldImage = tmpImage; - tmpImage = SDL_CreateRGBSurface(SDL_SWSURFACE, realWidth, realHeight, 32, - rmask, gmask, bmask, amask); + tmpImage = SDL_CreateRGBSurface(SDL_SWSURFACE, realWidth, realHeight, + 32, rmask, gmask, bmask, amask); - if (tmpImage == NULL) { + if (!tmpImage) { logger->log("Error, image convert failed: out of memory"); return NULL; } @@ -151,8 +130,6 @@ Image* Image::load(void *buffer, unsigned int bufferSize, GLuint texture; glGenTextures(1, &texture); - logger->log("Binding texture %d (%dx%d)", - texture, tmpImage->w, tmpImage->h); glBindTexture(GL_TEXTURE_2D, texture); if (SDL_MUSTLOCK(tmpImage)) { @@ -208,6 +185,24 @@ Image* Image::load(void *buffer, unsigned int bufferSize, } #endif + bool hasAlpha = false; + + // Figure out whether the image uses its alpha layer + for (int i = 0; i < tmpImage->w * tmpImage->h; ++i) + { + Uint8 r, g, b, a; + SDL_GetRGBA( + ((Uint32*) tmpImage->pixels)[i], + tmpImage->format, + &r, &g, &b, &a); + + if (a != 255) + { + hasAlpha = true; + break; + } + } + SDL_Surface *image; // Convert the surface to the current display format @@ -219,7 +214,7 @@ Image* Image::load(void *buffer, unsigned int bufferSize, } SDL_FreeSurface(tmpImage); - if (image == NULL) { + if (!image) { logger->log("Error: Image convert failed."); return NULL; } @@ -278,10 +273,22 @@ float Image::getAlpha() } #ifdef USE_OPENGL -void Image::setLoadAsOpenGL(bool useOpenGL) +void +Image::setLoadAsOpenGL(bool useOpenGL) { Image::mUseOpenGL = useOpenGL; } + +int +Image::powerOfTwo(int input) +{ + int value = 1; + while (value < input && value < 1024) + { + value <<= 1; + } + return value; +} #endif //============================================================================ diff --git a/src/resources/image.h b/src/resources/image.h index c769ecb6..1bdfbf48 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -123,6 +123,12 @@ class Image : public Resource #ifdef USE_OPENGL Image(const std::string &idPath, GLuint glimage, int width, int height, int texWidth, int texHeight); + + /** + * Returns the first power of two equal or bigger than the input. + */ + static int + powerOfTwo(int input); #endif Image(const std::string &idPath, SDL_Surface *image); -- cgit v1.2.3-70-g09d2