diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/animatedsprite.h | 17 | ||||
-rw-r--r-- | src/animatedsprite_unittest.cc | 151 | ||||
-rw-r--r-- | src/resources/animation.h | 13 |
4 files changed, 178 insertions, 4 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 662ae3f74..fb379d024 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -849,6 +849,7 @@ manaplus_SOURCES += \ if ENABLE_UNITTESTS manaplus_CXXFLAGS += -DUNITTESTS manaplus_SOURCES += \ + animatedsprite_unittest.cc \ gui/sdlfont_unittest.cc \ gui/widgets/browserbox_unittest.cc \ utils/stringutils_unittest.cc diff --git a/src/animatedsprite.h b/src/animatedsprite.h index 5a7dbde56..60df2f6e4 100644 --- a/src/animatedsprite.h +++ b/src/animatedsprite.h @@ -107,6 +107,23 @@ class AnimatedSprite final : public Sprite static void setEnableCache(const bool b) { mEnableCache = b; } +#ifdef UNITTESTS + SpriteDef *getSprite() + { return mSprite; } + + Frame *getFrame() + { return mFrame; } + + Animation *getAnimation() + { return mAnimation; } + + unsigned int getFrameIndex() + { return mFrameIndex; } + + unsigned int getFrameTime() + { return mFrameTime; } +#endif + private: bool updateCurrentAnimation(const unsigned int dt); diff --git a/src/animatedsprite_unittest.cc b/src/animatedsprite_unittest.cc new file mode 100644 index 000000000..d429f1329 --- /dev/null +++ b/src/animatedsprite_unittest.cc @@ -0,0 +1,151 @@ +/* + * 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 "animatedsprite.h" + +#include "client.h" + +#include "gui/theme.h" + +#include "resources/animation.h" +#include "resources/resourcemanager.h" +#include "resources/sdlimagehelper.h" + +#include "gtest/gtest.h" + +#include "utils/physfstools.h" + +#include "debug.h" + +static void init() +{ + PHYSFS_init("manaplus"); + dirSeparator = "/"; + XML::initXML(); + SDL_Init(SDL_INIT_VIDEO); + logger = new Logger(); + ResourceManager *resman = ResourceManager::getInstance(); + resman->addToSearchPath("data", false); + resman->addToSearchPath("../data", false); + + imageHelper = new SDLImageHelper(); + SDL_SetVideoMode(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE); + Theme *theme = Theme::instance(); +} + +TEST(AnimatedSprite, basic) +{ + Client::Options options; + Client client(options); + + init(); + AnimatedSprite *sprite = AnimatedSprite::load( + "graphics/sprites/error.xml", 0); + sprite->play(SpriteAction::DEFAULT); + + EXPECT_NE(nullptr, sprite); + EXPECT_NE(nullptr, sprite->getSprite()); + EXPECT_NE(nullptr, sprite->getAnimation()); + EXPECT_NE(nullptr, sprite->getFrame()); + EXPECT_EQ(0, sprite->getFrameIndex()); + EXPECT_EQ(0, sprite->getFrameTime()); + EXPECT_EQ(false, sprite->update(1)); + EXPECT_EQ(0, sprite->getFrameTime()); + EXPECT_EQ(false, sprite->update(11)); + EXPECT_EQ(10, sprite->getFrameTime()); + EXPECT_EQ(0, sprite->getFrameIndex()); +} + +TEST(AnimatedSprite, basic2) +{ + Client::Options options; + Client client(options); + + init(); + AnimatedSprite *sprite = AnimatedSprite::load( + "graphics/sprites/test.xml", 0); + sprite->play(SpriteAction::STAND); + + EXPECT_EQ(10, sprite->getAnimation()->getFrames().size()); + + EXPECT_NE(nullptr, sprite); + + EXPECT_EQ(false, sprite->update(1)); + EXPECT_EQ(0, sprite->getFrameTime()); + EXPECT_EQ(10, sprite->getFrame()->delay); + + EXPECT_EQ(false, sprite->update(1 + 10)); + EXPECT_EQ(0, sprite->getFrameIndex()); + EXPECT_EQ(10, sprite->getFrameTime()); + + EXPECT_EQ(true, sprite->update(1 + 10 + 5)); + EXPECT_EQ(1, sprite->getFrameIndex()); + EXPECT_EQ(5, sprite->getFrameTime()); + + EXPECT_EQ(false, sprite->update(1 + 10 + 5)); + EXPECT_EQ(1, sprite->getFrameIndex()); + EXPECT_EQ(5, sprite->getFrameTime()); + + EXPECT_EQ(false, sprite->update(1 + 10 + 20)); + EXPECT_EQ(1, sprite->getFrameIndex()); + EXPECT_EQ(20, sprite->getFrameTime()); + + EXPECT_EQ(true, sprite->update(1 + 10 + 20 + 1)); + EXPECT_EQ(2, sprite->getFrameIndex()); + EXPECT_EQ(1, sprite->getFrameTime()); + + EXPECT_EQ(false, sprite->update(1 + 10 + 20 + 10)); + EXPECT_EQ(2, sprite->getFrameIndex()); + EXPECT_EQ(10, sprite->getFrameTime()); + + EXPECT_EQ(true, sprite->update(1 + 10 + 20 + 10 + 1)); + EXPECT_EQ(4, sprite->getFrameIndex()); + EXPECT_EQ(1, sprite->getFrameTime()); + + EXPECT_EQ(false, sprite->update(1 + 10 + 20 + 10 + 25)); + EXPECT_EQ(4, sprite->getFrameIndex()); + EXPECT_EQ(25, sprite->getFrameTime()); + + EXPECT_EQ(true, sprite->update(1 + 10 + 20 + 10 + 25 + 1)); + EXPECT_EQ(6, sprite->getFrameIndex()); + EXPECT_EQ(1, sprite->getFrameTime()); + + EXPECT_EQ(true, sprite->update(1 + 10 + 20 + 10 + 25 + 10 + 1)); + EXPECT_EQ(8, sprite->getFrameIndex()); + EXPECT_EQ(1, sprite->getFrameTime()); + + EXPECT_EQ(true, sprite->update(1 + 10 + 20 + 10 + 25 + 10 + 10 + 1)); + EXPECT_EQ(4, sprite->getFrameIndex()); + EXPECT_EQ(1, sprite->getFrameTime()); + + AnimatedSprite *sprite2 = AnimatedSprite::load( + "graphics/sprites/test.xml", 0); + sprite2->play(SpriteAction::SIT); + + EXPECT_EQ(false, sprite2->update(1)); + EXPECT_EQ(2, sprite2->getAnimation()->getFrames().size()); + EXPECT_EQ(0, sprite2->getFrameTime()); + EXPECT_EQ(85, sprite2->getFrame()->delay); + + EXPECT_EQ(true, sprite2->update(1 + 10 + 20 + 10 + 25 + 10 + 10 + 1)); + EXPECT_EQ(1, sprite2->getFrameIndex()); + EXPECT_EQ(1, sprite2->getFrameTime()); +} + diff --git a/src/resources/animation.h b/src/resources/animation.h index 09738620c..9851e1c24 100644 --- a/src/resources/animation.h +++ b/src/resources/animation.h @@ -95,16 +95,21 @@ class Animation final void setLastFrameDelay(const int delay); + typedef std::vector<Frame> Frames; + typedef Frames::iterator FramesIter; + typedef Frames::reverse_iterator FramesRevIter; + +#ifdef UNITTESTS + Frames &getFrames() + { return mFrames; } +#endif + /** * Determines whether the given animation frame is a terminator. */ static bool isTerminator(const Frame &phase) A_WARN_UNUSED; protected: - typedef std::vector<Frame> Frames; - typedef Frames::iterator FramesIter; - typedef Frames::reverse_iterator FramesRevIter; - Frames mFrames; int mDuration; }; |