Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes AnimationSet(std::string filename) uninitialized mFileName warning #965

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion NAS2D/Resource/AnimationSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,51 @@ bool AnimationSet::Frame::isStopFrame() const
}


AnimationSet::AnimationSet(std::string fileName) : AnimationSet{processXml(std::move(fileName), animationImageCache)}
AnimationSet::AnimationSet(std::string fileName) :
mFileName{std::move(fileName)}
{
try
{
auto& filesystem = Utility<Filesystem>::get();
const auto basePath = filesystem.parentPath(mFileName);

Xml::XmlDocument xmlDoc;
xmlDoc.parse(filesystem.read(mFileName).c_str());

if (xmlDoc.error())
{
throw std::runtime_error("Sprite file has malformed XML: Row: " + std::to_string(xmlDoc.errorRow()) + " Column: " + std::to_string(xmlDoc.errorCol()) + " : " + xmlDoc.errorDesc());
}

// Find the Sprite node.
const auto* xmlRootElement = xmlDoc.firstChildElement("sprite");
if (!xmlRootElement)
{
throw std::runtime_error("Sprite file does not contain required <sprite> tag");
}

// Get the Sprite version.
const auto version = xmlRootElement->attribute("version");
if (version.empty())
{
throw std::runtime_error("Sprite file's root element does not specify a version");
}
if (version != SPRITE_VERSION)
{
throw std::runtime_error("Sprite version mismatch. Expected: " + std::string{SPRITE_VERSION} + " Actual: " + versionString());
}

// Note:
// Here instead of going through each element and calling a processing function to handle
// it, we just iterate through all nodes to find sprite sheets. This allows us to define
// image sheets anywhere in the sprite file.
mImageSheetMap = processImageSheets(basePath, xmlRootElement, animationImageCache);
mActions = processActions(mImageSheetMap, xmlRootElement, animationImageCache);
}
catch (const std::runtime_error& error)
{
throw std::runtime_error("Error parsing Sprite file: " + mFileName + "\nError: " + error.what());
}
}


Expand Down
2 changes: 1 addition & 1 deletion NAS2D/Resource/AnimationSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace NAS2D
const std::vector<Frame>& frames(const std::string& actionName) const;

private:
std::string mFileName;
std::string mFileName{"UNNAMED_ANIMATIONSET"};
std::map<std::string, std::string> mImageSheetMap;
std::map<std::string, std::vector<Frame>> mActions;
};
Expand Down