Skip to content

Commit

Permalink
implement audio player fallback (HarbourMasters#28)
Browse files Browse the repository at this point in the history
* InitializeAudioPlayer/WindowManager use parameter

* implement AudioPlayer_Init with fallback as a method

* fix compile errors

* clang-format

* Window::StartAudioPlayer -> AudioPlayerInit bridge
  • Loading branch information
Alto1772 authored Nov 17, 2022
1 parent 44c9386 commit 9040d26
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
85 changes: 46 additions & 39 deletions src/core/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,8 @@ void Window::Initialize(const std::vector<std::string>& otrFiles, const std::uno
mHeight = GetConfig()->getInt("Window.Height", 480);
}

mGfxBackend = GetConfig()->getString("Window.GfxBackend");
InitializeWindowManager();

mAudioBackend = GetConfig()->getString("Window.AudioBackend");
InitializeAudioPlayer();
InitializeWindowManager(GetConfig()->getString("Window.GfxBackend"));
InitializeAudioPlayer(GetConfig()->getString("Window.AudioBackend"));

gfx_init(mWindowManagerApi, mRenderingApi, GetName().c_str(), mIsFullscreen, mWidth, mHeight);
mWindowManagerApi->set_fullscreen_changed_callback(OnFullscreenChanged);
Expand Down Expand Up @@ -249,33 +246,62 @@ float Window::GetCurrentAspectRatio() {
return (float)GetCurrentWidth() / (float)GetCurrentHeight();
}

void Window::InitializeAudioPlayer() {
void Window::InitializeAudioPlayer(std::string_view audioBackend) {
// Param can override
mAudioBackend = audioBackend;
#ifdef _WIN32
if (audioBackend == "wasapi") {
mAudioPlayer = std::make_shared<WasapiAudioPlayer>();
return;
}
#endif
#if defined(__linux)
if (audioBackend == "pulse") {
mAudioPlayer = std::make_shared<PulseAudioPlayer>();
return;
}
#endif
if (audioBackend == "sdl") {
mAudioPlayer = std::make_shared<SDLAudioPlayer>();
return;
}

// Defaults if not on list above
#ifdef _WIN32
mAudioPlayer = std::make_shared<WasapiAudioPlayer>();
#elif defined(__linux)
mAudioPlayer = std::make_shared<PulseAudioPlayer>();
#else
mAudioPlayer = std::make_shared<SDLAudioPlayer>();
#endif
}

// Config can override
#ifdef _WIN32
if (mAudioBackend == "wasapi") {
mAudioPlayer = std::make_shared<WasapiAudioPlayer>();
void Window::InitializeWindowManager(std::string_view gfxBackend) {
// Param can override
mGfxBackend = gfxBackend;
#ifdef ENABLE_DX11
if (gfxBackend == "dx11") {
mRenderingApi = &gfx_direct3d11_api;
mWindowManagerApi = &gfx_dxgi_api;
return;
}
#endif
#if defined(__linux)
if (mAudioBackend == "pulse") {
mAudioPlayer = std::make_shared<PulseAudioPlayer>();
#ifdef ENABLE_OPENGL
if (gfxBackend == "sdl") {
mRenderingApi = &gfx_opengl_api;
mWindowManagerApi = &gfx_sdl;
return;
}
#endif
if (mAudioBackend == "sdl") {
mAudioPlayer = std::make_shared<SDLAudioPlayer>();
#if defined(__linux__) && defined(X11_SUPPORTED)
if (gfxBackend == "glx") {
mRenderingApi = &gfx_opengl_api;
mWindowManagerApi = &gfx_glx;
return;
}
}
#endif
#endif

void Window::InitializeWindowManager() {
// First set default
// Defaults if not on list above
#ifdef ENABLE_OPENGL
mRenderingApi = &gfx_opengl_api;
#if defined(__linux__) && defined(X11_SUPPORTED)
Expand All @@ -297,26 +323,6 @@ void Window::InitializeWindowManager() {
#ifdef __WIIU__
mRenderingApi = &gfx_gx2_api;
mWindowManagerApi = &gfx_wiiu;
#endif

// Config can override
#ifdef ENABLE_DX11
if (mGfxBackend == "dx11") {
mRenderingApi = &gfx_direct3d11_api;
mWindowManagerApi = &gfx_dxgi_api;
}
#endif
#ifdef ENABLE_OPENGL
if (mGfxBackend == "sdl") {
mRenderingApi = &gfx_opengl_api;
mWindowManagerApi = &gfx_sdl;
}
#if defined(__linux__) && defined(X11_SUPPORTED)
if (mGfxBackend == "glx") {
mRenderingApi = &gfx_opengl_api;
mWindowManagerApi = &gfx_glx;
}
#endif
#endif
}

Expand Down Expand Up @@ -490,4 +496,5 @@ int32_t Window::GetLastScancode() {
void Window::SetLastScancode(int32_t scanCode) {
mLastScancode = scanCode;
}

} // namespace Ship
4 changes: 2 additions & 2 deletions src/core/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Window {
const char* GetKeyName(int32_t scancode);
int32_t GetLastScancode();
void SetLastScancode(int32_t scanCode);
void InitializeAudioPlayer(std::string_view audioBackend);
void InitializeWindowManager(std::string_view gfxBackend);

protected:
Window() = default;
Expand All @@ -67,11 +69,9 @@ class Window {

void InitializeConfiguration();
void InitializeControlDeck();
void InitializeAudioPlayer();
void InitializeLogging();
void InitializeResourceManager(const std::vector<std::string>& otrFiles = {},
const std::unordered_set<uint32_t>& validHashes = {});
void InitializeWindowManager();

std::shared_ptr<spdlog::logger> mLogger;
std::shared_ptr<Mercury> mConfig;
Expand Down
16 changes: 15 additions & 1 deletion src/core/bridge/audioplayerbridge.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "audioplayerbridge.h"
#include "core/Window.h"
#include "menu/ImGuiImpl.h"

extern "C" {

Expand All @@ -8,8 +9,21 @@ bool AudioPlayerInit(void) {
if (audio == nullptr) {
return false;
}
if (audio->Init()) {
return true;
}

// loop over available audio apis if current fails