diff --git a/framework/graphics/gresource.hpp b/framework/graphics/gresource.hpp new file mode 100644 index 0000000..74381fe --- /dev/null +++ b/framework/graphics/gresource.hpp @@ -0,0 +1,27 @@ +#ifndef XNA_GRAPHICS_GRESOURCE_HPP +#define XNA_GRAPHICS_GRESOURCE_HPP + +#include "../default.hpp" + +namespace xna { + class GraphicsResource { + public: + GraphicsResource(GraphicsDevice* device) : m_device(device){} + + virtual ~GraphicsResource(){} + + virtual bool Bind(GraphicsDevice* device) { + if (device == m_device) + return false; + + m_device = device; + + return true; + } + + protected: + GraphicsDevice* m_device = nullptr; + }; +} + +#endif \ No newline at end of file diff --git a/framework/graphics/indexbuffer.hpp b/framework/graphics/indexbuffer.hpp index 342bd96..e5b7995 100644 --- a/framework/graphics/indexbuffer.hpp +++ b/framework/graphics/indexbuffer.hpp @@ -7,7 +7,7 @@ namespace xna { class IIndexBuffer { public: virtual ~IIndexBuffer() {} - virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) = 0; + virtual bool Initialize(xna_error_nullarg) = 0; }; } diff --git a/framework/graphics/vertexbuffer.hpp b/framework/graphics/vertexbuffer.hpp index 01f8d6b..7d81904 100644 --- a/framework/graphics/vertexbuffer.hpp +++ b/framework/graphics/vertexbuffer.hpp @@ -7,7 +7,8 @@ namespace xna { class IVertexBuffer { public: virtual ~IVertexBuffer(){} - virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) = 0; + virtual bool Initialize(xna_error_nullarg) = 0; + virtual bool Apply(xna_error_nullarg) = 0; }; } diff --git a/framework/platform/indexbuffer-dx.hpp b/framework/platform/indexbuffer-dx.hpp index c272376..eb4ebba 100644 --- a/framework/platform/indexbuffer-dx.hpp +++ b/framework/platform/indexbuffer-dx.hpp @@ -1,6 +1,7 @@ #ifndef XNA_PLATFORM_INDEXBUFFER_DX_HPP #define XNA_PLATFORM_INDEXBUFFER_DX_HPP +#include "../graphics/gresource.hpp" #include "../graphics/indexbuffer.hpp" #include "device-dx.hpp" #include "dxheaders.hpp" @@ -9,11 +10,11 @@ namespace xna { template - class IndexBuffer : public IIndexBuffer { + class IndexBuffer : public IIndexBuffer, public GraphicsResource { public: - constexpr IndexBuffer() = default; + constexpr IndexBuffer(GraphicsDevice* device) : GraphicsResource(device) {} - constexpr IndexBuffer(std::vector const& vertices) : data(vertices) {} + constexpr IndexBuffer(GraphicsDevice* device, std::vector const& indices) : data(indices), GraphicsResource(device) {} virtual ~IndexBuffer() override { if (dxBuffer) { @@ -22,18 +23,13 @@ namespace xna { } } - virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override { - if (!device._device) { - xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL); - return false; - } - - if (data.empty()) { + virtual bool Initialize(xna_error_ptr_arg) override { + if (!m_device || !m_device->_device || data.empty()) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - const auto hr = DirectX::CreateStaticBuffer(device._device, data.data(), data.size(), sizeof(T), D3D11_BIND_INDEX_BUFFER, &dxBuffer); + const auto hr = DirectX::CreateStaticBuffer(m_device->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_INDEX_BUFFER, &dxBuffer); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -43,6 +39,17 @@ namespace xna { return true; } + virtual bool Apply(xna_error_ptr_arg) override { + if (!m_device || !m_device->_context || !dxBuffer) { + xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); + return false; + } + + m_device->_context->IASetIndexBuffer(dxBuffer, DXGI_FORMAT_R16_UINT, 0); + + return true; + } + public: ID3D11Buffer* dxBuffer = nullptr; std::vector data; diff --git a/framework/platform/vertexbuffer-dx.hpp b/framework/platform/vertexbuffer-dx.hpp index 8a1b339..fdc89bb 100644 --- a/framework/platform/vertexbuffer-dx.hpp +++ b/framework/platform/vertexbuffer-dx.hpp @@ -7,14 +7,15 @@ #include #include #include "device-dx.hpp" +#include "../graphics/gresource.hpp" namespace xna { template - class VertexBuffer : public IVertexBuffer { + class VertexBuffer : public IVertexBuffer, public GraphicsResource { public: - constexpr VertexBuffer() = default; + constexpr VertexBuffer(GraphicsDevice* device) : GraphicsResource(device){} - constexpr VertexBuffer(std::vector const& vertices) : data(vertices) {} + constexpr VertexBuffer(GraphicsDevice* device, std::vector const& vertices) : data(vertices), GraphicsResource(device) {} virtual ~VertexBuffer() override { if (dxBuffer) { @@ -23,18 +24,13 @@ namespace xna { } } - virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override { - if (!device._device) { - xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL); - return false; - } - - if (data.empty()) { + virtual bool Initialize(xna_error_ptr_arg) override { + if (!m_device || !m_device->_device || data.empty()) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; - } + } - const auto hr = DirectX::CreateStaticBuffer(device._device, data.data(), data.size(), sizeof(T), D3D11_BIND_VERTEX_BUFFER, &dxBuffer); + const auto hr = DirectX::CreateStaticBuffer(m_device->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_VERTEX_BUFFER, &dxBuffer); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -44,9 +40,23 @@ namespace xna { return true; } + virtual bool Apply(xna_error_ptr_arg) override { + if (!m_device || !m_device->_context || !dxBuffer) { + xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); + return false; + } + + UINT stride = sizeof(T); + UINT offset = 0; + m_device->_context->IASetVertexBuffers(0, 1, + &dxBuffer, &stride, &offset); + + return true; + } + public: ID3D11Buffer* dxBuffer = nullptr; - std::vector data; + std::vector data; }; } diff --git a/framework/xna.cpp b/framework/xna.cpp index 6c5f80a..14a365f 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -32,8 +32,8 @@ namespace xna { data[0] = VertexPositionColor(Vector3(0.5F, -0.5F, 0.5F), Colors::Red); data[0] = VertexPositionColor(Vector3(-0.5F, -0.5F, 0.5F), Colors::AliceBlue); - VertexBuffer vbuffer(data); - vbuffer.Initialize(*_graphicsDevice, &err); + VertexBuffer vbuffer(_graphicsDevice.get(), data); + vbuffer.Initialize(&err); D3D11_BUFFER_DESC desc; vbuffer.dxBuffer->GetDesc(&desc);