From: Daniel Karbach Date: Sun, 2 Sep 2012 14:30:35 +0000 (+0200) Subject: added interpretation of colors X-Git-Url: http://git.localhorst.tv/?a=commitdiff_plain;h=215260d3f49f6bbeed7ee5f5f1da3a5ac4ecc111;p=l2e.git added interpretation of colors --- diff --git a/src/graphics/Color.h b/src/graphics/Color.h new file mode 100644 index 0000000..42cbc89 --- /dev/null +++ b/src/graphics/Color.h @@ -0,0 +1,42 @@ +/* + * Color.h + * + * Created on: Sep 1, 2012 + * Author: holy + */ + +#ifndef GRAPHICS_COLOR_H_ +#define GRAPHICS_COLOR_H_ + +#include + +namespace graphics { + +class Color { + +public: + Color() :r(0), g(0), b(0), a(255) { } + Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255) : r(r), g(g), b(b), a(a) { } + +public: + Uint8 RedChannel() const { return r; } + Uint8 GreenChannel() const { return g; } + Uint8 BlueChannel() const { return b; } + Uint8 AlphaChannel() const { return a; } + + Uint32 MapRGB(SDL_PixelFormat *f) const { return SDL_MapRGB(f, r, g, b); } + Uint32 MapRGBA(SDL_PixelFormat *f) const { return SDL_MapRGBA(f, r, g, b, a); } + + void SetRedChannel(Uint8 i) { r = i; } + void SetGreenChannel(Uint8 i) { g = i; } + void SetBlueChannel(Uint8 i) { b = i; } + void SetAlphaChannel(Uint8 i) { a = i; } + +private: + Uint8 r, g, b, a; + +}; + +} + +#endif /* GRAPHICS_COLOR_H_ */ diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 5d3b9fc..79fe4f3 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -35,6 +35,7 @@ using common::Item; using common::Spell; using common::TargetingMode; using graphics::Animation; +using graphics::Color; using graphics::Font; using graphics::Frame; using graphics::Gauge; @@ -127,6 +128,19 @@ bool Interpreter::GetBoolean(const std::string &name) const { } } +const Color &Interpreter::GetColor(const std::string &name) const { + map::const_iterator i(parsedDefinitions.find(name)); + if (i != parsedDefinitions.end()) { + if (i->second.type == COLOR) { + return colors[i->second.index]; + } else { + throw Error("cannot cast " + i->second.dfn->TypeName() + " to Color"); + } + } else { + throw Error("access to undefined Color " + name); + } +} + Font *Interpreter::GetFont(const std::string &name) { map::const_iterator i(parsedDefinitions.find(name)); if (i != parsedDefinitions.end()) { @@ -356,7 +370,8 @@ void Interpreter::ReadLiteral(const Definition &dfn) { parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, BOOLEAN, booleans.size() - 1))); break; case Literal::COLOR: - throw Error("unhandled literal: color"); + colors.push_back(Color(dfn.GetLiteral()->GetRed(), dfn.GetLiteral()->GetGreen(), dfn.GetLiteral()->GetBlue(), dfn.GetLiteral()->GetAlpha())); + parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, COLOR, colors.size() - 1))); break; case Literal::NUMBER: numbers.push_back(dfn.GetLiteral()->GetNumber()); @@ -418,6 +433,15 @@ bool Interpreter::GetBoolean(const Value &v) { } } +Color Interpreter::GetColor(const Value &v) { + if (v.IsLiteral()) { + return Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()); + } else { + ReadDefinition(source.GetDefinition(v.GetIdentifier())); + return GetColor(v.GetIdentifier()); + } +} + Font *Interpreter::GetFont(const Value &v) { if (v.IsLiteral()) { Font *f(new Font); diff --git a/src/loader/Interpreter.h b/src/loader/Interpreter.h index 7755a1b..b91e478 100644 --- a/src/loader/Interpreter.h +++ b/src/loader/Interpreter.h @@ -9,6 +9,7 @@ #define LOADER_INTERPRETER_H_ #include "../geometry/Vector.h" +#include "../graphics/Color.h" #include "../graphics/ComplexAnimation.h" #include @@ -68,6 +69,7 @@ public: public: graphics::Animation *GetAnimation(const std::string &name); bool GetBoolean(const std::string &name) const; + const graphics::Color &GetColor(const std::string &name) const; graphics::Font *GetFont(const std::string &name); graphics::Frame *GetFrame(const std::string &name); graphics::Gauge *GetGauge(const std::string &name); @@ -86,6 +88,7 @@ public: public: const std::vector &Booleans() const { return booleans; } + const std::vector &Colors() const { return colors; } const std::vector &ComplexAnimations() const { return complexAnimations; } const std::vector &Fonts() const { return fonts; } const std::vector &Frames() const { return frames; } @@ -110,6 +113,7 @@ private: void ReadObject(const Definition &); graphics::Animation *GetAnimation(const Value &); + graphics::Color GetColor(const Value &); bool GetBoolean(const Value &); graphics::Font *GetFont(const Value &); graphics::Frame *GetFrame(const Value &); @@ -150,6 +154,7 @@ private: const ParsedSource &source; enum Type { BOOLEAN, + COLOR, COMPLEX_ANIMATION, FONT, FRAME, @@ -183,6 +188,7 @@ private: std::map imageCache; std::vector booleans; + std::vector colors; std::vector complexAnimations; std::vector fonts; std::vector frames; diff --git a/src/main.cpp b/src/main.cpp index 803977a..daeb3c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -128,7 +128,7 @@ int main(int argc, char **argv) { battleRes.activeHeroTagFrame = intp.GetFrame("activeHeroTagFrame"); battleRes.smallHeroTagFrame = intp.GetFrame("smallHeroTagFrame"); battleRes.lastSmallHeroTagFrame = intp.GetFrame("lastSmallHeroTagFrame"); - battleRes.heroesBgColor = SDL_MapRGB(screen.Screen()->format, 0x18, 0x28, 0x31); + battleRes.heroesBgColor = intp.GetColor("heroesBgColor").MapRGB(screen.Screen()->format); battleRes.healthGauge = intp.GetGauge("healthGauge"); battleRes.manaGauge = intp.GetGauge("manaGauge"); diff --git a/test-data/test.l2s b/test-data/test.l2s index dd039e8..9cd2d0c 100644 --- a/test-data/test.l2s +++ b/test-data/test.l2s @@ -446,6 +446,7 @@ export Frame lastSmallHeroTagFrame { border: <8,16>, offset: <0,33> } +export Color heroesBgColor (24, 40, 49) export Gauge healthGauge { image: :"gauges.png",