diff options
author | Andrei Karas <akaras@inbox.ru> | 2013-04-20 23:00:42 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2013-04-20 23:05:09 +0300 |
commit | 52f78717dae3cb02eb1539841dd2a6bd351f74c1 (patch) | |
tree | 78b6bb12fcbb5e1624a76531ef43a25d263b3c79 | |
parent | e50c6b9fbcd7713b990012f17665efeb2a284dd2 (diff) | |
download | manaplus-52f78717dae3cb02eb1539841dd2a6bd351f74c1.tar.gz manaplus-52f78717dae3cb02eb1539841dd2a6bd351f74c1.tar.bz2 manaplus-52f78717dae3cb02eb1539841dd2a6bd351f74c1.tar.xz manaplus-52f78717dae3cb02eb1539841dd2a6bd351f74c1.zip |
add support for GIMP palettes for dye colors.
Palette file must be named palette.gpl and contain
colors with correct names.
To use it in dye string can be used character @.
Example:
<color id="2" name="black" value="@Untitled7,Untitled8,Untitled6"/>
in all dye string it not tested.
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/Makefile.am | 3 | ||||
-rw-r--r-- | src/client.cpp | 3 | ||||
-rw-r--r-- | src/resources/dye.cpp | 100 | ||||
-rw-r--r-- | src/resources/dye.h | 9 | ||||
-rw-r--r-- | src/resources/palettedb.cpp | 107 | ||||
-rw-r--r-- | src/resources/palettedb.h | 39 |
7 files changed, 214 insertions, 50 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7aaeee334..4aa8104e1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -427,6 +427,7 @@ SET(SRCS resources/cursor.h resources/dye.cpp resources/dye.h + resources/dyecolor.h resources/emotedb.cpp resources/emotedb.h resources/fboinfo.h @@ -452,6 +453,8 @@ SET(SRCS resources/npcdb.h resources/openglimagehelper.cpp resources/openglimagehelper.h + resources/palettedb.cpp + resources/palettedb.h resources/petdb.cpp resources/petdb.h resources/resource.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 22c7ffb14..b48c33de7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -428,6 +428,7 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ resources/cursor.h \ resources/dye.cpp \ resources/dye.h \ + resources/dyecolor.h \ resources/emotedb.cpp \ resources/emotedb.h \ resources/fboinfo.h \ @@ -453,6 +454,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ resources/npcdb.h \ resources/openglimagehelper.cpp \ resources/openglimagehelper.h \ + resources/palettedb.cpp \ + resources/palettedb.h \ resources/petdb.cpp \ resources/petdb.h \ resources/resource.cpp \ diff --git a/src/client.cpp b/src/client.cpp index 265054e18..45d8ff2e3 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -82,6 +82,7 @@ #include "resources/emotedb.h" #include "resources/imagehelper.h" #include "resources/openglimagehelper.h" +#include "resources/palettedb.h" #include "resources/sdlimagehelper.h" #include "resources/sounddb.h" #include "resources/itemdb.h" @@ -756,6 +757,7 @@ void Client::gameClear() ItemDB::unload(); MonsterDB::unload(); NPCDB::unload(); + PaletteDB::unload(); PETDB::unload(); StatusEffect::unload(); @@ -1382,6 +1384,7 @@ int Client::gameExec() // Load XML databases CharDB::load(); + PaletteDB::load(); ColorDB::load(); SoundDB::load(); MapDB::load(); diff --git a/src/resources/dye.cpp b/src/resources/dye.cpp index 960ac36ae..9997fc6ae 100644 --- a/src/resources/dye.cpp +++ b/src/resources/dye.cpp @@ -24,6 +24,8 @@ #include "logger.h" +#include "resources/palettedb.h" + #include <math.h> #include <sstream> @@ -33,42 +35,52 @@ DyePalette::DyePalette(const std::string &description, const int8_t blockSize) : mColors() { - const int size = static_cast<int>(description.length()); + const size_t size = static_cast<int>(description.length()); if (size == 0) return; - if (description[0] != '#') - { - // TODO: load palette from file. - return; - } - int pos = 1; - for ( ; ; ) + if (description[0] == '#') { - if (pos + blockSize > size) - break; - - Color color = + size_t pos = 1; + for ( ; ; ) { - {0, 0, 0, 0} - }; + if (pos + blockSize > size) + break; - for (int i = 0, colorIdx = 0; i < blockSize && colorIdx < 4; - i += 2, colorIdx ++) - { - color.value[colorIdx] = static_cast<unsigned char>(( - hexDecode(description[pos + i]) << 4) - + hexDecode(description[pos + i + 1])); - } - mColors.push_back(color); - pos += blockSize; + DyeColor color(0, 0, 0, 0); - if (pos == size) - return; - if (description[pos] != ',') - break; + for (int i = 0, colorIdx = 0; i < blockSize && colorIdx < 4; + i += 2, colorIdx ++) + { + color.value[colorIdx] = static_cast<unsigned char>(( + hexDecode(description[pos + i]) << 4) + + hexDecode(description[pos + i + 1])); + } + mColors.push_back(color); + pos += blockSize; - ++pos; + if (pos == size) + return; + if (description[pos] != ',') + break; + + ++pos; + } + } + else if (description[0] == '@') + { + size_t pos = 1; + for ( ; pos < size ; ) + { + const size_t idx = description.find(',', pos); + if (idx == std::string::npos) + return; + if (idx == pos) + break; + mColors.push_back(PaletteDB::getColor( + description.substr(pos, idx - pos))); + pos = idx + 1; + } } logger->log("Error, invalid embedded palette: %s", description.c_str()); @@ -185,15 +197,15 @@ void DyePalette::getColor(double intensity, int color[3]) const void DyePalette::replaceSColor(uint8_t *const color) const { - std::vector<Color>::const_iterator it = mColors.begin(); - const std::vector<Color>::const_iterator it_end = mColors.end(); + std::vector<DyeColor>::const_iterator it = mColors.begin(); + const std::vector<DyeColor>::const_iterator it_end = mColors.end(); while (it != it_end) { - const Color &col = *it; + const DyeColor &col = *it; ++ it; if (it == it_end) return; - const Color &col2 = *it; + const DyeColor &col2 = *it; if (color[0] == col.value[0] && color[1] == col.value[1] && color[2] == col.value[2]) { @@ -208,15 +220,15 @@ void DyePalette::replaceSColor(uint8_t *const color) const void DyePalette::replaceAColor(uint8_t *const color) const { - std::vector<Color>::const_iterator it = mColors.begin(); - const std::vector<Color>::const_iterator it_end = mColors.end(); + std::vector<DyeColor>::const_iterator it = mColors.begin(); + const std::vector<DyeColor>::const_iterator it_end = mColors.end(); while (it != it_end) { - const Color &col = *it; + const DyeColor &col = *it; ++ it; if (it == it_end) return; - const Color &col2 = *it; + const DyeColor &col2 = *it; if (color[1] == col.value[0] && color[2] == col.value[1] && color[3] == col.value[2] && color[0] == col.value[3]) { @@ -232,15 +244,15 @@ void DyePalette::replaceAColor(uint8_t *const color) const void DyePalette::replaceSOGLColor(uint8_t *const color) const { - std::vector<Color>::const_iterator it = mColors.begin(); - const std::vector<Color>::const_iterator it_end = mColors.end(); + std::vector<DyeColor>::const_iterator it = mColors.begin(); + const std::vector<DyeColor>::const_iterator it_end = mColors.end(); while (it != it_end) { - const Color &col = *it; + const DyeColor &col = *it; ++ it; if (it == it_end) return; - const Color &col2 = *it; + const DyeColor &col2 = *it; if (color[2] == col.value[0] && color[1] == col.value[1] && color[0] == col.value[2]) { @@ -255,15 +267,15 @@ void DyePalette::replaceSOGLColor(uint8_t *const color) const void DyePalette::replaceAOGLColor(uint8_t *const color) const { - std::vector<Color>::const_iterator it = mColors.begin(); - const std::vector<Color>::const_iterator it_end = mColors.end(); + std::vector<DyeColor>::const_iterator it = mColors.begin(); + const std::vector<DyeColor>::const_iterator it_end = mColors.end(); while (it != it_end) { - const Color &col = *it; + const DyeColor &col = *it; ++ it; if (it == it_end) return; - const Color &col2 = *it; + const DyeColor &col2 = *it; if (color[2] == col.value[0] && color[1] == col.value[1] && color[0] == col.value[2] && color[3] == col.value[3]) { diff --git a/src/resources/dye.h b/src/resources/dye.h index 442a514d7..efcf95420 100644 --- a/src/resources/dye.h +++ b/src/resources/dye.h @@ -23,11 +23,11 @@ #ifndef DYE_H #define DYE_H +#include "resources/dyecolor.h" + #include <string> #include <vector> -#include <SDL_stdinc.h> - #include "localconsts.h" const int dyePalateSize = 9; @@ -83,10 +83,7 @@ class DyePalette final static int hexDecode(const signed char c) A_WARN_UNUSED; private: - struct Color - { unsigned char value[4]; }; - - std::vector<Color> mColors; + std::vector<DyeColor> mColors; }; /** diff --git a/src/resources/palettedb.cpp b/src/resources/palettedb.cpp new file mode 100644 index 000000000..c770a9cdc --- /dev/null +++ b/src/resources/palettedb.cpp @@ -0,0 +1,107 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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 + * 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 "resources/palettedb.h" + +#include "logger.h" + +#include "resources/dyecolor.h" +#include "resources/resourcemanager.h" + +#include "utils/stringvector.h" + +#include <map> + +#include "debug.h" + +namespace +{ + bool mLoaded = false; + std::map<std::string, DyeColor> mColors; + DyeColor mEmpty(0, 0, 0, 0); +} + +void PaletteDB::load() +{ + if (mLoaded) + unload(); + + loadPalette(); +} + +void PaletteDB::loadPalette() +{ + mLoaded = true; + StringVect lines; + ResourceManager::loadTextFile("palette.gpl", lines); + StringVectCIter it = lines.begin(); + if (it == lines.end()) + { + logger->log("missing GIMP palette file"); + return; + } + if (*it != "GIMP Palette") + { + logger->log("wrong GIMP palette file"); + return; + } + ++ it; + // skip header + while (it != lines.end()) + { + const std::string line = *it; + if (!line.empty() && line[0] == '#') + break; + ++ it; + } + + char name[101]; + + // process colors and ignore commets + while (it != lines.end()) + { + const std::string line = *it; + ++ it; + + if (line.empty() || line[0] == '#') + continue; + + unsigned int r; + unsigned int g; + unsigned int b; + + if (sscanf(line.c_str(), "%u %u %u\t%s", &r, &g, &b, name) == 4) + mColors[name] = DyeColor(r, g, b); + } +} + +void PaletteDB::unload() +{ + mColors.clear(); +} + +const DyeColor &PaletteDB::getColor(const std::string &name) +{ + std::map<std::string, DyeColor>::const_iterator it = mColors.find(name); + if (it != mColors.end()) + return (*it).second; + else + return mEmpty; +} diff --git a/src/resources/palettedb.h b/src/resources/palettedb.h new file mode 100644 index 000000000..28b901799 --- /dev/null +++ b/src/resources/palettedb.h @@ -0,0 +1,39 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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 + * 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 PALETTEDB_H +#define PALETTEDB_H + +#include "localconsts.h" + +#include <string> + +struct DyeColor; + +namespace PaletteDB +{ + void load(); + void unload(); + void loadPalette(); + const DyeColor &getColor(const std::string &name); + +} // namespace PaletteDB + +#endif |