From e7cbf8bed185af1cc7a4652f7c6d8f5bd0231ccb Mon Sep 17 00:00:00 2001 From: Joseph DiMaria Date: Sat, 31 Jan 2026 14:29:01 -0800 Subject: [PATCH] Variable sound duration --- src/samples/ghhb_game.h | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/samples/ghhb_game.h b/src/samples/ghhb_game.h index c0420d8..8e01de7 100644 --- a/src/samples/ghhb_game.h +++ b/src/samples/ghhb_game.h @@ -9,6 +9,7 @@ #include #include #include +#include namespace { @@ -212,6 +213,7 @@ public: float screen_height = 0.0f; Sound note_sounds[MAX_INSTRUMENT_TYPES][LANE_COUNT * OCTAVE_COUNT] = {{0}}; bool note_sounds_loaded[MAX_INSTRUMENT_TYPES][LANE_COUNT * OCTAVE_COUNT] = {{false}}; + std::deque note_sounds_playing[LANE_COUNT][MAX_INSTRUMENT_TYPES]; static constexpr float PRESS_FLASH_DURATION = 0.12f; static constexpr float MISS_FLASH_DURATION = 0.15f; float press_flash_timer[LANE_COUNT] = {0}; @@ -327,6 +329,25 @@ public: return false; } + void stop_playing_released_notes(int lane, float song_time) + { + for (int i = 0; i < MAX_GAMEPADS; i++) + { + if (IsGamepadAvailable(i) && IsGamepadButtonDown(i, GAMEPAD_BUTTONS[lane])) + { + printf("Gamepad %d held lane %d at time: %f\n", i, lane, song_time); + continue; + } + + if (!note_sounds_playing[lane][i].empty()) + { + printf("Stop sound at [%d][%d] at time: %f\n", lane, i, song_time); + StopSound(note_sounds_playing[lane][i].front()); + note_sounds_playing[lane][i].pop_front(); + } + } + } + bool lane_pressed_by_instrument_owner(int lane, int instrument_slot) const { int physical_id = INSTRUMENT_PHYSICAL_GAMEPAD[instrument_slot]; @@ -370,7 +391,7 @@ public: return y >= upper_bar_y - HIT_ZONE_MARGIN && y <= hit_line_y + HIT_ZONE_MARGIN; } - void consume_note(Glyph* n) + void consume_note(Glyph* n, float song_time) { auto it = std::find_if(spawned.begin(), spawned.end(), [n](Glyph* p) { return p == n; }); if (it != spawned.end()) @@ -379,8 +400,12 @@ public: spawned.erase(it); completed_notes.insert(n); printf("note lane: %d, note octave: %d\n", n->lane, n->octave); - if (note_sounds_loaded[n->instrument_slot][n->octave]) - PlaySound(note_sounds[n->instrument_slot][n->octave]); + if (note_sounds_loaded[n->instrument_slot][n->octave]) { + Sound sound = note_sounds[n->instrument_slot][n->octave]; + PlaySound(sound); + printf("push playing sound to [%d][%d] at %f\n", n->lane, n->instrument_slot, song_time); + note_sounds_playing[n->lane][n->instrument_slot].push_back(sound); + } } combo++; score += 100 + std::min(combo * 10, 50); @@ -482,6 +507,7 @@ public: { miss_flash_timer[lane] = 0.0f; } + stop_playing_released_notes(lane, song_time); bool pressed = is_lane_pressed(lane); if (pressed) press_flash_timer[lane] = PRESS_FLASH_DURATION; @@ -513,9 +539,9 @@ public: best = n; } } - if (best != nullptr) + if (best != nullptr && pressed) { - consume_note(best); + consume_note(best, song_time); } else {