From e849f0ef2fc8c5cdc1f4cbe6e28dad95da085cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Sat, 9 Jul 2016 15:24:42 +0300 Subject: [PATCH] Cleanup|Resources: Moved color palettes to a new source file in libdoomsday --- .../client/include/resource/resourcesystem.h | 57 +------- doomsday/apps/client/src/gl/gl_main.cpp | 5 +- .../apps/client/src/gl/texturecontent.cpp | 3 +- .../apps/client/src/resource/api_resource.cpp | 18 +-- doomsday/apps/client/src/resource/image.cpp | 9 +- .../client/src/resource/resourcesystem.cpp | 109 ++------------ .../client/src/resource/texturevariant.cpp | 9 +- .../include/doomsday/resource/colorpalette.h | 2 + .../include/doomsday/resource/colorpalettes.h | 90 ++++++++++++ .../include/doomsday/resource/resources.h | 12 +- .../src/resource/colorpalettes.cpp | 135 ++++++++++++++++++ .../libdoomsday/src/resource/resources.cpp | 16 ++- 12 files changed, 291 insertions(+), 174 deletions(-) create mode 100644 doomsday/apps/libdoomsday/include/doomsday/resource/colorpalettes.h create mode 100644 doomsday/apps/libdoomsday/src/resource/colorpalettes.cpp diff --git a/doomsday/apps/client/include/resource/resourcesystem.h b/doomsday/apps/client/include/resource/resourcesystem.h index ec356d4bde..e2c4e23edd 100644 --- a/doomsday/apps/client/include/resource/resourcesystem.h +++ b/doomsday/apps/client/include/resource/resourcesystem.h @@ -90,9 +90,6 @@ class ResourceSystem : public Resources /// An unknown resource scheme was referenced. @ingroup errors DENG2_ERROR(UnknownSchemeError); - /// The referenced resource was not found. @ingroup errors - DENG2_ERROR(MissingResourceError); - /// An unknown material group was referenced. @ingroup errors DENG2_ERROR(UnknownMaterialGroupError); @@ -134,6 +131,8 @@ class ResourceSystem : public Resources void clearAllRuntimeResources(); void clearAllSystemResources(); + void addColorPalette(res::ColorPalette &newPalette, de::String const &name); + /** * Returns @c true if a Sprite exists with given unique @a id and @a frame number. */ @@ -739,58 +738,6 @@ class ResourceSystem : public Resources de::AnimGroup *animGroupForTexture(de::TextureManifest const &textureManifest); - /** - * Returns the total number of color palettes. - */ - de::dint colorPaletteCount() const; - - /** - * Destroys all the color palettes. - */ - void clearAllColorPalettes(); - - /** - * Returns the ColorPalette associated with unique @a id. - */ - res::ColorPalette &colorPalette(colorpaletteid_t id) const; - - /** - * Returns the symbolic name of the specified color @a palette. A zero-length - * string is returned if no name is associated. - */ - de::String colorPaletteName(res::ColorPalette &palette) const; - - /** - * Returns @c true iff a ColorPalette with the specified @a name is present. - */ - bool hasColorPalette(de::String name) const; - - /** - * Returns the ColorPalette associated with @a name. - * - * @see hasColorPalette() - */ - res::ColorPalette &colorPalette(de::String name) const; - - /** - * @param newPalette Color palette to add. Ownership of the palette is given - * to the resource system. - * @param name Symbolic name of the color palette. - */ - void addColorPalette(res::ColorPalette &newPalette, de::String const &name = de::String()); - - /** - * Returns the unique identifier of the current default color palette. - */ - colorpaletteid_t defaultColorPalette() const; - - /** - * Change the default color palette. - * - * @param newDefaultPalette The color palette to make default. - */ - void setDefaultColorPalette(res::ColorPalette *newDefaultPalette); - #ifdef __CLIENT__ /** diff --git a/doomsday/apps/client/src/gl/gl_main.cpp b/doomsday/apps/client/src/gl/gl_main.cpp index 0defb00478..39eae5faa7 100644 --- a/doomsday/apps/client/src/gl/gl_main.cpp +++ b/doomsday/apps/client/src/gl/gl_main.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include "clientapp.h" #include "sys_system.h" // novideo @@ -1118,7 +1119,7 @@ duint8 *GL_ConvertBuffer(duint8 const *in, dint width, dint height, dint informa exit(1); // Unreachable. } - res::ColorPalette *palette = (informat <= 2? &resSys().colorPalette(paletteId) : nullptr); + res::ColorPalette *palette = (informat <= 2? &resSys().colorPalettes().colorPalette(paletteId) : nullptr); auto *out = (duint8 *) M_Malloc(outformat * width * height); @@ -1163,7 +1164,7 @@ void GL_CalcLuminance(duint8 const *buffer, dint width, dint height, dint pixelS static duint8 const sizeLimit = 192, brightLimit = 224, colLimit = 192; - res::ColorPalette *palette = (pixelSize == 1? &resSys().colorPalette(paletteId) : nullptr); + res::ColorPalette *palette = (pixelSize == 1? &resSys().colorPalettes().colorPalette(paletteId) : nullptr); // Apply the defaults. // Default to the center of the texture. diff --git a/doomsday/apps/client/src/gl/texturecontent.cpp b/doomsday/apps/client/src/gl/texturecontent.cpp index 0427735a2f..5f0fe48231 100644 --- a/doomsday/apps/client/src/gl/texturecontent.cpp +++ b/doomsday/apps/client/src/gl/texturecontent.cpp @@ -22,6 +22,7 @@ #include "gl/texturecontent.h" #include +#include #include #include #include @@ -182,7 +183,7 @@ static dgltexformat_t prepareImageAsTexture(image_t &image, if(monochrome && !scaleSharp) { GL_DeSaturatePalettedImage(image.pixels, - App_ResourceSystem().colorPalette(image.paletteId), + App_ResourceSystem().colorPalettes().colorPalette(image.paletteId), image.size.x, image.size.y); } diff --git a/doomsday/apps/client/src/resource/api_resource.cpp b/doomsday/apps/client/src/resource/api_resource.cpp index 26f3203794..a7da47c64d 100644 --- a/doomsday/apps/client/src/resource/api_resource.cpp +++ b/doomsday/apps/client/src/resource/api_resource.cpp @@ -27,6 +27,8 @@ # include "render/r_main.h" // texGammaLut #endif +#include + using namespace de; #undef Textures_UniqueId2 @@ -111,9 +113,9 @@ DENG_EXTERN_C colorpaletteid_t R_CreateColorPalette(char const *colorFormatDescr res::ColorTableReader::read(colorFormatDescriptor, colorCount, colorData); // Replacing an existing palette? - if(App_ResourceSystem().hasColorPalette(name)) + if(App_ResourceSystem().colorPalettes().hasColorPalette(name)) { - res::ColorPalette &palette = App_ResourceSystem().colorPalette(name); + res::ColorPalette &palette = App_ResourceSystem().colorPalettes().colorPalette(name); palette.replaceColorTable(colors); return palette.id(); } @@ -142,7 +144,7 @@ DENG_EXTERN_C void R_CreateColorPaletteTranslation(colorpaletteid_t paletteId, try { - res::ColorPalette &palette = App_ResourceSystem().colorPalette(paletteId); + res::ColorPalette &palette = App_ResourceSystem().colorPalettes().colorPalette(paletteId); // Convert the mapping table. int const colorCount = palette.colorCount(); @@ -177,7 +179,7 @@ DENG_EXTERN_C colorpaletteid_t R_GetColorPaletteNumForName(char const *name) LOG_AS("R_GetColorPaletteNumForName"); try { - return App_ResourceSystem().colorPalette(name).id(); + return App_ResourceSystem().colorPalettes().colorPalette(name).id(); } catch(ResourceSystem::MissingResourceError const &er) { @@ -193,8 +195,8 @@ DENG_EXTERN_C char const *R_GetColorPaletteNameForNum(colorpaletteid_t id) LOG_AS("R_GetColorPaletteNameForNum"); try { - res::ColorPalette &palette = App_ResourceSystem().colorPalette(id); - return App_ResourceSystem().colorPaletteName(palette).toUtf8().constData(); + res::ColorPalette &palette = App_ResourceSystem().colorPalettes().colorPalette(id); + return App_ResourceSystem().colorPalettes().colorPaletteName(palette).toUtf8().constData(); } catch(ResourceSystem::MissingResourceError const &er) { @@ -221,7 +223,7 @@ DENG_EXTERN_C void R_GetColorPaletteRGBubv(colorpaletteid_t paletteId, int color try { - Vector3ub palColor = App_ResourceSystem().colorPalette(paletteId)[colorIdx]; + Vector3ub palColor = App_ResourceSystem().colorPalettes().colorPalette(paletteId)[colorIdx]; rgb[0] = palColor.x; rgb[1] = palColor.y; rgb[2] = palColor.z; @@ -256,7 +258,7 @@ DENG_EXTERN_C void R_GetColorPaletteRGBf(colorpaletteid_t paletteId, int colorId try { - res::ColorPalette &palette = App_ResourceSystem().colorPalette(paletteId); + res::ColorPalette &palette = App_ResourceSystem().colorPalettes().colorPalette(paletteId); if(applyTexGamma) { Vector3ub palColor = palette[colorIdx]; diff --git a/doomsday/apps/client/src/resource/image.cpp b/doomsday/apps/client/src/resource/image.cpp index 8cc7d009d4..bcadf4535e 100644 --- a/doomsday/apps/client/src/resource/image.cpp +++ b/doomsday/apps/client/src/resource/image.cpp @@ -22,6 +22,7 @@ #include "resource/image.h" #include +#include #include #include @@ -580,7 +581,7 @@ static String toTranslationId(int tclass, int tmap) static Block loadAndTranslatePatch(IByteArray const &data, colorpaletteid_t palId, int tclass = 0, int tmap = 0) { - res::ColorPalette &palette = App_ResourceSystem().colorPalette(palId); + res::ColorPalette &palette = App_ResourceSystem().colorPalettes().colorPalette(palId); if(res::ColorPaletteTranslation const *xlat = palette.translation(toTranslationId(tclass, tmap))) { return res::Patch::load(data, *xlat, res::Patch::ClipToLogicalDimensions); @@ -609,7 +610,7 @@ static Source loadPatch(image_t &image, FileHandle &hndl, int tclass = 0, { try { - colorpaletteid_t colorPaletteId = App_ResourceSystem().defaultColorPalette(); + colorpaletteid_t colorPaletteId = App_ResourceSystem().colorPalettes().defaultColorPalette(); Block patchImg = loadAndTranslatePatch(fileData, colorPaletteId, tclass, tmap); PatchMetadata info = Patch::loadMetadata(fileData); @@ -652,7 +653,7 @@ static Source loadPatchComposite(image_t &image, Texture const &tex, Image_Init(image); image.pixelSize = 1; image.size = Vector2ui(tex.width(), tex.height()); - image.paletteId = App_ResourceSystem().defaultColorPalette(); + image.paletteId = App_ResourceSystem().colorPalettes().defaultColorPalette(); image.pixels = (uint8_t *) M_Calloc(2 * image.size.x * image.size.y); @@ -714,7 +715,7 @@ static Source loadFlat(image_t &image, FileHandle &hndl) /// @todo not all flats are 64x64! image.size = Vector2ui(64, 64); image.pixelSize = 1; - image.paletteId = App_ResourceSystem().defaultColorPalette(); + image.paletteId = App_ResourceSystem().colorPalettes().defaultColorPalette(); File1 &file = hndl.file(); size_t fileLength = hndl.length(); diff --git a/doomsday/apps/client/src/resource/resourcesystem.cpp b/doomsday/apps/client/src/resource/resourcesystem.cpp index e60766e4bb..c9451c4406 100644 --- a/doomsday/apps/client/src/resource/resourcesystem.cpp +++ b/doomsday/apps/client/src/resource/resourcesystem.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include @@ -442,14 +443,6 @@ DENG2_PIMPL(ResourceSystem) typedef QList AnimGroups; AnimGroups animGroups; - typedef QMap ColorPalettes; - ColorPalettes colorPalettes; - - typedef QMap ColorPaletteNames; - ColorPaletteNames colorPaletteNames; - - colorpaletteid_t defaultColorPalette; - /// System subspace schemes containing the textures. TextureSchemes textureSchemes; QList textureSchemeCreationOrder; @@ -538,7 +531,6 @@ DENG2_PIMPL(ResourceSystem) Impl(Public *i) : Base(i) - , defaultColorPalette (0) , materialManifestIdMapSize(0) , materialManifestIdMap (0) #ifdef __CLIENT__ @@ -633,7 +625,6 @@ DENG2_PIMPL(ResourceSystem) #ifdef __CLIENT__ clearModels(); #endif - self.clearAllColorPalettes(); } inline de::FS1 &fileSys() @@ -2266,7 +2257,6 @@ void ResourceSystem::clear() #endif clearAllRuntimeResources(); clearAllAnimGroups(); - clearAllColorPalettes(); } void ResourceSystem::clearAllResources() @@ -2291,6 +2281,16 @@ void ResourceSystem::clearAllSystemResources() d->clearSystemTextures(); } +void ResourceSystem::addColorPalette(res::ColorPalette &newPalette, String const &name) +{ + colorPalettes().addColorPalette(newPalette, name); + +#ifdef __CLIENT__ + // Observe changes to the color table so we can schedule texture updates. + newPalette.audienceForColorTableChange += d; +#endif +} + dint ResourceSystem::spriteCount() { return d->sprites.count(); @@ -3593,93 +3593,6 @@ void ResourceSystem::initSprites() LOG_RES_VERBOSE("Sprites built in %.2f seconds") << begunAt.since(); } -void ResourceSystem::clearAllColorPalettes() -{ - d->colorPaletteNames.clear(); - - qDeleteAll(d->colorPalettes); - d->colorPalettes.clear(); - - d->defaultColorPalette = 0; -} - -dint ResourceSystem::colorPaletteCount() const -{ - return d->colorPalettes.count(); -} - -res::ColorPalette &ResourceSystem::colorPalette(colorpaletteid_t id) const -{ - // Choose the default palette? - if(!id) - { - id = d->defaultColorPalette; - } - - auto found = d->colorPalettes.find(id); - if(found != d->colorPalettes.end()) return *found.value(); - /// @throw MissingResourceError An unknown/invalid id was specified. - throw MissingResourceError("ResourceSystem::colorPalette", "Invalid id " + String::number(id)); -} - -String ResourceSystem::colorPaletteName(res::ColorPalette &palette) const -{ - QList const names = d->colorPaletteNames.keys(&palette); - if(!names.isEmpty()) - { - return names.first(); - } - return String(); -} - -bool ResourceSystem::hasColorPalette(String name) const -{ - return d->colorPaletteNames.contains(name); -} - -res::ColorPalette &ResourceSystem::colorPalette(String name) const -{ - auto found = d->colorPaletteNames.find(name); - if(found != d->colorPaletteNames.end()) return *found.value(); - /// @throw MissingResourceError An unknown name was specified. - throw MissingResourceError("ResourceSystem::colorPalette", "Unknown name '" + name + "'"); -} - -void ResourceSystem::addColorPalette(res::ColorPalette &newPalette, String const &name) -{ - // Do we already own this palette? - if(d->colorPalettes.contains(newPalette.id())) - return; - - d->colorPalettes.insert(newPalette.id(), &newPalette); - -#ifdef __CLIENT__ - // Observe changes to the color table so we can schedule texture updates. - newPalette.audienceForColorTableChange += d; -#endif - - if(!name.isEmpty()) - { - d->colorPaletteNames.insert(name, &newPalette); - } - - // If this is the first palette automatically set it as the default. - if(d->colorPalettes.count() == 1) - { - d->defaultColorPalette = newPalette.id(); - } -} - -colorpaletteid_t ResourceSystem::defaultColorPalette() const -{ - return d->defaultColorPalette; -} - -void ResourceSystem::setDefaultColorPalette(res::ColorPalette *newDefaultPalette) -{ - d->defaultColorPalette = newDefaultPalette ? newDefaultPalette->id().asUInt32() : 0; -} - #ifdef __CLIENT__ void ResourceSystem::purgeCacheQueue() diff --git a/doomsday/apps/client/src/resource/texturevariant.cpp b/doomsday/apps/client/src/resource/texturevariant.cpp index f87041bac9..f469261eb8 100644 --- a/doomsday/apps/client/src/resource/texturevariant.cpp +++ b/doomsday/apps/client/src/resource/texturevariant.cpp @@ -32,6 +32,7 @@ #include "render/rend_main.h" // misc global vars awaiting new home +#include #include #include // M_CeilPow @@ -403,7 +404,7 @@ static void performImageAnalyses(image_t const &image, else { FindAverageColorIdx(image.pixels, image.size.x, image.size.y, - App_ResourceSystem().colorPalette(image.paletteId), + App_ResourceSystem().colorPalettes().colorPalette(image.paletteId), false, &ac->color); } } @@ -431,7 +432,7 @@ static void performImageAnalyses(image_t const &image, else { FindAverageColorIdx(image.pixels, image.size.x, image.size.y, - App_ResourceSystem().colorPalette(image.paletteId), + App_ResourceSystem().colorPalettes().colorPalette(image.paletteId), false, &ac->color); } Vector3f color(ac->color.rgb); @@ -465,7 +466,7 @@ static void performImageAnalyses(image_t const &image, else { FindAverageLineColorIdx(image.pixels, image.size.x, image.size.y, 0, - App_ResourceSystem().colorPalette(image.paletteId), + App_ResourceSystem().colorPalettes().colorPalette(image.paletteId), false, &ac->color); } } @@ -494,7 +495,7 @@ static void performImageAnalyses(image_t const &image, { FindAverageLineColorIdx(image.pixels, image.size.x, image.size.y, image.size.y - 1, - App_ResourceSystem().colorPalette(image.paletteId), + App_ResourceSystem().colorPalettes().colorPalette(image.paletteId), false, &ac->color); } } diff --git a/doomsday/apps/libdoomsday/include/doomsday/resource/colorpalette.h b/doomsday/apps/libdoomsday/include/doomsday/resource/colorpalette.h index 7cb82409e6..3b0ba2e806 100644 --- a/doomsday/apps/libdoomsday/include/doomsday/resource/colorpalette.h +++ b/doomsday/apps/libdoomsday/include/doomsday/resource/colorpalette.h @@ -71,6 +71,8 @@ class LIBDOOMSDAY_PUBLIC ColorPalette /// Notified whenever the color table changes. DENG2_DEFINE_AUDIENCE(ColorTableChange, void colorPaletteColorTableChanged(ColorPalette &colorPalette)) + typedef de::duint Id; + /// Palette index translation mapping table. typedef QVector Translation; diff --git a/doomsday/apps/libdoomsday/include/doomsday/resource/colorpalettes.h b/doomsday/apps/libdoomsday/include/doomsday/resource/colorpalettes.h new file mode 100644 index 0000000000..71b092b5ad --- /dev/null +++ b/doomsday/apps/libdoomsday/include/doomsday/resource/colorpalettes.h @@ -0,0 +1,90 @@ +/** @file colorpalettes.h + * + * @authors Copyright © 2013-2015 Daniel Swanson + * @authors Copyright (c) 2016 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 LIBDOOMSDAY_RESOURCE_COLORPALETTES_H +#define LIBDOOMSDAY_RESOURCE_COLORPALETTES_H + +#include "colorpalette.h" + +namespace res { + +class ColorPalettes +{ +public: + ColorPalettes(); + + /** + * Returns the total number of color palettes. + */ + de::dint colorPaletteCount() const; + + /** + * Destroys all the color palettes. + */ + void clearAllColorPalettes(); + + /** + * Returns the ColorPalette associated with unique @a id. + */ + ColorPalette &colorPalette(ColorPalette::Id id) const; + + /** + * Returns the symbolic name of the specified color @a palette. A zero-length + * string is returned if no name is associated. + */ + de::String colorPaletteName(ColorPalette &palette) const; + + /** + * Returns @c true iff a ColorPalette with the specified @a name is present. + */ + bool hasColorPalette(de::String name) const; + + /** + * Returns the ColorPalette associated with @a name. + * + * @see hasColorPalette() + */ + ColorPalette &colorPalette(de::String name) const; + + /** + * @param newPalette Color palette to add. Ownership of the palette is given + * to the resource system. + * @param name Symbolic name of the color palette. + */ + void addColorPalette(ColorPalette &newPalette, de::String const &name = de::String()); + + /** + * Returns the unique identifier of the current default color palette. + */ + ColorPalette::Id defaultColorPalette() const; + + /** + * Change the default color palette. + * + * @param newDefaultPalette The color palette to make default. + */ + void setDefaultColorPalette(ColorPalette *newDefaultPalette); + +private: + DENG2_PRIVATE(d) +}; + +} // namespace res + +#endif // LIBDOOMSDAY_RESOURCE_COLORPALETTES_H diff --git a/doomsday/apps/libdoomsday/include/doomsday/resource/resources.h b/doomsday/apps/libdoomsday/include/doomsday/resource/resources.h index 14be54eb19..706f043091 100644 --- a/doomsday/apps/libdoomsday/include/doomsday/resource/resources.h +++ b/doomsday/apps/libdoomsday/include/doomsday/resource/resources.h @@ -26,7 +26,11 @@ #include #include -namespace res { class MapManifests; } +namespace res +{ + class MapManifests; + class ColorPalettes; +} /** * Base class for the resource management subsystem. @@ -36,6 +40,9 @@ namespace res { class MapManifests; } class LIBDOOMSDAY_PUBLIC Resources : public de::System { public: + /// The referenced resource was not found. @ingroup errors + DENG2_ERROR(MissingResourceError); + /// An unknown resource class identifier was specified. @ingroup errors DENG2_ERROR(UnknownResourceClassError); @@ -76,6 +83,9 @@ class LIBDOOMSDAY_PUBLIC Resources : public de::System res::MapManifests & mapManifests(); res::MapManifests const & mapManifests() const; + res::ColorPalettes & colorPalettes(); + res::ColorPalettes const & colorPalettes() const; + private: DENG2_PRIVATE(d) }; diff --git a/doomsday/apps/libdoomsday/src/resource/colorpalettes.cpp b/doomsday/apps/libdoomsday/src/resource/colorpalettes.cpp new file mode 100644 index 0000000000..d81f13beae --- /dev/null +++ b/doomsday/apps/libdoomsday/src/resource/colorpalettes.cpp @@ -0,0 +1,135 @@ +/** @file colorpalettes.cpp + * + * @authors Copyright © 2013-2015 Daniel Swanson + * @authors Copyright (c) 2016 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 "doomsday/resource/colorpalettes.h" +#include "doomsday/resource/resources.h" + +#include + +using namespace de; + +namespace res { + +DENG2_PIMPL_NOREF(ColorPalettes) +{ + typedef QMap ColorPalettes; + ColorPalettes colorPalettes; // owned + + typedef QMap ColorPaletteNames; + ColorPaletteNames colorPaletteNames; + + ColorPalette::Id defaultColorPalette = 0; + + ~Impl() + { + clear(); + } + + void clear() + { + qDeleteAll(colorPalettes); + colorPalettes.clear(); + colorPaletteNames.clear(); + + defaultColorPalette = 0; + } +}; + +ColorPalettes::ColorPalettes() + : d(new Impl) +{} + +void ColorPalettes::clearAllColorPalettes() +{ + d->clear(); +} + +dint ColorPalettes::colorPaletteCount() const +{ + return d->colorPalettes.count(); +} + +ColorPalette &ColorPalettes::colorPalette(ColorPalette::Id id) const +{ + // Choose the default palette? + if(!id) + { + id = d->defaultColorPalette; + } + + auto found = d->colorPalettes.find(id); + if(found != d->colorPalettes.end()) return *found.value(); + /// @throw MissingResourceError An unknown/invalid id was specified. + throw Resources::MissingResourceError("ColorPalettes::colorPalette", "Invalid id " + String::number(id)); +} + +String ColorPalettes::colorPaletteName(ColorPalette &palette) const +{ + QList const names = d->colorPaletteNames.keys(&palette); + if(!names.isEmpty()) + { + return names.first(); + } + return String(); +} + +bool ColorPalettes::hasColorPalette(String name) const +{ + return d->colorPaletteNames.contains(name); +} + +ColorPalette &ColorPalettes::colorPalette(String name) const +{ + auto found = d->colorPaletteNames.find(name); + if(found != d->colorPaletteNames.end()) return *found.value(); + /// @throw MissingResourceError An unknown name was specified. + throw Resources::MissingResourceError("ColorPalettes::colorPalette", "Unknown name '" + name + "'"); +} + +void ColorPalettes::addColorPalette(res::ColorPalette &newPalette, String const &name) +{ + // Do we already own this palette? + if(d->colorPalettes.contains(newPalette.id())) + return; + + d->colorPalettes.insert(newPalette.id(), &newPalette); + + if(!name.isEmpty()) + { + d->colorPaletteNames.insert(name, &newPalette); + } + + // If this is the first palette automatically set it as the default. + if(d->colorPalettes.count() == 1) + { + d->defaultColorPalette = newPalette.id(); + } +} + +ColorPalette::Id ColorPalettes::defaultColorPalette() const +{ + return d->defaultColorPalette; +} + +void ColorPalettes::setDefaultColorPalette(res::ColorPalette *newDefaultPalette) +{ + d->defaultColorPalette = newDefaultPalette ? newDefaultPalette->id().asUInt32() : 0; +} + +} // namespace res diff --git a/doomsday/apps/libdoomsday/src/resource/resources.cpp b/doomsday/apps/libdoomsday/src/resource/resources.cpp index 1042bdba51..603bc9ab46 100644 --- a/doomsday/apps/libdoomsday/src/resource/resources.cpp +++ b/doomsday/apps/libdoomsday/src/resource/resources.cpp @@ -18,6 +18,7 @@ #include "doomsday/resource/resources.h" #include "doomsday/resource/mapmanifests.h" +#include "doomsday/resource/colorpalettes.h" #include "doomsday/filesys/fs_main.h" #include @@ -34,6 +35,7 @@ DENG2_PIMPL(Resources) ResourceClasses resClasses; NullResourceClass nullResourceClass; NativePath nativeSavePath; + res::ColorPalettes colorPalettes; res::MapManifests mapManifests; Impl(Public *i) @@ -76,7 +78,9 @@ void Resources::timeChanged(Clock const &) } void Resources::clear() -{} +{ + d->colorPalettes.clearAllColorPalettes(); +} Resources &Resources::get() { @@ -123,6 +127,16 @@ res::MapManifests const &Resources::mapManifests() const return d->mapManifests; } +res::ColorPalettes &Resources::colorPalettes() +{ + return d->colorPalettes; +} + +const res::ColorPalettes &Resources::colorPalettes() const +{ + return d->colorPalettes; +} + ResourceClass &App_ResourceClass(String className) { return Resources::get().resClass(className);