Skip to content

Commit

Permalink
Refactor|Resources: Began remodeling ColorPalette management
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Nov 16, 2013
1 parent ed5af01 commit fe74d70
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 236 deletions.
13 changes: 12 additions & 1 deletion doomsday/client/include/resource/colorpalette.h
Expand Up @@ -21,6 +21,7 @@
#define DENG_RESOURCE_COLORPALETTE_H

#include "dd_types.h"
#include <de/Error>
#include <de/Vector>

/**
Expand All @@ -31,6 +32,9 @@
class ColorPalette
{
public:
/// Base class for color-format-related errors. @ingroup errors
DENG2_ERROR(ColorFormatError);

/// Maximum number of bits per color component.
static int const max_component_bits = 16;

Expand All @@ -55,6 +59,13 @@ class ColorPalette
ColorPalette(int const compOrder[3], uint8_t const compBits[3],
uint8_t const *colorData, int colorCount);

static void parseColorFormat(char const *fmt, int compOrder[3], uint8_t compSize[3]);

/// @see color()
inline de::Vector3ub operator [] (int colorIndex) const {
return color(colorIndex);
}

/**
* Returns the total number of colors in the palette.
*/
Expand Down Expand Up @@ -83,7 +94,7 @@ class ColorPalette
*
* @return Associated R8G8B8 color triplet.
*
* @see colorf()
* @see colorf(), operator []
*/
de::Vector3ub color(int colorIndex) const;

Expand Down
34 changes: 14 additions & 20 deletions doomsday/client/include/resource/colorpalettes.h
@@ -1,7 +1,6 @@
/**
* @file colorpalettes.h ColorPalette repository and related bookkeeping.
/** @file colorpalettes.h Color palette resource collection.
*
* @author Copyright &copy; 2009-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2009-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
Expand All @@ -18,8 +17,8 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_RESOURCE_COLORPALETTES_H
#define LIBDENG_RESOURCE_COLORPALETTES_H
#ifndef DENG_RESOURCE_COLORPALETTES_H
#define DENG_RESOURCE_COLORPALETTES_H

#include "dd_share.h" // For colorpaletteid_t
#include "resource/colorpalette.h"
Expand All @@ -30,29 +29,28 @@
#define NUM_TRANSLATION_TABLES (NUM_TRANSLATION_CLASSES * NUM_TRANSLATION_MAPS_PER_CLASS)

DENG_EXTERN_C colorpaletteid_t defaultColorPalette;
DENG_EXTERN_C byte* translationTables;

#ifdef __cplusplus
extern "C" {
#endif
DENG_EXTERN_C byte *translationTables;

void R_InitTranslationTables(void);
void R_UpdateTranslationTables(void);
void R_InitTranslationTables();
void R_UpdateTranslationTables();

byte const *R_TranslationTable(int tclass, int tmap);

/// Prepare for color palette creation.
void R_InitColorPalettes(void);
void R_InitColorPalettes();

/// To be called when any existing color palettes are no longer required.
void R_DestroyColorPalettes(void);
void R_DestroyColorPalettes();

/// @return Number of available color palettes.
int R_ColorPaletteCount(void);
int R_ColorPaletteCount();

/// @return ColorPalette associated with unique @a id, else @c NULL.
ColorPalette *R_ToColorPalette(colorpaletteid_t id);

DENG_EXTERN_C colorpaletteid_t R_GetColorPaletteNumForName(char const *name);
DENG_EXTERN_C char const *R_GetColorPaletteNameForNum(colorpaletteid_t id);

/**
* Given a color palette list index return the ColorPalette.
* @return ColorPalette if found else @c NULL
Expand All @@ -68,8 +66,4 @@ ColorPalette *R_GetColorPaletteByIndex(int paletteIdx);
*/
boolean R_SetDefaultColorPalette(colorpaletteid_t id);

#ifdef __cplusplus
} // extern "C"
#endif

#endif /* LIBDENG_RESOURCE_COLORPALETTES_H */
#endif // DENG_RESOURCE_COLORPALETTES_H
73 changes: 73 additions & 0 deletions doomsday/client/src/resource/colorpalette.cpp
Expand Up @@ -97,6 +97,79 @@ ColorPalette::ColorPalette(int const compOrder[3], uint8_t const compBits[3],
}
}

void ColorPalette::parseColorFormat(char const *fmt, int compOrder[], uint8_t compSize[])
{
static char const *compNames[] = { "red", "green", "blue" };

std::memset(compOrder, -1, sizeof(compOrder));

int pos = 0;
char const *end = fmt + (qstrlen(fmt) - 1);
char const *c = fmt;
do
{
int comp = -1;
if (*c == 'R' || *c == 'r') comp = 0;
else if(*c == 'G' || *c == 'g') comp = 1;
else if(*c == 'B' || *c == 'b') comp = 2;

if(comp != -1)
{
// Have we encountered this component yet?
if(compOrder[comp] == -1)
{
// No.
char const *start;
size_t numDigits;

compOrder[comp] = pos++;

// Read the number of bits.
start = ++c;
while((*c >= '0' && *c <= '9') && ++c < end)
{}

numDigits = c - start;
if(numDigits != 0 && numDigits <= 2)
{
char buf[3];

std::memset(buf, 0, sizeof(buf));
std::memcpy(buf, start, numDigits);

compSize[comp] = atoi(buf);

// Are we done?
if(pos == 3) break;

// Unread the last character.
c--;
continue;
}
}
}

/// @throw ColorFormatError
throw ColorFormatError("parseColorFormat", QString("Invalid character '%1' in format string at position %2.").arg(*c).arg((unsigned int) (c - fmt)));
} while(++c <= end);

if(pos != 3)
{
/// @throw ColorFormatError
throw ColorFormatError("parseColorFormat", "Incomplete specification.");
}

// Check validity of bits per component.
for(int i = 0; i < 3; ++i)
{
if(compSize[i] == 0 || compSize[i] > ColorPalette::max_component_bits)
{
/// @throw ColorFormatError
throw ColorFormatError("parseColorFormat", QString("Unsupported bit depth %1 for %2 component.").arg(compSize[i]).arg(compNames[compOrder[i]]));
}
}
}

void ColorPalette::replaceColorTable(int const compOrder[3],
uint8_t const compBits[3], uint8_t const *colorData, int colorCount)
{
Expand Down

0 comments on commit fe74d70

Please sign in to comment.