diff --git a/framework/graphics/gresource.hpp b/framework/graphics/gresource.hpp index 74381fe..59e441d 100644 --- a/framework/graphics/gresource.hpp +++ b/framework/graphics/gresource.hpp @@ -6,11 +6,11 @@ namespace xna { class GraphicsResource { public: - GraphicsResource(GraphicsDevice* device) : m_device(device){} + GraphicsResource(sptr const& device) : m_device(device){} virtual ~GraphicsResource(){} - virtual bool Bind(GraphicsDevice* device) { + virtual bool Bind(sptr const& device) { if (device == m_device) return false; @@ -20,7 +20,7 @@ namespace xna { } protected: - GraphicsDevice* m_device = nullptr; + sptr m_device = nullptr; }; } diff --git a/framework/platform/blendstate-dx.hpp b/framework/platform/blendstate-dx.hpp index fcd3e99..1941daa 100644 --- a/framework/platform/blendstate-dx.hpp +++ b/framework/platform/blendstate-dx.hpp @@ -21,7 +21,7 @@ namespace xna { class BlendState : public IBlendState, public GraphicsResource { public: - BlendState(GraphicsDevice* device) : GraphicsResource(device) {}; + BlendState(sptr const& device) : GraphicsResource(device) {}; virtual ~BlendState() override { if (dxBlendState) { diff --git a/framework/platform/content-readers/texture2Dreader-dx.hpp b/framework/platform/content-readers/texture2Dreader-dx.hpp index 1ae47ff..02dbedf 100644 --- a/framework/platform/content-readers/texture2Dreader-dx.hpp +++ b/framework/platform/content-readers/texture2Dreader-dx.hpp @@ -20,7 +20,7 @@ namespace xna { auto a_device = ContentManager::Services()->GetService(*typeof()); auto device = std::any_cast>(a_device); - auto texture2D = New(device.get(), width, height, mipMaps, format); + auto texture2D = New(device, width, height, mipMaps, format); for (size_t level = 0; level < mipMaps; ++level) { auto elementCount = input.ReadInt32(); diff --git a/framework/platform/depthstencilstate-dx.hpp b/framework/platform/depthstencilstate-dx.hpp index 4126faa..76b16e4 100644 --- a/framework/platform/depthstencilstate-dx.hpp +++ b/framework/platform/depthstencilstate-dx.hpp @@ -8,7 +8,7 @@ namespace xna { class DepthStencilState : public IDepthStencilState, public GraphicsResource { public: - DepthStencilState(GraphicsDevice* device) : GraphicsResource(device) { + DepthStencilState(sptr const& device) : GraphicsResource(device) { dxDescription = defaultDesc(); } diff --git a/framework/platform/device-dx.cpp b/framework/platform/device-dx.cpp index 8e491dd..d068eb0 100644 --- a/framework/platform/device-dx.cpp +++ b/framework/platform/device-dx.cpp @@ -22,6 +22,8 @@ namespace xna { bool GraphicsDevice::Initialize(GameWindow& gameWindow) { reset(); + + auto _this = shared_from_this(); if (!createDevice()) return false; @@ -41,13 +43,13 @@ namespace xna { _backgroundColor[2] = GetBValue(color) / 255.0f; _backgroundColor[3] = 1.0f; - _swapChain = New(this); + _swapChain = New(_this); _swapChain->Initialize(); hr = _factory->MakeWindowAssociation(gameWindow.WindowHandle(), DXGI_MWA_NO_ALT_ENTER); if (FAILED(hr)) return false; - _renderTarget2D = New(this); + _renderTarget2D = New(_this); if (!_renderTarget2D->Initialize()) return false; @@ -64,7 +66,7 @@ namespace xna { _context->RSSetViewports(1, &view); _blendState = BlendState::NonPremultiplied(); - _blendState->Bind(this); + _blendState->Bind(_this); _blendState->Apply(); return true; diff --git a/framework/platform/device-dx.hpp b/framework/platform/device-dx.hpp index 07e013a..c9a6a4e 100644 --- a/framework/platform/device-dx.hpp +++ b/framework/platform/device-dx.hpp @@ -14,7 +14,7 @@ #include "presentparameters-dx.hpp" namespace xna { - class GraphicsDevice : public IGraphicsDevice { + class GraphicsDevice : public IGraphicsDevice, public std::enable_shared_from_this { public: GraphicsDevice(); GraphicsDevice(GraphicsDeviceInformation const& info); diff --git a/framework/platform/rasterizerstate-dx.hpp b/framework/platform/rasterizerstate-dx.hpp index 33f088e..3b46701 100644 --- a/framework/platform/rasterizerstate-dx.hpp +++ b/framework/platform/rasterizerstate-dx.hpp @@ -8,7 +8,7 @@ namespace xna { class RasterizerState : public IRasterizerState, public GraphicsResource { public: - RasterizerState(GraphicsDevice* device) : GraphicsResource(device){} + RasterizerState(sptr const& device) : GraphicsResource(device){} virtual ~RasterizerState() override { if (dxRasterizerState) { diff --git a/framework/platform/rendertarget-dx.hpp b/framework/platform/rendertarget-dx.hpp index d7430c0..0ed0667 100644 --- a/framework/platform/rendertarget-dx.hpp +++ b/framework/platform/rendertarget-dx.hpp @@ -9,7 +9,7 @@ namespace xna { class RenderTarget2D : public IRenderTarget2D, public Texture2D { public: - RenderTarget2D(GraphicsDevice* device) : Texture2D(device){} + RenderTarget2D(sptr const& device) : Texture2D(device){} virtual ~RenderTarget2D() override { if (_renderTargetView) { diff --git a/framework/platform/samplerstate-dx.hpp b/framework/platform/samplerstate-dx.hpp index 9e93f38..2b2dfe7 100644 --- a/framework/platform/samplerstate-dx.hpp +++ b/framework/platform/samplerstate-dx.hpp @@ -8,7 +8,7 @@ namespace xna { class SamplerState : public ISamplerState, public GraphicsResource { public: - SamplerState(GraphicsDevice* device) : GraphicsResource(device) { + SamplerState(sptr const& device) : GraphicsResource(device) { _description.MaxAnisotropy = 4; } diff --git a/framework/platform/shader-dx.hpp b/framework/platform/shader-dx.hpp index c0842e0..e7938f7 100644 --- a/framework/platform/shader-dx.hpp +++ b/framework/platform/shader-dx.hpp @@ -8,7 +8,7 @@ namespace xna { class Shader : public IShader, public GraphicsResource { public: - Shader(GraphicsDevice* device) : GraphicsResource(device){} + Shader(sptr const& device) : GraphicsResource(device){} virtual ~Shader() override {} @@ -19,7 +19,7 @@ namespace xna { class VertexShader : public Shader { public: - VertexShader(GraphicsDevice* device) : Shader(device){} + VertexShader(sptr const& device) : Shader(device){} virtual ~VertexShader() override { if (_vertexShader) { @@ -36,7 +36,7 @@ namespace xna { class PixelShader : public Shader { public: - PixelShader(GraphicsDevice* device) : Shader(device) {} + PixelShader(sptr const& device) : Shader(device) {} virtual ~PixelShader() override { if (_pixelShader) { diff --git a/framework/platform/swapchain-dx.hpp b/framework/platform/swapchain-dx.hpp index 9a12b0b..a229f76 100644 --- a/framework/platform/swapchain-dx.hpp +++ b/framework/platform/swapchain-dx.hpp @@ -9,7 +9,7 @@ namespace xna { class SwapChain : public ISwapChain, public GraphicsResource { public: - SwapChain(GraphicsDevice* device): GraphicsResource(device){} + SwapChain(sptr const& device): GraphicsResource(device){} virtual ~SwapChain() override { if (dxSwapChain) { diff --git a/framework/platform/texture-dx.cpp b/framework/platform/texture-dx.cpp index e5b602d..ff3c847 100644 --- a/framework/platform/texture-dx.cpp +++ b/framework/platform/texture-dx.cpp @@ -5,7 +5,8 @@ namespace xna { sptr Texture2D::FromStream(GraphicsDevice& device, String const& fileName, xna_error_ptr_arg) { - auto texture2d = New(&device); + auto _this = device.shared_from_this(); + auto texture2d = New(_this); ID3D11Resource* resource = nullptr; auto wstr = XnaHToWString(fileName); @@ -94,17 +95,17 @@ namespace xna { return true; } - Texture2D::Texture2D(GraphicsDevice* device, size_t width, size_t height) : GraphicsResource(device) { + Texture2D::Texture2D(sptr const& device, size_t width, size_t height) : GraphicsResource(device) { setDefaultDesc(); dxDescription.Width = static_cast(width); dxDescription.Height = static_cast(height); } - Texture2D::Texture2D(GraphicsDevice* device) : GraphicsResource(device) { + Texture2D::Texture2D(sptr const& device) : GraphicsResource(device) { setDefaultDesc(); } - Texture2D::Texture2D(GraphicsDevice* device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : GraphicsResource(device) + Texture2D::Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : GraphicsResource(device) { setDefaultDesc(); dxDescription.Width = static_cast(width); @@ -235,7 +236,8 @@ namespace xna { sptr Texture2D::FromMemory(GraphicsDevice& device, std::vector const& data, xna_error_ptr_arg) { - auto texture2d = New(&device); + auto _this = device.shared_from_this(); + auto texture2d = New(_this); ID3D11Resource* resource = nullptr; auto hr = DirectX::CreateWICTextureFromMemory( diff --git a/framework/platform/texture-dx.hpp b/framework/platform/texture-dx.hpp index 60f8fe4..8bfb433 100644 --- a/framework/platform/texture-dx.hpp +++ b/framework/platform/texture-dx.hpp @@ -14,9 +14,9 @@ namespace xna { setDefaultDesc(); } - Texture2D(GraphicsDevice* device); - Texture2D(GraphicsDevice* device, size_t width, size_t height); - Texture2D(GraphicsDevice* device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); + Texture2D(sptr const& device); + Texture2D(sptr const& device, size_t width, size_t height); + Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); virtual ~Texture2D() override { if (dxTexture2D) {