Skip to content

Commit

Permalink
Cleanup|Resources: Moved color palettes to a new source file in libdo…
Browse files Browse the repository at this point in the history
…omsday
  • Loading branch information
skyjake committed Jul 9, 2016
1 parent 39f941c commit e849f0e
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 174 deletions.
57 changes: 2 additions & 55 deletions doomsday/apps/client/include/resource/resourcesystem.h
Expand Up @@ -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);

Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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__

/**
Expand Down
5 changes: 3 additions & 2 deletions doomsday/apps/client/src/gl/gl_main.cpp
Expand Up @@ -39,6 +39,7 @@
#include <doomsday/console/var.h>
#include <doomsday/defs/mapinfo.h>
#include <doomsday/filesys/fs_main.h>
#include <doomsday/resource/colorpalettes.h>
#include "clientapp.h"
#include "sys_system.h" // novideo

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion doomsday/apps/client/src/gl/texturecontent.cpp
Expand Up @@ -22,6 +22,7 @@
#include "gl/texturecontent.h"

#include <cstring>
#include <doomsday/resource/colorpalettes.h>
#include <de/concurrency.h>
#include <de/memory.h>
#include <de/GLInfo>
Expand Down Expand Up @@ -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);
}

Expand Down
18 changes: 10 additions & 8 deletions doomsday/apps/client/src/resource/api_resource.cpp
Expand Up @@ -27,6 +27,8 @@
# include "render/r_main.h" // texGammaLut
#endif

#include <doomsday/resource/colorpalettes.h>

using namespace de;

#undef Textures_UniqueId2
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
9 changes: 5 additions & 4 deletions doomsday/apps/client/src/resource/image.cpp
Expand Up @@ -22,6 +22,7 @@
#include "resource/image.h"

#include <doomsday/resource/patch.h>
#include <doomsday/resource/colorpalettes.h>

#include <de/memory.h>
#include <de/Log>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
109 changes: 11 additions & 98 deletions doomsday/apps/client/src/resource/resourcesystem.cpp
Expand Up @@ -57,6 +57,7 @@
#include <doomsday/resource/patch.h>
#include <doomsday/resource/patchname.h>
#include <doomsday/resource/mapmanifests.h>
#include <doomsday/resource/colorpalettes.h>
#include <doomsday/SavedSession>
#include <doomsday/Session>

Expand Down Expand Up @@ -442,14 +443,6 @@ DENG2_PIMPL(ResourceSystem)
typedef QList<AnimGroup *> AnimGroups;
AnimGroups animGroups;

typedef QMap<colorpaletteid_t, res::ColorPalette *> ColorPalettes;
ColorPalettes colorPalettes;

typedef QMap<String, res::ColorPalette *> ColorPaletteNames;
ColorPaletteNames colorPaletteNames;

colorpaletteid_t defaultColorPalette;

/// System subspace schemes containing the textures.
TextureSchemes textureSchemes;
QList<TextureScheme *> textureSchemeCreationOrder;
Expand Down Expand Up @@ -538,7 +531,6 @@ DENG2_PIMPL(ResourceSystem)

Impl(Public *i)
: Base(i)
, defaultColorPalette (0)
, materialManifestIdMapSize(0)
, materialManifestIdMap (0)
#ifdef __CLIENT__
Expand Down Expand Up @@ -633,7 +625,6 @@ DENG2_PIMPL(ResourceSystem)
#ifdef __CLIENT__
clearModels();
#endif
self.clearAllColorPalettes();
}

inline de::FS1 &fileSys()
Expand Down Expand Up @@ -2266,7 +2257,6 @@ void ResourceSystem::clear()
#endif
clearAllRuntimeResources();
clearAllAnimGroups();
clearAllColorPalettes();
}

void ResourceSystem::clearAllResources()
Expand All @@ -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();
Expand Down Expand Up @@ -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<String> 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()
Expand Down

0 comments on commit e849f0e

Please sign in to comment.