diff options
| author | realtradam <[email protected]> | 2023-05-02 20:45:40 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2023-05-02 20:45:40 -0400 |
| commit | 87ca456c3f3e97f149604bc033fdd57998e2dcd4 (patch) | |
| tree | b326cacf8e135c939bfc90ba4dd75618cb8598e9 | |
| parent | 4b6ae783ce2c9f08d21d2a2969826412cc587de2 (diff) | |
| download | RodeoKit-87ca456c3f3e97f149604bc033fdd57998e2dcd4.tar.gz RodeoKit-87ca456c3f3e97f149604bc033fdd57998e2dcd4.zip | |
added sample audio system
| -rw-r--r-- | .gitmodules | 3 | ||||
| -rw-r--r-- | CMakeLists.txt | 5 | ||||
| m--------- | external/SDL_mixer | 0 | ||||
| -rw-r--r-- | include/rodeo.h | 1 | ||||
| -rw-r--r-- | include/rodeo/audio.h | 15 | ||||
| -rw-r--r-- | src/audio/rodeo_audio.c | 57 | ||||
| -rw-r--r-- | src/compile_flags.txt | 1 | ||||
| -rw-r--r-- | src/rodeo.c | 7 |
8 files changed, 88 insertions, 1 deletions
diff --git a/.gitmodules b/.gitmodules index 74a9ca9..4dfe4cf 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "external/SDL_image"] path = external/SDL_image url = https://github.com/libsdl-org/SDL_image +[submodule "external/SDL_mixer"] + path = external/SDL_mixer + url = https://github.com/libsdl-org/SDL_mixer.git diff --git a/CMakeLists.txt b/CMakeLists.txt index a19af39..de61cfd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ add_library(${PROJECT_NAME} "src/rodeo_math.c" "src/log/rodeo_log.c" "src/input/rodeo_input.c" + "src/audio/rodeo_audio.c" ) set_property(TARGET RodeoKit PROPERTY C_STANDARD 99) @@ -105,6 +106,7 @@ elseif(DEFINED UNIX AND NOT DEFINED APPLE) add_subdirectory(external/SDL) add_subdirectory(external/SDL_image) + add_subdirectory(external/SDL_mixer) target_compile_options(${PROJECT_NAME} PRIVATE -Wall @@ -132,6 +134,7 @@ elseif(DEFINED UNIX AND NOT DEFINED APPLE) target_include_directories(${PROJECT_NAME} PRIVATE external/SDL/include PRIVATE external/SDL_image/include + PRIVATE external/SDL_mixer/include PRIVATE external/bgfx/include PRIVATE external/bx/include PRIVATE external/cglm/include @@ -146,6 +149,7 @@ elseif(DEFINED UNIX AND NOT DEFINED APPLE) target_link_directories(${PROJECT_NAME} PRIVATE external/SDL PRIVATE external/SDL_image + PRIVATE external/SDL_mixer PRIVATE external/bgfx ) target_link_libraries(${PROJECT_NAME} @@ -153,6 +157,7 @@ elseif(DEFINED UNIX AND NOT DEFINED APPLE) #PRIVATE SDL2::SDL2main PRIVATE SDL2::SDL2 # dynamic lib PRIVATE SDL2_image + PRIVATE SDL2_mixer PRIVATE cglm PRIVATE bgfx ) diff --git a/external/SDL_mixer b/external/SDL_mixer new file mode 160000 +Subproject 0cd90476b8294e736e11cc11a5d66d5848ee3c0 diff --git a/include/rodeo.h b/include/rodeo.h index ba47491..c49bd5e 100644 --- a/include/rodeo.h +++ b/include/rodeo.h @@ -5,6 +5,7 @@ #include "rodeo/input.h" #include "rodeo/log.h" #include "rodeo/common.h" +#include "rodeo/audio.h" // -- external -- #include "stc/cstr.h" diff --git a/include/rodeo/audio.h b/include/rodeo/audio.h new file mode 100644 index 0000000..172fe48 --- /dev/null +++ b/include/rodeo/audio.h @@ -0,0 +1,15 @@ + +void +rodeo_audio_initialize(void); + +void +rodeo_audio_deinitialize(void); + +void +rodeo_audio_loadSample(void); + +void +rodeo_audio_freeSample(void); + +void +rodeo_audio_playSample(void); diff --git a/src/audio/rodeo_audio.c b/src/audio/rodeo_audio.c new file mode 100644 index 0000000..34d0f33 --- /dev/null +++ b/src/audio/rodeo_audio.c @@ -0,0 +1,57 @@ + +// -- internal -- +// public +#include "rodeo/audio.h" +#include "rodeo/log.h" + +// -- external -- +#include "SDL.h" +#include "SDL_mixer.h" + +Mix_Chunk *sample_sound = NULL; + +void +rodeo_audio_initialize(void) +{ + if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) + { + rodeo_log( + rodeo_logLevel_error, + "Failed to initialize SDL Audio. SDL_Error: %s", + SDL_GetError() + ); + } +} + +void +rodeo_audio_deinitialize(void) +{ + Mix_Quit(); +} + +void +rodeo_audio_loadSample(void) +{ + sample_sound = Mix_LoadWAV("assets/sample.wav"); + if(NULL == sample_sound) + { + rodeo_log( + rodeo_logLevel_error, + "Failed to load sound. Mix_Error: %s", + Mix_GetError() + ); + } +} + +void +rodeo_audio_freeSample(void) +{ + Mix_FreeChunk(sample_sound); + sample_sound = NULL; +} + +void +rodeo_audio_playSample(void) +{ + Mix_PlayChannel(-1, sample_sound, 0); +} diff --git a/src/compile_flags.txt b/src/compile_flags.txt index 26d1cc4..df3f821 100644 --- a/src/compile_flags.txt +++ b/src/compile_flags.txt @@ -1,6 +1,7 @@ -I./ -I../include -I../external/SDL/include +-I../external/SDL_mixer/include -I../external/bgfx/include -I../external/bx/include -I../external/cglm/include diff --git a/src/rodeo.c b/src/rodeo.c index 71c4666..c6ee71b 100644 --- a/src/rodeo.c +++ b/src/rodeo.c @@ -1,3 +1,4 @@ + // -- internal -- // public #include "rodeo.h" @@ -13,6 +14,7 @@ #endif #include "SDL2/SDL.h" #include "SDL2/SDL_image.h" +#include "SDL2/SDL_mixer.h" #include "SDL2/SDL_syswm.h" #include "bgfx/c99/bgfx.h" /*#define CGLM_FORCE_LEFT_HANDED*/ @@ -44,7 +46,7 @@ rodeo_window_init( "Initializing SDL..." ); - if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0) + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) < 0) { rodeo_log( rodeo_logLevel_error, @@ -241,6 +243,8 @@ rodeo_window_init( state.default_texture.width = 1; state.default_texture.height = 1; + rodeo_audio_initialize(); + state.active_texture_p = &state.default_texture.internal_texture->texture_bgfx; } @@ -260,6 +264,7 @@ rodeo_window_deinit(void) bgfx_shutdown(); SDL_DestroyWindow(state.window); + Mix_Quit(); SDL_Quit(); } |
