Skip to content

Commit

Permalink
Show sprite file name for most related errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tobbi committed Aug 5, 2024
1 parent 1fbfd10 commit 4fbdd4a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/sprite/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Sprite::set_action(const std::string& name, int loops)

const SpriteData::Action* newaction = m_data.get_action(name);
if (!newaction) {
log_warning << "Action '" << name << "' not found." << std::endl;
log_warning << m_data.get_sprite_path() << ": Action '" << name << "' not found." << std::endl;
return;
}

Expand Down
34 changes: 21 additions & 13 deletions src/sprite/sprite_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ SpriteData::Action::Action() :
{
}

SpriteData::SpriteData(const ReaderMapping& mapping) :
SpriteData::SpriteData(const ReaderMapping& mapping, const std::string& image) :
actions(),
name()
name(),
m_sprite_path(image)
{
auto iter = mapping.get_iter();
while (iter.next())
Expand All @@ -60,20 +61,21 @@ SpriteData::SpriteData(const ReaderMapping& mapping) :
} else if (iter.get_key() == "action") {
parse_action(iter.as_mapping());
} else {
log_warning << "Unknown sprite field: " << iter.get_key() << std::endl;
log_warning << m_sprite_path << ": Unknown sprite field: " << iter.get_key() << std::endl;
}
}
if (actions.empty())
throw std::runtime_error("Error: Sprite without actions.");
throw std::runtime_error(m_sprite_path + ": Error: Sprite without actions.");
}

