FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
gui_imageloader.cpp
Go to the documentation of this file.
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 {
42  }
43 
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
69  bool prev = rb->isImageCompressingEnabled();
70  rb->setImageCompressingEnabled(false);
71  m_atlases[block->page]->forceLoadInternal();
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 }
virtual ImagePtr create(IResourceLoader *loader=0)
Creates a blank Image but does not load it immediately.
uint32_t page
Definition: atlasbook.h:43
AtlasBlock * getBlock(uint32_t width, uint32_t height)
Definition: atlasbook.cpp:197
Abstract interface for all the renderbackends.
virtual ImagePtr loadBlank(uint32_t width, uint32_t height)
Loads a blank resource.
virtual bool exists(const std::string &name)
Checks to see if an Image exists.
ImageManager.
Definition: imagemanager.h:54
virtual fcn::Image * load(const std::string &filename, bool convertToDisplayFormat=true)
virtual ImagePtr get(const std::string &name)
Gets a shared pointer to the Image.
virtual void remove(ImagePtr &resource)
Removes an Image from the manager.
static const uint32_t ATLAS_SIZE
static ImageManager * instance()
Definition: singleton.h:84
bool isImageCompressingEnabled() const
uint32_t top
Definition: atlasbook.h:44
uint32_t getHeight() const
Definition: atlasbook.h:78
uint32_t left
Definition: atlasbook.h:44
virtual void useSharedImage(const ImagePtr &shared, const Rect &region)=0
After this call all image data will be taken from the given image and its subregion.
std::vector< ImagePtr > m_atlases
unsigned int uint32_t
Definition: core.h:40
void setImageCompressingEnabled(bool enabled)
Enables or disable compressing images by video driver.
virtual ImagePtr load(const std::string &name, IResourceLoader *loader=0)
Creates a blank resource and loads it from disk.
uint32_t getWidth() const
Definition: atlasbook.h:77