Skip to content

Commit

Permalink
Fixed sprite deletion leaving stale references in animations.
Browse files Browse the repository at this point in the history
- Now, deleting a sprite will delete all frames that use it.
  • Loading branch information
Espyo committed Aug 14, 2024
1 parent 80172b4 commit 7a849f1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Source/documents/Todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ Current tasks (tasks being worked on, but not yet committed)

Next tasks (roughly sorted most important first)
--- To fix ---
lockpick man's resize bug
Looks like editing some sprites' scales and then trying to preview an interpolated animation makes things weird
I think it might actually happen when you delete a sprite that was in use in animations


--- 0.26 ---
https://github.com/Helodity/Pikifen/tree/throwing-improvements
Expand Down Expand Up @@ -642,6 +640,8 @@ Small tasks (for when I'm feeling lazy)
Cleanup
Remove magic numbers and magic strings (make numbers and strings into constants)
Try to refactor the mission strategy patterns to also work for the area editor logic
Replace the ++i in for loops with i++
Replace all instances of "number" when in reality it should be "index"


Misc things (so I don't forget)
Expand Down
23 changes: 23 additions & 0 deletions Source/source/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,29 @@ void animation_database::destroy() {
}


/**
* @brief Deletes a sprite, adjusting any animations that use it.
*
* @param idx Sprite index.
*/
void animation_database::delete_sprite(size_t idx) {
for(size_t a = 0; a < animations.size(); ++a) {
animation* a_ptr = animations[a];

for(size_t f = 0; f < a_ptr->frames.size();) {
frame* f_ptr = &a_ptr->frames[f];
if(f_ptr->sprite_idx == idx) {
a_ptr->frames.erase(a_ptr->frames.begin() + f);
} else {
++f;
}
}
}

sprites.erase(sprites.begin() + idx);
}


/**
* @brief Fills each frame's sound index cache variable, where applicable.
*
Expand Down
1 change: 1 addition & 0 deletions Source/source/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class animation_database : public content {
void create_conversions(
vector<std::pair<size_t, string> > conversions, const data_node* file
);
void delete_sprite(size_t idx);
void fill_sound_idx_caches(mob_type* mt_ptr);
void fix_body_part_pointers();
void load_from_data_node(data_node* node);
Expand Down
6 changes: 4 additions & 2 deletions Source/source/game_states/animation_editor/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,7 @@ void animation_editor::process_gui_panel_sprite() {
) {
string deleted_sprite_name = cur_sprite->name;
size_t nr = anims.find_sprite(deleted_sprite_name);
anims.sprites.erase(anims.sprites.begin() + nr);
anims.delete_sprite(nr);
if(anims.sprites.empty()) {
cur_sprite = nullptr;
cur_hitbox = nullptr;
Expand All @@ -1816,7 +1816,9 @@ void animation_editor::process_gui_panel_sprite() {
set_status("Deleted sprite \"" + deleted_sprite_name + "\".");
}
set_tooltip(
"Delete the current sprite."
"Delete the current sprite.\n"
"Any frame that makes use of this sprite\n"
"will be deleted from its animation."
);
}

Expand Down

0 comments on commit 7a849f1

Please sign in to comment.