From 77643a4bcd0263e75bd96d5c2e0880a43de07586 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 4 Feb 2026 00:04:17 -0500 Subject: [PATCH] Can now specify a background to render the sprite on. --- assets/animate2.json | 23 +++++++++++++++ tools/animator.cpp | 66 ++++++++++++++++++++++++++++++++++++++------ tools/animator.hpp | 6 ++-- 3 files changed, 85 insertions(+), 10 deletions(-) diff --git a/assets/animate2.json b/assets/animate2.json index 119111a..1e0131d 100644 --- a/assets/animate2.json +++ b/assets/animate2.json @@ -21,5 +21,28 @@ "toggled": false, "looped": true } + }, + "rat_king_meth": { + "sheet": { + "frames": 2, + "frame_width": 720, + "frame_height": 720 + }, + "sequence": { + "frames": [0, 1], + "durations": [33, 33] + }, + "transform": { + "min_x": 0.6, + "min_y": 0.6, + "max_x": 0.8, + "max_y": 0.8, + "simple": false, + "flipped": false, + "ease_rate": 5.0, + "scaled": true, + "toggled": false, + "looped": true + } } } diff --git a/tools/animator.cpp b/tools/animator.cpp index bfd0e07..7f18cd9 100644 --- a/tools/animator.cpp +++ b/tools/animator.cpp @@ -8,6 +8,16 @@ #include "constants.hpp" #include "animate2.hpp" #include "tools/animator.hpp" +#include + +/* + * TODO: + * + * - Setting a background. + * - Options parsing for that. + * - Easing functions from strings in json. + * - Map of animation forms. + */ using namespace std::chrono_literals; @@ -15,9 +25,11 @@ bool YES_SYNC=true; namespace animator { - void FSM::init(const std::string &sprite_name) { + void FSM::init(const std::string &sprite_name, const std::string &anim_name, const std::string &background) { $timer.start(); $sprite_name = sprite_name; + $anim_name = anim_name; + $background = background; // this loads the animation reload(); @@ -26,7 +38,7 @@ namespace animator { $window = sf::RenderWindow(sf::VideoMode(new_size), "Animation Crafting Tool"); $window.setPosition({0,0}); - $ui.init($sprite_name, new_size.x, new_size.y); + $ui.init($sprite_name, $background, new_size.x, new_size.y); $sprite = $ui.get_sprite(); // need to keep these aroung @@ -37,7 +49,6 @@ namespace animator { $window.setVerticalSyncEnabled(VSYNC); if(FRAME_LIMIT) $window.setFramerateLimit(FRAME_LIMIT); } - } void FSM::event(Event ev, std::any data) { @@ -111,7 +122,7 @@ namespace animator { } void FSM::reload() { - $anim = animate2::load("assets/animate2.json", $sprite_name); + $anim = animate2::load("assets/animate2.json", $anim_name); $anim.play(); $last_mod_time = std::filesystem::last_write_time("assets/animate2.json"); } @@ -153,10 +164,16 @@ namespace animator { return !in_state(State::END); } - void UI::init(const std::string& sprite_name, int width, int height) { + void UI::init(const std::string& sprite_name, const std::string& background, int width, int height) { $ui.position(0,0, width, height); $ui.layout("[=viewer]"); + if(background != "") { + $ui.set($ui.MAIN, {$ui.$parser, guecs::THEME.TRANSPARENT}); + auto& bg = $ui.get($ui.MAIN); + bg.set_sprite(background, true); + } + auto viewer = $ui.entity("viewer"); $ui.set(viewer, { sprite_name, 0, false}); @@ -181,6 +198,11 @@ namespace animator { } } +int error() { + fmt::println("USAGE: animator -h -b -s -a "); + return 1; +} + int main(int argc, char* argv[]) { shaders::init(); components::init(); @@ -189,14 +211,42 @@ int main(int argc, char* argv[]) { ai::init("ai"); animation::init(); - dbc::check(argc == 2, "USAGE: animator "); - std::string sprite_name{argv[1]}; + std::string sprite_name; + std::string background; + std::string anim_name; + int opt = 0; + + while((opt = getopt(argc, argv, "hb:s:a:")) != -1) { + switch(opt) { + case 'b': + background = optarg; + break; + case 's': + sprite_name = optarg; + break; + case 'a': + anim_name = optarg; + break; + case 'h': // fallthrough + error(); + break; + default: + return error(); + break; + } + } + + if(sprite_name == "") { + return error(); + } else if(anim_name == "") { + anim_name = sprite_name; // default to the same + } sound::mute(true); sound::play("ambient_1", true); animator::FSM main; - main.init(sprite_name); + main.init(sprite_name, anim_name, background); while(main.active()) { main.tick(); diff --git a/tools/animator.hpp b/tools/animator.hpp index 7fce351..66a745b 100644 --- a/tools/animator.hpp +++ b/tools/animator.hpp @@ -25,7 +25,7 @@ namespace animator { guecs::UI $ui; void button(const std::string& name, std::function cb); - void init(const std::string& sprite_name, int width, int height); + void init(const std::string& sprite_name, const std::string& background, int width, int height); void render(sf::RenderWindow& window, bool debug=false); bool mouse(float x, float y, guecs::Modifiers mods); std::shared_ptr get_sprite(); @@ -40,10 +40,12 @@ namespace animator { std::shared_ptr $sprite = nullptr; animate2::Animate2 $anim; std::string $sprite_name=""; + std::string $anim_name=""; + std::string $background=""; std::filesystem::file_time_type $last_mod_time; sf::Clock $timer; - void init(const std::string &sprite_name); + void init(const std::string &sprite_name, const std::string& background, const std::string &anim_name); void event(Event ev, std::any data={}); void START(Event ev); void ANIMATE(Event ev);