FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
imageloader.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // 3rd party library includes
25 #include <boost/scoped_array.hpp>
26 #include <boost/scoped_ptr.hpp>
27 #include <SDL.h>
28 #include <SDL_image.h>
29 
30 // FIFE includes
31 // These includes are split up in two parts, separated by one empty line
32 // First block: files included from the FIFE root src directory
33 // Second block: files included from the same folder
34 #include "controller/engine.h"
35 #include "util/base/exception.h"
36 #include "util/resource/resource.h"
37 #include "vfs/raw/rawdata.h"
38 #include "vfs/vfs.h"
39 #include "video/renderbackend.h"
40 #include "video/image.h"
41 
42 #include "imageloader.h"
43 
44 namespace FIFE {
46  VFS* vfs = VFS::instance();
47 
48  Image* img = dynamic_cast<Image*>(res);
49 
50  //Have to save the images x and y shift or it gets lost when it's
51  //loaded again.
52  int32_t xShiftSave = img->getXShift();
53  int32_t yShiftSave = img->getYShift();
54 
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));
62 
63  SDL_Surface* surface = IMG_Load_RW(rwops, false);
64 
65  if (!surface) {
66  throw SDLException(std::string("Fatal Error when loading image into a SDL_Surface: ") + SDL_GetError());
67  }
68 
70  // in case of SDL we don't need to convert the surface
71  if (rb->getName() == "SDL") {
72  img->setSurface(surface);
73  // in case of OpenGL we need a 32bit surface
74  } else {
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;
79 
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, 0);
84  dst_format.BitsPerPixel = dstbits;
85 
86  if (!conv) {
87  throw SDLException(std::string("Fatal Error when converting surface to the screen format: ") + SDL_GetError());
88  }
89 
90  img->setSurface(conv);
91  SDL_FreeSurface(surface);
92  } else {
93  img->setSurface(surface);
94  }
95  }
96 
97  SDL_FreeRW(rwops);
98  }
99  //restore saved x and y shifts
100  img->setXShift(xShiftSave);
101  img->setYShift(yShiftSave);
102  }
103 } //FIFE
Abstract interface for all the renderbackends.
void setXShift(int32_t xshift)
Definition: image.h:119
Base Class for Images.
Definition: image.h:48
int32_t getXShift() const
Definition: image.h:122
virtual const std::string & getName() const =0
The name of the renderbackend.
RawData * open(const std::string &path)
Open a file.
Definition: vfs.cpp:172
int32_t getYShift() const
Definition: image.h:128
unsigned char uint8_t
Definition: core.h:38
virtual void load(IResource *res)
Definition: imageloader.cpp:45
virtual void setSurface(SDL_Surface *surface)=0
This frees the current suface and replaces it with the surface passed in the parameter (which can be ...
virtual const std::string & getName()
Definition: resource.h:66
bool isSharedImage() const
Returns true if this image shares data with another one.
Definition: image.h:148
the main VFS (virtual file system) class
Definition: vfs.h:58
const SDL_PixelFormat & getPixelFormat() const
Gets the current screen rgba format.
void setYShift(int32_t yshift)
Definition: image.h:125