Use shared_ptr more for UiContext in relation to DrawingEngine

This commit is contained in:
Ted John 2018-04-21 00:29:12 +01:00
parent 7dc170ef85
commit 7b610fd3c0
21 changed files with 125 additions and 95 deletions

View File

@ -374,7 +374,7 @@ namespace OpenRCT2::Ui
static void ThrowMissingDialogApp() static void ThrowMissingDialogApp()
{ {
IUiContext * uiContext = GetContext()->GetUiContext(); auto uiContext = GetContext()->GetUiContext();
std::string dialogMissingWarning = language_get_string(STR_MISSING_DIALOG_APPLICATION_ERROR); std::string dialogMissingWarning = language_get_string(STR_MISSING_DIALOG_APPLICATION_ERROR);
uiContext->ShowMessageBox(dialogMissingWarning); uiContext->ShowMessageBox(dialogMissingWarning);
throw std::runtime_error(dialogMissingWarning); throw std::runtime_error(dialogMissingWarning);

View File

@ -34,7 +34,7 @@
#include <openrct2/ui/WindowManager.h> #include <openrct2/ui/WindowManager.h>
#include <openrct2/Version.h> #include <openrct2/Version.h>
#include "CursorRepository.h" #include "CursorRepository.h"
#include "drawing/engines/DrawingEngines.h" #include "drawing/engines/DrawingEngineFactory.hpp"
#include "input/KeyboardShortcuts.h" #include "input/KeyboardShortcuts.h"
#include "SDLException.h" #include "SDLException.h"
#include "TextComposition.h" #include "TextComposition.h"
@ -261,20 +261,9 @@ public:
} }
// Drawing // Drawing
IDrawingEngine * CreateDrawingEngine(DRAWING_ENGINE_TYPE type) override std::shared_ptr<Drawing::IDrawingEngineFactory> GetDrawingEngineFactory() override
{ {
switch ((sint32)type) { return std::make_shared<DrawingEngineFactory>();
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;
}
} }
// Text input // Text input
@ -821,6 +810,6 @@ std::unique_ptr<IUiContext> OpenRCT2::Ui::CreateUiContext(std::shared_ptr<IPlatf
InGameConsole& OpenRCT2::Ui::GetInGameConsole() InGameConsole& OpenRCT2::Ui::GetInGameConsole()
{ {
auto uiContext = static_cast<UiContext *>(GetContext()->GetUiContext()); auto uiContext = std::static_pointer_cast<UiContext>(GetContext()->GetUiContext());
return uiContext->GetInGameConsole(); return uiContext->GetInGameConsole();
} }

View File

@ -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 <memory>
#include <openrct2/common.h>
#include <openrct2/drawing/IDrawingEngine.h>
namespace OpenRCT2
{
namespace Ui
{
using namespace OpenRCT2::Drawing;
interface IUiContext;
Drawing::IDrawingEngine * CreateSoftwareDrawingEngine(std::shared_ptr<IUiContext> uiContext);
Drawing::IDrawingEngine * CreateHardwareDisplayDrawingEngine(std::shared_ptr<IUiContext> uiContext);
#ifndef DISABLE_OPENGL
Drawing::IDrawingEngine * CreateOpenGLDrawingEngine(std::shared_ptr<IUiContext> uiContext);
#endif
class DrawingEngineFactory final : public IDrawingEngineFactory
{
public:
IDrawingEngine * Create(DRAWING_ENGINE_TYPE type, std::shared_ptr<IUiContext> 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;
}
}
};
}
}

View File

@ -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 <openrct2/common.h>
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
}
}

View File

