play sound

This commit is contained in:
KikooDX 2022-03-02 00:16:38 +01:00
parent 021934137c
commit 76e13d2909
2 changed files with 70 additions and 1 deletions

View File

@ -32,12 +32,14 @@ extern "C" {
#ifdef FXCG50
typedef void LZY_Music;
typedef void LZY_Sound;
#else
#ifndef LZY_SDL_MIXER_INCLUDE
#define LZY_SDL_MIXER_INCLUDE <SDL_mixer.h>
#endif
#include LZY_SDL_MIXER_INCLUDE
typedef Mix_Music LZY_Music;
typedef Mix_Chunk LZY_Sound;
#endif
int LZY_Init(const char *title, int target_fps, const char *tset_path,
@ -55,6 +57,9 @@ int LZY_DrawChar(unsigned char chr, int x, int y);
LZY_Music *LZY_MusicLoad(const char *path);
int LZY_MusicDestroy(LZY_Music *music);
int LZY_MusicPlay(LZY_Music *music, int loops);
LZY_Sound *LZY_SoundLoad(const char *path);
int LZY_SoundDestroy(LZY_Sound *sound);
int LZY_SoundPlay(LZY_Sound *sound, int loops);
void LZY_CycleEvents(void);
int LZY_KeyDown(unsigned int key);
int LZY_ShouldQuit(void);
@ -289,16 +294,34 @@ LZY_Music *LZY_MusicLoad(const char *path) {
LZY_UNUSED(path);
return NULL;
}
int LZY_MusicDestroy(LZY_Music *music) {
LZY_UNUSED(music);
return -1;
}
int LZY_MusicPlay(LZY_Music *music, int loops) {
LZY_UNUSED(music);
LZY_UNUSED(loops);
return -1;
}
LZY_Sound *LZY_SoundLoad(const char *path) {
LZY_UNUSED(path);
return NULL;
}
int LZY_SoundDestroy(LZY_Sound *sound) {
LZY_UNUSED(sound);
return -1;
}
int LZY_SoundPlay(LZY_Sound *sound, int loops) {
LZY_UNUSED(sound);
LZY_UNUSED(loops);
return -1;
}
void LZY_CycleEvents(void) {
clearevents();
should_quit = should_quit || keydown(KEY_EXIT);
@ -629,9 +652,14 @@ LZY_Music *LZY_MusicLoad(const char *path) {
}
int LZY_MusicPlay(LZY_Music *music, int loops) {
if (music == NULL) {
error = "music is NULL";
return -1;
}
if (Mix_PlayMusic(music, loops) < 0) {
error = Mix_GetError();
return -1;
return -2;
}
return 0;
}
@ -646,6 +674,37 @@ int LZY_MusicDestroy(LZY_Music *music) {
return 0;
}
LZY_Sound *LZY_SoundLoad(const char *path) {
LZY_Sound *const sound = Mix_LoadWAV(path);
if (sound == NULL)
error = Mix_GetError();
return sound;
}
int LZY_SoundPlay(LZY_Sound *sound, int loops) {
if (sound == NULL) {
error = "sound is NULL";
return -1;
}
if (Mix_PlayChannel(-1, sound, loops) < 0) {
error = Mix_GetError();
return -2;
}
return 0;
}
int LZY_SoundDestroy(LZY_Sound *sound) {
if (sound == NULL) {
error = "sound is NULL";
return -1;
}
Mix_FreeChunk(sound);
return 0;
}
void LZY_CycleEvents(void) {
static const SDL_Scancode sc[LZYK_COUNT] = {
SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP,

View File

@ -9,6 +9,8 @@
static void draw_player(int x, int y);
int main(void) {
LZY_Music *music;
LZY_Sound *sound;
int x = 0;
int y = 0;
@ -19,6 +21,11 @@ int main(void) {
}
LZY_Log("init was great success!");
sound = LZY_SoundLoad("res/sound.wav");
music = LZY_MusicLoad("res/music.ogg");
LZY_SoundPlay(sound, 0);
LZY_MusicPlay(music, 0);
do {
/* update */
LZY_CycleEvents();
@ -42,6 +49,9 @@ int main(void) {
LZY_DrawEnd();
} while (!LZY_ShouldQuit());
LZY_MusicDestroy(music);
LZY_SoundDestroy(sound);
LZY_Log("cya");
LZY_Quit();
return 0;