Debian Codegraph @

engine/core/loaders/native/video/imageloader.cpp

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 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 #include <memory>
24
25 // 3rd party library includes
26 #include <SDL.h>
27 #include <SDL_image.h>
28
29 // FIFE includes
30 // These includes are split up in two parts, separated by one empty line
31 // First block: files included from the FIFE root src directory
32 // Second block: files included from the same folder
33 #include "controller/engine.h"
34 #include "util/base/exception.h"
35 #include "util/resource/resource.h"
36 #include "vfs/raw/rawdata.h"
37 #include "vfs/vfs.h"
38 #include "video/renderbackend.h"
39 #include "video/image.h"
40
41 #include "imageloader.h"
42
43 namespace FIFE {
44 void ImageLoader::load(IResource* res) {
45 VFS* vfs = VFS::instance();
46
47 Image* img = dynamic_cast<Image*>(res);
48
49 //Have to save the images x and y shift or it gets lost when it's
50 //loaded again.
51 int32_t xShiftSave = img->getXShift();
52 int32_t yShiftSave = img->getYShift();
53
54 if(!img->isSharedImage()) {
55 const std::string& filename = img->getName();
56 std::unique_ptr<RawData> data(vfs->open(filename));
57 size_t datalen = data->getDataLength();
58 std::unique_ptr<uint8_t[]> darray(new uint8_t[datalen]);
59 data->readInto(darray.get(), datalen);
60 SDL_RWops* rwops = SDL_RWFromConstMem(darray.get(), static_cast<int>(datalen));
61
62 SDL_Surface* surface = IMG_Load_RW(rwops, false);
63
64 if (!surface) {
65 throw SDLException(std::string("Fatal Error when loading image into a SDL_Surface: ") + SDL_GetError());
66 }
67
68 RenderBackend* rb = RenderBackend::instance();
69 // in case of SDL we don't need to convert the surface
70 if (rb->getName() == "SDL") {
71 img->setSurface(surface);
72 // in case of OpenGL we need a 32bit surface
73 } else {
74 SDL_PixelFormat dst_format = rb->getPixelFormat();
75 SDL_PixelFormat src_format = *surface->format;
76 uint8_t dstbits = dst_format.BitsPerPixel;
77 uint8_t srcbits = src_format.BitsPerPixel;
78
79 if (srcbits != 32 || dst_format.Rmask != src_format.Rmask || dst_format.Gmask != src_format.Gmask ||
80 dst_format.Bmask != src_format.Bmask || dst_format.Amask != src_format.Amask) {
81 dst_format.BitsPerPixel = 32;
82 SDL_Surface* conv = SDL_ConvertSurface(surface, &dst_format, 0);
83 dst_format.BitsPerPixel = dstbits;
84
85 if (!conv) {
86 throw SDLException(std::string("Fatal Error when converting surface to the screen format: ") + SDL_GetError());
87 }
88
89 img->setSurface(conv);
90 SDL_FreeSurface(surface);
91 } else {
92 img->setSurface(surface);
93 }
94 }
95
96 SDL_FreeRW(rwops);
97 }
98 //restore saved x and y shifts
99 img->setXShift(xShiftSave);
100 img->setYShift(yShiftSave);
101 }
102 } //FIFE