47 Log::warn(
"No action \"%s\" defined!", action.c_str());
56 std::string::size_type pos = animationFile.find(
'|');
58 if (pos != std::string::npos)
59 palettes = animationFile.substr(pos + 1);
67 if (!rootNode || rootNode.
name() !=
"sprite")
69 Log::info(
"Error, failed to parse %s", animationFile.c_str());
73 if (animationFile != errorFile)
75 return load(errorFile, 0);
82 def->loadSprite(rootNode, variant, palettes);
83 def->substituteActions();
113 const std::string &palettes)
116 const int variantCount = spriteNode.
getProperty(
"variants", 0);
117 int variant_offset = 0;
119 if (variantCount > 0 && variant < variantCount)
122 variant * spriteNode.
getProperty(
"variant_offset", 0);
125 for (
auto node : spriteNode.
children())
127 if (node.name() ==
"imageset")
131 else if (node.name() ==
"action")
135 else if (node.name() ==
"include")
144 const std::string name = node.
getProperty(
"name",
"");
153 std::string imageSrc = node.
getProperty(
"src",
"");
157 auto imageSet = resman->
getImageSet(imageSrc, width, height);
160 Log::critical(
strprintf(
"Couldn't load imageset (%s)!",
164 imageSet->setOffsetX(node.
getProperty(
"offsetX", 0));
165 imageSet->setOffsetY(node.
getProperty(
"offsetY", 0));
171 const std::string actionName = node.
getProperty(
"name",
"");
172 const std::string imageSetName = node.
getProperty(
"imageset",
"");
177 Log::warn(
"imageset \"%s\" not defined in %s",
178 imageSetName.c_str(),
getIdPath().c_str());
183 if (actionName == SpriteAction::INVALID)
185 Log::warn(
"Unknown action \"%s\" defined in %s",
186 actionName.c_str(),
getIdPath().c_str());
189 auto *action =
new Action;
195 mActions[SpriteAction::DEFAULT] = action;
199 for (
auto animationNode : node.
children())
201 if (animationNode.name() ==
"animation")
203 loadAnimation(animationNode, action, imageSet, variant_offset);
212 const std::string directionName =
218 Log::warn(
"Unknown direction \"%s\" used in %s",
219 directionName.c_str(),
getIdPath().c_str());
227 for (
auto frameNode : animationNode.
children())
229 const int delay = frameNode.getProperty(
"delay",
231 int offsetX = frameNode.getProperty(
"offsetX", 0) +
233 int offsetY = frameNode.getProperty(
"offsetY", 0) +
236 if (frameNode.name() ==
"frame")
238 const int index = frameNode.getProperty(
"index", -1);
246 Image *img = imageSet->
get(index + variant_offset);
250 Log::info(
"No image at index %d", index + variant_offset);
254 animation->addFrame(img, delay, offsetX, offsetY);
256 else if (frameNode.name() ==
"sequence")
258 int start = frameNode.getProperty(
"start", -1);
259 const int end = frameNode.getProperty(
"end", -1);
261 if (start < 0 || end < 0)
263 Log::info(
"No valid value for 'start' or 'end'");
269 Image *img = imageSet->
get(start + variant_offset);
273 Log::info(
"No image at index %d", start + variant_offset);
277 animation->addFrame(img, delay, offsetX, offsetY);
281 else if (frameNode.name() ==
"end")
283 animation->addTerminator();
290 std::string filename = includeNode.
getProperty(
"file",
"");
292 if (filename.empty())
298 Log::info(
"Error, Tried to include %s which already is included.",
307 if (!rootNode || rootNode.
name() !=
"sprite")
309 Log::info(
"Error, no sprite root node in %s", filename.c_str());
319 std::set<Action*> actions;
322 actions.insert(action.second);
325 for (
auto action : actions)
333 if (direction.empty() || direction ==
"default")
335 if (direction ==
"up")
337 if (direction ==
"left")
339 if (direction ==
"right")
341 if (direction ==
"down")
An action consists of several animations, one for each direction.
void setAnimation(int direction, Animation *animation)
An animation consists of several frames, each with their own delay and offset.
std::string getStringValue(const std::string &key) const
static void instantiate(std::string &target, const std::string &palettes)
Fills the blank in a dye placeholder with some palette names.
Stores a set of subimages originating from a single image.
Image * get(size_t i) const
Defines a class for loading and storing images.
A class for loading and managing resources.
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.
const std::string & getIdPath() const
Return the path identifying this resource.
Defines a class to load an animation.
void loadAnimation(XML::Node animationNode, Action *action, ImageSet *imageSet, int variant_offset)
Loads an animation element.
void loadAction(XML::Node node, int variant_offset)
Loads an action element.
void loadSprite(XML::Node spriteNode, int variant, const std::string &palettes=std::string())
Loads a sprite element.
void substituteActions()
Complete missing actions by copying existing ones.
std::map< std::string, Action * > mActions
std::map< std::string, ResourceRef< ImageSet > > mImageSets
void includeSprite(XML::Node includeNode)
Include another sprite into this one.
void loadImageSet(XML::Node node, const std::string &palettes)
Loads an imageset element.
static SpriteDirection makeSpriteDirection(const std::string &direction)
Converts a string into a SpriteDirection enum.
void substituteAction(std::string complete, std::string with)
When there are no animations defined for the action "complete", its animations become a copy of those...
static SpriteDef * load(const std::string &file, int variant)
Loads a sprite definition file.
Action * getAction(const std::string &action) const
Returns the specified action.
A helper class for parsing an XML document, which also cleans it up again (RAII).
Node rootNode() const
Returns the root node of the document (or NULL if there was a load error).
std::string_view name() const
int getProperty(const char *name, int def) const
Children children() const
Configuration paths
XML default paths information reader.
void warn(const char *log_text,...) LOG_PRINTF_ATTR
void info(const char *log_text,...) LOG_PRINTF_ATTR
const int DEFAULT_FRAME_DELAY
std::set< std::string > processedFiles
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.