added audio enabled flag

This commit is contained in:
Noel Berry 2022-11-20 19:32:38 -08:00
parent b161457c66
commit 4d7f7a993a
4 changed files with 880 additions and 875 deletions

View File

@ -18,6 +18,7 @@ namespace Blah
constexpr u32 VSync = 1 << 1; constexpr u32 VSync = 1 << 1;
constexpr u32 Fullscreen = 1 << 2; constexpr u32 Fullscreen = 1 << 2;
constexpr u32 Resizable = 1 << 3; constexpr u32 Resizable = 1 << 3;
constexpr u32 AudioEnabled = 1 << 4;
} }
// Application Configuration // Application Configuration
@ -51,7 +52,7 @@ namespace Blah
int audio_frequency_in_Hz = 44100; int audio_frequency_in_Hz = 44100;
// default starting flags // default starting flags
u32 flags = Flags::VSync | Flags::Resizable | Flags::FixedTimestep; u32 flags = Flags::VSync | Flags::Resizable | Flags::FixedTimestep | Flags::AudioEnabled;
// Callback on application startup // Callback on application startup
AppEventFn on_startup = nullptr; AppEventFn on_startup = nullptr;

View File

@ -12,15 +12,13 @@
using namespace Blah; using namespace Blah;
// Internal Audio bool
bool Internal::audio_is_init = false;
namespace namespace
{ {
// Global App State // Global App State
Config app_config; Config app_config;
bool app_is_running = false; bool app_is_running = false;
bool app_is_exiting = false; bool app_is_exiting = false;
bool app_is_audio_running = false;
u64 app_time_last; u64 app_time_last;
u64 app_time_accumulator = 0; u64 app_time_accumulator = 0;
u32 app_flags = 0; u32 app_flags = 0;
@ -37,6 +35,23 @@ namespace
Platform::get_draw_size(w, h); Platform::get_draw_size(w, h);
} }
void set_audio_system(bool on)
{
if (on && !app_is_audio_running)
{
int more_on_emscripten = 1;
#ifdef __EMSCRIPTEN__
more_on_emscripten = 4;
#endif
app_is_audio_running = Internal::audio_init(app_config.audio_frequency_in_Hz, 1024 * more_on_emscripten);
}
else if (!on && app_is_audio_running)
{
Internal::audio_shutdown();
app_is_audio_running = false;
}
}
// A dummy Target that represents the Back Buffer. // A dummy Target that represents the Back Buffer.
// It doesn't contain any data, rather it forwards calls along to the actual BackBuffer. // It doesn't contain any data, rather it forwards calls along to the actual BackBuffer.
class BackBuffer final : public Target class BackBuffer final : public Target
@ -101,15 +116,8 @@ bool App::run(const Config* c)
} }
// initialize audio // initialize audio
{ if (get_flag(Flags::AudioEnabled))
if (!Internal::audio_is_init) { set_audio_system(true);
int more_on_emscripten = 1;
#ifdef __EMSCRIPTEN__
more_on_emscripten = 4;
#endif
Internal::audio_is_init = Internal::audio_init(c->audio_frequency_in_Hz, 1024 * more_on_emscripten);
}
}
// initialize graphics // initialize graphics
{ {
@ -267,27 +275,21 @@ void Internal::app_step()
} }
// Update audio // Update audio
if (Internal::audio_is_init) if (app_is_audio_running)
Blah::Internal::audio_update(); Blah::Internal::audio_update();
} }
void Internal::app_shutdown() void Internal::app_shutdown()
{ {
// shutdown systems
Internal::input_shutdown(); Internal::input_shutdown();
if (app_renderer_api) if (app_renderer_api)
{ {
app_renderer_api->shutdown(); app_renderer_api->shutdown();
delete app_renderer_api; delete app_renderer_api;
app_renderer_api = nullptr;
} }
app_renderer_api = nullptr; set_audio_system(false);
if (Internal::audio_is_init)
{
Internal::audio_shutdown();
Internal::audio_is_init = false;
}
Platform::shutdown(); Platform::shutdown();
// clear static App state // clear static App state
@ -410,9 +412,13 @@ void App::set_flag(u32 flag, bool enabled)
if (was != app_flags) if (was != app_flags)
{ {
// tell platform & renderer
Platform::set_app_flags(app_flags); Platform::set_app_flags(app_flags);
if (app_renderer_api) if (app_renderer_api)
app_renderer_api->set_app_flags(app_flags); app_renderer_api->set_app_flags(app_flags);
// potentially toggle audio system
set_audio_system(get_flag(Flags::AudioEnabled));
} }
} }

View File

@ -19,16 +19,16 @@ namespace Blah
bool audio_init(unsigned play_frequency_in_Hz, int buffered_samples) bool audio_init(unsigned play_frequency_in_Hz, int buffered_samples)
{ {
cs_error_t err = cs_init(Platform::d3d11_get_hwnd(), play_frequency_in_Hz, buffered_samples, NULL); cs_error_t err = cs_init(Platform::d3d11_get_hwnd(), play_frequency_in_Hz, buffered_samples, NULL);
if (err != CUTE_SOUND_ERROR_NONE) { if (err != CUTE_SOUND_ERROR_NONE)
{
Log::error(cs_error_as_string(err)); Log::error(cs_error_as_string(err));
return false; return false;
} else {
return true;
} }
#ifndef BLAH_NO_THREADING #ifndef BLAH_NO_THREADING
cs_spawn_mix_thread(); cs_spawn_mix_thread();
#endif #endif
return true;
} }
void audio_shutdown() void audio_shutdown()

View File

@ -9,8 +9,6 @@ namespace Blah
{ {
namespace Internal namespace Internal
{ {
extern bool audio_is_init;
void app_step(); void app_step();
void app_shutdown(); void app_shutdown();
Renderer* app_renderer(); Renderer* app_renderer();