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

63 lines
1.6 KiB
C++
Raw Normal View History

2024-04-11 10:38:56 -03:00
#ifndef XNA_PLATFORM_VERTEXBUFFER_DX_HPP
#define XNA_PLATFORM_VERTEXBUFFER_DX_HPP
#include "../graphics/vertexbuffer.hpp"
#include "../graphics/vertexposition.hpp"
2024-04-24 14:40:14 -03:00
#include "dxheaders.hpp"
#include <BufferHelpers.h>
#include <VertexTypes.h>
#include "device-dx.hpp"
2024-04-25 09:43:21 -03:00
#include "../graphics/gresource.hpp"
2024-04-11 10:38:56 -03:00
namespace xna {
template <typename T>
2024-04-25 09:43:21 -03:00
class VertexBuffer : public IVertexBuffer, public GraphicsResource {
2024-04-11 10:38:56 -03:00
public:
2024-04-25 09:43:21 -03:00
constexpr VertexBuffer(GraphicsDevice* device) : GraphicsResource(device){}
2024-04-11 10:38:56 -03:00
2024-04-25 09:43:21 -03:00
constexpr VertexBuffer(GraphicsDevice* device, std::vector<T> const& vertices) : data(vertices), GraphicsResource(device) {}
2024-04-11 10:38:56 -03:00
virtual ~VertexBuffer() override {
if (dxBuffer) {
dxBuffer->Release();
dxBuffer = nullptr;
2024-04-11 10:38:56 -03:00
}
}
2024-04-25 09:43:21 -03:00
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;
2024-04-25 09:43:21 -03:00
}
2024-04-25 09:43:21 -03:00
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);
return false;
}
return true;
}
2024-04-25 09:43:21 -03:00
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;
2024-04-25 09:43:21 -03:00
std::vector<T> data;
2024-04-11 10:38:56 -03:00
};
}
#endif