introduce audio_model = "push" to twn.toml

This commit is contained in:
veclavtalica 2025-02-04 00:21:30 +03:00
parent 277d1b2e10
commit c6cbf941a2
4 changed files with 30 additions and 27 deletions

View File

@ -27,7 +27,6 @@ set(TWN_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
# feature configuration, set them with -DFEATURE=ON/OFF in cli # feature configuration, set them with -DFEATURE=ON/OFF in cli
option(TWN_FEATURE_DYNLIB_GAME "Enable dynamic library loading support" ON) option(TWN_FEATURE_DYNLIB_GAME "Enable dynamic library loading support" ON)
option(TWN_FEATURE_PUSH_AUDIO "Enable frame based audio push for easy realtime audio" ON)
option(TWN_USE_AMALGAM "Enable use of twn_amalgam.c as a single compilation unit" ON) option(TWN_USE_AMALGAM "Enable use of twn_amalgam.c as a single compilation unit" ON)
# todo: figure out how to compile for dynamic linking instead # todo: figure out how to compile for dynamic linking instead
@ -136,8 +135,6 @@ set_target_properties(${TWN_TARGET} PROPERTIES
C_STANDARD_REQUIRED ON C_STANDARD_REQUIRED ON
C_EXTENSIONS ON) # extensions are required by stb_ds.h C_EXTENSIONS ON) # extensions are required by stb_ds.h
target_compile_definitions(${TWN_TARGET} PRIVATE $<$<BOOL:${TWN_FEATURE_PUSH_AUDIO}>:TWN_FEATURE_PUSH_AUDIO>)
# precompile commonly used not-so-small headers # precompile commonly used not-so-small headers
target_precompile_headers(${TWN_TARGET} PRIVATE target_precompile_headers(${TWN_TARGET} PRIVATE
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:third-party/glad/include/glad/glad.h> $<$<NOT:$<BOOL:${EMSCRIPTEN}>>:third-party/glad/include/glad/glad.h>

View File

@ -259,9 +259,8 @@ void audio_play(const char *path,
request.format = AUDIO_F32; request.format = AUDIO_F32;
request.channels = 2; request.channels = 2;
request.samples = 4096; request.samples = 4096;
#ifndef TWN_FEATURE_PUSH_AUDIO if (!ctx.push_audio_model)
request.callback = audio_callback; request.callback = audio_callback;
#endif
/* TODO: check for errors */ /* TODO: check for errors */
ctx.audio_device = SDL_OpenAudioDevice(NULL, 0, &request, &got, 0); ctx.audio_device = SDL_OpenAudioDevice(NULL, 0, &request, &got, 0);
ctx.audio_stream_format = got.format; ctx.audio_stream_format = got.format;
@ -279,6 +278,8 @@ void audio_play(const char *path,
ctx.audio_initialized = true; ctx.audio_initialized = true;
} }
SDL_LockAudioDevice(ctx.audio_device);
if (channel) { if (channel) {
AudioChannelItem *pair = shgetp_null(ctx.audio_channels, channel); AudioChannelItem *pair = shgetp_null(ctx.audio_channels, channel);
@ -331,6 +332,8 @@ void audio_play(const char *path,
arrpush(ctx.unnamed_audio_channels, new_channel); arrpush(ctx.unnamed_audio_channels, new_channel);
} }
SDL_UnlockAudioDevice(ctx.audio_device);
} }

View File

@ -83,6 +83,7 @@ typedef struct EngineContext {
/* signals mouse focus, used to disable mouse capture */ /* signals mouse focus, used to disable mouse capture */
bool window_mouse_resident; bool window_mouse_resident;
bool audio_initialized; bool audio_initialized;
bool push_audio_model;
bool cull_faces; bool cull_faces;
} EngineContext; } EngineContext;

View File

@ -226,8 +226,9 @@ static void main_loop(void) {
input_state_update_postframe(&ctx.input); input_state_update_postframe(&ctx.input);
/* TODO: make it works when ctx.ticks_per_second != 60 */ /* TODO: make it works when ctx.ticks_per_second != 60 */
#ifdef TWN_FEATURE_PUSH_AUDIO if (ctx.push_audio_model) {
uint64_t const queued_frames = SDL_GetQueuedAudioSize(ctx.audio_device) / sizeof (float) / 2; uint64_t const queued_frames = SDL_GetQueuedAudioSize(ctx.audio_device) / sizeof (float) / 2;
/* note: this is here to reduce drift which sometimes appears */
if (queued_frames >= 4096 * 2) if (queued_frames >= 4096 * 2)
SDL_ClearQueuedAudio(ctx.audio_device); SDL_ClearQueuedAudio(ctx.audio_device);
static uint8_t audio_buffer[(AUDIO_FREQUENCY / 60) * sizeof (float) * 2]; static uint8_t audio_buffer[(AUDIO_FREQUENCY / 60) * sizeof (float) * 2];
@ -236,7 +237,7 @@ static void main_loop(void) {
if (SDL_QueueAudio(ctx.audio_device, audio_buffer, sizeof audio_buffer)) if (SDL_QueueAudio(ctx.audio_device, audio_buffer, sizeof audio_buffer))
CRY_SDL("Error queueing audio: "); CRY_SDL("Error queueing audio: ");
} }
#endif }
/* TODO: ctx.game_copy = ctx.game should be placed after it, but it messes with state used in render() */ /* TODO: ctx.game_copy = ctx.game should be placed after it, but it messes with state used in render() */
preserve_persistent_ctx_fields(); preserve_persistent_ctx_fields();
@ -639,11 +640,10 @@ static bool initialize(void) {
} else { } else {
ctx.cull_faces = datum_cull_faces.u.b; ctx.cull_faces = datum_cull_faces.u.b;
} }
}
/* these are dynamic arrays and will be allocated lazily by stb_ds */ toml_datum_t datum_audio_model = toml_string_in(engine, "audio_model");
ctx.render_queue_2d = NULL; ctx.push_audio_model = datum_audio_model.ok ? SDL_strncmp(datum_audio_model.u.s, "push", sizeof "push" - 1) == 0 : false;
ctx.uncolored_mesh_batches = NULL; SDL_free(datum_audio_model.u.s);
/* input */ /* input */
toml_datum_t datum_keybind_slots = toml_int_in(engine, "keybind_slots"); toml_datum_t datum_keybind_slots = toml_int_in(engine, "keybind_slots");
@ -656,6 +656,8 @@ static bool initialize(void) {
ctx.keybind_slots = datum_keybind_slots.u.i; ctx.keybind_slots = datum_keybind_slots.u.i;
} }
} }
}
input_state_init(&ctx.input); input_state_init(&ctx.input);
ctx.render_double_buffered = true; ctx.render_double_buffered = true;