diff --git a/framework/platform/rasterizerstate-dx.cpp b/framework/platform/rasterizerstate-dx.cpp index a3f84b9..8267dde 100644 --- a/framework/platform/rasterizerstate-dx.cpp +++ b/framework/platform/rasterizerstate-dx.cpp @@ -1,15 +1,27 @@ -#include "platform-dx/rasterizerstate-dx.hpp" +#include "graphics/rasterizerstate.hpp" +#include "platform-dx/implementations.hpp" #include "platform-dx/device-dx.hpp" namespace xna { + + RasterizerState::RasterizerState() : GraphicsResource(nullptr){} + + RasterizerState::RasterizerState(sptr const& device) : GraphicsResource(device) {} + + RasterizerState::~RasterizerState() { + impl = nullptr; + } + bool RasterizerState::Initialize(xna_error_ptr_arg) { - if (!m_device || !m_device->_device) { + if (!impl || !m_device || !m_device->_device) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - const auto hr = m_device->_device->CreateRasterizerState(&dxDescription, &dxRasterizerState); + const auto hr = m_device->_device->CreateRasterizerState( + &impl->dxDescription, + &impl->dxRasterizerState); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -21,45 +33,73 @@ namespace xna { bool RasterizerState::Apply(xna_error_ptr_arg) { - if (!m_device || !m_device->_context) { + if (!impl || !m_device || !m_device->_context) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - if (!dxRasterizerState) { + if (!impl->dxRasterizerState) { xna_error_apply(err, XnaErrorCode::UNINTIALIZED_RESOURCE); return false; } - m_device->_context->RSSetState(dxRasterizerState); + m_device->_context->RSSetState(impl->dxRasterizerState); return true; } uptr RasterizerState::CullNone() { - auto raster = std::unique_ptr(new RasterizerState()); - raster->dxDescription.FillMode = D3D11_FILL_SOLID; - raster->dxDescription.CullMode = D3D11_CULL_MODE::D3D11_CULL_NONE; - raster->dxDescription.DepthClipEnable = true; + auto raster = uNew(); + raster->impl->dxDescription.FillMode = D3D11_FILL_SOLID; + raster->impl->dxDescription.CullMode = D3D11_CULL_MODE::D3D11_CULL_NONE; + raster->impl->dxDescription.DepthClipEnable = true; return raster; } uptr RasterizerState::CullClockwise() { - auto raster = std::unique_ptr(new RasterizerState()); - raster->dxDescription.FillMode = D3D11_FILL_SOLID; - raster->dxDescription.CullMode = D3D11_CULL_MODE::D3D11_CULL_FRONT; - raster->dxDescription.DepthClipEnable = true; + auto raster = uNew(); + raster->impl->dxDescription.FillMode = D3D11_FILL_SOLID; + raster->impl->dxDescription.CullMode = D3D11_CULL_MODE::D3D11_CULL_FRONT; + raster->impl->dxDescription.DepthClipEnable = true; return raster; } uptr RasterizerState::CullCounterClockwise() { - auto raster = std::unique_ptr(new RasterizerState()); - raster->dxDescription.FillMode = D3D11_FILL_SOLID; - raster->dxDescription.CullMode = D3D11_CULL_MODE::D3D11_CULL_BACK; - raster->dxDescription.DepthClipEnable = true; + auto raster = uNew(); + raster->impl->dxDescription.FillMode = D3D11_FILL_SOLID; + raster->impl->dxDescription.CullMode = D3D11_CULL_MODE::D3D11_CULL_BACK; + raster->impl->dxDescription.DepthClipEnable = true; return raster; } + + xna::CullMode RasterizerState::CullMode() const { + if (!impl) + return xna::CullMode::None; + + return static_cast(static_cast(impl->dxDescription.CullMode) - 1); + } + + void RasterizerState::CullMode(xna::CullMode value) { + if (!impl) + return; + + impl->dxDescription.CullMode = static_cast(static_cast(value) + 1); + } + + xna::FillMode RasterizerState::FillMode() const { + if (!impl) + return xna::FillMode::WireFrame; + + return static_cast(static_cast(impl->dxDescription.FillMode) - 2); + } + + void RasterizerState::FillMode(xna::FillMode value) { + if (!impl) + return; + + impl->dxDescription.FillMode = static_cast(static_cast(value) + 2); + } } diff --git a/framework/platform/sprite-dx.cpp b/framework/platform/sprite-dx.cpp index d6ac299..ea158d1 100644 --- a/framework/platform/sprite-dx.cpp +++ b/framework/platform/sprite-dx.cpp @@ -1,5 +1,5 @@ #include "platform-dx/device-dx.hpp" -#include "platform-dx/rasterizerstate-dx.hpp" +#include "graphics/rasterizerstate.hpp" #include "platform-dx/samplerstate-dx.hpp" #include "platform-dx/texture-dx.hpp" #include "common/color.hpp" @@ -81,7 +81,7 @@ namespace xna { blendState ? blendState->impl->dxBlendState : nullptr, samplerState ? samplerState->_samplerState : nullptr, depthStencil ? depthStencil->impl->dxDepthStencil : nullptr, - rasterizerState ? rasterizerState->dxRasterizerState : nullptr, + rasterizerState ? rasterizerState->impl->dxRasterizerState : nullptr, nullptr, matrix ); diff --git a/inc/graphics/gresource.hpp b/inc/graphics/gresource.hpp index 59e441d..13e6e2a 100644 --- a/inc/graphics/gresource.hpp +++ b/inc/graphics/gresource.hpp @@ -11,7 +11,7 @@ namespace xna { virtual ~GraphicsResource(){} virtual bool Bind(sptr const& device) { - if (device == m_device) + if (!device || device == m_device) return false; m_device = device; diff --git a/inc/graphics/rasterizerstate.hpp b/inc/graphics/rasterizerstate.hpp index 0528c9b..6763dfe 100644 --- a/inc/graphics/rasterizerstate.hpp +++ b/inc/graphics/rasterizerstate.hpp @@ -2,17 +2,28 @@ #define XNA_GRAPHICS_RASTERIZER_HPP #include "../default.hpp" +#include "gresource.hpp" namespace xna { - class IRasterizerState { + class RasterizerState : GraphicsResource { public: - virtual ~IRasterizerState() {} - virtual bool Initialize(xna_error_nullarg) = 0; - virtual bool Apply(xna_error_nullarg) = 0; - virtual xna::CullMode CullMode() const = 0; - virtual void CullMode(xna::CullMode value) = 0; - virtual xna::FillMode FillMode() const = 0; - virtual void FillMode(xna::FillMode value) = 0; + RasterizerState(); + RasterizerState(sptr const& device); + ~RasterizerState(); + bool Initialize(xna_error_nullarg); + bool Apply(xna_error_nullarg); + xna::CullMode CullMode() const; + void CullMode(xna::CullMode value); + xna::FillMode FillMode() const; + void FillMode(xna::FillMode value); + + static uptr CullNone(); + static uptr CullClockwise(); + static uptr CullCounterClockwise(); + + public: + struct PlatformImplementation; + uptr impl = nullptr; }; } diff --git a/inc/input/gamepad.hpp b/inc/input/gamepad.hpp index 043bc16..b2c32a1 100644 --- a/inc/input/gamepad.hpp +++ b/inc/input/gamepad.hpp @@ -221,12 +221,12 @@ namespace xna { return !IsButtonDown(button); } - GamePadButtons Buttons; - GamePadDPad Dpad; - bool IsConnected; - Ulong PackedNumber; - GamePadThumbSticks Sticks; - GamePadTriggers Triggers; + GamePadButtons Buttons{}; + GamePadDPad Dpad{}; + bool IsConnected{false}; + Ulong PackedNumber{0}; + GamePadThumbSticks Sticks{}; + GamePadTriggers Triggers{}; }; class GamePad { diff --git a/inc/platform-dx/implementations.hpp b/inc/platform-dx/implementations.hpp index 8bc18bc..5fed4f6 100644 --- a/inc/platform-dx/implementations.hpp +++ b/inc/platform-dx/implementations.hpp @@ -9,6 +9,7 @@ #include "input/gamepad.hpp" #include "input/keyboard.hpp" #include "input/mouse.hpp" +#include "graphics/rasterizerstate.hpp" #include "graphics/presentparams.hpp" #include "platform-dx/rendertarget-dx.hpp" #include "platform-dx/swapchain-dx.hpp" @@ -201,4 +202,16 @@ namespace xna { _dxMouse->ProcessMessage(message, wParam, lParam); } }; + + struct RasterizerState::PlatformImplementation { + ~PlatformImplementation() { + if (dxRasterizerState) { + dxRasterizerState->Release(); + dxRasterizerState = nullptr; + } + } + + ID3D11RasterizerState* dxRasterizerState = nullptr; + D3D11_RASTERIZER_DESC dxDescription{}; + }; } \ No newline at end of file diff --git a/inc/platform-dx/rasterizerstate-dx.hpp b/inc/platform-dx/rasterizerstate-dx.hpp deleted file mode 100644 index 3b46701..0000000 --- a/inc/platform-dx/rasterizerstate-dx.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef XNA_PLATFORM_RASTERIZERSTATE_DX_HPP -#define XNA_PLATFORM_RASTERIZERSTATE_DX_HPP - -#include "../graphics/rasterizerstate.hpp" -#include "../graphics/gresource.hpp" -#include "dxheaders.hpp" - -namespace xna { - class RasterizerState : public IRasterizerState, public GraphicsResource { - public: - RasterizerState(sptr const& device) : GraphicsResource(device){} - - virtual ~RasterizerState() override { - if (dxRasterizerState) { - dxRasterizerState->Release(); - dxRasterizerState = nullptr; - } - } - - virtual bool Initialize(xna_error_nullarg) override; - virtual bool Apply(xna_error_nullarg) override; - - virtual constexpr xna::CullMode CullMode() const override { - return static_cast(static_cast(dxDescription.CullMode) - 1); - } - - virtual constexpr void CullMode(xna::CullMode value) override { - dxDescription.CullMode = static_cast(static_cast(value) + 1); - } - - virtual constexpr xna::FillMode FillMode() const override { - return static_cast(static_cast(dxDescription.FillMode) - 2); - } - - virtual constexpr void FillMode(xna::FillMode value) override { - dxDescription.FillMode = static_cast(static_cast(value) + 2); - } - - static uptr CullNone(); - static uptr CullClockwise(); - static uptr CullCounterClockwise(); - - public: - ID3D11RasterizerState* dxRasterizerState = nullptr; - D3D11_RASTERIZER_DESC dxDescription{}; - - private: - RasterizerState() : GraphicsResource(nullptr){} - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/xna-dx.hpp b/inc/platform-dx/xna-dx.hpp index a2d6468..aee68dd 100644 --- a/inc/platform-dx/xna-dx.hpp +++ b/inc/platform-dx/xna-dx.hpp @@ -7,7 +7,6 @@ #include "gdeviceinfo-dx.hpp" #include "gdevicemanager-dx.hpp" #include "init-dx.hpp" -#include "rasterizerstate-dx.hpp" #include "rendertarget-dx.hpp" #include "samplerstate-dx.hpp" #include "shader-dx.hpp"