Skip to content

Commit

Permalink
libgui: Added TextureBank, a very simple atlas-based bank for images
Browse files Browse the repository at this point in the history
Allocates images on an AtlasTexture so they can be efficiently and
easily shared by many users for drawing.
  • Loading branch information
skyjake committed Mar 17, 2014
1 parent aeef104 commit e31b64c
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 1 deletion.
1 change: 1 addition & 0 deletions doomsday/libgui/include/de/TextureBank
@@ -0,0 +1 @@
#include "gui/texturebank.h"
74 changes: 74 additions & 0 deletions doomsday/libgui/include/de/gui/texturebank.h
@@ -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
5 changes: 4 additions & 1 deletion doomsday/libgui/libgui.pro
Expand Up @@ -77,6 +77,7 @@ publicHeaders(root, \
include/de/NativeFont \
include/de/PersistentCanvasWindow \
include/de/RowAtlasAllocator \
include/de/TextureBank \
include/de/VertexBuilder \
)

Expand Down Expand Up @@ -117,6 +118,7 @@ publicHeaders(gui, \
include/de/gui/opengl.h \
include/de/gui/persistentcanvaswindow.h \
include/de/gui/rowatlasallocator.h \
include/de/gui/texturebank.h \
include/de/gui/vertexbuilder.h \
)

Expand Down Expand Up @@ -154,10 +156,11 @@ SOURCES += \
src/mouseevent.cpp \
src/mouseeventsource.cpp \
src/nativefont.cpp \
src/qtnativefont.cpp \
src/qtnativefont.h \
src/persistentcanvaswindow.cpp \
src/rowatlasallocator.cpp \
src/qtnativefont.cpp
src/texturebank.cpp

macx:!deng_macx6_32bit_64bit: SOURCES += \
src/coretextnativefont_macx.h \
Expand Down
82 changes: 82 additions & 0 deletions doomsday/libgui/src/texturebank.cpp
@@ -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

0 comments on commit e31b64c

Please sign in to comment.