Sound is now working, and the animator tools will play them and also has mute.

This commit is contained in:
Zed A. Shaw 2026-02-06 13:39:34 -05:00
parent a4ffacdb18
commit 46f34828e4
5 changed files with 38 additions and 19 deletions

View file

@ -5,6 +5,7 @@
#include "rand.hpp"
#include <iostream>
#include <fstream>
#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()");

View file

@ -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<std::string, std::string>;
using Sound = std::pair<size_t, std::string>;
class Animate2 {
public:
@ -94,6 +94,7 @@ namespace animate2 {
std::unordered_map<std::string, Sequence> sequences;
std::unordered_map<std::string, Transform> transforms;
std::unordered_map<std::string, Form> forms;
std::unordered_map<std::string, std::vector<Sound>> sounds;
OnFrameHandler onFrame = nullptr;
Sequence sequence{};
@ -110,6 +111,7 @@ namespace animate2 {
std::vector<sf::IntRect> 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);
}

View file

@ -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"]]
}
}
}

View file

@ -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);

View file

@ -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={});