From 7b26645c1b28b212c011f98a5420c0989a7f0adc Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 15 Apr 2013 20:54:59 +0300 Subject: improve imagehelper class. --- src/resources/imagehelper.cpp | 2 +- src/resources/imagehelper.h | 25 +++++++++++++------------ src/resources/openglimagehelper.cpp | 21 +++++++++++---------- src/resources/openglimagehelper.h | 24 +++++++++++++----------- src/resources/sdlimagehelper.cpp | 21 +++++++++++---------- src/resources/sdlimagehelper.h | 14 ++++++++------ 6 files changed, 57 insertions(+), 50 deletions(-) diff --git a/src/resources/imagehelper.cpp b/src/resources/imagehelper.cpp index b0e02b75f..6a4a47505 100644 --- a/src/resources/imagehelper.cpp +++ b/src/resources/imagehelper.cpp @@ -41,7 +41,7 @@ ImageHelper *sdlImageHelper = nullptr; bool ImageHelper::mEnableAlpha = true; -Image *ImageHelper::load(SDL_RWops *const rw) +Image *ImageHelper::load(SDL_RWops *const rw) const { SDL_Surface *const tmpImage = loadPng(rw); if (!tmpImage) diff --git a/src/resources/imagehelper.h b/src/resources/imagehelper.h index e894bc66b..b55714787 100644 --- a/src/resources/imagehelper.h +++ b/src/resources/imagehelper.h @@ -59,30 +59,31 @@ class ImageHelper * @return NULL if an error occurred, a valid pointer * otherwise. */ - Image *load(SDL_RWops *const rw) A_WARN_UNUSED; + Image *load(SDL_RWops *const rw) const A_WARN_UNUSED; #ifdef __GNUC__ - virtual Image *load(SDL_RWops *rw, Dye const &dye) A_WARN_UNUSED = 0; + virtual Image *load(SDL_RWops *const rw, Dye + const &dye) const A_WARN_UNUSED = 0; - virtual Image *load(SDL_Surface *) A_WARN_UNUSED = 0; + virtual Image *load(SDL_Surface *const) const A_WARN_UNUSED = 0; - virtual Image *createTextSurface(SDL_Surface *tmpImage, - int width, int height, - float alpha) A_WARN_UNUSED = 0; + virtual Image *createTextSurface(SDL_Surface *const tmpImage, + const int width, const int height, + float alpha) const A_WARN_UNUSED = 0; - virtual int useOpenGL() A_WARN_UNUSED = 0; + virtual int useOpenGL() const A_WARN_UNUSED = 0; #else - virtual Image *load(SDL_RWops *rw, Dye const &dye) A_WARN_UNUSED + virtual Image *load(SDL_RWops *rw, Dye const &dye) const A_WARN_UNUSED { return nullptr; } - virtual Image *load(SDL_Surface *) A_WARN_UNUSED + virtual Image *load(SDL_Surface *) const A_WARN_UNUSED { return nullptr; } virtual Image *createTextSurface(SDL_Surface *const tmpImage, - const float alpha) A_WARN_UNUSED + const float alpha) const A_WARN_UNUSED { return nullptr; } - virtual int useOpenGL() A_WARN_UNUSED + virtual int useOpenGL() const A_WARN_UNUSED { return 0; } #endif @@ -92,7 +93,7 @@ class ImageHelper void dumpSurfaceFormat(const SDL_Surface *const image) const; virtual SDL_Surface *create32BitSurface(int width, int height) - A_WARN_UNUSED = 0; + const A_WARN_UNUSED = 0; static void setEnableAlpha(const bool n) { mEnableAlpha = n; } diff --git a/src/resources/openglimagehelper.cpp b/src/resources/openglimagehelper.cpp index 9843d5026..a107b0099 100644 --- a/src/resources/openglimagehelper.cpp +++ b/src/resources/openglimagehelper.cpp @@ -51,7 +51,7 @@ bool OpenGLImageHelper::mBlur = true; int OpenGLImageHelper::mUseOpenGL = 0; bool OpenGLImageHelper::mUseTextureSampler = false; -Image *OpenGLImageHelper::load(SDL_RWops *const rw, Dye const &dye) +Image *OpenGLImageHelper::load(SDL_RWops *const rw, Dye const &dye) const { SDL_Surface *const tmpImage = loadPng(rw); if (!tmpImage) @@ -125,14 +125,14 @@ Image *OpenGLImageHelper::load(SDL_RWops *const rw, Dye const &dye) return image; } -Image *OpenGLImageHelper::load(SDL_Surface *const tmpImage) +Image *OpenGLImageHelper::load(SDL_Surface *const tmpImage) const { return glLoad(tmpImage); } Image *OpenGLImageHelper::createTextSurface(SDL_Surface *const tmpImage, - int width, int height, - const float alpha) + const int width, const int height, + const float alpha) const { if (!tmpImage) return nullptr; @@ -143,7 +143,7 @@ Image *OpenGLImageHelper::createTextSurface(SDL_Surface *const tmpImage, return img; } -int OpenGLImageHelper::powerOfTwo(int input) const +int OpenGLImageHelper::powerOfTwo(const int input) const { int value; if (mTextureType == GL_TEXTURE_2D) @@ -159,7 +159,8 @@ int OpenGLImageHelper::powerOfTwo(int input) const return value >= mTextureSize ? mTextureSize : value; } -Image *OpenGLImageHelper::glLoad(SDL_Surface *tmpImage, int width, int height) +Image *OpenGLImageHelper::glLoad(SDL_Surface *tmpImage, + int width, int height) const { if (!tmpImage) return nullptr; @@ -297,17 +298,17 @@ Image *OpenGLImageHelper::glLoad(SDL_Surface *tmpImage, int width, int height) return new Image(texture, width, height, realWidth, realHeight); } -void OpenGLImageHelper::setLoadAsOpenGL(int useOpenGL) +void OpenGLImageHelper::setLoadAsOpenGL(const int useOpenGL) { OpenGLImageHelper::mUseOpenGL = useOpenGL; } -int OpenGLImageHelper::useOpenGL() +int OpenGLImageHelper::useOpenGL() const { return mUseOpenGL; } -void OpenGLImageHelper::initTextureSampler(GLint id) +void OpenGLImageHelper::initTextureSampler(const GLint id) { if (mBlur) { @@ -321,7 +322,7 @@ void OpenGLImageHelper::initTextureSampler(GLint id) } } -SDL_Surface *OpenGLImageHelper::create32BitSurface(int width, int height) +SDL_Surface *OpenGLImageHelper::create32BitSurface(int width, int height) const { #if SDL_BYTEORDER == SDL_BIG_ENDIAN const int rmask = 0xff000000; diff --git a/src/resources/openglimagehelper.h b/src/resources/openglimagehelper.h index 1c1f398eb..5c144809a 100644 --- a/src/resources/openglimagehelper.h +++ b/src/resources/openglimagehelper.h @@ -78,16 +78,18 @@ class OpenGLImageHelper final : public ImageHelper * @return NULL if an error occurred, a valid pointer * otherwise. */ - Image *load(SDL_RWops *const rw, Dye const &dye) A_WARN_UNUSED; + Image *load(SDL_RWops *const rw, + Dye const &dye) const override A_WARN_UNUSED; /** * Loads an image from an SDL surface. */ - Image *load(SDL_Surface *const tmpImage) A_WARN_UNUSED; + Image *load(SDL_Surface *const tmpImage) const override A_WARN_UNUSED; Image *createTextSurface(SDL_Surface *const tmpImage, - int width, int height, - const float alpha) A_WARN_UNUSED; + const int width, const int height, + const float alpha) + const override A_WARN_UNUSED; // OpenGL only public functions @@ -95,7 +97,7 @@ class OpenGLImageHelper final : public ImageHelper * Sets the target image format. Use false for SDL and * true for OpenGL. */ - static void setLoadAsOpenGL(int useOpenGL); + static void setLoadAsOpenGL(const int useOpenGL); static int getTextureType() A_WARN_UNUSED { return mTextureType; } @@ -117,26 +119,26 @@ class OpenGLImageHelper final : public ImageHelper * Tells if the image was loaded using OpenGL or SDL * @return true if OpenGL, false if SDL. */ - int useOpenGL() A_WARN_UNUSED; + int useOpenGL() const override A_WARN_UNUSED; static int getTextureSize() A_WARN_UNUSED { return mTextureSize; } - static void initTextureSampler(GLint id); + static void initTextureSampler(const GLint id); - static void setUseTextureSampler(bool b) + static void setUseTextureSampler(const bool b) { mUseTextureSampler = b; } - SDL_Surface *create32BitSurface(int width, int height); + SDL_Surface *create32BitSurface(int width, int height) const override; protected: /** * Returns the first power of two equal or bigger than the input. */ - int powerOfTwo(int input) const A_WARN_UNUSED; + int powerOfTwo(const int input) const A_WARN_UNUSED; Image *glLoad(SDL_Surface *tmpImage, - int width = 0, int height = 0) A_WARN_UNUSED; + int width = 0, int height = 0) const A_WARN_UNUSED; static int mUseOpenGL; static int mTextureSize; diff --git a/src/resources/sdlimagehelper.cpp b/src/resources/sdlimagehelper.cpp index 44478d297..0318014cd 100644 --- a/src/resources/sdlimagehelper.cpp +++ b/src/resources/sdlimagehelper.cpp @@ -38,7 +38,7 @@ bool SDLImageHelper::mEnableAlphaCache = false; -Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye) +Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye) const { SDL_Surface *const tmpImage = loadPng(rw); if (!tmpImage) @@ -58,7 +58,8 @@ Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye) rgba.Bmask = 0x0000FF00; rgba.Bloss = 0; rgba.Bshift = 8; rgba.Amask = 0x000000FF; rgba.Aloss = 0; rgba.Ashift = 0; - SDL_Surface *surf = SDL_ConvertSurface(tmpImage, &rgba, SDL_SWSURFACE); + SDL_Surface *const surf = SDL_ConvertSurface( + tmpImage, &rgba, SDL_SWSURFACE); SDL_FreeSurface(tmpImage); uint32_t *pixels = static_cast(surf->pixels); @@ -122,15 +123,15 @@ Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye) return image; } -Image *SDLImageHelper::load(SDL_Surface *const tmpImage) +Image *SDLImageHelper::load(SDL_Surface *const tmpImage) const { return _SDLload(tmpImage); } Image *SDLImageHelper::createTextSurface(SDL_Surface *const tmpImage, - int width A_UNUSED, - int height A_UNUSED, - const float alpha) + const int width A_UNUSED, + const int height A_UNUSED, + const float alpha) const { if (!tmpImage) return nullptr; @@ -145,7 +146,7 @@ Image *SDLImageHelper::createTextSurface(SDL_Surface *const tmpImage, // The alpha channel to be filled with alpha values uint8_t *alphaChannel = new uint8_t[sz]; - const SDL_PixelFormat * const fmt = tmpImage->format; + const SDL_PixelFormat *const fmt = tmpImage->format; if (fmt->Amask) { for (int i = 0; i < sz; ++ i) @@ -235,7 +236,7 @@ Image *SDLImageHelper::_SDLload(SDL_Surface *tmpImage) const // Figure out whether the image uses its alpha layer if (!tmpImage->format->palette) { - const SDL_PixelFormat * const fmt = tmpImage->format; + const SDL_PixelFormat *const fmt = tmpImage->format; if (fmt->Amask) { for (int i = 0; i < sz; ++ i) @@ -299,12 +300,12 @@ Image *SDLImageHelper::_SDLload(SDL_Surface *tmpImage) const return new Image(image, hasAlpha, alphaChannel); } -int SDLImageHelper::useOpenGL() +int SDLImageHelper::useOpenGL() const { return 0; } -SDL_Surface *SDLImageHelper::create32BitSurface(int width, int height) +SDL_Surface *SDLImageHelper::create32BitSurface(int width,int height) const { #if SDL_BYTEORDER == SDL_BIG_ENDIAN const int rmask = 0xff000000; diff --git a/src/resources/sdlimagehelper.h b/src/resources/sdlimagehelper.h index 0c643d0ca..8a538ca3c 100644 --- a/src/resources/sdlimagehelper.h +++ b/src/resources/sdlimagehelper.h @@ -57,16 +57,18 @@ class SDLImageHelper final : public ImageHelper * @return NULL if an error occurred, a valid pointer * otherwise. */ - Image *load(SDL_RWops *const rw, Dye const &dye) A_WARN_UNUSED; + Image *load(SDL_RWops *const rw, + Dye const &dye) const override A_WARN_UNUSED; /** * Loads an image from an SDL surface. */ - Image *load(SDL_Surface *const tmpImage) A_WARN_UNUSED; + Image *load(SDL_Surface *const tmpImage) const override A_WARN_UNUSED; Image *createTextSurface(SDL_Surface *const tmpImage, - int width, int height, - const float alpha) A_WARN_UNUSED; + const int width, const int height, + const float alpha) + const override A_WARN_UNUSED; static void SDLSetEnableAlphaCache(const bool n) { mEnableAlphaCache = n; } @@ -78,12 +80,12 @@ class SDLImageHelper final : public ImageHelper * Tells if the image was loaded using OpenGL or SDL * @return true if OpenGL, false if SDL. */ - int useOpenGL() A_WARN_UNUSED; + int useOpenGL() const override A_WARN_UNUSED; static SDL_Surface* SDLDuplicateSurface(SDL_Surface *const tmpImage) A_WARN_UNUSED; - SDL_Surface *create32BitSurface(int width, int height); + SDL_Surface *create32BitSurface(int width, int height) const override; protected: /** SDL_Surface to SDL_Surface Image loader */ -- cgit v1.2.3-70-g09d2