diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-10-18 18:39:48 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-10-18 18:39:48 +0000 |
commit | 86441fda9c561dd264039edc68ddabbf7cb54ce2 (patch) | |
tree | f9d15edf9587bc149a42399369bfcf3b9d5c5b27 /src/resources | |
parent | d3ab0ef7be4c1f0098acc08748c308091e75dfff (diff) | |
download | mana-86441fda9c561dd264039edc68ddabbf7cb54ce2.tar.gz mana-86441fda9c561dd264039edc68ddabbf7cb54ce2.tar.bz2 mana-86441fda9c561dd264039edc68ddabbf7cb54ce2.tar.xz mana-86441fda9c561dd264039edc68ddabbf7cb54ce2.zip |
Merged removal of dependency on Guichan OpenGL from trunk to 0.0 branch,
including optimization of OpenGL memory usage on modern OpenGL drivers.
Patches by Guillaume Melquiond.
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/image.cpp | 66 | ||||
-rw-r--r-- | src/resources/image.h | 9 | ||||
-rw-r--r-- | src/resources/imageloader.cpp | 99 | ||||
-rw-r--r-- | src/resources/imageloader.h | 69 | ||||
-rw-r--r-- | src/resources/iteminfo.cpp | 1 | ||||
-rw-r--r-- | src/resources/openglsdlimageloader.cpp | 39 | ||||
-rw-r--r-- | src/resources/openglsdlimageloader.h | 38 | ||||
-rw-r--r-- | src/resources/sdlimageloader.cpp | 34 | ||||
-rw-r--r-- | src/resources/sdlimageloader.h | 37 |
9 files changed, 221 insertions, 171 deletions
diff --git a/src/resources/image.cpp b/src/resources/image.cpp index ad280eeb..995e34b0 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -29,6 +29,8 @@ #ifdef USE_OPENGL bool Image::mUseOpenGL = false; +int Image::mTextureType = 0; +int Image::mTextureSize = 0; #endif Image::Image(const std::string &idPath, SDL_Surface *image): @@ -89,23 +91,21 @@ Image* Image::load(void *buffer, unsigned int bufferSize, return NULL; } - // Determine 32-bit masks based on byte order - Uint32 rmask, gmask, bmask, amask; -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - rmask = 0xff000000; - gmask = 0x00ff0000; - bmask = 0x0000ff00; - amask = 0x000000ff; -#else - rmask = 0x000000ff; - gmask = 0x0000ff00; - bmask = 0x00ff0000; - amask = 0xff000000; -#endif + Image *image = load(tmpImage, idPath); + + SDL_FreeSurface(tmpImage); + return image; +} + +Image *Image::load(SDL_Surface *tmpImage, std::string const &idPath) +{ #ifdef USE_OPENGL if (mUseOpenGL) { + // Flush current error flag. + glGetError(); + int width = tmpImage->w; int height = tmpImage->h; int realWidth = powerOfTwo(width); @@ -120,6 +120,20 @@ Image* Image::load(void *buffer, unsigned int bufferSize, // Make sure the alpha channel is not used, but copied to destination SDL_SetAlpha(tmpImage, 0, SDL_ALPHA_OPAQUE); + // Determine 32-bit masks based on byte order + Uint32 rmask, gmask, bmask, amask; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + rmask = 0xff000000; + gmask = 0x00ff0000; + bmask = 0x0000ff00; + amask = 0x000000ff; +#else + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; +#endif + SDL_Surface *oldImage = tmpImage; tmpImage = SDL_CreateRGBSurface(SDL_SWSURFACE, realWidth, realHeight, 32, rmask, gmask, bmask, amask); @@ -130,25 +144,24 @@ Image* Image::load(void *buffer, unsigned int bufferSize, } SDL_BlitSurface(oldImage, NULL, tmpImage, NULL); - SDL_FreeSurface(oldImage); GLuint texture; glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); + glBindTexture(mTextureType, texture); if (SDL_MUSTLOCK(tmpImage)) { SDL_LockSurface(tmpImage); } glTexImage2D( - GL_TEXTURE_2D, 0, 4, + mTextureType, 0, 4, tmpImage->w, tmpImage->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpImage->pixels); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(mTextureType, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(mTextureType, GL_TEXTURE_MAG_FILTER, GL_NEAREST); if (SDL_MUSTLOCK(tmpImage)) { SDL_UnlockSurface(tmpImage); @@ -219,7 +232,6 @@ Image* Image::load(void *buffer, unsigned int bufferSize, else { image = SDL_DisplayFormat(tmpImage); } - SDL_FreeSurface(tmpImage); if (!image) { logger->log("Error: Image convert failed."); @@ -289,12 +301,20 @@ Image::setLoadAsOpenGL(bool useOpenGL) int Image::powerOfTwo(int input) { - int value = 1; - while (value < input && value < 1024) + int value; + if (mTextureType == GL_TEXTURE_2D) + { + value = 1; + while (value < input && value < mTextureSize) + { + value <<= 1; + } + } + else { - value <<= 1; + value = input; } - return value; + return value >= mTextureSize ? mTextureSize : value; } #endif diff --git a/src/resources/image.h b/src/resources/image.h index 23715ecb..34515dda 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -24,6 +24,8 @@ #ifndef _TMW_IMAGE_H #define _TMW_IMAGE_H +#include "../main.h" + #include <SDL.h> #ifdef USE_OPENGL @@ -69,6 +71,11 @@ class Image : public Resource load(void* buffer, unsigned int bufferSize, const std::string &idPath); /** + * Loads an image from an SDL surface. + */ + static Image *load(SDL_Surface *, std::string const &idPath); + + /** * Frees the resources created by SDL. */ virtual void @@ -141,6 +148,8 @@ class Image : public Resource int mTexWidth, mTexHeight; static bool mUseOpenGL; + static int mTextureType; + static int mTextureSize; #endif SDL_Surface *mImage; float mAlpha; diff --git a/src/resources/imageloader.cpp b/src/resources/imageloader.cpp new file mode 100644 index 00000000..81a5cf63 --- /dev/null +++ b/src/resources/imageloader.cpp @@ -0,0 +1,99 @@ +/* + * The Mana World + * Copyright 2007 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id: imageloader.cpp 3515 2007-08-25 16:56:52Z gmelquio $ + */ + +#include <cassert> +#include <string> +#include <guichan/color.hpp> +#include <guichan/sdl/sdlpixel.hpp> + +#include "imageloader.h" + +#include "image.h" +#include "resourcemanager.h" + +ProxyImage::ProxyImage(SDL_Surface *s): + mImage(NULL), mSDLImage(s) +{ +} + +ProxyImage::~ProxyImage() +{ + free(); +} + +void ProxyImage::free() +{ + if (mSDLImage) + { + SDL_FreeSurface(mSDLImage); + mSDLImage = NULL; + } + else if (mImage) + { + delete mImage; + mImage = NULL; + } +} + +int ProxyImage::getWidth() const +{ + return mSDLImage ? mSDLImage->w : mImage->getWidth(); +} + +int ProxyImage::getHeight() const +{ + return mSDLImage ? mSDLImage->h : mImage->getHeight(); +} + +gcn::Color ProxyImage::getPixel(int x, int y) +{ + assert(mSDLImage); + return gcn::SDLgetPixel(mSDLImage, x, y); +} + +void ProxyImage::putPixel(int x, int y, gcn::Color const &color) +{ + assert(mSDLImage); + gcn::SDLputPixel(mSDLImage, x, y, color); +} + +void ProxyImage::convertToDisplayFormat() +{ + assert(mSDLImage); + /* The picture is most probably full of the pink pixels Guichan uses for + transparency, as this function will only be called when setting an image + font. Get rid of them. */ + SDL_SetColorKey(mSDLImage, SDL_SRCCOLORKEY, + SDL_MapRGB(mSDLImage->format, 255, 0, 255)); + mImage = ::Image::load(mSDLImage, std::string()); + SDL_FreeSurface(mSDLImage); + mSDLImage = NULL; +} + +gcn::Image *ImageLoader::load(std::string const &filename, bool convert) +{ + ResourceManager *resman = ResourceManager::getInstance(); + ProxyImage *i = new ProxyImage(resman->loadSDLSurface(filename)); + if (convert) i->convertToDisplayFormat(); + return i; +} diff --git a/src/resources/imageloader.h b/src/resources/imageloader.h new file mode 100644 index 00000000..89f38eda --- /dev/null +++ b/src/resources/imageloader.h @@ -0,0 +1,69 @@ +/* + * The Mana World + * Copyright 2007 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id: imageloader.h 3515 2007-08-25 16:56:52Z gmelquio $ + */ + +#ifndef _TMW_IMAGELOADER_H +#define _TMW_IMAGELOADER_H + +#include <string> + +#include <guichan/image.hpp> +#include <guichan/imageloader.hpp> + +class Image; +struct SDL_Surface; + +class ProxyImage : public gcn::Image +{ + public: + ProxyImage(SDL_Surface *); + ~ProxyImage(); + void free(); + int getWidth() const; + int getHeight() const; + gcn::Color getPixel(int x, int y); + void putPixel(int x, int y, gcn::Color const &color); + void convertToDisplayFormat(); + + /** + * Gets the image handled by this proxy. + */ + ::Image *getImage() const + { return mImage; } + + private: + ::Image *mImage; /**< The real image. */ + + /** + * An SDL surface kept around until Guichan is done reading pixels from + * an OpenGL texture. + */ + SDL_Surface *mSDLImage; +}; + +class ImageLoader : public gcn::ImageLoader +{ + public: + gcn::Image *load(std::string const &filename, bool convertToDisplayFormat); +}; + +#endif diff --git a/src/resources/iteminfo.cpp b/src/resources/iteminfo.cpp index c2be5396..b5b25ac0 100644 --- a/src/resources/iteminfo.cpp +++ b/src/resources/iteminfo.cpp @@ -18,6 +18,7 @@ * along with The Mana World; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * + * $Id$ */ #include "iteminfo.h" diff --git a/src/resources/openglsdlimageloader.cpp b/src/resources/openglsdlimageloader.cpp deleted file mode 100644 index 5915fb49..00000000 --- a/src/resources/openglsdlimageloader.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * The Mana World - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id: sdlimageloader.cpp 2121 2006-01-31 02:55:26Z der_doener $ - */ - -#ifdef USE_OPENGL - -#include "openglsdlimageloader.h" - -#include <string> - -#include "resourcemanager.h" - -SDL_Surface* -OpenGLSDLImageLoader::loadSDLSurface(const std::string &filename) -{ - ResourceManager *resman = ResourceManager::getInstance(); - return resman->loadSDLSurface(filename); -} - -#endif diff --git a/src/resources/openglsdlimageloader.h b/src/resources/openglsdlimageloader.h deleted file mode 100644 index b79dde15..00000000 --- a/src/resources/openglsdlimageloader.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * The Mana World - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id: sdlimageloader.h 1724 2005-09-12 22:15:35Z der_doener $ - */ - -#ifndef _TMW_OPENGLSDLIMAGELOADER_H -#define _TMW_OPENGLSDLIMAGELOADER_H - -#include <iosfwd> - -#include <guichan/opengl/openglsdlimageloader.hpp> - -class OpenGLSDLImageLoader : public gcn::OpenGLSDLImageLoader -{ - protected: - SDL_Surface* - loadSDLSurface(const std::string &filename); -}; - -#endif diff --git a/src/resources/sdlimageloader.cpp b/src/resources/sdlimageloader.cpp deleted file mode 100644 index ae4f4dcb..00000000 --- a/src/resources/sdlimageloader.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * The Mana World - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - -#include "sdlimageloader.h" - -#include <string> - -#include "resourcemanager.h" - -SDL_Surface* SDLImageLoader::loadSDLSurface(const std::string& filename) -{ - ResourceManager *resman = ResourceManager::getInstance(); - return resman->loadSDLSurface(filename); -} diff --git a/src/resources/sdlimageloader.h b/src/resources/sdlimageloader.h deleted file mode 100644 index 50b1d4f1..00000000 --- a/src/resources/sdlimageloader.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * The Mana World - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - -#ifndef _TMW_SDLIMAGELOADER_H -#define _TMW_SDLIMAGELOADER_H - -#include <iosfwd> - -#include <guichan/sdl/sdlimageloader.hpp> - -class SDLImageLoader : public gcn::SDLImageLoader -{ - protected: - SDL_Surface* loadSDLSurface(const std::string& filename); -}; - -#endif |