1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-22 15:23:01 +01:00

Fix openrct2-cli build

By using a dummy audio context, we can now make openrct2-cli start a headless instance without any SDL2 calls. SDL2 is still a dependency until we remove out the input code to openrct2-ui.
This commit is contained in:
Ted John
2017-06-03 23:10:20 +01:00
parent acd3b09c80
commit 94e982289e
8 changed files with 134 additions and 30 deletions

View File

@@ -15,6 +15,7 @@
#pragma endregion
#include <openrct2/Context.h>
#include <openrct2/OpenRCT2.h>
using namespace OpenRCT2;
@@ -23,8 +24,16 @@ using namespace OpenRCT2;
*/
int main(int argc, char * * argv)
{
IContext * context = CreateContext();
int exitCode = context->RunOpenRCT2(argc, argv);
delete context;
return exitCode;
core_init();
int runGame = cmdline_run((const char * *)argv, argc);
if (runGame == 1)
{
gOpenRCT2Headless = true;
// Run OpenRCT2 with a plain context
auto context = CreateContext();
context->RunOpenRCT2(argc, argv);
delete context;
}
return gExitCode;
}

View File

@@ -39,16 +39,26 @@ int main(int argc, char * * argv)
int runGame = cmdline_run((const char * *)argv, argc);
if (runGame == 1)
{
// Run OpenRCT2 with a UI context
IAudioContext * audioContext = CreateAudioContext();
IUiContext * uiContext = gOpenRCT2Headless ? CreateDummyUiContext() : CreateUiContext();
IContext * context = CreateContext(audioContext, uiContext);
if (gOpenRCT2Headless)
{
// Run OpenRCT2 with a plain context
auto context = CreateContext();
context->RunOpenRCT2(argc, argv);
delete context;
}
else
{
// Run OpenRCT2 with a UI context
auto audioContext = CreateAudioContext();
auto uiContext = CreateUiContext();
auto context = CreateContext(audioContext, uiContext);
context->RunOpenRCT2(argc, argv);
context->RunOpenRCT2(argc, argv);
delete context;
delete uiContext;
delete audioContext;
delete context;
delete uiContext;
delete audioContext;
}
}
return gExitCode;
}

View File

@@ -17,6 +17,7 @@
#include <exception>
#include <memory>
#include <string>
#include "audio/AudioContext.h"
#include "Context.h"
#include "ui/UiContext.h"
#include "core/Console.hpp"
@@ -139,8 +140,7 @@ namespace OpenRCT2
_finished = true;
}
private:
bool Initialise()
bool Initialise() final override
{
#ifndef DISABLE_NETWORK
gHashCTX = EVP_MD_CTX_create();
@@ -233,6 +233,7 @@ namespace OpenRCT2
return true;
}
private:
IPlatformEnvironment * SetupEnvironment()
{
utf8 userPath[MAX_PATH];
@@ -544,11 +545,30 @@ namespace OpenRCT2
}
};
class PlainContext final : public Context
{
std::unique_ptr<IAudioContext> _audioContext;
std::unique_ptr<IUiContext> _uiContext;
public:
PlainContext()
: PlainContext(CreateDummyAudioContext(), CreateDummyUiContext())
{
}
PlainContext(IAudioContext * audioContext, IUiContext * uiContext)
: Context(audioContext, uiContext)
{
_audioContext = std::unique_ptr<IAudioContext>(audioContext);
_uiContext = std::unique_ptr<IUiContext>(uiContext);
}
};
Context * Context::Instance = nullptr;
IContext * CreateContext()
{
return new Context(nullptr, nullptr);
return new PlainContext();
}
IContext * CreateContext(Audio::IAudioContext * audioContext, IUiContext * uiContext)

View File

@@ -80,6 +80,8 @@ namespace OpenRCT2
virtual Ui::IUiContext * GetUiContext() abstract;
virtual sint32 RunOpenRCT2(int argc, char * * argv) abstract;
virtual bool Initialise() abstract;
virtual void Finish() abstract;
};

View File

@@ -60,5 +60,7 @@ namespace OpenRCT2
virtual void StopTitleMusic() abstract;
virtual void StopVehicleSounds() abstract;
};
IAudioContext * CreateDummyAudioContext();
}
}

View File

@@ -59,16 +59,19 @@ void * Mixer_Play_Effect(size_t id, sint32 loop, sint32 volume, float pan, doubl
else
{
IAudioMixer * mixer = GetMixer();
mixer->Lock();
IAudioSource * source = mixer->GetSoundSource((sint32)id);
channel = mixer->Play(source, loop, deleteondone != 0, false);
if (channel != nullptr)
if (mixer != nullptr)
{
channel->SetVolume(volume);
channel->SetPan(pan);
channel->SetRate(rate);
mixer->Lock();
IAudioSource * source = mixer->GetSoundSource((sint32)id);
channel = mixer->Play(source, loop, deleteondone != 0, false);
if (channel != nullptr)
{
channel->SetVolume(volume);
channel->SetPan(pan);
channel->SetRate(rate);
}
mixer->Unlock();
}
mixer->Unlock();
}
}
return channel;

View File

@@ -0,0 +1,52 @@
#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
#include "AudioContext.h"
namespace OpenRCT2 { namespace Audio
{
class DummyAudioContext final : public IAudioContext
{
IAudioMixer * GetMixer() override { return nullptr; }
std::vector<std::string> GetOutputDevices() override { return std::vector<std::string>(); }
void SetOutputDevice(const std::string &deviceName) override { }
IAudioSource * CreateStreamFromWAV(const std::string &path) override { return nullptr; }
void StartTitleMusic() override { }
IAudioChannel * PlaySound(sint32 soundId, sint32 volume, sint32 pan) override { return nullptr; }
IAudioChannel * PlaySoundAtLocation(sint32 soundId, sint16 x, sint16 y, sint16 z) override { return nullptr; }
IAudioChannel * PlaySoundPanned(sint32 soundId, sint32 pan, sint16 x, sint16 y, sint16 z) override { return nullptr; }
void ToggleAllSounds() override { }
void PauseSounds() override { }
void UnpauseSounds() override { }
void StopAll() override { }
void StopCrowdSound() override { }
void StopRainSound() override { }
void StopRideMusic() override { }
void StopTitleMusic() override { }
void StopVehicleSounds() override { }
};
IAudioContext * CreateDummyAudioContext()
{
return new DummyAudioContext();
}
} }

View File

@@ -210,13 +210,19 @@ uint8 platform_get_currency_value(const char *currCode) {
void core_init()
{
bitcount_init();
static bool initialised = false;
if (!initialised)
{
initialised = true;
bitcount_init();
#if defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200)
kern_return_t ret = mach_timebase_info(&_mach_base_info);
if (ret != 0) {
log_fatal("Unable to get mach_timebase_info.");
exit(-1);
}
kern_return_t ret = mach_timebase_info(&_mach_base_info);
if (ret != 0) {
log_fatal("Unable to get mach_timebase_info.");
exit(-1);
}
#endif
}
}