From 46f34828e44fab2645915a8c41698610f7c5bb3e Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 6 Feb 2026 13:39:34 -0500 Subject: [PATCH] Sound is now working, and the animator tools will play them and also has mute. --- animate2.cpp | 16 ++++++++++++++-- animate2.hpp | 8 +++++--- assets/animate2.json | 24 +++++++++++++----------- tools/animator.cpp | 8 +++++--- tools/animator.hpp | 1 + 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/animate2.cpp b/animate2.cpp index 1b8dc52..fa4a621 100644 --- a/animate2.cpp +++ b/animate2.cpp @@ -5,6 +5,7 @@ #include "rand.hpp" #include #include +#include "sound.hpp" constexpr float SUB_FRAME_SENSITIVITY = 0.999f; @@ -43,14 +44,23 @@ namespace animate2 { // need one for each kind of thing to animate // NOTE: possibly find a way to only run apply on frame change? void Animate2::apply(sf::Sprite& sprite) { - dbc::check(!transform.simple, "can't call ::apply() on a simple animation, only ::motion()"); - dbc::check(sequence.current < $frame_rects.size(), "current frame past $frame_rects"); // NOTE: pos is not updated yet auto& rect = $frame_rects.at(sequence.current); sprite.setTextureRect(rect); } + void Animate2::play_sound() { + if(sounds.contains(form_name)) { + fmt::println("Playing sound for {}", form_name); + for(auto& [at_frame, sound_name] : sounds.at(form_name)) { + if(sequence.current == at_frame) { + sound::play(sound_name); + } + } + } + } + // replaces step void Animate2::update_frame() { dbc::check(playing, "attempt to update animation that's not playing"); @@ -76,6 +86,8 @@ namespace animate2 { playing = onLoop(sequence, transform); } + if(frame_change) play_sound(); + if(frame_change && onFrame != nullptr) onFrame(); dbc::check(sequence.current < sequence.frame_count, "onLoop fail: current frame out of frames.size()"); diff --git a/animate2.hpp b/animate2.hpp index 01c7491..6cdd8c2 100644 --- a/animate2.hpp +++ b/animate2.hpp @@ -52,7 +52,6 @@ namespace animate2 { float min_y{1.0f}; float max_x{1.0f}; float max_y{1.0f}; - bool simple{true}; bool flipped{false}; float ease_rate{0.5f}; bool scaled{false}; @@ -87,6 +86,7 @@ namespace animate2 { } using Form = std::pair; + using Sound = std::pair; class Animate2 { public: @@ -94,6 +94,7 @@ namespace animate2 { std::unordered_map sequences; std::unordered_map transforms; std::unordered_map forms; + std::unordered_map> sounds; OnFrameHandler onFrame = nullptr; Sequence sequence{}; @@ -110,6 +111,7 @@ namespace animate2 { std::vector calc_frames(); void play(); + void play_sound(); void stop(); void set_form(const std::string& form); void apply(sf::Sprite& sprite); @@ -123,7 +125,7 @@ namespace animate2 { ENROLL_COMPONENT(Sheet, frames, frame_width, frame_height); ENROLL_COMPONENT(Sequence, frames, durations); - ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y, simple, + ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y, flipped, ease_rate, scaled, toggled, looped, easing, motion); - ENROLL_COMPONENT(Animate2, sheet, sequences, transforms, forms); + ENROLL_COMPONENT(Animate2, sheet, sequences, transforms, forms, sounds); } diff --git a/assets/animate2.json b/assets/animate2.json index 028761d..f1ed41f 100644 --- a/assets/animate2.json +++ b/assets/animate2.json @@ -6,9 +6,9 @@ "frame_height": 720 }, "sequences": { - "normal": {"frames": [0, 1], "durations": [800, 240] }, - "reversed": {"frames": [1, 0], "durations": [800, 200] }, - "meth": {"frames": [0, 1], "durations": [33, 33] } + "idle": {"frames": [0], "durations": [800] }, + "hurt": {"frames": [1, 1], "durations": [800, 240] }, + "attack": {"frames": [1, 0], "durations": [800, 200] } }, "transforms": { "rushing": { @@ -16,7 +16,6 @@ "min_y": 0.6, "max_x": 0.8, "max_y": 0.8, - "simple": false, "flipped": false, "ease_rate": 5.0, "scaled": true, @@ -25,17 +24,16 @@ "easing": "in_out_back", "motion": "move_rush" }, - "meth": { + "shake": { "min_x": -20.0, "min_y": -20.0, "max_x": 20.0, "max_y": 20.0, - "simple": false, "flipped": true, "ease_rate": 5.0, "scaled": true, "toggled": false, - "looped": true, + "looped": false, "easing": "normal_dist", "motion": "move_shake" }, @@ -44,7 +42,6 @@ "min_y": 0.90, "max_x": 1.0, "max_y": 1.0, - "simple": true, "flipped": false, "ease_rate": 3.5, "scaled": true, @@ -55,9 +52,14 @@ } }, "forms": { - "idle": ["normal", "breathe"], - "attack": ["normal", "rushing"], - "hurt": ["reversed", "meth"] + "idle": ["idle", "breathe"], + "attack": ["attack", "rushing"], + "hurt": ["hurt", "shake"] + }, + "sounds": { + "idle": [], + "attack": [[0, "Sword_Hit_1"], [1, "Marmot_Scream_1"]], + "hurt": [[1, "Marmot_Scream_1"]] } } } diff --git a/tools/animator.cpp b/tools/animator.cpp index ea504a4..8f0a909 100644 --- a/tools/animator.cpp +++ b/tools/animator.cpp @@ -126,7 +126,7 @@ namespace animator { void FSM::run_animation() { if($anim.playing) { $anim.update(); - if(!$anim.transform.simple) $anim.apply(*$sprite); + $anim.apply(*$sprite); $anim.motion(*$sprite, $pos, $scale); } } @@ -181,6 +181,9 @@ namespace animator { event(Event::PREV_FORM); } else if($router.scancode == KEY::Down) { event(Event::NEXT_FORM); + } else if($router.scancode == KEY::M) { + $mute = !$mute; + sound::mute($mute); } break; case MOUSE_CLICK: @@ -302,8 +305,7 @@ int main(int argc, char* argv[]) { anim_name = sprite_name; // default to the same } - sound::mute(true); - sound::play("ambient_1", true); + sound::mute(false); animator::FSM main; main.init(sprite_name, anim_name, background); diff --git a/tools/animator.hpp b/tools/animator.hpp index e0a4a44..ff1aff1 100644 --- a/tools/animator.hpp +++ b/tools/animator.hpp @@ -50,6 +50,7 @@ namespace animator { sf::Clock $timer; std::string $cur_form = "idle"; int $cur_form_i = 0; + bool $mute = false; void init(const std::string &sprite_name, const std::string& background, const std::string &anim_name); void event(Event ev, std::any data={});