25 #include <boost/scoped_array.hpp>
26 #include <boost/scoped_ptr.hpp>
28 #include <SDL_image.h>
34 #include "controller/engine.h"
35 #include "util/base/exception.h"
36 #include "util/resource/resource.h"
37 #include "vfs/raw/rawdata.h"
39 #include "video/renderbackend.h"
40 #include "video/image.h"
42 #include "imageloader.h"
45 void ImageLoader::load(IResource* res) {
46 VFS* vfs = VFS::instance();
48 Image* img =
dynamic_cast<Image*
>(res);
52 int32_t xShiftSave = img->getXShift();
53 int32_t yShiftSave = img->getYShift();
55 if(!img->isSharedImage()) {
56 const std::string& filename = img->getName();
57 boost::scoped_ptr<RawData> data (vfs->open(filename));
58 size_t datalen = data->getDataLength();
59 boost::scoped_array<uint8_t> darray(
new uint8_t[datalen]);
60 data->readInto(darray.get(), datalen);
61 SDL_RWops* rwops = SDL_RWFromConstMem(darray.get(),
static_cast<int>(datalen));
63 SDL_Surface* surface = IMG_Load_RW(rwops,
false);
66 throw SDLException(std::string(
"Fatal Error when loading image into a SDL_Surface: ") + SDL_GetError());
69 RenderBackend* rb = RenderBackend::instance();
71 if (rb->getName() ==
"SDL") {
72 img->setSurface(surface);
75 SDL_PixelFormat dst_format = rb->getPixelFormat();
76 SDL_PixelFormat src_format = *surface->format;
77 uint8_t dstbits = dst_format.BitsPerPixel;
78 uint8_t srcbits = src_format.BitsPerPixel;
80 if (srcbits != 32 || dst_format.Rmask != src_format.Rmask || dst_format.Gmask != src_format.Gmask ||
81 dst_format.Bmask != src_format.Bmask || dst_format.Amask != src_format.Amask) {
82 dst_format.BitsPerPixel = 32;
83 SDL_Surface* conv = SDL_ConvertSurface(surface, &dst_format, SDL_SWSURFACE | SDL_SRCALPHA);
84 dst_format.BitsPerPixel = dstbits;
87 throw SDLException(std::string(
"Fatal Error when converting surface to the screen format: ") + SDL_GetError());
90 img->setSurface(conv);
91 SDL_FreeSurface(surface);
93 img->setSurface(surface);
100 img->setXShift(xShiftSave);
101 img->setYShift(yShiftSave);