Skip to content

Commit

Permalink
libgui|TextureBank: Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Oct 17, 2015
1 parent c955c63 commit 539776c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
10 changes: 5 additions & 5 deletions doomsday/sdk/libappfw/src/guirootwidget.cpp
Expand Up @@ -13,7 +13,7 @@
* 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>
* http://www.gnu.org/licenses</small>
*/

#include "de/GuiRootWidget"
Expand Down Expand Up @@ -94,9 +94,9 @@ DENG2_PIMPL(GuiRootWidget)
}
};
struct StyleImage : public TextureBank::ImageSource {
StyleImage(DotPath const &id) : ImageSource(id) {}
StyleImage(DotPath const &sourcePath) : ImageSource(sourcePath) {}
Image load() const {
return Style::get().images().image(id());
return Style::get().images().image(sourcePath());
}
};

Expand Down Expand Up @@ -137,7 +137,7 @@ DENG2_PIMPL(GuiRootWidget)
Atlas::BackingStore | Atlas::AllowDefragment,
GLTexture::maximumSize().min(GLTexture::Size(4096, 4096))));
uTexAtlas = *atlas;
texBank.setAtlas(*atlas);
texBank.setAtlas(atlas.data());

// Load a set of general purpose textures (derived classes may extend this).
self.loadCommonTextures();
Expand Down Expand Up @@ -288,7 +288,7 @@ void GuiRootWidget::handleEventAsFallback(Event const &)
{}

void GuiRootWidget::loadCommonTextures()
{
{
d->initBankContents();
}

Expand Down
19 changes: 15 additions & 4 deletions doomsday/sdk/libgui/include/de/graphics/texturebank.h
Expand Up @@ -41,8 +41,8 @@ class LIBGUI_PUBLIC TextureBank : public Bank
class LIBGUI_PUBLIC ImageSource : public ISource
{
public:
ImageSource(DotPath const &id = "");
DotPath const &id() const;
ImageSource(DotPath const &sourcePath = "");
DotPath const &sourcePath() const;

virtual Image load() const = 0;

Expand All @@ -56,12 +56,23 @@ class LIBGUI_PUBLIC TextureBank : public Bank
/**
* Sets the atlas where the images are to be allocated from.
*
* @param atlas Texture atlas.
* @param atlas Texture atlas. Ownership not taken.
*/
void setAtlas(AtlasTexture &atlas);
void setAtlas(AtlasTexture *atlas);

AtlasTexture *atlas();

Id const &texture(DotPath const &id);

/**
* Returns the source path of an image that has been loaded into the atlas.
*
* @param id Atlas allocation id.
*
* @return ImageSource path.
*/
Path sourcePathForAtlasId(Id const &id) const;

protected:
IData *loadFromSource(ISource &source);

Expand Down
62 changes: 44 additions & 18 deletions doomsday/sdk/libgui/src/graphics/texturebank.cpp
Expand Up @@ -18,65 +18,91 @@

#include "de/TextureBank"

#include <QHash>

namespace de {

DENG2_PIMPL_NOREF(TextureBank::ImageSource)
{
DotPath id;
DotPath sourcePath;
};

TextureBank::ImageSource::ImageSource(DotPath const &id) : d(new Instance)
TextureBank::ImageSource::ImageSource(DotPath const &sourcePath) : d(new Instance)
{
d->id = id;
d->sourcePath = sourcePath;
}

DotPath const &TextureBank::ImageSource::id() const
DotPath const &TextureBank::ImageSource::sourcePath() const
{
return d->id;
return d->sourcePath;
}

DENG2_PIMPL_NOREF(TextureBank)
DENG2_PIMPL(TextureBank)
{
struct TextureData : public IData
{
AtlasTexture *atlas;
Id id;
Instance *d;
Id id { Id::None };

TextureData(Image const &image, AtlasTexture *atlasTex) : atlas(atlasTex)
TextureData(Image const &image, Instance *owner) : d(owner)
{
id = atlas->alloc(image);
id = d->atlas->alloc(image);

/// @todo Reduce size if doesn't fit? Can be expanded when requested for use.
}

~TextureData()
{
atlas->release(id);
d->pathForAtlasId.remove(id);
d->atlas->release(id);
}
};

AtlasTexture *atlas;
AtlasTexture *atlas { nullptr };
QHash<Id::Type, String> pathForAtlasId; // reverse lookup

Instance(Public *i) : Base(i) {}

Instance() : atlas(0) {}
~Instance()
{
// Get rid of items before the reverse lookup hash is destroyed.
self.clear();
}
};

TextureBank::TextureBank() : Bank("TextureBank"), d(new Instance)
TextureBank::TextureBank() : Bank("TextureBank"), d(new Instance(this))
{}

void TextureBank::setAtlas(AtlasTexture &atlas)
void TextureBank::setAtlas(AtlasTexture *atlas)
{
d->atlas = atlas;
}

AtlasTexture *TextureBank::atlas()
{
d->atlas = &atlas;
return d->atlas;
}

Id const &TextureBank::texture(DotPath const &id)
{
return data(id).as<Instance::TextureData>().id;
}

Path TextureBank::sourcePathForAtlasId(Id const &id) const
{
auto found = d->pathForAtlasId.constFind(id);
if(found != d->pathForAtlasId.constEnd())
{
return found.value();
}
return "";
}

Bank::IData *TextureBank::loadFromSource(ISource &source)
{
DENG2_ASSERT(d->atlas != 0);
return new Instance::TextureData(source.as<ImageSource>().load(), d->atlas);
auto *data = new Instance::TextureData(source.as<ImageSource>().load(), d);
d->pathForAtlasId.insert(data->id, source.as<ImageSource>().sourcePath());
return data;
}

} // namespace de

0 comments on commit 539776c

Please sign in to comment.