1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-24 00:03:11 +01:00

Allow drawing engine registration via context interfaces

This commit is contained in:
Ted John
2017-03-24 02:47:43 +00:00
committed by Gymnasiast
parent c7b66ab193
commit bf3749833d
11 changed files with 304 additions and 32 deletions

View File

@@ -20,7 +20,9 @@
#include <stdlib.h>
#include <windows.h>
#include <shellapi.h>
#include <openrct2/Context.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2-ui/UiContext.h>
#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;
}

View File

@@ -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);
}

View File

@@ -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 <openrct2/common.h>
namespace OpenRCT2
{
interface IContext;
namespace Ui
{
interface IUiContext
{
virtual ~IUiContext() { }
};
IUiContext * CreateContext(IContext * context);
}
}

View File

@@ -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 <openrct2/common.h>
#include <openrct2/drawing/IDrawingEngine.h>
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

View File

@@ -21,6 +21,7 @@
#include <openrct2/drawing/IDrawingContext.h>
#include <openrct2/drawing/IDrawingEngine.h>
#include <openrct2/drawing/Rain.h>
#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);
}

View File

@@ -29,17 +29,7 @@ IDrawingEngine * DrawingEngineFactory::CreateOpenGL()
#include <vector>
#include <SDL_platform.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"
#include <openrct2/config/Config.h>
#include <openrct2/core/Console.hpp>
#include <openrct2/core/Exception.hpp>
#include <openrct2/core/Math.hpp>
@@ -51,13 +41,24 @@ IDrawingEngine * DrawingEngineFactory::CreateOpenGL()
extern "C"
{
#include <openrct2/config/Config.h>
#include <openrct2/interface/screenshot.h>
#include <openrct2/interface/window.h>
#include <openrct2/intro.h>
#include <openrct2/drawing/drawing.h>
}
#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();
}

View File

@@ -33,8 +33,10 @@
<ClCompile Include="drawing\engines\opengl\SwapFramebuffer.cpp" />
<ClCompile Include="drawing\engines\opengl\TextureCache.cpp" />
<ClCompile Include="drawing\engines\SoftwareDrawingEngine.cpp" />
<ClCompile Include="UiContext.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="drawing\engines\DrawingEngines.h" />
<ClInclude Include="drawing\engines\opengl\CopyFramebufferShader.h" />
<ClInclude Include="drawing\engines\opengl\DrawCommands.h" />
<ClInclude Include="drawing\engines\opengl\DrawImageShader.h" />
@@ -46,6 +48,7 @@
<ClInclude Include="drawing\engines\opengl\OpenGLShaderProgram.h" />
<ClInclude Include="drawing\engines\opengl\SwapFramebuffer.h" />
<ClInclude Include="drawing\engines\opengl\TextureCache.h" />
<ClInclude Include="UiContext.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{8DD8AB7D-2EA6-44E3-8265-BAF08E832951}</ProjectGuid>

42
src/openrct2/Context.cpp Normal file
View File

@@ -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();
}

33
src/openrct2/Context.h Normal file
View File

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

View File

@@ -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

View File

@@ -31,6 +31,25 @@ extern "C"
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)