@ -22,7 +22,7 @@
#include <openrct2/drawing/IDrawingEngine.h> #include <openrct2/drawing/IDrawingEngine.h>
#include <openrct2/drawing/X8DrawingEngine.h> #include <openrct2/drawing/X8DrawingEngine.h>
#include <openrct2/ui/UiContext.h> #include <openrct2/ui/UiContext.h>
#include "DrawingEngines.h" #include "DrawingEngineFactory.hpp"
#include <openrct2/drawing/LightFX.h> #include <openrct2/drawing/LightFX.h>
#include <openrct2/Game.h> #include <openrct2/Game.h>
@ -37,7 +37,7 @@ class HardwareDisplayDrawingEngine final : public X8DrawingEngine
private: private:
constexpr static uint32 DIRTY_VISUAL_TIME = 32; constexpr static uint32 DIRTY_VISUAL_TIME = 32;
IUiContext * const _uiContext; std::shared_ptr<IUiContext> const _uiContext;
SDL_Window * _window = nullptr; SDL_Window * _window = nullptr;
SDL_Renderer * _sdlRenderer = nullptr; SDL_Renderer * _sdlRenderer = nullptr;
SDL_Texture * _screenTexture = nullptr; SDL_Texture * _screenTexture = nullptr;
@ -60,7 +60,7 @@ private:
bool smoothNN = false; bool smoothNN = false;
public: public:
explicit HardwareDisplayDrawingEngine(IUiContext * uiContext) explicit HardwareDisplayDrawingEngine(std::shared_ptr<IUiContext> uiContext)
: X8DrawingEngine(uiContext), : X8DrawingEngine(uiContext),
_uiContext(uiContext) _uiContext(uiContext)
{ {
@ -415,7 +415,7 @@ private:
} }
}; };
IDrawingEngine * OpenRCT2::Ui::CreateHardwareDisplayDrawingEngine(IUiContext * uiContext) IDrawingEngine * OpenRCT2::Ui::CreateHardwareDisplayDrawingEngine(std::shared_ptr<IUiContext> uiContext)
{ {
return new HardwareDisplayDrawingEngine(uiContext); return new HardwareDisplayDrawingEngine(uiContext);
} }

View File

@ -23,7 +23,7 @@
#include <openrct2/drawing/X8DrawingEngine.h> #include <openrct2/drawing/X8DrawingEngine.h>
#include <openrct2/Game.h> #include <openrct2/Game.h>
#include <openrct2/ui/UiContext.h> #include <openrct2/ui/UiContext.h>
#include "DrawingEngines.h" #include "DrawingEngineFactory.hpp"
using namespace OpenRCT2; using namespace OpenRCT2;
using namespace OpenRCT2::Drawing; using namespace OpenRCT2::Drawing;
@ -32,14 +32,14 @@ using namespace OpenRCT2::Ui;
class SoftwareDrawingEngine final : public X8DrawingEngine class SoftwareDrawingEngine final : public X8DrawingEngine
{ {
private: private:
IUiContext * const _uiContext; std::shared_ptr<IUiContext> const _uiContext;
SDL_Window * _window = nullptr; SDL_Window * _window = nullptr;
SDL_Surface * _surface = nullptr; SDL_Surface * _surface = nullptr;
SDL_Surface * _RGBASurface = nullptr; SDL_Surface * _RGBASurface = nullptr;
SDL_Palette * _palette = nullptr; SDL_Palette * _palette = nullptr;
public: public:
explicit SoftwareDrawingEngine(IUiContext * uiContext) explicit SoftwareDrawingEngine(std::shared_ptr<IUiContext> uiContext)
: X8DrawingEngine(uiContext), : X8DrawingEngine(uiContext),
_uiContext(uiContext) _uiContext(uiContext)
{ {
@ -163,7 +163,7 @@ private:
} }
}; };
IDrawingEngine * OpenRCT2::Ui::CreateSoftwareDrawingEngine(IUiContext * uiContext) IDrawingEngine * OpenRCT2::Ui::CreateSoftwareDrawingEngine(std::shared_ptr<IUiContext> uiContext)
{ {
return new SoftwareDrawingEngine(uiContext); return new SoftwareDrawingEngine(uiContext);
} }

View File

