Skip to content

Commit

Permalink
Merge pull request #2168 from ivan-mogilko/361--tracksdlsubsystems
Browse files Browse the repository at this point in the history
Record inited SDL subsystems and use this info during uninit
  • Loading branch information
ivan-mogilko authored Oct 11, 2023
2 parents ec5207d + 1033d82 commit 56ccbb5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Engine/media/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ void shutdown_sound()
stop_all_sound_and_music(); // game logic
audio_core_shutdown(); // audio core system
soundcache_clear(); // clear cached data
if(usetup.audio_enabled) sys_audio_shutdown(); // backend
sys_audio_shutdown(); // backend; NOTE: sys_main will know if it's required
usetup.audio_enabled = false;
}

Expand Down
26 changes: 18 additions & 8 deletions Engine/platform/base/sys_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
using namespace AGS::Common;
using namespace AGS::Engine;

// TODO: prehaps refactor the whole sys_main into the singleton class at some point
struct SysMainInfo
{
// Indicates which subsystems did we initialize
uint32_t SDLSubsystems = 0u;
} static gl_SysMainInfo;

// ----------------------------------------------------------------------------
// INIT / SHUTDOWN
// ----------------------------------------------------------------------------
Expand All @@ -43,19 +50,17 @@ int sys_main_init(/*config*/) {
Debug::Printf(kDbgMsg_Error, "Unable to initialize SDL: %s", SDL_GetError());
return -1;
}
if(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0) {
// In non-desktop pc platforms, there is a chance that the gamecontroller is indeed necessary
// For now, it's better to just warn and rely on other input methods, in ags4 we can review this
Debug::Printf(kDbgMsg_Warn, "Unable to initialize SDL Gamepad: %s", SDL_GetError());
}
gl_SysMainInfo.SDLSubsystems =
SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS;
return 0;
}

void sys_main_shutdown() {
sys_window_destroy();
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS);
// TO-DO: find somewhere to save if gamecontroller subsystem was initialized or not and quit it specifically too
sys_audio_shutdown(); // in case it's still on
SDL_QuitSubSystem(gl_SysMainInfo.SDLSubsystems);
SDL_Quit();
gl_SysMainInfo.SDLSubsystems = 0u;
}

void sys_set_background_mode(bool /*on*/) {
Expand Down Expand Up @@ -114,6 +119,8 @@ void sys_renderer_set_output(const String &name)

bool sys_audio_init(const String &driver_name)
{
if ((gl_SysMainInfo.SDLSubsystems & SDL_INIT_AUDIO) != 0)
return true;
// IMPORTANT: we must use a combination of SDL_setenv and SDL_InitSubSystem
// here, and NOT use SDL_AudioInit, because SDL_AudioInit does not increment
// subsystem's reference count. Which in turn may cause problems down the
Expand Down Expand Up @@ -142,14 +149,17 @@ bool sys_audio_init(const String &driver_name)
else
Debug::Printf(kDbgMsg_Error, "Failed to initialize any audio driver; error: %s",
SDL_GetError());
gl_SysMainInfo.SDLSubsystems |= SDL_INIT_AUDIO * res;
return res;
}

void sys_audio_shutdown()
{
// Note: a subsystem that failed to initialize, doesn't increment ref-count
// Additionally, we may not have init it at all, see engine_init_audio
SDL_QuitSubSystem(SDL_INIT_AUDIO);
if ((gl_SysMainInfo.SDLSubsystems & SDL_INIT_AUDIO) != 0)
SDL_QuitSubSystem(SDL_INIT_AUDIO);
gl_SysMainInfo.SDLSubsystems &= ~SDL_INIT_AUDIO;
}


Expand Down

0 comments on commit 56ccbb5

Please sign in to comment.