diff --git a/src/openrct2-ui/UiContext.Linux.cpp b/src/openrct2-ui/UiContext.Linux.cpp index 8d9fa7cd1a..5aba86cebc 100644 --- a/src/openrct2-ui/UiContext.Linux.cpp +++ b/src/openrct2-ui/UiContext.Linux.cpp @@ -374,7 +374,7 @@ namespace OpenRCT2::Ui static void ThrowMissingDialogApp() { - IUiContext * uiContext = GetContext()->GetUiContext(); + auto uiContext = GetContext()->GetUiContext(); std::string dialogMissingWarning = language_get_string(STR_MISSING_DIALOG_APPLICATION_ERROR); uiContext->ShowMessageBox(dialogMissingWarning); throw std::runtime_error(dialogMissingWarning); diff --git a/src/openrct2-ui/UiContext.cpp b/src/openrct2-ui/UiContext.cpp index bf63f3222f..6f16ce6715 100644 --- a/src/openrct2-ui/UiContext.cpp +++ b/src/openrct2-ui/UiContext.cpp @@ -34,7 +34,7 @@ #include #include #include "CursorRepository.h" -#include "drawing/engines/DrawingEngines.h" +#include "drawing/engines/DrawingEngineFactory.hpp" #include "input/KeyboardShortcuts.h" #include "SDLException.h" #include "TextComposition.h" @@ -261,20 +261,9 @@ public: } // Drawing - IDrawingEngine * CreateDrawingEngine(DRAWING_ENGINE_TYPE type) override + std::shared_ptr GetDrawingEngineFactory() override { - switch ((sint32)type) { - case DRAWING_ENGINE_SOFTWARE: - return CreateSoftwareDrawingEngine(this); - case DRAWING_ENGINE_SOFTWARE_WITH_HARDWARE_DISPLAY: - return CreateHardwareDisplayDrawingEngine(this); -#ifndef DISABLE_OPENGL - case DRAWING_ENGINE_OPENGL: - return CreateOpenGLDrawingEngine(this); -#endif - default: - return nullptr; - } + return std::make_shared(); } // Text input @@ -821,6 +810,6 @@ std::unique_ptr OpenRCT2::Ui::CreateUiContext(std::shared_ptr(GetContext()->GetUiContext()); + auto uiContext = std::static_pointer_cast(GetContext()->GetUiContext()); return uiContext->GetInGameConsole(); } diff --git a/src/openrct2-ui/drawing/engines/DrawingEngineFactory.hpp b/src/openrct2-ui/drawing/engines/DrawingEngineFactory.hpp new file mode 100644 index 0000000000..5c75063c85 --- /dev/null +++ b/src/openrct2-ui/drawing/engines/DrawingEngineFactory.hpp @@ -0,0 +1,57 @@ +#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 + +#pragma once + +#include +#include +#include + +namespace OpenRCT2 +{ + namespace Ui + { + using namespace OpenRCT2::Drawing; + + interface IUiContext; + + Drawing::IDrawingEngine * CreateSoftwareDrawingEngine(std::shared_ptr uiContext); + Drawing::IDrawingEngine * CreateHardwareDisplayDrawingEngine(std::shared_ptr uiContext); +#ifndef DISABLE_OPENGL + Drawing::IDrawingEngine * CreateOpenGLDrawingEngine(std::shared_ptr uiContext); +#endif + + class DrawingEngineFactory final : public IDrawingEngineFactory + { + public: + IDrawingEngine * Create(DRAWING_ENGINE_TYPE type, std::shared_ptr uiContext) override + { + switch ((sint32)type) { + case DRAWING_ENGINE_SOFTWARE: + return CreateSoftwareDrawingEngine(uiContext); + case DRAWING_ENGINE_SOFTWARE_WITH_HARDWARE_DISPLAY: + return CreateHardwareDisplayDrawingEngine(uiContext); +#ifndef DISABLE_OPENGL + case DRAWING_ENGINE_OPENGL: + return CreateOpenGLDrawingEngine(uiContext); +#endif + default: + return nullptr; + } + } + }; + } +} diff --git a/src/openrct2-ui/drawing/engines/DrawingEngines.h b/src/openrct2-ui/drawing/engines/DrawingEngines.h deleted file mode 100644 index 00b36cd355..0000000000 --- a/src/openrct2-ui/drawing/engines/DrawingEngines.h +++ /dev/null @@ -1,38 +0,0 @@ -#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 - -#pragma once - -#include - -namespace OpenRCT2 -{ - namespace Drawing - { - interface IDrawingEngine; - } - - namespace Ui - { - interface IUiContext; - - Drawing::IDrawingEngine * CreateSoftwareDrawingEngine(IUiContext * uiContext); - Drawing::IDrawingEngine * CreateHardwareDisplayDrawingEngine(IUiContext * uiContext); -#ifndef DISABLE_OPENGL - Drawing::IDrawingEngine * CreateOpenGLDrawingEngine(IUiContext * uiContext); -#endif - } -} diff --git a/src/openrct2-ui/drawing/engines/HardwareDisplayDrawingEngine.cpp b/src/openrct2-ui/drawing/engines/HardwareDisplayDrawingEngine.cpp index 6c2bcaab81..e807a03f4c 100644 --- a/src/openrct2-ui/drawing/engines/HardwareDisplayDrawingEngine.cpp +++ b/src/openrct2-ui/drawing/engines/HardwareDisplayDrawingEngine.cpp @@ -22,7 +22,7 @@ #include #include #include -#include "DrawingEngines.h" +#include "DrawingEngineFactory.hpp" #include #include @@ -37,7 +37,7 @@ class HardwareDisplayDrawingEngine final : public X8DrawingEngine private: constexpr static uint32 DIRTY_VISUAL_TIME = 32; - IUiContext * const _uiContext; + std::shared_ptr const _uiContext; SDL_Window * _window = nullptr; SDL_Renderer * _sdlRenderer = nullptr; SDL_Texture * _screenTexture = nullptr; @@ -60,7 +60,7 @@ private: bool smoothNN = false; public: - explicit HardwareDisplayDrawingEngine(IUiContext * uiContext) + explicit HardwareDisplayDrawingEngine(std::shared_ptr uiContext) : X8DrawingEngine(uiContext), _uiContext(uiContext) { @@ -415,7 +415,7 @@ private: } }; -IDrawingEngine * OpenRCT2::Ui::CreateHardwareDisplayDrawingEngine(IUiContext * uiContext) +IDrawingEngine * OpenRCT2::Ui::CreateHardwareDisplayDrawingEngine(std::shared_ptr uiContext) { return new HardwareDisplayDrawingEngine(uiContext); } diff --git a/src/openrct2-ui/drawing/engines/SoftwareDrawingEngine.cpp b/src/openrct2-ui/drawing/engines/SoftwareDrawingEngine.cpp index 77fdbb4f7a..59eed47786 100644 --- a/src/openrct2-ui/drawing/engines/SoftwareDrawingEngine.cpp +++ b/src/openrct2-ui/drawing/engines/SoftwareDrawingEngine.cpp @@ -23,7 +23,7 @@ #include #include #include -#include "DrawingEngines.h" +#include "DrawingEngineFactory.hpp" using namespace OpenRCT2; using namespace OpenRCT2::Drawing; @@ -32,14 +32,14 @@ using namespace OpenRCT2::Ui; class SoftwareDrawingEngine final : public X8DrawingEngine { private: - IUiContext * const _uiContext; + std::shared_ptr const _uiContext; SDL_Window * _window = nullptr; SDL_Surface * _surface = nullptr; SDL_Surface * _RGBASurface = nullptr; SDL_Palette * _palette = nullptr; public: - explicit SoftwareDrawingEngine(IUiContext * uiContext) + explicit SoftwareDrawingEngine(std::shared_ptr uiContext) : X8DrawingEngine(uiContext), _uiContext(uiContext) { @@ -163,7 +163,7 @@ private: } }; -IDrawingEngine * OpenRCT2::Ui::CreateSoftwareDrawingEngine(IUiContext * uiContext) +IDrawingEngine * OpenRCT2::Ui::CreateSoftwareDrawingEngine(std::shared_ptr uiContext) { return new SoftwareDrawingEngine(uiContext); } diff --git a/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp b/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp index 7976a47812..76814f2cdb 100644 --- a/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp +++ b/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp @@ -31,7 +31,7 @@ #include #include #include -#include "../DrawingEngines.h" +#include "../DrawingEngineFactory.hpp" #include "ApplyPaletteShader.h" #include "DrawCommands.h" #include "DrawLineShader.h" @@ -121,7 +121,7 @@ public: class OpenGLDrawingEngine : public IDrawingEngine { private: - IUiContext * const _uiContext = nullptr; + std::shared_ptr const _uiContext; SDL_Window * _window = nullptr; SDL_GLContext _context = nullptr; @@ -144,7 +144,7 @@ public: SDL_Color Palette[256]; vec4 GLPalette[256]; - explicit OpenGLDrawingEngine(IUiContext * uiContext) + explicit OpenGLDrawingEngine(std::shared_ptr uiContext) : _uiContext(uiContext) { _window = (SDL_Window *)_uiContext->GetWindow(); @@ -419,7 +419,7 @@ private: } }; -IDrawingEngine * OpenRCT2::Ui::CreateOpenGLDrawingEngine(IUiContext * uiContext) +IDrawingEngine * OpenRCT2::Ui::CreateOpenGLDrawingEngine(std::shared_ptr uiContext) { return new OpenGLDrawingEngine(uiContext); } diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 74fc2a1ab0..f711ada36f 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -154,19 +154,19 @@ namespace OpenRCT2 Instance = nullptr; } - IAudioContext * GetAudioContext() override + std::shared_ptr GetAudioContext() override { - return _audioContext.get(); + return _audioContext; } - IUiContext * GetUiContext() override + std::shared_ptr GetUiContext() override { - return _uiContext.get(); + return _uiContext; } - IPlatformEnvironment * GetPlatformEnvironment() override + std::shared_ptr GetPlatformEnvironment() override { - return _env.get(); + return _env; } IObjectManager * GetObjectManager() override diff --git a/src/openrct2/Context.h b/src/openrct2/Context.h index e368935b2f..2a4460e46e 100644 --- a/src/openrct2/Context.h +++ b/src/openrct2/Context.h @@ -89,9 +89,9 @@ namespace OpenRCT2 { virtual ~IContext() = default; - virtual Audio::IAudioContext * GetAudioContext() abstract; - virtual Ui::IUiContext * GetUiContext() abstract; - virtual IPlatformEnvironment * GetPlatformEnvironment() abstract; + virtual std::shared_ptr GetAudioContext() abstract; + virtual std::shared_ptr GetUiContext() abstract; + virtual std::shared_ptr GetPlatformEnvironment() abstract; virtual IObjectManager * GetObjectManager() abstract; virtual IObjectRepository * GetObjectRepository() abstract; virtual ITrackDesignRepository * GetTrackDesignRepository() abstract; diff --git a/src/openrct2/audio/Audio.cpp b/src/openrct2/audio/Audio.cpp index 8672512a26..d744899879 100644 --- a/src/openrct2/audio/Audio.cpp +++ b/src/openrct2/audio/Audio.cpp @@ -157,7 +157,7 @@ void audio_populate_devices() { SafeFree(gAudioDevices); - IAudioContext * audioContext = OpenRCT2::GetContext()->GetAudioContext(); + auto audioContext = OpenRCT2::GetContext()->GetAudioContext(); std::vector devices = audioContext->GetOutputDevices(); // Replace blanks with localised unknown string diff --git a/src/openrct2/audio/AudioMixer.cpp b/src/openrct2/audio/AudioMixer.cpp index 955ef4170b..edd2f64108 100644 --- a/src/openrct2/audio/AudioMixer.cpp +++ b/src/openrct2/audio/AudioMixer.cpp @@ -28,13 +28,13 @@ using namespace OpenRCT2::Audio; static IAudioMixer * GetMixer() { - IAudioContext * audioContext = GetContext()->GetAudioContext(); + auto audioContext = GetContext()->GetAudioContext(); return audioContext->GetMixer(); } void Mixer_Init(const char * device) { - IAudioContext * audioContext = GetContext()->GetAudioContext(); + auto audioContext = GetContext()->GetAudioContext(); if (device == nullptr) { device = ""; @@ -140,7 +140,7 @@ void * Mixer_Play_Music(sint32 pathId, sint32 loop, sint32 streaming) { const utf8 * path = context_get_path_legacy(pathId); - IAudioContext * audioContext = GetContext()->GetAudioContext(); + auto audioContext = GetContext()->GetAudioContext(); IAudioSource * source = audioContext->CreateStreamFromWAV(path); if (source != nullptr) { diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index e09e6cf205..e321585bc5 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -742,7 +742,7 @@ bool config_find_or_browse_install_directory() { while (true) { - IUiContext * uiContext = GetContext()->GetUiContext(); + auto uiContext = GetContext()->GetUiContext(); uiContext->ShowMessageBox("OpenRCT2 needs files from the original RollerCoaster Tycoon 2 in order to work. \nPlease select the directory where you installed RollerCoaster Tycoon 2."); std::string installPath = uiContext->ShowDirectoryDialog("Please select your RCT2 directory"); diff --git a/src/openrct2/drawing/IDrawingEngine.h b/src/openrct2/drawing/IDrawingEngine.h index 448622967a..04ce7badc5 100644 --- a/src/openrct2/drawing/IDrawingEngine.h +++ b/src/openrct2/drawing/IDrawingEngine.h @@ -16,6 +16,7 @@ #pragma once +#include #include "../common.h" enum DRAWING_ENGINE @@ -40,8 +41,14 @@ enum DRAWING_ENGINE_FLAGS struct rct_drawpixelinfo; struct rct_palette_entry; +namespace OpenRCT2::Ui +{ + interface IUiContext; +} // namespace OpenRCT2::Ui + namespace OpenRCT2::Drawing { + enum class DRAWING_ENGINE_TYPE; interface IDrawingContext; interface IDrawingEngine @@ -70,6 +77,12 @@ namespace OpenRCT2::Drawing virtual void InvalidateImage(uint32 image) abstract; }; + interface IDrawingEngineFactory + { + virtual ~IDrawingEngineFactory() { } + virtual IDrawingEngine * Create(DRAWING_ENGINE_TYPE type, std::shared_ptr uiContext) abstract; + }; + interface IRainDrawer { virtual ~IRainDrawer() { } diff --git a/src/openrct2/drawing/NewDrawing.cpp b/src/openrct2/drawing/NewDrawing.cpp index c7efa10dfb..27c799a2f7 100644 --- a/src/openrct2/drawing/NewDrawing.cpp +++ b/src/openrct2/drawing/NewDrawing.cpp @@ -71,7 +71,8 @@ void drawing_engine_init() auto context = GetContext(); auto uiContext = context->GetUiContext(); - auto drawingEngine = uiContext->CreateDrawingEngine((DRAWING_ENGINE_TYPE)_drawingEngineType); + auto drawingEngineFactory = uiContext->GetDrawingEngineFactory(); + auto drawingEngine = drawingEngineFactory->Create((DRAWING_ENGINE_TYPE)_drawingEngineType, uiContext); if (drawingEngine == nullptr) { @@ -131,7 +132,7 @@ void drawing_engine_resize() { if (_drawingEngine != nullptr) { - IUiContext * uiContext = GetContext()->GetUiContext(); + auto uiContext = GetContext()->GetUiContext(); _drawingEngine->Resize(uiContext->GetWidth(), uiContext->GetHeight()); } } diff --git a/src/openrct2/drawing/Sprite.cpp b/src/openrct2/drawing/Sprite.cpp index ab212555f8..2d800ce902 100644 --- a/src/openrct2/drawing/Sprite.cpp +++ b/src/openrct2/drawing/Sprite.cpp @@ -267,7 +267,7 @@ bool gfx_load_g1(const IPlatformEnvironment& env) log_fatal("Unable to load g1 graphics"); if (!gOpenRCT2Headless) { - IUiContext * uiContext = GetContext()->GetUiContext(); + auto uiContext = GetContext()->GetUiContext(); uiContext->ShowMessageBox("Unable to load g1.dat. Your RollerCoaster Tycoon 2 path may be incorrectly set."); } return false; @@ -330,7 +330,7 @@ bool gfx_load_g2() log_fatal("Unable to load g2 graphics"); if (!gOpenRCT2Headless) { - IUiContext * uiContext = GetContext()->GetUiContext(); + auto uiContext = GetContext()->GetUiContext(); uiContext->ShowMessageBox("Unable to load g2.dat"); } } diff --git a/src/openrct2/drawing/X8DrawingEngine.cpp b/src/openrct2/drawing/X8DrawingEngine.cpp index 3fc5d77361..252a01b5e2 100644 --- a/src/openrct2/drawing/X8DrawingEngine.cpp +++ b/src/openrct2/drawing/X8DrawingEngine.cpp @@ -133,7 +133,7 @@ void X8RainDrawer::Restore() #pragma GCC diagnostic ignored "-Wsuggest-final-methods" #endif -X8DrawingEngine::X8DrawingEngine(Ui::IUiContext * uiContext) +X8DrawingEngine::X8DrawingEngine(std::shared_ptr uiContext) { _drawingContext = new X8DrawingContext(this); #ifdef __ENABLE_LIGHTFX__ diff --git a/src/openrct2/drawing/X8DrawingEngine.h b/src/openrct2/drawing/X8DrawingEngine.h index 2d6dd6d6a0..f0aecb24b0 100644 --- a/src/openrct2/drawing/X8DrawingEngine.h +++ b/src/openrct2/drawing/X8DrawingEngine.h @@ -91,7 +91,7 @@ namespace OpenRCT2 X8DrawingContext * _drawingContext; public: - explicit X8DrawingEngine(Ui::IUiContext * uiContext); + explicit X8DrawingEngine(std::shared_ptr uiContext); ~X8DrawingEngine() override; void Initialise() override; diff --git a/src/openrct2/paint/Painter.cpp b/src/openrct2/paint/Painter.cpp index d1168c95c5..7edcf106bf 100644 --- a/src/openrct2/paint/Painter.cpp +++ b/src/openrct2/paint/Painter.cpp @@ -34,7 +34,7 @@ using namespace OpenRCT2::Drawing; using namespace OpenRCT2::Paint; using namespace OpenRCT2::Ui; -Painter::Painter(IUiContext * uiContext) +Painter::Painter(std::shared_ptr uiContext) : _uiContext(uiContext) { } diff --git a/src/openrct2/paint/Painter.h b/src/openrct2/paint/Painter.h index a91f4f9bd4..31d1263105 100644 --- a/src/openrct2/paint/Painter.h +++ b/src/openrct2/paint/Painter.h @@ -38,14 +38,14 @@ namespace OpenRCT2 class Painter final { private: - Ui::IUiContext * const _uiContext; + std::shared_ptr const _uiContext; time_t _lastSecond = 0; sint32 _currentFPS = 0; sint32 _frames = 0; public: - explicit Painter(Ui::IUiContext * uiContext); + explicit Painter(std::shared_ptr uiContext); void Paint(Drawing::IDrawingEngine * de); private: diff --git a/src/openrct2/ui/DummyUiContext.cpp b/src/openrct2/ui/DummyUiContext.cpp index 24bd70554f..704df70b5b 100644 --- a/src/openrct2/ui/DummyUiContext.cpp +++ b/src/openrct2/ui/DummyUiContext.cpp @@ -66,10 +66,18 @@ namespace OpenRCT2::Ui const uint8 * GetKeysPressed() override { return nullptr; } void SetKeysPressed(uint32 keysym, uint8 scancode) override { } - // Drawing - Drawing::IDrawingEngine * CreateDrawingEngine(Drawing::DRAWING_ENGINE_TYPE type) override + class X8DrawingEngineFactory final : public IDrawingEngineFactory { - return new X8DrawingEngine(this); + IDrawingEngine * Create(DRAWING_ENGINE_TYPE type, std::shared_ptr uiContext) override + { + return new X8DrawingEngine(uiContext); + } + }; + + // Drawing + std::shared_ptr GetDrawingEngineFactory() override + { + return std::make_shared(); } // Text input diff --git a/src/openrct2/ui/UiContext.h b/src/openrct2/ui/UiContext.h index c90b55a18d..b80c725ea2 100644 --- a/src/openrct2/ui/UiContext.h +++ b/src/openrct2/ui/UiContext.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include #include "../common.h" @@ -28,8 +29,7 @@ namespace OpenRCT2 { namespace Drawing { - enum class DRAWING_ENGINE_TYPE; - interface IDrawingEngine; + interface IDrawingEngineFactory; } // namespace Drawing namespace Ui @@ -130,7 +130,7 @@ namespace OpenRCT2 virtual void SetKeysPressed(uint32 keysym, uint8 scancode) abstract; // Drawing - virtual Drawing::IDrawingEngine * CreateDrawingEngine(Drawing::DRAWING_ENGINE_TYPE type) abstract; + virtual std::shared_ptr GetDrawingEngineFactory() abstract; // Text input virtual bool IsTextInputActive() abstract;