mirror of
https://github.com/OpenRCT2/OpenRCT2.git
synced 2025-01-24 11:22:02 -05:00
Refactor registration of drawing engines
This commit is contained in:
parent
bf3749833d
commit
006a76c099
9 changed files with 143 additions and 78 deletions
|
@ -14,6 +14,10 @@
|
|||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <openrct2/Context.h>
|
||||
#include <openrct2/core/Registration.hpp>
|
||||
#include "UiContext.h"
|
||||
#include "drawing\engines\DrawingEngines.h"
|
||||
|
||||
|
@ -24,6 +28,7 @@ class UiContext : public IUiContext
|
|||
{
|
||||
private:
|
||||
IContext * const _context;
|
||||
std::vector<std::unique_ptr<IRegistration>> _registrations;
|
||||
|
||||
// Drawing engines
|
||||
SoftwareDrawingEngineFactory _softwareDrawingFactory;
|
||||
|
@ -36,6 +41,9 @@ public:
|
|||
UiContext(IContext * context) :
|
||||
_context(context)
|
||||
{
|
||||
_registrations.emplace_back(context->RegisterDrawingEngine(DRAWING_ENGINE_SOFTWARE, &_softwareDrawingFactory));
|
||||
_registrations.emplace_back(context->RegisterDrawingEngine(DRAWING_ENGINE_SOFTWARE_WITH_HARDWARE_DISPLAY, &_hardwareDrawingFactory));
|
||||
_registrations.emplace_back(context->RegisterDrawingEngine(DRAWING_ENGINE_OPENGL, &_openglDrawingFactory));
|
||||
}
|
||||
|
||||
~UiContext() override
|
||||
|
|
|
@ -19,32 +19,27 @@
|
|||
#include <openrct2/common.h>
|
||||
#include <openrct2/drawing/IDrawingEngine.h>
|
||||
|
||||
interface IDrawingEngineFactory;
|
||||
|
||||
class SoftwareDrawingEngineFactory : public IDrawingEngineFactory
|
||||
namespace OpenRCT2 { namespace Ui
|
||||
{
|
||||
public:
|
||||
SoftwareDrawingEngineFactory();
|
||||
~SoftwareDrawingEngineFactory() override;
|
||||
IDrawingEngine * Create() override;
|
||||
};
|
||||
class SoftwareDrawingEngineFactory : public Drawing::IDrawingEngineFactory
|
||||
{
|
||||
public:
|
||||
IDrawingEngine * Create() override;
|
||||
};
|
||||
|
||||
class HardwareDisplayDrawingEngineFactory : public IDrawingEngineFactory
|
||||
{
|
||||
public:
|
||||
HardwareDisplayDrawingEngineFactory();
|
||||
~HardwareDisplayDrawingEngineFactory() override;
|
||||
IDrawingEngine * Create() override;
|
||||
};
|
||||
class HardwareDisplayDrawingEngineFactory : public Drawing::IDrawingEngineFactory
|
||||
{
|
||||
public:
|
||||
IDrawingEngine * Create() override;
|
||||
};
|
||||
|
||||
#ifndef DISABLE_OPENGL
|
||||
#ifndef DISABLE_OPENGL
|
||||
|
||||
class OpenGLDrawingEngineFactory : public IDrawingEngineFactory
|
||||
{
|
||||
public:
|
||||
OpenGLDrawingEngineFactory();
|
||||
~OpenGLDrawingEngineFactory() override;
|
||||
IDrawingEngine * Create() override;
|
||||
};
|
||||
class OpenGLDrawingEngineFactory : public Drawing::IDrawingEngineFactory
|
||||
{
|
||||
public:
|
||||
IDrawingEngine * Create() override;
|
||||
};
|
||||
|
||||
#endif // DISABLE_OPENGL
|
||||
#endif // DISABLE_OPENGL
|
||||
} }
|
||||
|
|
|
@ -828,32 +828,12 @@ private:
|
|||
}
|
||||
};
|
||||
|
||||
SoftwareDrawingEngineFactory::SoftwareDrawingEngineFactory()
|
||||
{
|
||||
DrawingEngineFactory::Register(DRAWING_ENGINE_SOFTWARE, this);
|
||||
}
|
||||
|
||||
SoftwareDrawingEngineFactory::~SoftwareDrawingEngineFactory()
|
||||
{
|
||||
DrawingEngineFactory::Unregister(DRAWING_ENGINE_SOFTWARE);
|
||||
}
|
||||
|
||||
IDrawingEngine * SoftwareDrawingEngineFactory::Create()
|
||||
IDrawingEngine * OpenRCT2::Ui::SoftwareDrawingEngineFactory::Create()
|
||||
{
|
||||
return new SoftwareDrawingEngine(false);
|
||||
}
|
||||
|
||||
HardwareDisplayDrawingEngineFactory::HardwareDisplayDrawingEngineFactory()
|
||||
{
|
||||
DrawingEngineFactory::Register(DRAWING_ENGINE_SOFTWARE_WITH_HARDWARE_DISPLAY, this);
|
||||
}
|
||||
|
||||
HardwareDisplayDrawingEngineFactory::~HardwareDisplayDrawingEngineFactory()
|
||||
{
|
||||
DrawingEngineFactory::Unregister(DRAWING_ENGINE_SOFTWARE_WITH_HARDWARE_DISPLAY);
|
||||
}
|
||||
|
||||
IDrawingEngine * HardwareDisplayDrawingEngineFactory::Create()
|
||||
IDrawingEngine * OpenRCT2::Ui::HardwareDisplayDrawingEngineFactory::Create()
|
||||
{
|
||||
return new SoftwareDrawingEngine(true);
|
||||
}
|
||||
|
|
|
@ -499,17 +499,7 @@ private:
|
|||
}
|
||||
};
|
||||
|
||||
OpenGLDrawingEngineFactory::OpenGLDrawingEngineFactory()
|
||||
{
|
||||
DrawingEngineFactory::Register(DRAWING_ENGINE_OPENGL, this);
|
||||
}
|
||||
|
||||
OpenGLDrawingEngineFactory::~OpenGLDrawingEngineFactory()
|
||||
{
|
||||
DrawingEngineFactory::Unregister(DRAWING_ENGINE_OPENGL);
|
||||
}
|
||||
|
||||
IDrawingEngine * OpenGLDrawingEngineFactory::Create()
|
||||
IDrawingEngine * OpenRCT2::Ui::OpenGLDrawingEngineFactory::Create()
|
||||
{
|
||||
return new OpenGLDrawingEngine();
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#include "drawing/IDrawingEngine.h"
|
||||
#include "Context.h"
|
||||
#include "OpenRCT2.h"
|
||||
|
||||
|
@ -30,6 +31,11 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
IRegistration * RegisterDrawingEngine(sint32 type, Drawing::IDrawingEngineFactory * factory) override
|
||||
{
|
||||
return Drawing::DrawingEngineFactory::Register((DRAWING_ENGINE)type, factory);
|
||||
}
|
||||
|
||||
sint32 RunOpenRCT2(int argc, char * * argv) override
|
||||
{
|
||||
return ::RunOpenRCT2(argc, argv);
|
||||
|
|
|
@ -20,12 +20,20 @@
|
|||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
interface IRegistration;
|
||||
|
||||
namespace Drawing
|
||||
{
|
||||
interface IDrawingEngineFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents an instance of OpenRCT2 and can be used to get various services.
|
||||
*/
|
||||
interface IContext
|
||||
{
|
||||
virtual ~IContext() { }
|
||||
virtual ~IContext() = default;
|
||||
virtual IRegistration * RegisterDrawingEngine(sint32 type, Drawing::IDrawingEngineFactory * factory) abstract;
|
||||
virtual sint32 RunOpenRCT2(int argc, char * * argv) abstract;
|
||||
};
|
||||
|
||||
|
|
67
src/openrct2/core/Registration.hpp
Normal file
67
src/openrct2/core/Registration.hpp
Normal file
|
@ -0,0 +1,67 @@
|
|||
#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 "../common.h"
|
||||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
/**
|
||||
* Represents a registration of some service which when deleted will be
|
||||
* unregistered.
|
||||
*/
|
||||
interface IRegistration
|
||||
{
|
||||
virtual ~IRegistration() = default;
|
||||
};
|
||||
|
||||
class Registration
|
||||
{
|
||||
private:
|
||||
/**
|
||||
* Class which can wrap a function in an IRegistration.
|
||||
*/
|
||||
template<typename T>
|
||||
struct CallbackRegistration : public IRegistration
|
||||
{
|
||||
private:
|
||||
T _callback;
|
||||
|
||||
public:
|
||||
CallbackRegistration(T callback) :
|
||||
_callback(callback)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~CallbackRegistration() override
|
||||
{
|
||||
_callback();
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
/**
|
||||
* Creates a new IRegistration which when deleted, calls the given
|
||||
* function.
|
||||
*/
|
||||
template<typename T>
|
||||
static IRegistration * Create(T unregisterCallback)
|
||||
{
|
||||
return new CallbackRegistration<T>(unregisterCallback);
|
||||
}
|
||||
};
|
||||
}
|
|
@ -67,18 +67,6 @@ interface IDrawingEngine
|
|||
virtual void InvalidateImage(uint32 image) abstract;
|
||||
};
|
||||
|
||||
interface IDrawingEngineFactory
|
||||
{
|
||||
virtual ~IDrawingEngineFactory() { }
|
||||
virtual IDrawingEngine * Create() abstract;
|
||||
};
|
||||
|
||||
namespace DrawingEngineFactory
|
||||
{
|
||||
void Register(DRAWING_ENGINE type, IDrawingEngineFactory * factory);
|
||||
void Unregister(DRAWING_ENGINE type);
|
||||
}
|
||||
|
||||
interface IRainDrawer
|
||||
{
|
||||
virtual ~IRainDrawer() { }
|
||||
|
@ -90,4 +78,23 @@ interface IRainDrawer
|
|||
sint32 yStart) abstract;
|
||||
};
|
||||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
interface IRegistration;
|
||||
|
||||
namespace Drawing
|
||||
{
|
||||
interface IDrawingEngineFactory
|
||||
{
|
||||
virtual ~IDrawingEngineFactory() { }
|
||||
virtual IDrawingEngine * Create() abstract;
|
||||
};
|
||||
|
||||
namespace DrawingEngineFactory
|
||||
{
|
||||
IRegistration * Register(DRAWING_ENGINE type, IDrawingEngineFactory * factory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#pragma endregion
|
||||
|
||||
#include "../core/Exception.hpp"
|
||||
#include "../core/Registration.hpp"
|
||||
#include "IDrawingContext.h"
|
||||
#include "IDrawingEngine.h"
|
||||
#include "NewDrawing.h"
|
||||
|
@ -29,26 +30,29 @@ extern "C"
|
|||
#include "../rct2.h"
|
||||
}
|
||||
|
||||
using namespace OpenRCT2;
|
||||
using namespace OpenRCT2::Drawing;
|
||||
|
||||
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)
|
||||
IRegistration * 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)
|
||||
return Registration::Create([type, factory]() -> void
|
||||
{
|
||||
throw std::invalid_argument("Engine not registered.");
|
||||
}
|
||||
_drawingEngineFactories[type] = nullptr;
|
||||
if (_drawingEngineFactories[type] != factory)
|
||||
{
|
||||
throw std::invalid_argument("Engine not registered.");
|
||||
}
|
||||
_drawingEngineFactories[type] = nullptr;
|
||||
});
|
||||
}
|
||||
|
||||
extern "C"
|
||||
|
|
Loading…
Add table
Reference in a new issue