diff options
| -rw-r--r-- | CMakeLists.txt | 6 | ||||
| -rw-r--r-- | include/rodeo/audio.h | 26 | ||||
| -rw-r--r-- | include/rodeo/audio_t.h | 3 | ||||
| -rw-r--r-- | src/audio/rodeo_audio.c | 55 | ||||
| -rw-r--r-- | src/audio/rodeo_audio_t.h | 11 | ||||
| -rw-r--r-- | src/compile_flags.txt | 2 | ||||
| -rw-r--r-- | src/rodeo.c | 2 |
7 files changed, 90 insertions, 15 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index fbd4e7a..7fe7c04 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,13 +100,19 @@ elseif(DEFINED UNIX AND NOT DEFINED APPLE) add_subdirectory(external/SDL_image) add_subdirectory(external/SDL_mixer) +if(${CMAKE_BUILD_TYPE} EQUAL Debug) target_compile_options(${PROJECT_NAME} PRIVATE -Wall PRIVATE -Wextra PRIVATE -Wpedantic #PRIVATE -Werror PRIVATE -Wconversion + PUBLIC -fsanitize=address,undefined,leak ) + target_link_options(${PROJECT_NAME} + PUBLIC -fsanitize=address,undefined,leak + ) +endif() ExternalProject_Add(project_bgfx #BUILD_IN_SOURCE true # this just doesn't work diff --git a/include/rodeo/audio.h b/include/rodeo/audio.h index 172fe48..2d03250 100644 --- a/include/rodeo/audio.h +++ b/include/rodeo/audio.h @@ -1,15 +1,33 @@ +#pragma once + +// -- external -- +#include "stc/cstr.h" + +// -- system -- +#include <inttypes.h> + +typedef struct rodeo_audio_sound_t rodeo_audio_sound_t; void -rodeo_audio_initialize(void); +rodeo_audio_initialize( + int32_t num_sound_pools, + int32_t size_sound_pools +); void rodeo_audio_deinitialize(void); void -rodeo_audio_loadSample(void); +rodeo_audio_masterVolume_set(float volume_level); + +float +rodeo_audio_masterVolume_get(void); + +rodeo_audio_sound_t* +rodeo_audio_sound_create_from_path(cstr path); void -rodeo_audio_freeSample(void); +rodeo_audio_sound_destroy(rodeo_audio_sound_t* sound); void -rodeo_audio_playSample(void); +rodeo_audio_sound_play(rodeo_audio_sound_t *sound, int32_t pool_id); diff --git a/include/rodeo/audio_t.h b/include/rodeo/audio_t.h new file mode 100644 index 0000000..45dcbb0 --- /dev/null +++ b/include/rodeo/audio_t.h @@ -0,0 +1,3 @@ +#pragma once + + diff --git a/src/audio/rodeo_audio.c b/src/audio/rodeo_audio.c index 34d0f33..0adb3eb 100644 --- a/src/audio/rodeo_audio.c +++ b/src/audio/rodeo_audio.c @@ -3,15 +3,17 @@ // public #include "rodeo/audio.h" #include "rodeo/log.h" +// private +#include "audio/rodeo_audio_t.h" // -- external -- #include "SDL.h" #include "SDL_mixer.h" -Mix_Chunk *sample_sound = NULL; +static int32_t **irodeo_audio_channelPool; void -rodeo_audio_initialize(void) +rodeo_audio_initialize(int32_t num_sound_pools, int32_t size_sound_pools) { if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) { @@ -21,6 +23,24 @@ rodeo_audio_initialize(void) SDL_GetError() ); } + else + { + irodeo_audio_channelPool = malloc((uint32_t)(num_sound_pools * size_sound_pools) * sizeof(int32_t)); + Mix_AllocateChannels(num_sound_pools * size_sound_pools); + + int32_t temp_channel_id = 0; + for(int i = 0; i < num_sound_pools; ++i) + { + //irodeo_audio_channelPool[i] = + for(int j = 0; j < size_sound_pools; ++j) + { + irodeo_audio_channelPool[i][j] = temp_channel_id; + temp_channel_id += 1; + } + } + + rodeo_audio_masterVolume_set(0.5f); + } } void @@ -30,10 +50,24 @@ rodeo_audio_deinitialize(void) } void -rodeo_audio_loadSample(void) +rodeo_audio_masterVolume_set(float volume_level) +{ + Mix_MasterVolume((int32_t)(volume_level * 128)); + Mix_VolumeMusic((int32_t)(volume_level * 128)); +} + +float +rodeo_audio_masterVolume_get(void) +{ + return ((float)Mix_MasterVolume(-1)) / (float)MIX_MAX_VOLUME; +} + +rodeo_audio_sound_t* +rodeo_audio_sound_create_from_path(cstr path) { - sample_sound = Mix_LoadWAV("assets/sample.wav"); - if(NULL == sample_sound) + rodeo_audio_sound_t *sample_sound = malloc(sizeof(rodeo_audio_sound_t)); + sample_sound->sdl_sound = Mix_LoadWAV(cstr_str(&path)); + if(NULL == sample_sound->sdl_sound) { rodeo_log( rodeo_logLevel_error, @@ -41,17 +75,18 @@ rodeo_audio_loadSample(void) Mix_GetError() ); } + return sample_sound; } void -rodeo_audio_freeSample(void) +rodeo_audio_sound_destroy(rodeo_audio_sound_t* sound) { - Mix_FreeChunk(sample_sound); - sample_sound = NULL; + Mix_FreeChunk(sound->sdl_sound); + free(sound); } void -rodeo_audio_playSample(void) +rodeo_audio_sound_play(rodeo_audio_sound_t *sound, int32_t pool_id) { - Mix_PlayChannel(-1, sample_sound, 0); + Mix_PlayChannel(pool_id, sound->sdl_sound, 0); } diff --git a/src/audio/rodeo_audio_t.h b/src/audio/rodeo_audio_t.h new file mode 100644 index 0000000..6e3ce68 --- /dev/null +++ b/src/audio/rodeo_audio_t.h @@ -0,0 +1,11 @@ +#pragma once +// -- external -- +#include "SDL.h" +#include "SDL_mixer.h" + +struct +rodeo_audio_sound_t +{ + Mix_Chunk *sdl_sound; +}; + diff --git a/src/compile_flags.txt b/src/compile_flags.txt index 09b2ca8..29708f7 100644 --- a/src/compile_flags.txt +++ b/src/compile_flags.txt @@ -12,4 +12,6 @@ -Wextra -Wpedantic -Wconversion +-fsanitize=address,undefined -std=c99 + diff --git a/src/rodeo.c b/src/rodeo.c index 1ab12c0..c101839 100644 --- a/src/rodeo.c +++ b/src/rodeo.c @@ -252,7 +252,7 @@ rodeo_window_init( state.default_texture.width = 1; state.default_texture.height = 1; - rodeo_audio_initialize(); + rodeo_audio_initialize(4, 4); state.active_texture_p = &state.default_texture.internal_texture->texture_bgfx; } |
