Can now specify a background to render the sprite on.

This commit is contained in:
Zed A. Shaw 2026-02-04 00:04:17 -05:00
parent fc01579232
commit 77643a4bcd
3 changed files with 85 additions and 10 deletions

View file

@ -8,6 +8,16 @@
#include "constants.hpp"
#include "animate2.hpp"
#include "tools/animator.hpp"
#include <unistd.h>
/*
* 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<guecs::Background>($ui.MAIN, {$ui.$parser, guecs::THEME.TRANSPARENT});
auto& bg = $ui.get<guecs::Background>($ui.MAIN);
bg.set_sprite(background, true);
}
auto viewer = $ui.entity("viewer");
$ui.set<guecs::Sprite>(viewer, { sprite_name, 0, false});
@ -181,6 +198,11 @@ namespace animator {
}
}
int error() {
fmt::println("USAGE: animator -h -b <background> -s <sprite> -a <animation>");
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 <sprite>");
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();