1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementa GraphicsResource

This commit is contained in:
Danilo 2024-04-25 09:43:21 -03:00
parent 3f0e294bbd
commit a7cef87f59
6 changed files with 73 additions and 28 deletions

View File

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

View File

@ -7,7 +7,7 @@ namespace xna {
class IIndexBuffer { class IIndexBuffer {
public: public:
virtual ~IIndexBuffer() {} virtual ~IIndexBuffer() {}
virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) = 0; virtual bool Initialize(xna_error_nullarg) = 0;
}; };
} }

View File

@ -7,7 +7,8 @@ namespace xna {
class IVertexBuffer { class IVertexBuffer {
public: public:
virtual ~IVertexBuffer(){} 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;
}; };
} }

View File

@ -1,6 +1,7 @@
#ifndef XNA_PLATFORM_INDEXBUFFER_DX_HPP #ifndef XNA_PLATFORM_INDEXBUFFER_DX_HPP
#define XNA_PLATFORM_INDEXBUFFER_DX_HPP #define XNA_PLATFORM_INDEXBUFFER_DX_HPP
#include "../graphics/gresource.hpp"
#include "../graphics/indexbuffer.hpp" #include "../graphics/indexbuffer.hpp"
#include "device-dx.hpp" #include "device-dx.hpp"
#include "dxheaders.hpp" #include "dxheaders.hpp"
@ -9,11 +10,11 @@
namespace xna { namespace xna {
template <typename T> template <typename T>
class IndexBuffer : public IIndexBuffer { class IndexBuffer : public IIndexBuffer, public GraphicsResource {
public: public:
constexpr IndexBuffer() = default; constexpr IndexBuffer(GraphicsDevice* device) : GraphicsResource(device) {}
constexpr IndexBuffer(std::vector<T> const& vertices) : data(vertices) {} constexpr IndexBuffer(GraphicsDevice* device, std::vector<T> const& indices) : data(indices), GraphicsResource(device) {}
virtual ~IndexBuffer() override { virtual ~IndexBuffer() override {
if (dxBuffer) { if (dxBuffer) {
@ -22,18 +23,13 @@ namespace xna {
} }
} }
virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override { virtual bool Initialize(xna_error_ptr_arg) override {
if (!device._device) { if (!m_device || !m_device->_device || data.empty()) {
xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL);
return false;
}
if (data.empty()) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false; 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)) { if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
@ -43,6 +39,17 @@ namespace xna {
return true; 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: public:
ID3D11Buffer* dxBuffer = nullptr; ID3D11Buffer* dxBuffer = nullptr;
std::vector<T> data; std::vector<T> data;

View File

@ -7,14 +7,15 @@
#include <BufferHelpers.h> #include <BufferHelpers.h>
#include <VertexTypes.h> #include <VertexTypes.h>
#include "device-dx.hpp" #include "device-dx.hpp"
#include "../graphics/gresource.hpp"
namespace xna { namespace xna {
template <typename T> template <typename T>
class VertexBuffer : public IVertexBuffer { class VertexBuffer : public IVertexBuffer, public GraphicsResource {
public: public:
constexpr VertexBuffer() = default; constexpr VertexBuffer(GraphicsDevice* device) : GraphicsResource(device){}
constexpr VertexBuffer(std::vector<T> const& vertices) : data(vertices) {} constexpr VertexBuffer(GraphicsDevice* device, std::vector<T> const& vertices) : data(vertices), GraphicsResource(device) {}
virtual ~VertexBuffer() override { virtual ~VertexBuffer() override {
if (dxBuffer) { if (dxBuffer) {
@ -23,18 +24,13 @@ namespace xna {
} }
} }
virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override { virtual bool Initialize(xna_error_ptr_arg) override {
if (!device._device) { if (!m_device || !m_device->_device || data.empty()) {
xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL);
return false;
}
if (data.empty()) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false; 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)) { if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
@ -44,9 +40,23 @@ namespace xna {
return true; 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: public:
ID3D11Buffer* dxBuffer = nullptr; ID3D11Buffer* dxBuffer = nullptr;
std::vector<T> data; std::vector<T> data;
}; };
} }

View File

@ -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::Red);
data[0] = VertexPositionColor(Vector3(-0.5F, -0.5F, 0.5F), Colors::AliceBlue); data[0] = VertexPositionColor(Vector3(-0.5F, -0.5F, 0.5F), Colors::AliceBlue);
VertexBuffer<VertexPositionColor> vbuffer(data); VertexBuffer<VertexPositionColor> vbuffer(_graphicsDevice.get(), data);
vbuffer.Initialize(*_graphicsDevice, &err); vbuffer.Initialize(&err);
D3D11_BUFFER_DESC desc; D3D11_BUFFER_DESC desc;
vbuffer.dxBuffer->GetDesc(&desc); vbuffer.dxBuffer->GetDesc(&desc);