From: Daniel Karbach Date: Fri, 31 Aug 2012 19:43:26 +0000 (+0200) Subject: added interpretation of Font and Frame objects X-Git-Url: http://git.localhorst.tv/?a=commitdiff_plain;h=8f54789ae7d974035b9103982c38e1714a15728b;p=l2e.git added interpretation of Font and Frame objects --- diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 9f4b77d..d5a61a6 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -12,6 +12,8 @@ #include "../battle/Monster.h" #include "../battle/PartyLayout.h" #include "../graphics/ComplexAnimation.h" +#include "../graphics/Font.h" +#include "../graphics/Frame.h" #include "../graphics/SimpleAnimation.h" #include "../graphics/Sprite.h" @@ -24,6 +26,8 @@ using battle::Monster; using battle::PartyLayout; using battle::Stats; using graphics::Animation; +using graphics::Font; +using graphics::Frame; using graphics::ComplexAnimation; using graphics::SimpleAnimation; using graphics::Sprite; @@ -43,6 +47,12 @@ Interpreter::~Interpreter() { for (vector::const_iterator i(heroes.begin()), end(heroes.end()); i != end; ++i) { delete *i; } + for (vector::const_iterator i(fonts.begin()), end(fonts.end()); i != end; ++i) { + delete *i; + } + for (vector::const_iterator i(frames.begin()), end(frames.end()); i != end; ++i) { + delete *i; + } for (vector::const_iterator i(images.begin()), end(images.end()); i != end; ++i) { SDL_FreeSurface(*i); } @@ -92,6 +102,32 @@ bool Interpreter::GetBoolean(const std::string &name) const { } } +Font *Interpreter::GetFont(const std::string &name) { + map::const_iterator i(parsedDefinitions.find(name)); + if (i != parsedDefinitions.end()) { + if (i->second.type == FONT) { + return fonts[i->second.index]; + } else { + throw Error("cannot cast " + i->second.dfn->TypeName() + " to Font"); + } + } else { + throw Error("access to undefined Font " + name); + } +} + +Frame *Interpreter::GetFrame(const std::string &name) { + map::const_iterator i(parsedDefinitions.find(name)); + if (i != parsedDefinitions.end()) { + if (i->second.type == FRAME) { + return frames[i->second.index]; + } else { + throw Error("cannot cast " + i->second.dfn->TypeName() + " to Frame"); + } + } else { + throw Error("access to undefined Frame " + name); + } +} + Hero *Interpreter::GetHero(const std::string &name) { map::const_iterator i(parsedDefinitions.find(name)); if (i != parsedDefinitions.end()) { @@ -269,6 +305,28 @@ bool Interpreter::GetBoolean(const Value &v) { } } +Font *Interpreter::GetFont(const Value &v) { + if (v.IsLiteral()) { + Font *f(new Font); + ReadFont(*f, *v.GetLiteral().GetProperties()); + return f; + } else { + ReadDefinition(source.GetDefinition(v.GetIdentifier())); + return GetFont(v.GetIdentifier()); + } +} + +Frame *Interpreter::GetFrame(const Value &v) { + if (v.IsLiteral()) { + Frame *f(new Frame); + ReadFrame(*f, *v.GetLiteral().GetProperties()); + return f; + } else { + ReadDefinition(source.GetDefinition(v.GetIdentifier())); + return GetFrame(v.GetIdentifier()); + } +} + SDL_Surface *Interpreter::GetImage(const Value &v) { const char *file(GetString(v)); SDL_Surface *image(IMG_Load(file)); @@ -357,6 +415,18 @@ void Interpreter::ReadObject(const Definition &dfn) { complexAnimations.push_back(animation); ReadComplexAnimation(*animation, *dfn.GetProperties()); parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, COMPLEX_ANIMATION, index))); + } else if (dfn.TypeName() == "Font") { + Font *font(new Font); + int index(fonts.size()); + fonts.push_back(font); + ReadFont(*font, *dfn.GetProperties()); + parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, FONT, index))); + } else if (dfn.TypeName() == "Frame") { + Frame *frame(new Frame); + int index(frames.size()); + frames.push_back(frame); + ReadFrame(*frame, *dfn.GetProperties()); + parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, FRAME, index))); } else if (dfn.TypeName() == "Hero") { Hero *hero(new Hero); int index(heroes.size()); @@ -428,6 +498,36 @@ void Interpreter::ReadComplexAnimationFrame(ComplexAnimation::FrameProp &f, cons } } +void Interpreter::ReadFont(Font &f, const PropertyList &props) { + for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) { + if (i->first == "sprite") { + f.SetSprite(GetSprite(*i->second)); + } else if (i->first == "columnoffset") { + f.SetColOffset(GetNumber(*i->second)); + } else if (i->first == "rowoffset") { + f.SetRowOffset(GetNumber(*i->second)); + } else { + throw Error("unknown Font property: " + i->first); + } + } +} + +void Interpreter::ReadFrame(Frame &f, const PropertyList &props) { + for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) { + if (i->first == "image") { + f.SetSurface(GetImage(*i->second)); + } else if (i->first == "border") { + f.SetBorderSize(GetVector(*i->second)); + } else if (i->first == "repeat") { + f.SetRepeatSize(GetVector(*i->second)); + } else if (i->first == "offset") { + f.SetOffset(GetVector(*i->second)); + } else { + throw Error("unknown Frame property: " + i->first); + } + } +} + void Interpreter::ReadHero(Hero &h, const PropertyList &props) { for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) { if (i->first == "name") { diff --git a/src/loader/Interpreter.h b/src/loader/Interpreter.h index 3f53b3f..ca9900c 100644 --- a/src/loader/Interpreter.h +++ b/src/loader/Interpreter.h @@ -26,6 +26,8 @@ namespace battle { namespace graphics { class Animation; + class Font; + class Frame; class SimpleAnimation; class Sprite; } @@ -58,6 +60,8 @@ public: public: graphics::Animation *GetAnimation(const std::string &name); bool GetBoolean(const std::string &name) const; + graphics::Font *GetFont(const std::string &name); + graphics::Frame *GetFrame(const std::string &name); battle::Hero *GetHero(const std::string &name); battle::Monster *GetMonster(const std::string &name); int GetNumber(const std::string &name) const; @@ -69,6 +73,8 @@ public: public: const std::vector &Booleans() const { return booleans; } const std::vector &ComplexAnimations() const { return complexAnimations; } + const std::vector &Fonts() const { return fonts; } + const std::vector &Frames() const { return frames; } const std::vector &Heroes() const { return heroes; } const std::vector &Images() const { return images; } const std::vector &Monsters() const { return monsters; } @@ -86,6 +92,8 @@ private: graphics::Animation *GetAnimation(const Value &); bool GetBoolean(const Value &); + graphics::Font *GetFont(const Value &); + graphics::Frame *GetFrame(const Value &); SDL_Surface *GetImage(const Value &); int GetNumber(const Value &); battle::PartyLayout *GetPartyLayout(const Value &); @@ -98,6 +106,8 @@ private: void ReadComplexAnimation(graphics::ComplexAnimation &, const PropertyList &); void ReadComplexAnimationFrame(graphics::ComplexAnimation::FrameProp &, const PropertyList &); + void ReadFont(graphics::Font &, const PropertyList &); + void ReadFrame(graphics::Frame &, const PropertyList &); void ReadHero(battle::Hero &, const PropertyList &); void ReadMonster(battle::Monster &, const PropertyList &); void ReadPartyLayout(battle::PartyLayout &, const PropertyList &); @@ -110,6 +120,8 @@ private: enum Type { BOOLEAN, COMPLEX_ANIMATION, + FONT, + FRAME, HERO, IMAGE, MONSTER, @@ -133,6 +145,8 @@ private: std::vector booleans; std::vector complexAnimations; + std::vector fonts; + std::vector frames; std::vector heroes; std::vector images; std::vector monsters; diff --git a/src/main.cpp b/src/main.cpp index 1c87e33..e196930 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -107,16 +107,8 @@ int main(int argc, char **argv) { battleRes.attackIcons = intp.GetSprite("attackIcons"); battleRes.attackChoiceIcons = intp.GetSprite("attackChoiceIcons"); battleRes.moveIcons = intp.GetSprite("moveIcons"); - - SDL_Surface *titleFrameImg(IMG_Load("test-data/title-frame.png")); - Frame titleFrame(titleFrameImg, 16, 16); - battleRes.titleFrame = &titleFrame; - - SDL_Surface *largeFontImg(IMG_Load("test-data/large-font.png")); - Sprite largeFontSprite(largeFontImg, 16, 32); - Font largeFont(&largeFontSprite, 0, -2); - battleRes.titleFont = &largeFont; - + battleRes.titleFrame = intp.GetFrame("titleFrame"); + battleRes.titleFont = intp.GetFont("largeFont"); battleRes.numberAnimationPrototype = intp.GetAnimation("numberAnimationPrototype"); battleRes.bigNumberSprite = intp.GetSprite("bigNumbers"); battleRes.greenNumberSprite = intp.GetSprite("bigGreenNumbers"); @@ -133,20 +125,11 @@ int main(int argc, char **argv) { battleRes.ikariLabelCol = 0; battleRes.ikariLabelRow = 4; - SDL_Surface *numbersImg(IMG_Load("test-data/numbers.png")); - Sprite numbersSprite(numbersImg, 16, 16); - Font heroTagFont(&numbersSprite, 0, -3); - battleRes.heroTagFont = &heroTagFont; - SDL_Surface *tagFramesImg(IMG_Load("test-data/tag-frames.png")); - Frame heroTagFrame(tagFramesImg, 16, 16, 1, 1, 0, 33); - battleRes.heroTagFrame = &heroTagFrame; - Frame activeHeroTagFrame(tagFramesImg, 16, 16); - battleRes.activeHeroTagFrame = &activeHeroTagFrame; - SDL_Surface *smallTagFrameImg(IMG_Load("test-data/small-tag-frame.png")); - Frame smallTagFrame(smallTagFrameImg, 8, 16); - battleRes.smallHeroTagFrame = &smallTagFrame; - Frame lastSmallTagFrame(smallTagFrameImg, 8, 16, 1, 1, 0, 33); - battleRes.lastSmallHeroTagFrame = &lastSmallTagFrame; + battleRes.heroTagFont = intp.GetFont("heroTagFont"); + battleRes.heroTagFrame = intp.GetFrame("heroTagFrame"); + 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); SDL_Surface *gauges(IMG_Load("test-data/gauges.png")); diff --git a/test-data/test.l2s b/test-data/test.l2s index 36c119a..b7e1acd 100644 --- a/test-data/test.l2s +++ b/test-data/test.l2s @@ -349,6 +349,19 @@ export Sprite moveIcons { size: <32,32> } +export Frame titleFrame { + image: "test-data/title-frame.png", + border: <16,16> +} + +export Font largeFont { + sprite: Sprite { + image: "test-data/large-font.png", + size: <16,32> + }, + rowoffset: -2 +} + export ComplexAnimation numberAnimationPrototype { frametime: frameTime, repeat: false, @@ -403,3 +416,30 @@ export Sprite heroTagLabels { image: "test-data/hero-tag-sprites.png", size: <32,16> } +export Font heroTagFont { + sprite: Sprite { + image: "test-data/numbers.png", + size: <16,16> + }, + rowoffset: -3 +} + +export Frame activeHeroTagFrame { + image: "test-data/tag-frames.png", + border: <16,16> +} +export Frame heroTagFrame { + image: "test-data/tag-frames.png", + border: <16,16>, + offset: < 0,33> +} + +export Frame smallHeroTagFrame { + image: "test-data/small-tag-frame.png", + border: <8,16> +} +export Frame lastSmallHeroTagFrame { + image: "test-data/small-tag-frame.png", + border: <8,16>, + offset: <0,33> +} \ No newline at end of file