From 021934137cbfab28ca505dde2df53b4d475ff94b Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 2 Mar 2022 00:04:31 +0100 Subject: [PATCH] play music --- inc/lzy.h | 86 ++++++++++++++++++++++++++++++++++++++++++++++++----- meson.build | 1 + 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/inc/lzy.h b/inc/lzy.h index 3b7ae59..2f773f4 100644 --- a/inc/lzy.h +++ b/inc/lzy.h @@ -30,6 +30,16 @@ extern "C" { #define LZY_UNUSED(x) (void)(x) +#ifdef FXCG50 +typedef void LZY_Music; +#else +#ifndef LZY_SDL_MIXER_INCLUDE +#define LZY_SDL_MIXER_INCLUDE +#endif +#include LZY_SDL_MIXER_INCLUDE +typedef Mix_Music LZY_Music; +#endif + int LZY_Init(const char *title, int target_fps, const char *tset_path, const char *font_path); void LZY_Quit(void); @@ -42,6 +52,9 @@ int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h); int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h); int LZY_DrawTile(unsigned int id, int x, int y); 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); void LZY_CycleEvents(void); int LZY_KeyDown(unsigned int key); int LZY_ShouldQuit(void); @@ -87,6 +100,9 @@ enum LZY_ScanCode { #ifndef LZY_SDL_IMAGE_INCLUDE #define LZY_SDL_IMAGE_INCLUDE #endif +#ifndef LZY_SDL_MIXER_INCLUDE +#define LZY_SDL_MIXER_INCLUDE +#endif #ifndef LZY_DISPLAY_WIDTH #define LZY_DISPLAY_WIDTH 396 #endif @@ -269,6 +285,20 @@ int LZY_DrawText(const char *text, int x, int y) { #endif } +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; +} + void LZY_CycleEvents(void) { clearevents(); should_quit = should_quit || keydown(KEY_EXIT); @@ -293,6 +323,7 @@ const char *LZY_GetError(void) { #else /* end FXCG50, begin SDL2 */ #include LZY_SDL_INCLUDE #include LZY_SDL_IMAGE_INCLUDE +#include LZY_SDL_MIXER_INCLUDE #include static const char *error = NULL; @@ -310,11 +341,23 @@ static uint64_t next_time; int LZY_Init(const char *title, int target_fps, const char *tset_path, const char *font_path) { - if (SDL_Init(SDL_INIT_VIDEO) < 0) { + const int img_flags = IMG_INIT_PNG; + + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { error = SDL_GetError(); return -1; } + if ((IMG_Init(img_flags) & img_flags) != img_flags) { + error = IMG_GetError(); + return -2; + } + + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) { + error = Mix_GetError(); + return -3; + } + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0); window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, @@ -322,7 +365,7 @@ int LZY_Init(const char *title, int target_fps, const char *tset_path, LZY_DISPLAY_HEIGHT, SDL_WINDOW_RESIZABLE); if (window == NULL) { error = SDL_GetError(); - return -2; + return -4; } SDL_SetWindowMinimumSize(window, LZY_DISPLAY_WIDTH, LZY_DISPLAY_HEIGHT); @@ -330,7 +373,7 @@ int LZY_Init(const char *title, int target_fps, const char *tset_path, renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (renderer == NULL) { error = SDL_GetError(); - return -3; + return -5; } target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, @@ -338,18 +381,18 @@ int LZY_Init(const char *title, int target_fps, const char *tset_path, LZY_DISPLAY_HEIGHT); if (target == NULL) { error = SDL_GetError(); - return -4; + return -6; } tset = IMG_LoadTexture(renderer, tset_path); if (tset == NULL) { error = IMG_GetError(); - return -5; + return -7; } if (SDL_QueryTexture(tset, NULL, NULL, &tset_width, &tset_height) < 0) { error = SDL_GetError(); - return -6; + return -8; } tset_width /= LZY_TILE_SIZE; tset_height /= LZY_TILE_SIZE; @@ -357,12 +400,12 @@ int LZY_Init(const char *title, int target_fps, const char *tset_path, font = IMG_LoadTexture(renderer, font_path); if (font == NULL) { error = IMG_GetError(); - return -7; + return -9; } if (SDL_QueryTexture(font, NULL, NULL, &font_width, &font_height) < 0) { error = SDL_GetError(); - return -8; + return -10; } font_width /= LZY_CHR_WIDTH; font_height /= LZY_CHR_HEIGHT; @@ -397,6 +440,8 @@ void LZY_Quit(void) { window = NULL; } + Mix_Quit(); + IMG_Quit(); SDL_Quit(); } @@ -576,6 +621,31 @@ int LZY_DrawText(const char *text, int x, int y) { return err; } +LZY_Music *LZY_MusicLoad(const char *path) { + LZY_Music *const music = Mix_LoadMUS(path); + if (music == NULL) + error = Mix_GetError(); + return music; +} + +int LZY_MusicPlay(LZY_Music *music, int loops) { + if (Mix_PlayMusic(music, loops) < 0) { + error = Mix_GetError(); + return -1; + } + return 0; +} + +int LZY_MusicDestroy(LZY_Music *music) { + if (music == NULL) { + error = "music is NULL"; + return -1; + } + + Mix_FreeMusic(music); + return 0; +} + void LZY_CycleEvents(void) { static const SDL_Scancode sc[LZYK_COUNT] = { SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP, diff --git a/meson.build b/meson.build index fcf350a..75ff221 100644 --- a/meson.build +++ b/meson.build @@ -5,6 +5,7 @@ cc = meson.get_compiler('c') sdl2deps = [ dependency('sdl2', version: '>=2.0.0'), dependency('SDL2_image', version: '>=2.0.0'), + dependency('SDL2_mixer', version: '>=2.0.0'), cc.find_library('m', required: true), cc.find_library('dl', required: true), ]