Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libgui: Added TextureBank, a very simple atlas-based bank for images
Allocates images on an AtlasTexture so they can be efficiently and easily shared by many users for drawing.
- Loading branch information
Showing
4 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "gui/texturebank.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** @file texturebank.h Bank for images stored in a texture atlas. | ||
* | ||
* @authors Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* LGPL: http://www.gnu.org/licenses/lgpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | ||
* General Public License for more details. You should have received a copy of | ||
* the GNU Lesser General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#ifndef LIBGUI_TEXTUREBANK_H | ||
#define LIBGUI_TEXTUREBANK_H | ||
|
||
#include <de/Bank> | ||
#include "../AtlasTexture" | ||
|
||
namespace de { | ||
|
||
/** | ||
* Bank that stores images on a texture atlas for use in GL drawing. | ||
* | ||
* The data item sources in the bank must be derived from TextureBank::ImageSource. | ||
* | ||
* @ingroup gl | ||
*/ | ||
class LIBGUI_PUBLIC TextureBank : public Bank | ||
{ | ||
public: | ||
/** | ||
* Base classs for entries in the bank. When requested, provides the Image data | ||
* of the specified item. | ||
*/ | ||
class LIBGUI_PUBLIC ImageSource : public ISource | ||
{ | ||
public: | ||
ImageSource(DotPath const &id = ""); | ||
DotPath const &id() const; | ||
|
||
virtual Image load() const = 0; | ||
|
||
private: | ||
DENG2_PRIVATE(d) | ||
}; | ||
|
||
public: | ||
TextureBank(); | ||
|
||
/** | ||
* Sets the atlas where the images are to be allocated from. | ||
* | ||
* @param atlas Texture atlas. | ||
*/ | ||
void setAtlas(AtlasTexture &atlas); | ||
|
||
Id const &texture(DotPath const &id); | ||
|
||
protected: | ||
IData *loadFromSource(ISource &source); | ||
|
||
private: | ||
DENG2_PRIVATE(d) | ||
}; | ||
|
||
} // namespace de | ||
|
||
#endif // LIBGUI_TEXTUREBANK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** @file texturebank.cpp | ||
* | ||
* @authors Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* LGPL: http://www.gnu.org/licenses/lgpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | ||
* General Public License for more details. You should have received a copy of | ||
* the GNU Lesser General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#include "de/TextureBank" | ||
|
||
namespace de { | ||
|
||
DENG2_PIMPL_NOREF(TextureBank::ImageSource) | ||
{ | ||
DotPath id; | ||
}; | ||
|
||
TextureBank::ImageSource::ImageSource(DotPath const &id) : d(new Instance) | ||
{ | ||
d->id = id; | ||
} | ||
|
||
DotPath const &TextureBank::ImageSource::id() const | ||
{ | ||
return d->id; | ||
} | ||
|
||
DENG2_PIMPL_NOREF(TextureBank) | ||
{ | ||
struct TextureData : public IData | ||
{ | ||
AtlasTexture *atlas; | ||
Id id; | ||
|
||
TextureData(Image const &image, AtlasTexture *atlasTex) : atlas(atlasTex) | ||
{ | ||
id = atlas->alloc(image); | ||
|
||
/// @todo Reduce size if doesn't fit? Can be expanded when requested for use. | ||
} | ||
|
||
~TextureData() | ||
{ | ||
atlas->release(id); | ||
} | ||
}; | ||
|
||
AtlasTexture *atlas; | ||
|
||
Instance() : atlas(0) {} | ||
}; | ||
|
||
TextureBank::TextureBank() : d(new Instance) | ||
{} | ||
|
||
void TextureBank::setAtlas(AtlasTexture &atlas) | ||
{ | ||
d->atlas = &atlas; | ||
} | ||
|
||
Id const &TextureBank::texture(DotPath const &id) | ||
{ | ||
return data(id).as<Instance::TextureData>().id; | ||
} | ||
|
||
Bank::IData *TextureBank::loadFromSource(ISource &source) | ||
{ | ||
DENG2_ASSERT(d->atlas != 0); | ||
return new Instance::TextureData(source.as<ImageSource>().load(), d->atlas); | ||
} | ||
|
||
} // namespace de |