From 1740b281aa32ba2c30a2a6203f7748af6a21fd39 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 7 Aug 2016 01:25:14 +0300 Subject: Extend horses.xml with separate offset for each direction for rider, up and down sprites. --- src/CMakeLists.txt | 1 + src/Makefile.am | 1 + src/being/being.cpp | 13 +++-- src/resources/db/horsedb.cpp | 127 ++++++++++++++++++++++++++++++++++--------- src/resources/horseinfo.h | 28 +++++----- src/resources/horseoffset.h | 47 ++++++++++++++++ 6 files changed, 172 insertions(+), 45 deletions(-) create mode 100644 src/resources/horseoffset.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b7c22d413..60a856bc0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -655,6 +655,7 @@ SET(SRCS resources/fboinfo.h resources/frame.h resources/horseinfo.h + resources/horseoffset.h resources/image/image.cpp resources/image/image.h resources/imagehelper.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 47f553f53..03454a1d7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1312,6 +1312,7 @@ manaplus_SOURCES += main.cpp \ resources/emotesprite.h \ resources/equipmentslots.h \ resources/horseinfo.h \ + resources/horseoffset.h \ resources/sprite/spritedisplay.h \ resources/sprite/spritereference.h \ resources/atlas/textureatlas.h \ diff --git a/src/being/being.cpp b/src/being/being.cpp index 8ce14bc40..e3a690441 100644 --- a/src/being/being.cpp +++ b/src/being/being.cpp @@ -3513,23 +3513,24 @@ void Being::drawPlayer(Graphics *restrict const graphics, const int py = mPixelY - mapTileSize + offsetY; if (mHorseInfo) { + HorseOffset &offset = mHorseInfo->offsets[mSpriteDirection]; for_each_horses(mDownHorseSprites) { (*it)->draw(graphics, - px + mHorseInfo->downOffsetX, - py + mHorseInfo->downOffsetY); + px + offset.downOffsetX, + py + offset.downOffsetY); } drawBeingCursor(graphics, px, py); drawPlayerSpriteAt(graphics, - px + mHorseInfo->riderOffsetX, - py + mHorseInfo->riderOffsetY); + px + offset.riderOffsetX, + py + offset.riderOffsetY); for_each_horses(mUpHorseSprites) { (*it)->draw(graphics, - px + mHorseInfo->upOffsetX, - py + mHorseInfo->upOffsetY); + px + offset.upOffsetX, + py + offset.upOffsetY); } } else diff --git a/src/resources/db/horsedb.cpp b/src/resources/db/horsedb.cpp index 0784a1e75..cdcc9ef3b 100644 --- a/src/resources/db/horsedb.cpp +++ b/src/resources/db/horsedb.cpp @@ -23,6 +23,8 @@ #include "configuration.h" +#include "enums/resources/spritedirection.h" + #include "resources/beingcommon.h" #include "resources/horseinfo.h" @@ -71,11 +73,6 @@ void HorseDB::load() currentSprite->variant = 0; mUnknown.upSprites.push_back(currentSprite); - mUnknown.upOffsetX = 0; - mUnknown.upOffsetY = 0; - mUnknown.downOffsetX = 0; - mUnknown.downOffsetY = 0; - logger->log1("Initializing horse database..."); loadXmlFile(paths.getStringValue("horsesFile"), SkipError_false); @@ -85,6 +82,69 @@ void HorseDB::load() mLoaded = true; } +static int parseDirectionName(const std::string &name) +{ + int id = -1; + if (name == "down") + { + id = SpriteDirection::DOWN; + } + else if (name == "downleft" || name == "leftdown") + { + id = SpriteDirection::DOWNLEFT; + } + else if (name == "left") + { + id = SpriteDirection::LEFT; + } + else if (name == "upleft" || name == "leftup") + { + id = SpriteDirection::UPLEFT; + } + else if (name == "up") + { + id = SpriteDirection::UP; + } + else if (name == "upright" || name == "rightup") + { + id = SpriteDirection::UPRIGHT; + } + else if (name == "right") + { + id = SpriteDirection::RIGHT; + } + else if (name == "downright" || name == "rightdown") + { + id = SpriteDirection::DOWNRIGHT; + } + // hack for died action. + else if (name == "died") + { + id = 9; + } + + return id; +} + +static void loadRiderOffset(XmlNodePtrConst node, + HorseInfo *const currentInfo) +{ + const std::string dirName = XML::getProperty(node, + "direction", ""); + const int dir = parseDirectionName(dirName); + if (dir == -1) + { + reportAlways("Wrong or missing horse rider direcion: %s", + dirName.c_str()); + return; + } + HorseOffset &offset = currentInfo->offsets[dir]; + offset.riderOffsetX = XML::getProperty(node, + "riderOffsetX", 0); + offset.riderOffsetY = XML::getProperty(node, + "riderOffsetY", 0); +} + void HorseDB::loadXmlFile(const std::string &fileName, const SkipError skipError) { @@ -131,44 +191,58 @@ void HorseDB::loadXmlFile(const std::string &fileName, if (!currentInfo) continue; - currentInfo->upOffsetX = XML::getProperty(horseNode, - "upOffsetX", 0); - currentInfo->upOffsetY = XML::getProperty(horseNode, - "upOffsetY", 0); - currentInfo->downOffsetX = XML::getProperty(horseNode, - "downOffsetX", 0); - currentInfo->downOffsetY = XML::getProperty(horseNode, - "downOffsetY", 0); - currentInfo->riderOffsetX = XML::getProperty(horseNode, - "riderOffsetX", 0); - currentInfo->riderOffsetY = XML::getProperty(horseNode, - "riderOffsetY", 0); - for_each_xml_child_node(spriteNode, horseNode) { - if (!XmlHaveChildContent(spriteNode)) - continue; - - if (xmlNameEqual(spriteNode, "sprite")) - loadSprite(downSprites) - if (xmlNameEqual(spriteNode, "down")) loadDownSprites(spriteNode, currentInfo); else if (xmlNameEqual(spriteNode, "up")) loadUpSprites(spriteNode, currentInfo); + else if (xmlNameEqual(spriteNode, "offset")) + loadRiderOffset(spriteNode, currentInfo); } mHorseInfos[id] = currentInfo; } } +static void loadOffset(XmlNodePtrConst node, + HorseInfo *const currentInfo, + const bool isUp) +{ + const std::string dirName = XML::getProperty(node, + "direction", ""); + const int dir = parseDirectionName(dirName); + if (dir == -1) + { + reportAlways("Wrong or missing horse direcion: %s", + dirName.c_str()); + return; + } + HorseOffset &offset = currentInfo->offsets[dir]; + if (isUp) + { + offset.upOffsetX = XML::getProperty(node, + "horseOffsetX", 0); + offset.upOffsetY = XML::getProperty(node, + "horseOffsetY", 0); + } + else + { + offset.downOffsetX = XML::getProperty(node, + "horseOffsetX", 0); + offset.downOffsetY = XML::getProperty(node, + "horseOffsetY", 0); + } +} + static void loadDownSprites(XmlNodePtrConst parentNode, HorseInfo *const currentInfo) { for_each_xml_child_node(spriteNode, parentNode) { + if (xmlNameEqual(spriteNode, "offset")) + loadOffset(spriteNode, currentInfo, false); if (!XmlHaveChildContent(spriteNode)) continue; - if (xmlNameEqual(spriteNode, "sprite")) loadSprite(downSprites) } @@ -179,9 +253,10 @@ static void loadUpSprites(XmlNodePtrConst parentNode, { for_each_xml_child_node(spriteNode, parentNode) { + if (xmlNameEqual(spriteNode, "offset")) + loadOffset(spriteNode, currentInfo, true); if (!XmlHaveChildContent(spriteNode)) continue; - if (xmlNameEqual(spriteNode, "sprite")) loadSprite(upSprites) } diff --git a/src/resources/horseinfo.h b/src/resources/horseinfo.h index 4ff1dfb09..97bcdcc6f 100644 --- a/src/resources/horseinfo.h +++ b/src/resources/horseinfo.h @@ -24,6 +24,8 @@ #include "resources/sprite/spritereference.h" +#include "resources/horseoffset.h" + #include "localconsts.h" struct HorseInfo final @@ -31,24 +33,24 @@ struct HorseInfo final HorseInfo() : downSprites(), upSprites(), - downOffsetX(0), - downOffsetY(0), - upOffsetX(0), - upOffsetY(0), - riderOffsetX(0), - riderOffsetY(0) - { } + offsets() + { + for (int f = 0; f < 10; f ++) + { + offsets[f].upOffsetX = 0; + offsets[f].upOffsetY = 0; + offsets[f].downOffsetX = 0; + offsets[f].downOffsetY = 0; + offsets[f].riderOffsetX = 0; + offsets[f].riderOffsetY = 0; + } + } A_DELETE_COPY(HorseInfo) std::vector downSprites; std::vector upSprites; - int downOffsetX; - int downOffsetY; - int upOffsetX; - int upOffsetY; - int riderOffsetX; - int riderOffsetY; + HorseOffset offsets[10]; // by direction }; #endif // RESOURCES_HORSEINFO_H diff --git a/src/resources/horseoffset.h b/src/resources/horseoffset.h new file mode 100644 index 000000000..be7a3e76b --- /dev/null +++ b/src/resources/horseoffset.h @@ -0,0 +1,47 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011-2016 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 . + */ + +#ifndef RESOURCES_HORSEOFFSET_H +#define RESOURCES_HORSEOFFSET_H + +#include "localconsts.h" + +struct HorseOffset final +{ + HorseOffset() : + upOffsetX(0), + upOffsetY(0), + downOffsetX(0), + downOffsetY(0), + riderOffsetX(0), + riderOffsetY(0) + { } + + A_DELETE_COPY(HorseOffset) + + int upOffsetX; + int upOffsetY; + int downOffsetX; + int downOffsetY; + int riderOffsetX; + int riderOffsetY; +}; + +#endif // RESOURCES_HORSEOFFSET_H -- cgit v1.2.3-70-g09d2