diff --git a/doomsday/libgui/include/de/ImageBank b/doomsday/libgui/include/de/ImageBank new file mode 100644 index 0000000000..f9a30cccab --- /dev/null +++ b/doomsday/libgui/include/de/ImageBank @@ -0,0 +1 @@ +#include "gui/imagebank.h" diff --git a/doomsday/libgui/include/de/gui/imagebank.h b/doomsday/libgui/include/de/gui/imagebank.h new file mode 100644 index 0000000000..09b812cc70 --- /dev/null +++ b/doomsday/libgui/include/de/gui/imagebank.h @@ -0,0 +1,62 @@ +/** @file imagebank.h Bank containing Image instances loaded from files. + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 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 General + * Public License for more details. You should have received a copy of the GNU + * General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#ifndef LIBGUI_IMAGEBANK_H +#define LIBGUI_IMAGEBANK_H + +#include "libgui.h" +#include "../Image" + +#include + +namespace de { + +/** + * Bank containing Image instances loaded from files. + * + * @ingroup data + */ +class LIBGUI_PUBLIC ImageBank : public Bank +{ +public: + /** + * Constructs a new image bank. + * + * @param flags Properties for the bank. By default, the bank uses a + * background thread because large images may take some + * time to load. Hot storage is disabled because loading + * images from their potentially encoded source data is + * faster/more efficient than deserializating raw pixel data. + */ + ImageBank(Flags const &flags = BackgroundThread | DisableHotStorage); + + void add(Path const &path, String const &imageFilePath); + + Image &image(Path const &path) const; + +protected: + IData *loadFromSource(ISource &source); + IData *newData(); + +private: + DENG2_PRIVATE(d) +}; + +} // namespace de + +#endif // LIBGUI_IMAGEBANK_H diff --git a/doomsday/libgui/libgui.pro b/doomsday/libgui/libgui.pro index 3fc98dfb2c..584ef921d8 100644 --- a/doomsday/libgui/libgui.pro +++ b/doomsday/libgui/libgui.pro @@ -61,6 +61,7 @@ HEADERS += \ include/de/GLUniform \ include/de/GuiApp \ include/de/Image \ + include/de/ImageBank \ include/de/KeyEvent \ include/de/KeyEventSource \ include/de/MouseEventSource \ @@ -85,6 +86,7 @@ HEADERS += \ include/de/gui/gluniform.h \ include/de/gui/guiapp.h \ include/de/gui/image.h \ + include/de/gui/imagebank.h \ include/de/gui/keyevent.h \ include/de/gui/keyeventsource.h \ include/de/gui/libgui.h \ @@ -111,6 +113,7 @@ SOURCES += \ src/gluniform.cpp \ src/guiapp.cpp \ src/image.cpp \ + src/imagebank.cpp \ src/keyevent.cpp \ src/persistentcanvaswindow.cpp \ src/rowatlasallocator.cpp diff --git a/doomsday/libgui/src/imagebank.cpp b/doomsday/libgui/src/imagebank.cpp new file mode 100644 index 0000000000..e57aeb3236 --- /dev/null +++ b/doomsday/libgui/src/imagebank.cpp @@ -0,0 +1,84 @@ +/** @file imagebank.h Bank containing Image instances loaded from files. + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 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 General + * Public License for more details. You should have received a copy of the GNU + * General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#include "de/ImageBank" +#include "de/App" + +namespace de { + +DENG2_PIMPL_NOREF(ImageBank) +{ + struct ImageSource : public ISource + { + String filePath; + + ImageSource(String const &path) : filePath(path) + {} + + Time modifiedAt() const + { + return App::rootFolder().locate(filePath).status().modifiedAt; + } + + Image load() const + { + Block imageData; + App::rootFolder().locate(filePath) >> imageData; + return QImage::fromData(imageData); + } + }; + + struct ImageData : public IData + { + Image image; + + ImageData() {} + ImageData(Image const &img) : image(img) {} + + duint sizeInMemory() const + { + return image.byteCount(); + } + }; +}; + +ImageBank::ImageBank(Flags const &flags) + : Bank(flags), d(new Instance) +{} + +void ImageBank::add(Path const &path, String const &imageFilePath) +{ + Bank::add(path, new Instance::ImageSource(imageFilePath)); +} + +Image &ImageBank::image(Path const &path) const +{ + return static_cast(data(path)).image; +} + +Bank::IData *ImageBank::loadFromSource(ISource &source) +{ + return new Instance::ImageData(static_cast(source).load()); +} + +Bank::IData *ImageBank::newData() +{ + return new Instance::ImageData(); +} + +} // namespace de