Mana
Loading...
Searching...
No Matches
animation.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "resources/animation.h"
23
24#include "dye.h"
25#include "game.h"
26#include "log.h"
27#include "resourcemanager.h"
28
29void Animation::addFrame(Image *image, int delay, int offsetX, int offsetY)
30{
31 auto &frame = mFrames.emplace_back();
32 frame.image = image;
33 frame.delay = delay;
34 frame.offsetX = offsetX;
35 frame.offsetY = offsetY;
36
37 mDuration += delay;
38}
39
41{
42 addFrame(nullptr, 0, 0, 0);
43}
44
45bool Animation::isTerminator(const Frame &candidate)
46{
47 return candidate.image == nullptr;
48}
49
50Animation Animation::fromXML(XML::Node node, const std::string &dyePalettes)
51{
52 Animation animation;
53
54 std::string imagePath = node.getProperty("imageset", std::string());
55
56 // Instanciate the dye coloration.
57 Dye::instantiate(imagePath, dyePalettes);
58
60 imagePath,
61 node.getProperty("width", 0),
62 node.getProperty("height", 0)
63 );
64
65 if (!imageSet)
66 return animation;
67
68 // Get animation frames
69 for (auto frameNode : node.children())
70 {
71 int delay = frameNode.getProperty("delay", 0);
72 int offsetX = frameNode.getProperty("offsetX", 0);
73 int offsetY = frameNode.getProperty("offsetY", 0);
74 Game *game = Game::instance();
75 if (game)
76 {
77 offsetX -= imageSet->getWidth() / 2 - game->getCurrentTileWidth() / 2;
78 offsetY -= imageSet->getHeight() - game->getCurrentTileHeight();
79 }
80
81 if (frameNode.name() == "frame")
82 {
83 int index = frameNode.getProperty("index", -1);
84
85 if (index < 0)
86 {
87 Log::info("No valid value for 'index'");
88 continue;
89 }
90
91 Image *img = imageSet->get(index);
92
93 if (!img)
94 {
95 Log::info("No image at index %d", index);
96 continue;
97 }
98
99 animation.addFrame(img, delay, offsetX, offsetY);
100 }
101 else if (frameNode.name() == "sequence")
102 {
103 int start = frameNode.getProperty("start", -1);
104 int end = frameNode.getProperty("end", -1);
105
106 if (start < 0 || end < 0)
107 {
108 Log::info("No valid value for 'start' or 'end'");
109 continue;
110 }
111
112 while (end >= start)
113 {
114 Image *img = imageSet->get(start);
115
116 if (!img)
117 {
118 Log::info("No image at index %d", start);
119 continue;
120 }
121
122 animation.addFrame(img, delay, offsetX, offsetY);
123 start++;
124 }
125 }
126 else if (frameNode.name() == "end")
127 {
128 animation.addTerminator();
129 }
130 }
131
132 animation.mImageSet = imageSet;
133
134 return animation;
135}
An animation consists of several frames, each with their own delay and offset.
Definition animation.h:47
static bool isTerminator(const Frame &phase)
Determines whether the given animation frame is a terminator.
Definition animation.cpp:45
void addFrame(Image *image, int delay, int offsetX, int offsetY)
Appends a new animation at the end of the sequence.
Definition animation.cpp:29
int mDuration
Definition animation.h:91
std::vector< Frame > mFrames
Definition animation.h:90
ResourceRef< ImageSet > mImageSet
Definition animation.h:89
static Animation fromXML(XML::Node node, const std::string &dyePalettes={})
Loads an animation from XML.
Definition animation.cpp:50
void addTerminator()
Appends an animation terminator that states that the animation should not loop.
Definition animation.cpp:40
static void instantiate(std::string &target, const std::string &palettes)
Fills the blank in a dye placeholder with some palette names.
Definition dye.cpp:252
The main class responsible for running the game.
Definition game.h:37
int getCurrentTileWidth() const
Convenience functions used to get the current tile width and height.
Definition game.cpp:917
int getCurrentTileHeight() const
Definition game.cpp:925
static Game * instance()
Provides access to the game instance.
Definition game.h:53
Defines a class for loading and storing images.
Definition image.h:45
static ResourceManager * getInstance()
Returns an instance of the class, creating one if it does not already exist.
ResourceRef< ImageSet > getImageSet(const std::string &imagePath, int w, int h)
Loads a image set based on the image referenced by the given path and the supplied sprite sizes.
int getProperty(const char *name, int def) const
Definition xml.h:144
Children children() const
Definition xml.h:97
void info(const char *log_text,...) LOG_PRINTF_ATTR
A single frame in an animation, with a delay and an offset.
Definition animation.h:35
Image * image
Definition animation.h:36