From 5db3519a8746bf5d1174c84ff4d276d6310d6843 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 11 Feb 2025 10:45:44 +0100 Subject: [PATCH] add audio looping --- src/game/game.c | 8 ++++++-- src/game/game.h | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/game/game.c b/src/game/game.c index d18813b..c329d0f 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -14,7 +14,6 @@ #include "SDL_audio.h" #include "tetromino/placing.h" - // shuffle the array using a Fisher–Yates shuffle static inline void shuffle(uint8_t const size, shape_id* const elmnts) { for (uint8_t i = 0; i < (size - 1); i++) { @@ -66,7 +65,6 @@ void game_init(game_data* const dat) { // initialize audio dat->audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096); dat->music = audio_wav_load(dat->audio_device, "korobeiniki.wav"); - audio_play(dat->audio_device, &dat->music); } // called every time the game's state is updated @@ -74,6 +72,12 @@ void game_update(game_data* const dat, uint8_t const* const keys) { if (keys[SDL_SCANCODE_ESCAPE]) stop(); + time_t ctime = time(NULL); + if (ctime > dat->music_timer) { + dat->music_timer = ctime + dat->music.sec; + audio_play(dat->audio_device, &dat->music); + } + InputData move = MOVE_NONE; if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) move |= MOVE_LEFT; if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) move |= MOVE_RIGHT; diff --git a/src/game/game.h b/src/game/game.h index 840dd7c..3ec4285 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "../window/audio.h" #include "../window/colour8.h" @@ -17,6 +18,7 @@ typedef struct { row rows[ROWS]; audio_device* audio_device; audio_data music; + time_t music_timer; uint16_t score; shape_id nxt[7]; // the order of the shape ids that they should appear in uint8_t curr_idx; // current shape index