@ -31,7 +31,7 @@
#include <openrct2/Intro.h> #include <openrct2/Intro.h>
#include <openrct2-ui/interface/Window.h> #include <openrct2-ui/interface/Window.h>
#include <openrct2/ui/UiContext.h> #include <openrct2/ui/UiContext.h>
#include "../DrawingEngines.h" #include "../DrawingEngineFactory.hpp"
#include "ApplyPaletteShader.h" #include "ApplyPaletteShader.h"
#include "DrawCommands.h" #include "DrawCommands.h"
#include "DrawLineShader.h" #include "DrawLineShader.h"
@ -121,7 +121,7 @@ public:
class OpenGLDrawingEngine : public IDrawingEngine class OpenGLDrawingEngine : public IDrawingEngine
{ {
private: private:
IUiContext * const _uiContext = nullptr; std::shared_ptr<IUiContext> const _uiContext;
SDL_Window * _window = nullptr; SDL_Window * _window = nullptr;
SDL_GLContext _context = nullptr; SDL_GLContext _context = nullptr;
@ -144,7 +144,7 @@ public:
SDL_Color Palette[256]; SDL_Color Palette[256];
vec4 GLPalette[256]; vec4 GLPalette[256];
explicit OpenGLDrawingEngine(IUiContext * uiContext) explicit OpenGLDrawingEngine(std::shared_ptr<IUiContext> uiContext)
: _uiContext(uiContext) : _uiContext(uiContext)
{ {
_window = (SDL_Window *)_uiContext->GetWindow(); _window = (SDL_Window *)_uiContext->GetWindow();
@ -419,7 +419,7 @@ private:
} }
}; };
IDrawingEngine * OpenRCT2::Ui::CreateOpenGLDrawingEngine(IUiContext * uiContext) IDrawingEngine * OpenRCT2::Ui::CreateOpenGLDrawingEngine(std::shared_ptr<IUiContext> uiContext)
{ {
return new OpenGLDrawingEngine(uiContext); return new OpenGLDrawingEngine(uiContext);
} }

View File

@ -154,19 +154,19 @@ namespace OpenRCT2
Instance = nullptr; Instance = nullptr;
} }
IAudioContext * GetAudioContext() override std::shared_ptr<IAudioContext> GetAudioContext() override
{ {
return _audioContext.get(); return _audioContext;
} }
IUiContext * GetUiContext() override std::shared_ptr<IUiContext> GetUiContext() override
{ {
return _uiContext.get(); return _uiContext;
} }
IPlatformEnvironment * GetPlatformEnvironment() override std::shared_ptr<IPlatformEnvironment> GetPlatformEnvironment() override
{ {
return _env.get(); return _env;
} }
IObjectManager * GetObjectManager() override IObjectManager * GetObjectManager() override

View File

