| 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 |
|
| 24 |
// 3rd party library includes |
| 25 |
|
| 26 |
// FIFE includes |
| 27 |
// These includes are split up in two parts, separated by one empty line |
| 28 |
// First block: files included from the FIFE root src directory |
| 29 |
// Second block: files included from the same folder |
| 30 |
#include "video/image.h" |
| 31 |
#include "video/imagemanager.h" |
| 32 |
#include "video/atlasbook.h" |
| 33 |
#include "video/renderbackend.h" |
| 34 |
|
| 35 |
#include "gui_imageloader.h" |
| 36 |
|
| 37 |
static const uint32_t ATLAS_SIZE = 512; |
| 38 |
|
| 39 |
namespace FIFE { |
| 40 |
GuiImageLoader::GuiImageLoader() { |
| 41 |
m_atlasbook = new AtlasBook(ATLAS_SIZE, ATLAS_SIZE); |
| 42 |
} |
| 43 |
|
| 44 |
GuiImageLoader::~GuiImageLoader() { |
| 45 |
delete m_atlasbook; |
| 46 |
} |
| 47 |
|
| 48 |
fcn::Image* GuiImageLoader::load(const std::string& filename, bool convertToDisplayFormat) { |
| 49 |
ImageManager* imgManager = ImageManager::instance(); |
| 50 |
|
| 51 |
if(imgManager->exists(filename)) { |
| 52 |
return new GuiImage(imgManager->get(filename)); |
| 53 |
} |
| 54 |
// load demanded image |
| 55 |
ImagePtr tmpimg = imgManager->load(filename); |
| 56 |
if(tmpimg->getWidth() >= ATLAS_SIZE || tmpimg->getHeight() >= ATLAS_SIZE) { |
| 57 |
return new GuiImage(tmpimg); |
| 58 |
} |
| 59 |
// look for a place for an image of given size |
| 60 |
AtlasBlock* block = m_atlasbook->getBlock(tmpimg->getWidth(), tmpimg->getHeight()); |
| 61 |
|
| 62 |
// if it can't fit, we need to add new 'page' |
| 63 |
if(block->page >= m_atlases.size()) { |
| 64 |
m_atlases.push_back(imgManager->loadBlank(ATLAS_SIZE, ATLAS_SIZE)); |
| 65 |
|
| 66 |
// because we gonna update texture on-the fly (via TexSubImage) |
| 67 |
// we cant really use compressed texture |
| 68 |
RenderBackend* rb = RenderBackend::instance(); |
| 69 |
bool prev = rb->isImageCompressingEnabled(); |
| 70 |
rb->setImageCompressingEnabled(false); |
| 71 |
m_atlases[block->page]->forceLoadInternal(); |
| 72 |
rb->setImageCompressingEnabled(prev); |
| 73 |
} |
| 74 |
|
| 75 |
// update atlas page with given image |
| 76 |
m_atlases[block->page]->copySubimage(block->left, block->top, tmpimg); |
| 77 |
|
| 78 |
// we dont really need this image anymore |
| 79 |
tmpimg->free(); |
| 80 |
imgManager->remove(tmpimg); |
| 81 |
|
| 82 |
// create shared image and return it |
| 83 |
ImagePtr img = imgManager->create(filename); |
| 84 |
Rect region(block->left, block->top, block->getWidth(), block->getHeight()); |
| 85 |
img->useSharedImage(m_atlases[block->page], region); |
| 86 |
|
| 87 |
return new GuiImage(img); |
| 88 |
} |
| 89 |
} |