From bf3749833d8c5039d47cc963215bb055f51fccad Mon Sep 17 00:00:00 2001 From: Ted John Date: Fri, 24 Mar 2017 02:47:43 +0000 Subject: [PATCH] Allow drawing engine registration via context interfaces --- src/openrct2-dll/openrct2-dll.cpp | 11 +++- src/openrct2-ui/UiContext.cpp | 49 ++++++++++++++++++ src/openrct2-ui/UiContext.h | 34 +++++++++++++ .../drawing/engines/DrawingEngines.h | 50 +++++++++++++++++++ .../drawing/engines/SoftwareDrawingEngine.cpp | 25 +++++++++- .../engines/opengl/OpenGLDrawingEngine.cpp | 37 +++++++++----- src/openrct2-ui/libopenrct2ui.vcxproj | 3 ++ src/openrct2/Context.cpp | 42 ++++++++++++++++ src/openrct2/Context.h | 33 ++++++++++++ src/openrct2/drawing/IDrawingEngine.h | 12 +++-- src/openrct2/drawing/NewDrawing.cpp | 40 ++++++++++----- 11 files changed, 304 insertions(+), 32 deletions(-) create mode 100644 src/openrct2-ui/UiContext.cpp create mode 100644 src/openrct2-ui/UiContext.h create mode 100644 src/openrct2-ui/drawing/engines/DrawingEngines.h create mode 100644 src/openrct2/Context.cpp create mode 100644 src/openrct2/Context.h diff --git a/src/openrct2-dll/openrct2-dll.cpp b/src/openrct2-dll/openrct2-dll.cpp index 622160bb42..8e08fd6d39 100644 --- a/src/openrct2-dll/openrct2-dll.cpp +++ b/src/openrct2-dll/openrct2-dll.cpp @@ -20,7 +20,9 @@ #include #include #include +#include #include +#include #define DLLEXPORT extern "C" __declspec(dllexport) @@ -37,7 +39,14 @@ DLLEXPORT int LaunchOpenRCT2(int argc, wchar_t * * argvW) return -1; } - int exitCode = RunOpenRCT2(argc, argv); + OpenRCT2::IContext * context = OpenRCT2::CreateContext(); + OpenRCT2::Ui::IUiContext * uiContext = OpenRCT2::Ui::CreateContext(context); + + int exitCode = context->RunOpenRCT2(argc, argv); + + delete uiContext; + delete context; + FreeCommandLineArgs(argc, argv); return exitCode; } diff --git a/src/openrct2-ui/UiContext.cpp b/src/openrct2-ui/UiContext.cpp new file mode 100644 index 0000000000..007b10a286 --- /dev/null +++ b/src/openrct2-ui/UiContext.cpp @@ -0,0 +1,49 @@ +#pragma region Copyright (c) 2014-2016 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 "UiContext.h" +#include "drawing\engines\DrawingEngines.h" + +using namespace OpenRCT2; +using namespace OpenRCT2::Ui; + +class UiContext : public IUiContext +{ +private: + IContext * const _context; + + // Drawing engines + SoftwareDrawingEngineFactory _softwareDrawingFactory; + HardwareDisplayDrawingEngineFactory _hardwareDrawingFactory; +#ifndef DISABLE_OPENGL + OpenGLDrawingEngineFactory _openglDrawingFactory; +#endif + +public: + UiContext(IContext * context) : + _context(context) + { + } + + ~UiContext() override + { + } +}; + +IUiContext * OpenRCT2::Ui::CreateContext(IContext * context) +{ + return new UiContext(context); +} diff --git a/src/openrct2-ui/UiContext.h b/src/openrct2-ui/UiContext.h new file mode 100644 index 0000000000..ff3b7727d9 --- /dev/null +++ b/src/openrct2-ui/UiContext.h @@ -0,0 +1,34 @@ +#pragma region Copyright (c) 2014-2016 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 +{ + interface IContext; + + namespace Ui + { + interface IUiContext + { + virtual ~IUiContext() { } + }; + + IUiContext * CreateContext(IContext * context); + } +} diff --git a/src/openrct2-ui/drawing/engines/DrawingEngines.h b/src/openrct2-ui/drawing/engines/DrawingEngines.h new file mode 100644 index 0000000000..f824c203d3 --- /dev/null +++ b/src/openrct2-ui/drawing/engines/DrawingEngines.h @@ -0,0 +1,50 @@ +#pragma region Copyright (c) 2014-2016 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 + +interface IDrawingEngineFactory; + +class SoftwareDrawingEngineFactory : public IDrawingEngineFactory +{ +public: + SoftwareDrawingEngineFactory(); + ~SoftwareDrawingEngineFactory() override; + IDrawingEngine * Create() override; +}; + +class HardwareDisplayDrawingEngineFactory : public IDrawingEngineFactory +{ +public: + HardwareDisplayDrawingEngineFactory(); + ~HardwareDisplayDrawingEngineFactory() override; + IDrawingEngine * Create() override; +}; + +#ifndef DISABLE_OPENGL + +class OpenGLDrawingEngineFactory : public IDrawingEngineFactory +{ +public: + OpenGLDrawingEngineFactory(); + ~OpenGLDrawingEngineFactory() override; + IDrawingEngine * Create() override; +}; + +#endif // DISABLE_OPENGL diff --git a/src/openrct2-ui/drawing/engines/SoftwareDrawingEngine.cpp b/src/openrct2-ui/drawing/engines/SoftwareDrawingEngine.cpp index f54ad0853d..d31ce3c916 100644 --- a/src/openrct2-ui/drawing/engines/SoftwareDrawingEngine.cpp +++ b/src/openrct2-ui/drawing/engines/SoftwareDrawingEngine.cpp @@ -21,6 +21,7 @@ #include #include #include +#include "DrawingEngines.h" extern "C" { @@ -827,12 +828,32 @@ private: } }; -IDrawingEngine * DrawingEngineFactory::CreateSoftware() +SoftwareDrawingEngineFactory::SoftwareDrawingEngineFactory() +{ + DrawingEngineFactory::Register(DRAWING_ENGINE_SOFTWARE, this); +} + +SoftwareDrawingEngineFactory::~SoftwareDrawingEngineFactory() +{ + DrawingEngineFactory::Unregister(DRAWING_ENGINE_SOFTWARE); +} + +IDrawingEngine * SoftwareDrawingEngineFactory::Create() { return new SoftwareDrawingEngine(false); } -IDrawingEngine * DrawingEngineFactory::CreateSoftwareWithHardwareDisplay() +HardwareDisplayDrawingEngineFactory::HardwareDisplayDrawingEngineFactory() +{ + DrawingEngineFactory::Register(DRAWING_ENGINE_SOFTWARE_WITH_HARDWARE_DISPLAY, this); +} + +HardwareDisplayDrawingEngineFactory::~HardwareDisplayDrawingEngineFactory() +{ + DrawingEngineFactory::Unregister(DRAWING_ENGINE_SOFTWARE_WITH_HARDWARE_DISPLAY); +} + +IDrawingEngine * HardwareDisplayDrawingEngineFactory::Create() { return new SoftwareDrawingEngine(true); } diff --git a/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp b/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp index 8e95f504d8..0d7d431902 100644 --- a/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp +++ b/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp @@ -29,17 +29,7 @@ IDrawingEngine * DrawingEngineFactory::CreateOpenGL() #include #include -#include "GLSLTypes.h" -#include "OpenGLAPI.h" -#include "OpenGLFramebuffer.h" -#include "CopyFramebufferShader.h" -#include "DrawImageShader.h" -#include "DrawLineShader.h" -#include "FillRectShader.h" -#include "SwapFramebuffer.h" -#include "TextureCache.h" -#include "DrawCommands.h" - +#include #include #include #include @@ -51,13 +41,24 @@ IDrawingEngine * DrawingEngineFactory::CreateOpenGL() extern "C" { - #include #include #include #include #include } +#include "../DrawingEngines.h" +#include "GLSLTypes.h" +#include "OpenGLAPI.h" +#include "OpenGLFramebuffer.h" +#include "CopyFramebufferShader.h" +#include "DrawImageShader.h" +#include "DrawLineShader.h" +#include "FillRectShader.h" +#include "SwapFramebuffer.h" +#include "TextureCache.h" +#include "DrawCommands.h" + struct OpenGLVersion { GLint Major; @@ -498,7 +499,17 @@ private: } }; -IDrawingEngine * DrawingEngineFactory::CreateOpenGL() +OpenGLDrawingEngineFactory::OpenGLDrawingEngineFactory() +{ + DrawingEngineFactory::Register(DRAWING_ENGINE_OPENGL, this); +} + +OpenGLDrawingEngineFactory::~OpenGLDrawingEngineFactory() +{ + DrawingEngineFactory::Unregister(DRAWING_ENGINE_OPENGL); +} + +IDrawingEngine * OpenGLDrawingEngineFactory::Create() { return new OpenGLDrawingEngine(); } diff --git a/src/openrct2-ui/libopenrct2ui.vcxproj b/src/openrct2-ui/libopenrct2ui.vcxproj index 6a6d997ccd..9e3a847bee 100644 --- a/src/openrct2-ui/libopenrct2ui.vcxproj +++ b/src/openrct2-ui/libopenrct2ui.vcxproj @@ -33,8 +33,10 @@ + + @@ -46,6 +48,7 @@ + {8DD8AB7D-2EA6-44E3-8265-BAF08E832951} diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp new file mode 100644 index 0000000000..d8b66ec73d --- /dev/null +++ b/src/openrct2/Context.cpp @@ -0,0 +1,42 @@ +#pragma region Copyright (c) 2014-2016 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 "Context.h" +#include "OpenRCT2.h" + +using namespace OpenRCT2; + +class Context : public IContext +{ +public: + Context() + { + } + + ~Context() override + { + } + + sint32 RunOpenRCT2(int argc, char * * argv) override + { + return ::RunOpenRCT2(argc, argv); + } +}; + +IContext * OpenRCT2::CreateContext() +{ + return new Context(); +} diff --git a/src/openrct2/Context.h b/src/openrct2/Context.h new file mode 100644 index 0000000000..5557f153f8 --- /dev/null +++ b/src/openrct2/Context.h @@ -0,0 +1,33 @@ +#pragma region Copyright (c) 2014-2016 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 +{ + /** + * Represents an instance of OpenRCT2 and can be used to get various services. + */ + interface IContext + { + virtual ~IContext() { } + virtual sint32 RunOpenRCT2(int argc, char * * argv) abstract; + }; + + IContext * CreateContext(); +} diff --git a/src/openrct2/drawing/IDrawingEngine.h b/src/openrct2/drawing/IDrawingEngine.h index a9e7287201..1c3a706f4b 100644 --- a/src/openrct2/drawing/IDrawingEngine.h +++ b/src/openrct2/drawing/IDrawingEngine.h @@ -26,6 +26,7 @@ enum DRAWING_ENGINE DRAWING_ENGINE_SOFTWARE, DRAWING_ENGINE_SOFTWARE_WITH_HARDWARE_DISPLAY, DRAWING_ENGINE_OPENGL, + DRAWING_ENGINE_COUNT, }; enum DRAWING_ENGINE_FLAGS @@ -66,11 +67,16 @@ interface IDrawingEngine virtual void InvalidateImage(uint32 image) abstract; }; +interface IDrawingEngineFactory +{ + virtual ~IDrawingEngineFactory() { } + virtual IDrawingEngine * Create() abstract; +}; + namespace DrawingEngineFactory { - IDrawingEngine * CreateSoftware(); - IDrawingEngine * CreateSoftwareWithHardwareDisplay(); - IDrawingEngine * CreateOpenGL(); + void Register(DRAWING_ENGINE type, IDrawingEngineFactory * factory); + void Unregister(DRAWING_ENGINE type); } interface IRainDrawer diff --git a/src/openrct2/drawing/NewDrawing.cpp b/src/openrct2/drawing/NewDrawing.cpp index 5e2b479ad8..9f5360632c 100644 --- a/src/openrct2/drawing/NewDrawing.cpp +++ b/src/openrct2/drawing/NewDrawing.cpp @@ -29,8 +29,27 @@ extern "C" #include "../rct2.h" } -static sint32 _drawingEngineType = DRAWING_ENGINE_SOFTWARE; -static IDrawingEngine * _drawingEngine = nullptr; +static sint32 _drawingEngineType = DRAWING_ENGINE_SOFTWARE; +static IDrawingEngine * _drawingEngine = nullptr; +static IDrawingEngineFactory * _drawingEngineFactories[DRAWING_ENGINE_COUNT] = { nullptr }; + +void DrawingEngineFactory::Register(DRAWING_ENGINE type, IDrawingEngineFactory * factory) +{ + if (_drawingEngineFactories[type] != nullptr) + { + throw std::invalid_argument("Engine already registered."); + } + _drawingEngineFactories[type] = factory; +} + +void DrawingEngineFactory::Unregister(DRAWING_ENGINE type) +{ + if (_drawingEngineFactories[type] == nullptr) + { + throw std::invalid_argument("Engine not registered."); + } + _drawingEngineFactories[type] = nullptr; +} extern "C" { @@ -69,20 +88,15 @@ extern "C" { assert(_drawingEngine == nullptr); - IDrawingEngine * drawingEngine = nullptr; _drawingEngineType = gConfigGeneral.drawing_engine; - switch (_drawingEngineType) { - case DRAWING_ENGINE_SOFTWARE: - drawingEngine = DrawingEngineFactory::CreateSoftware(); - break; - case DRAWING_ENGINE_SOFTWARE_WITH_HARDWARE_DISPLAY: - drawingEngine = DrawingEngineFactory::CreateSoftwareWithHardwareDisplay(); - break; - case DRAWING_ENGINE_OPENGL: - drawingEngine = DrawingEngineFactory::CreateOpenGL(); - break; + IDrawingEngineFactory * deFactory = _drawingEngineFactories[_drawingEngineType]; + if (deFactory == nullptr) + { + log_fatal("Drawing engine not registered."); + exit(-1); } + IDrawingEngine * drawingEngine = deFactory->Create(); if (drawingEngine == nullptr) { if (_drawingEngineType == DRAWING_ENGINE_SOFTWARE)