From a4112e4311eb226c4f43cb697aa7f6c34bd7b9a5 Mon Sep 17 00:00:00 2001 From: kdx Date: Sun, 30 Apr 2023 16:37:46 +0200 Subject: music playing --- headers/TZR_globals.h | 1 + headers/TZR_sound.h | 5 +++++ headers/TZR_types.h | 1 + sources/TZR_PlayMusic.c | 19 +++++++++++++++++++ sources/TZR_StopMusic.c | 14 ++++++++++++++ sources/globals.c | 1 + 6 files changed, 41 insertions(+) create mode 100644 sources/TZR_PlayMusic.c create mode 100644 sources/TZR_StopMusic.c diff --git a/headers/TZR_globals.h b/headers/TZR_globals.h index 25919ad..cfedc43 100644 --- a/headers/TZR_globals.h +++ b/headers/TZR_globals.h @@ -23,3 +23,4 @@ extern TZR_KeyState ___tzr_mousestates[256]; extern float ___tzr_scale; extern int ___tzr_off_x; extern int ___tzr_off_y; +extern Mix_Music *___tzr_music; diff --git a/headers/TZR_sound.h b/headers/TZR_sound.h index b24823e..271995b 100644 --- a/headers/TZR_sound.h +++ b/headers/TZR_sound.h @@ -9,3 +9,8 @@ #endif #endif int _TZR_PlaySound(const TZR_PlaySoundArgs *args); + +/* Return -1 on error. */ +int TZR_PlayMusic(const char *path, int loop); + +void TZR_StopMusic(void); diff --git a/headers/TZR_types.h b/headers/TZR_types.h index 972c45e..b9ec3eb 100644 --- a/headers/TZR_types.h +++ b/headers/TZR_types.h @@ -41,6 +41,7 @@ typedef struct TZR_Sound TZR_Sound; typedef struct TZR_Resource TZR_Resource; typedef enum TZR_EventType TZR_EventType; typedef struct TZR_Event TZR_Event; +typedef struct TZR_Music TZR_Music; typedef struct TZR_DrawImageArgs TZR_DrawImageArgs; typedef struct TZR_DrawRectangleArgs TZR_DrawRectangleArgs; typedef struct TZR_PlaySoundArgs TZR_PlaySoundArgs; diff --git a/sources/TZR_PlayMusic.c b/sources/TZR_PlayMusic.c new file mode 100644 index 0000000..10725b6 --- /dev/null +++ b/sources/TZR_PlayMusic.c @@ -0,0 +1,19 @@ +#include "TZR_sound.h" +#include "TZR_globals.h" +#include "sdl_error.h" + +int +TZR_PlayMusic(const char *path, int loop) +{ + if (!___tzr_config.mixer) { + fprintf(stderr, "mixer is disabled\n"); + return -1; + } + TZR_StopMusic(); + ___tzr_music = Mix_LoadMUS(path); + if (___tzr_music == NULL) + return sdl_error(-1); + if (Mix_PlayMusic(___tzr_music, loop) < 0) + return sdl_error(-1); + return 0; +} diff --git a/sources/TZR_StopMusic.c b/sources/TZR_StopMusic.c new file mode 100644 index 0000000..94eda2f --- /dev/null +++ b/sources/TZR_StopMusic.c @@ -0,0 +1,14 @@ +#include "TZR_sound.h" +#include "TZR_globals.h" + +void +TZR_StopMusic(void) +{ + if (!___tzr_config.mixer) + return; + if (Mix_PlayingMusic()) + Mix_HaltMusic(); + if (___tzr_music != NULL) + Mix_FreeMusic(___tzr_music); + ___tzr_music = NULL; +} diff --git a/sources/globals.c b/sources/globals.c index c968caf..02f9181 100644 --- a/sources/globals.c +++ b/sources/globals.c @@ -23,3 +23,4 @@ TZR_KeyState ___tzr_mousestates[256] = {0}; //doc says than mouse button is u8 float ___tzr_scale = 1.0; int ___tzr_off_x = 1.0; int ___tzr_off_y = 1.0; +Mix_Music *___tzr_music = NULL; -- cgit v1.2.3