gui_imageloader.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
00030 #include "video/image.h"
00031 #include "video/imagemanager.h"
00032 #include "video/atlasbook.h"
00033 #include "video/renderbackend.h"
00034 
00035 #include "gui_imageloader.h"
00036 
00037 static const uint32_t ATLAS_SIZE = 512;
00038 
00039 namespace FIFE {
00040     GuiImageLoader::GuiImageLoader() {
00041         m_atlasbook = new AtlasBook(ATLAS_SIZE, ATLAS_SIZE);
00042     }
00043 
00044     GuiImageLoader::~GuiImageLoader() {
00045         delete m_atlasbook;
00046     }
00047 
00048     gcn::Image* GuiImageLoader::load(const std::string& filename, bool convertToDisplayFormat) {
00049         ImageManager* imgManager = ImageManager::instance();
00050 
00051         if(imgManager->exists(filename)) {
00052             return new GuiImage(imgManager->get(filename));
00053         }
00054         // load demanded image
00055         ImagePtr tmpimg = imgManager->load(filename);
00056         if(tmpimg->getWidth() >= ATLAS_SIZE || tmpimg->getHeight() >= ATLAS_SIZE) {
00057             return new GuiImage(tmpimg);
00058         }
00059         // look for a place for an image of given size
00060         AtlasBlock* block = m_atlasbook->getBlock(tmpimg->getWidth(), tmpimg->getHeight());
00061 
00062         // if it can't fit, we need to add new 'page'
00063         if(block->page >= m_atlases.size()) {
00064             m_atlases.push_back(imgManager->loadBlank(ATLAS_SIZE, ATLAS_SIZE));
00065 
00066             // because we gonna update texture on-the fly (via TexSubImage)
00067             // we cant really use compressed texture 
00068             RenderBackend* rb = RenderBackend::instance();
00069             bool prev = rb->isImageCompressingEnabled();
00070             rb->setImageCompressingEnabled(false);
00071             m_atlases[block->page]->forceLoadInternal();
00072             rb->setImageCompressingEnabled(prev);
00073         }
00074         
00075         // update atlas page with given image
00076         m_atlases[block->page]->copySubimage(block->left, block->top, tmpimg);
00077     
00078         // we dont really need this image anymore 
00079         tmpimg->free();
00080         imgManager->remove(tmpimg);
00081 
00082         // create shared image and return it
00083         ImagePtr img = imgManager->create(filename);
00084         Rect region(block->left, block->top, block->getWidth(), block->getHeight());
00085         img->useSharedImage(m_atlases[block->page], region);
00086 
00087         return new GuiImage(img);
00088     }
00089 }