aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-04-30 16:37:46 +0200
committerkdx <kikoodx@paranoici.org>2023-04-30 16:37:46 +0200
commita4112e4311eb226c4f43cb697aa7f6c34bd7b9a5 (patch)
treefca5829693c41fac4bf2e70cd4c63f1d1742d51a
parent6044a3bac4433e9ce8d114bdbc180ca7b48b2344 (diff)
downloadtzr-a4112e4311eb226c4f43cb697aa7f6c34bd7b9a5.tar.gz
music playing
-rw-r--r--headers/TZR_globals.h1
-rw-r--r--headers/TZR_sound.h5
-rw-r--r--headers/TZR_types.h1
-rw-r--r--sources/TZR_PlayMusic.c19
-rw-r--r--sources/TZR_StopMusic.c14
-rw-r--r--sources/globals.c1
6 files changed, 41 insertions, 0 deletions
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;