00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "resources/imageloader.h"
00023
00024 #include "resources/image.h"
00025 #include "resources/resourcemanager.h"
00026
00027 #include <guichan/color.hpp>
00028 #include <guichan/sdl/sdlpixel.hpp>
00029
00030 #include <cassert>
00031
00032 ProxyImage::ProxyImage(SDL_Surface *s):
00033 mImage(NULL), mSDLImage(s)
00034 {
00035 }
00036
00037 ProxyImage::~ProxyImage()
00038 {
00039 free();
00040 }
00041
00042 void ProxyImage::free()
00043 {
00044 if (mSDLImage)
00045 {
00046 SDL_FreeSurface(mSDLImage);
00047 mSDLImage = NULL;
00048 }
00049 else if (mImage)
00050 {
00051 delete mImage;
00052 mImage = NULL;
00053 }
00054 }
00055
00056 int ProxyImage::getWidth() const
00057 {
00058 return mSDLImage ? mSDLImage->w : mImage->getWidth();
00059 }
00060
00061 int ProxyImage::getHeight() const
00062 {
00063 return mSDLImage ? mSDLImage->h : mImage->getHeight();
00064 }
00065
00066 gcn::Color ProxyImage::getPixel(int x, int y)
00067 {
00068 assert(mSDLImage);
00069 return gcn::SDLgetPixel(mSDLImage, x, y);
00070 }
00071
00072 void ProxyImage::putPixel(int x, int y, gcn::Color const &color)
00073 {
00074 assert(mSDLImage);
00075 gcn::SDLputPixel(mSDLImage, x, y, color);
00076 }
00077
00078 void ProxyImage::convertToDisplayFormat()
00079 {
00080 assert(mSDLImage);
00081
00082
00083
00084 SDL_SetColorKey(mSDLImage, SDL_SRCCOLORKEY,
00085 SDL_MapRGB(mSDLImage->format, 255, 0, 255));
00086 mImage = ::Image::load(mSDLImage);
00087 SDL_FreeSurface(mSDLImage);
00088 mSDLImage = NULL;
00089 }
00090
00091 gcn::Image *ImageLoader::load(const std::string &filename, bool convert)
00092 {
00093 ResourceManager *resman = ResourceManager::getInstance();
00094 ProxyImage *i = new ProxyImage(resman->loadSDLSurface(filename));
00095 if (convert) i->convertToDisplayFormat();
00096 return i;
00097 }