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

Corrige IndexBuffer

This commit is contained in:
Danilo 2024-05-21 14:20:37 -03:00
parent 9443d20ef7
commit 80ff2d2fe5
11 changed files with 74 additions and 87 deletions

View File

@ -20,7 +20,7 @@ add_executable (xna WIN32
"platform/shader-dx.cpp"
"platform/rasterizerstate-dx.cpp"
"platform/vertexbuffer-dx.cpp"
"platform/indexbuffer-dx.cpp"
"platform/samplerstate-dx.cpp"

View File

@ -6,7 +6,7 @@
namespace xna {
ConstantBuffer::ConstantBuffer() : GraphicsResource(nullptr){
impl = uNew<PlatformImplementation>();
impl = uNew<PlatformImplementation>();
}
ConstantBuffer::ConstantBuffer(sptr<GraphicsDevice> const& device) : GraphicsResource(device){
@ -67,4 +67,27 @@ namespace xna {
return true;
}
IndexBuffer::IndexBuffer() : GraphicsResource(nullptr) {
impl = uNew<PlatformImplementation>();
}
IndexBuffer::IndexBuffer(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
impl = uNew<PlatformImplementation>();
}
IndexBuffer::~IndexBuffer() {
impl = nullptr;
}
bool IndexBuffer::Apply(xna_error_ptr_arg) {
if (!m_device || !m_device->_context || !impl || !impl->dxBuffer) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
m_device->_context->IASetIndexBuffer(impl->dxBuffer, DXGI_FORMAT_R16_UINT, 0);
return true;
}
}

View File

@ -41,6 +41,10 @@ namespace xna {
}
void Game::Initialize() {
Keyboard::Initialize();
Mouse::Initialize();
GamePad::Initialize();
#if (_WIN32_WINNT >= 0x0A00 /*_WIN32_WINNT_WIN10*/)
Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
if (FAILED(initialize))
@ -53,10 +57,7 @@ namespace xna {
{
MessageBox(nullptr, "Ocorreu um erro ao chamar CoInitializeEx.", "XN65", MB_OK);
}
#endif
Keyboard::Initialize();
Mouse::Initialize();
GamePad::Initialize();
#endif
_audioEngine = New<AudioEngine>();

View File

@ -1,5 +0,0 @@
#include "platform-dx/indexbuffer-dx.hpp"
#include "platform-dx/device-dx.hpp"
namespace xna {
}

View File

@ -24,6 +24,21 @@ namespace xna {
~DataBuffer();
bool Initialize(xna_error_nullarg);
public:
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
};
class IndexBuffer : public GraphicsResource {
public:
IndexBuffer();
IndexBuffer(sptr<GraphicsDevice> const&);
~IndexBuffer();
template <typename T>
bool Initialize(std::vector<T> const& data, xna_error_nullarg);
bool Apply(xna_error_nullarg);
public:
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;

View File

@ -1,14 +0,0 @@
#ifndef XNA_GRAPHICS_INDEXBUFFER_HPP
#define XNA_GRAPHICS_INDEXBUFFER_HPP
#include "../default.hpp"
namespace xna {
class IIndexBuffer {
public:
virtual ~IIndexBuffer() {}
virtual bool Initialize(xna_error_nullarg) = 0;
};
}
#endif

View File

@ -157,7 +157,7 @@ namespace xna {
};
struct GamePadState {
GamePadState() = default;
constexpr GamePadState() = default;
constexpr bool IsButtonDown(xna::Buttons button) const {
switch (button)

View File

@ -150,4 +150,32 @@ namespace xna {
inline static uptr<DirectX::GamePad> _dxGamePad = nullptr;
};
struct IndexBuffer::PlatformImplementation {
~PlatformImplementation() {
if (dxBuffer) {
dxBuffer->Release();
dxBuffer = nullptr;
}
}
ID3D11Buffer* dxBuffer = nullptr;
};
template <typename T>
inline bool IndexBuffer::Initialize(std::vector<T> const& data, xna_error_ptr_arg) {
if (!impl || !m_device || !m_device->_device || data.empty()) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
const auto hr = DirectX::CreateStaticBuffer(m_device->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_INDEX_BUFFER, &impl->dxBuffer);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
return true;
}
}

View File

@ -1,59 +0,0 @@
#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"
#include <BufferHelpers.h>
#include <VertexTypes.h>
namespace xna {
template <typename T>
class IndexBuffer : public IIndexBuffer, public GraphicsResource {
public:
constexpr IndexBuffer(GraphicsDevice* device) : GraphicsResource(device) {}
constexpr IndexBuffer(GraphicsDevice* device, std::vector<T> const& indices) : data(indices), GraphicsResource(device) {}
virtual ~IndexBuffer() override {
if (dxBuffer) {
dxBuffer->Release();
dxBuffer = nullptr;
}
}
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(m_device->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_INDEX_BUFFER, &dxBuffer);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
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<T> data;
};
}
#endif

View File

@ -6,7 +6,6 @@
#include "game-dx.hpp"
#include "gdeviceinfo-dx.hpp"
#include "gdevicemanager-dx.hpp"
#include "indexbuffer-dx.hpp"
#include "init-dx.hpp"
#include "keyboard-dx.hpp"
#include "mouse-dx.hpp"

View File

@ -37,7 +37,6 @@
#include "graphics/device.hpp"
#include "graphics/displaymode.hpp"
#include "graphics/gresource.hpp"
#include "graphics/indexbuffer.hpp"
#include "graphics/presentparams.hpp"
#include "graphics/rasterizerstate.hpp"
#include "graphics/rendertarget.hpp"