SpriteData::SpriteData(const std::string& image) :
actions(),
name()
name(),
m_sprite_path(image)
{
auto surface = Surface::from_file(image);
if (!TextureManager::current()->last_load_successful())
throw std::runtime_error("Cannot load image.");
throw std::runtime_error(m_sprite_path + ": Cannot load image.");

auto action = create_action_from_surface(surface);
action->name = "default";
Expand All @@ -82,7 +84,8 @@ SpriteData::SpriteData(const std::string& image) :

SpriteData::SpriteData() :
actions(),
name()
name(),
m_sprite_path()
{
auto surface = Surface::from_texture(TextureManager::current()->create_dummy_texture());
auto action = create_action_from_surface(surface);
Expand Down Expand Up @@ -110,7 +113,7 @@ SpriteData::parse_action(const ReaderMapping& mapping)
if (!mapping.get("name", action->name))
{
if (!actions.empty())
throw std::runtime_error("If there are more than one action, they need names!");
throw std::runtime_error(m_sprite_path + ": If there are more than one action, they need names!");
}

std::vector<float> hitbox;
Expand All @@ -128,7 +131,7 @@ SpriteData::parse_action(const ReaderMapping& mapping)
break;

default:
throw std::runtime_error("hitbox should specify 2/4 coordinates");
throw std::runtime_error(m_sprite_path + ": hitbox should specify 2/4 coordinates");
}
}
mapping.get("unisolid", action->hitbox_unisolid);
Expand All @@ -141,7 +144,7 @@ SpriteData::parse_action(const ReaderMapping& mapping)
{
if (action->loop_frame < 1)
{
log_warning << "'loop-frame' of action '" << action->name << "' in sprite '" << name << "' set to a value below 1." << std::endl;
log_warning << m_sprite_path << ": 'loop-frame' of action '" << action->name << "' in sprite '" << name << "' set to a value below 1." << std::endl;
action->loop_frame = 1;
}
}
Expand All @@ -160,6 +163,7 @@ SpriteData::parse_action(const ReaderMapping& mapping)
if (act_tmp == nullptr)
{
std::ostringstream msg;
msg << m_sprite_path << ": ";
msg << "Could not mirror action. Action not found: \"" << mirror_action << "\"\n"
<< "Mirror actions must be defined after the real one!";
throw std::runtime_error(msg.str());
Expand Down Expand Up @@ -207,6 +211,7 @@ SpriteData::parse_action(const ReaderMapping& mapping)
if (act_tmp == nullptr)
{
std::ostringstream msg;
msg << m_sprite_path << ": ";
msg << "Could not flip action. Action not found: \"" << flip_action << "\"\n"
<< "Flip actions must be defined after the real one!";
throw std::runtime_error(msg.str());
Expand Down Expand Up @@ -253,6 +258,7 @@ SpriteData::parse_action(const ReaderMapping& mapping)
if (act_tmp == nullptr)
{
std::ostringstream msg;
msg << m_sprite_path << ": ";
msg << "Could not clone action. Action not found: \"" << clone_action << "\"\n"
<< "Clone actions must be defined after the real one!";
throw std::runtime_error(msg.str());
Expand Down Expand Up @@ -286,15 +292,15 @@ SpriteData::parse_action(const ReaderMapping& mapping)
{
if (iter.get_key() != "region")
{
log_warning << "Unknown field '" << iter.get_key() << "' under 'regions'." << std::endl;
log_warning << m_sprite_path << ": Unknown field '" << iter.get_key() << "' under 'regions'." << std::endl;
continue;
}

const auto& sx = iter.as_mapping().get_sexp();
const auto& arr = sx.as_array();
if (arr.size() != 6)
{
log_warning << "(region IMAGE_FILE X Y WIDTH HEIGHT) tag malformed: " << sx << std::endl;
log_warning << m_sprite_path << ": (region IMAGE_FILE X Y WIDTH HEIGHT) tag malformed: " << sx << std::endl;
continue;
}

Expand Down Expand Up @@ -343,6 +349,7 @@ SpriteData::parse_action(const ReaderMapping& mapping)
else
{
std::stringstream msg;
msg << m_sprite_path << ":";
msg << "Sprite '" << name << "' unknown tag in 'surfaces' << " << i.get_name();
throw std::runtime_error(msg.str());
}
Expand All @@ -362,6 +369,7 @@ SpriteData::parse_action(const ReaderMapping& mapping)
else
{
std::stringstream msg;
msg << m_sprite_path << ": ";
msg << "Sprite '" << name << "' contains no images in action '"
<< action->name << "'.";
throw std::runtime_error(msg.str());
Expand All @@ -372,7 +380,7 @@ SpriteData::parse_action(const ReaderMapping& mapping)
const int frames = static_cast<int>(action->surfaces.size());
if (action->loop_frame > frames && frames > 0)
{
log_warning << "'loop-frame' of action '" << action->name << "' in sprite '" << name << "' not-in-range of total frames." << std::endl;
log_warning << m_sprite_path << ": 'loop-frame' of action '" << action->name << "' in sprite '" << name << "' not-in-range of total frames." << std::endl;
action->loop_frame = 1;
}

Expand Down
8 changes: 7 additions & 1 deletion src/sprite/sprite_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SpriteData final
* Sprite from data.
* `mapping` has to be a pointer to data in the form of "((hitbox 5 10 0 0) ...)".
*/
SpriteData(const ReaderMapping& mapping);
SpriteData(const ReaderMapping& mapping, const std::string& image = nullptr);
/** Single-image sprite */
SpriteData(const std::string& image);
/** Dummy texture sprite */
Expand All @@ -45,6 +45,11 @@ class SpriteData final
return name;
}

const std::string& get_sprite_path() const
{
return m_sprite_path;
}

private:
struct Action
{
Expand Down Expand Up @@ -98,6 +103,7 @@ class SpriteData final
private:
Actions actions;
std::string name;
std::string m_sprite_path;
};

#endif
Expand Down
2 changes: 1 addition & 1 deletion src/sprite/sprite_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ SpriteManager::load(const std::string& filename)
}
else
{
sprite_data = std::make_unique<SpriteData>(root.get_mapping());
sprite_data = std::make_unique<SpriteData>(root.get_mapping(), filename);
}
}
else
Expand Down

0 comments on commit 4fbdd4a

Please sign in to comment.