@ -89,9 +89,9 @@ namespace OpenRCT2
{ {
virtual ~IContext() = default; virtual ~IContext() = default;
virtual Audio::IAudioContext * GetAudioContext() abstract; virtual std::shared_ptr<Audio::IAudioContext> GetAudioContext() abstract;
virtual Ui::IUiContext * GetUiContext() abstract; virtual std::shared_ptr<Ui::IUiContext> GetUiContext() abstract;
virtual IPlatformEnvironment * GetPlatformEnvironment() abstract; virtual std::shared_ptr<IPlatformEnvironment> GetPlatformEnvironment() abstract;
virtual IObjectManager * GetObjectManager() abstract; virtual IObjectManager * GetObjectManager() abstract;
virtual IObjectRepository * GetObjectRepository() abstract; virtual IObjectRepository * GetObjectRepository() abstract;
virtual ITrackDesignRepository * GetTrackDesignRepository() abstract; virtual ITrackDesignRepository * GetTrackDesignRepository() abstract;

View File

@ -157,7 +157,7 @@ void audio_populate_devices()
{ {
SafeFree(gAudioDevices); SafeFree(gAudioDevices);
IAudioContext * audioContext = OpenRCT2::GetContext()->GetAudioContext(); auto audioContext = OpenRCT2::GetContext()->GetAudioContext();
std::vector<std::string> devices = audioContext->GetOutputDevices(); std::vector<std::string> devices = audioContext->GetOutputDevices();
// Replace blanks with localised unknown string // Replace blanks with localised unknown string

View File

@ -28,13 +28,13 @@ using namespace OpenRCT2::Audio;
static IAudioMixer * GetMixer() static IAudioMixer * GetMixer()
{ {
IAudioContext * audioContext = GetContext()->GetAudioContext(); auto audioContext = GetContext()->GetAudioContext();
return audioContext->GetMixer(); return audioContext->GetMixer();
} }
void Mixer_Init(const char * device) void Mixer_Init(const char * device)
{ {
IAudioContext * audioContext = GetContext()->GetAudioContext(); auto audioContext = GetContext()->GetAudioContext();
if (device == nullptr) if (device == nullptr)
{ {
device = ""; device = "";
@ -140,7 +140,7 @@ void * Mixer_Play_Music(sint32 pathId, sint32 loop, sint32 streaming)
{ {
const utf8 * path = context_get_path_legacy(pathId); const utf8 * path = context_get_path_legacy(pathId);
IAudioContext * audioContext = GetContext()->GetAudioContext(); auto audioContext = GetContext()->GetAudioContext();
IAudioSource * source = audioContext->CreateStreamFromWAV(path); IAudioSource * source = audioContext->CreateStreamFromWAV(path);
if (source != nullptr) if (source != nullptr)
{ {

View File

@ -742,7 +742,7 @@ bool config_find_or_browse_install_directory()
{ {
while (true) 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."); 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"); std::string installPath = uiContext->ShowDirectoryDialog("Please select your RCT2 directory");

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include "../common.h" #include "../common.h"
enum DRAWING_ENGINE enum DRAWING_ENGINE
@ -40,8 +41,14 @@ enum DRAWING_ENGINE_FLAGS
struct rct_drawpixelinfo; struct rct_drawpixelinfo;
struct rct_palette_entry; struct rct_palette_entry;
namespace OpenRCT2::Ui
{
interface IUiContext;
} // namespace OpenRCT2::Ui
namespace OpenRCT2::Drawing namespace OpenRCT2::Drawing
{ {
enum class DRAWING_ENGINE_TYPE;
interface IDrawingContext; interface IDrawingContext;
interface IDrawingEngine interface IDrawingEngine
@ -70,6 +77,12 @@ namespace OpenRCT2::Drawing
virtual void InvalidateImage(uint32 image) abstract; virtual void InvalidateImage(uint32 image) abstract;
}; };
interface IDrawingEngineFactory
{
virtual ~IDrawingEngineFactory() { }
virtual IDrawingEngine * Create(DRAWING_ENGINE_TYPE type, std::shared_ptr<OpenRCT2::Ui::IUiContext> uiContext) abstract;
};
interface IRainDrawer interface IRainDrawer
{ {
virtual ~IRainDrawer() { } virtual ~IRainDrawer() { }

View File

@ -71,7 +71,8 @@ void drawing_engine_init()
auto context = GetContext(); auto context = GetContext();
auto uiContext = context->GetUiContext(); 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) if (drawingEngine == nullptr)
{ {
@ -131,7 +132,7 @@ void drawing_engine_resize()
{ {
if (_drawingEngine != nullptr) if (_drawingEngine != nullptr)
{ {
IUiContext * uiContext = GetContext()->GetUiContext(); auto uiContext = GetContext()->GetUiContext();
_drawingEngine->Resize(uiContext->GetWidth(), uiContext->GetHeight()); _drawingEngine->Resize(uiContext->GetWidth(), uiContext->GetHeight());
} }
} }

View File

@ -267,7 +267,7 @@ bool gfx_load_g1(const IPlatformEnvironment& env)
log_fatal("Unable to load g1 graphics"); log_fatal("Unable to load g1 graphics");
if (!gOpenRCT2Headless) 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."); uiContext->ShowMessageBox("Unable to load g1.dat. Your RollerCoaster Tycoon 2 path may be incorrectly set.");
} }
return false; return false;
@ -330,7 +330,7 @@ bool gfx_load_g2()
log_fatal("Unable to load g2 graphics"); log_fatal("Unable to load g2 graphics");
if (!gOpenRCT2Headless) if (!gOpenRCT2Headless)
{ {
IUiContext * uiContext = GetContext()->GetUiContext(); auto uiContext = GetContext()->GetUiContext();
uiContext->ShowMessageBox("Unable to load g2.dat"); uiContext->ShowMessageBox("Unable to load g2.dat");
} }
} }

View File

@ -133,7 +133,7 @@ void X8RainDrawer::Restore()
#pragma GCC diagnostic ignored "-Wsuggest-final-methods" #pragma GCC diagnostic ignored "-Wsuggest-final-methods"
#endif #endif
X8DrawingEngine::X8DrawingEngine(Ui::IUiContext * uiContext) X8DrawingEngine::X8DrawingEngine(std::shared_ptr<Ui::IUiContext> uiContext)
{ {
_drawingContext = new X8DrawingContext(this); _drawingContext = new X8DrawingContext(this);
#ifdef __ENABLE_LIGHTFX__ #ifdef __ENABLE_LIGHTFX__

View File

@ -91,7 +91,7 @@ namespace OpenRCT2
X8DrawingContext * _drawingContext; X8DrawingContext * _drawingContext;
public: public:
explicit X8DrawingEngine(Ui::IUiContext * uiContext); explicit X8DrawingEngine(std::shared_ptr<Ui::IUiContext> uiContext);
~X8DrawingEngine() override; ~X8DrawingEngine() override;
void Initialise() override; void Initialise() override;

View File

@ -34,7 +34,7 @@ using namespace OpenRCT2::Drawing;
using namespace OpenRCT2::Paint; using namespace OpenRCT2::Paint;
using namespace OpenRCT2::Ui; using namespace OpenRCT2::Ui;
Painter::Painter(IUiContext * uiContext) Painter::Painter(std::shared_ptr<IUiContext> uiContext)
: _uiContext(uiContext) : _uiContext(uiContext)
{ {
} }

View File

@ -38,14 +38,14 @@ namespace OpenRCT2
class Painter final class Painter final
{ {
private: private:
Ui::IUiContext * const _uiContext; std::shared_ptr<Ui::IUiContext> const _uiContext;
time_t _lastSecond = 0; time_t _lastSecond = 0;
sint32 _currentFPS = 0; sint32 _currentFPS = 0;
sint32 _frames = 0; sint32 _frames = 0;
public: public:
explicit Painter(Ui::IUiContext * uiContext); explicit Painter(std::shared_ptr<Ui::IUiContext> uiContext);
void Paint(Drawing::IDrawingEngine * de); void Paint(Drawing::IDrawingEngine * de);
private: private:

View File

@ -66,10 +66,18 @@ namespace OpenRCT2::Ui
const uint8 * GetKeysPressed() override { return nullptr; } const uint8 * GetKeysPressed() override { return nullptr; }
void SetKeysPressed(uint32 keysym, uint8 scancode) override { } void SetKeysPressed(uint32 keysym, uint8 scancode) override { }
// Drawing class X8DrawingEngineFactory final : public IDrawingEngineFactory
Drawing::IDrawingEngine * CreateDrawingEngine(Drawing::DRAWING_ENGINE_TYPE type) override
{ {
return new X8DrawingEngine(this); IDrawingEngine * Create(DRAWING_ENGINE_TYPE type, std::shared_ptr<IUiContext> uiContext) override
{
return new X8DrawingEngine(uiContext);
}
};
// Drawing
std::shared_ptr<IDrawingEngineFactory> GetDrawingEngineFactory() override
{
return std::make_shared<X8DrawingEngineFactory>();
} }
// Text input // Text input

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "../common.h" #include "../common.h"
@ -28,8 +29,7 @@ namespace OpenRCT2
{ {
namespace Drawing namespace Drawing
{ {
enum class DRAWING_ENGINE_TYPE; interface IDrawingEngineFactory;
interface IDrawingEngine;
} // namespace Drawing } // namespace Drawing
namespace Ui namespace Ui
@ -130,7 +130,7 @@ namespace OpenRCT2
virtual void SetKeysPressed(uint32 keysym, uint8 scancode) abstract; virtual void SetKeysPressed(uint32 keysym, uint8 scancode) abstract;
// Drawing // Drawing
virtual Drawing::IDrawingEngine * CreateDrawingEngine(Drawing::DRAWING_ENGINE_TYPE type) abstract; virtual std::shared_ptr<Drawing::IDrawingEngineFactory> GetDrawingEngineFactory() abstract;
// Text input // Text input
virtual bool IsTextInputActive() abstract; virtual bool IsTextInputActive() abstract;