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

Implementações em Vertex e IndexBuffer

This commit is contained in:
Danilo 2024-04-24 21:19:39 -03:00
parent da12db2c47
commit 3f0e294bbd
9 changed files with 100 additions and 132 deletions

View File

@ -68,10 +68,10 @@ namespace xna {
using BlendOperation = BlendFunction;
enum class BufferUsage {
Static,
Dynamic,
Default,
Immutable,
Staging
Dynamic,
Static,
};
enum class Buttons {

View File

@ -128,11 +128,7 @@ namespace xna {
class SpriteBatch;
using PSpriteBatch = std::shared_ptr<SpriteBatch>;
class SpriteFont;
using PSpriteFont = std::shared_ptr<SpriteFont>;
class VertexBuffer;
using PVertexBuffer = std::shared_ptr<VertexBuffer>;
class VertexInputLayout;
using PVertexInputLayout = std::shared_ptr<VertexInputLayout>;
using PSpriteFont = std::shared_ptr<SpriteFont>;
struct VertexPositionColor;
using PVertexPositionColor = std::shared_ptr<VertexPositionColor>;
class VertexShader;
@ -140,25 +136,15 @@ namespace xna {
struct Viewport;
using PViewport = std::shared_ptr<Viewport>;
//Input
class GamePad;
using PGamePad = std::shared_ptr<GamePad>;
//Input
struct GamePadTriggers;
using PGamePagTriggers = std::shared_ptr<GamePadTriggers>;
struct GamePadThumbSticks;
using PGamePadThumbSticks = std::shared_ptr<GamePadThumbSticks>;
struct GamePadDPad;
using PGamePadDPad = std::shared_ptr<GamePadDPad>;
struct GamePadCapabilities;
using PGamePadCapabilities = std::shared_ptr<GamePadCapabilities>;
struct GamePadButtons;
using PGamePadButtons = std::shared_ptr<GamePadButtons>;
struct GamePadState;
using PGamePadState = std::shared_ptr<GamePadState>;
struct KeyboardState;
using PKeyboardState = std::shared_ptr<KeyboardState>;
struct MouseState;
using PMouseState = std::shared_ptr<MouseState>;
}
#endif

View File

@ -11,6 +11,8 @@ namespace xna {
Color color{};
constexpr VertexPositionColor() = default;
constexpr VertexPositionColor(Vector3 const& position, Color const& color):
position(position), color(color){}
constexpr bool operator==(const VertexPositionColor& other) const {
return position == other.position && color == other.color;
@ -27,6 +29,11 @@ namespace xna {
return position == other.position
&& textureCoordinate == other.textureCoordinate;
}
VertexPositionTexture(const Vector3& position, const Vector2& textureCoordinate)
: position(position), textureCoordinate(textureCoordinate)
{
}
};
struct VertexPositionColorTexture {
@ -41,6 +48,11 @@ namespace xna {
&& textureCoodinate == other.textureCoodinate
&& color == other.color;
}
VertexPositionColorTexture(const Vector3& position, const Vector2& textureCoodinate, const Color& color)
: position(position), textureCoodinate(textureCoodinate), color(color)
{
}
};
struct VertexPositionNormalTexture {
@ -55,6 +67,11 @@ namespace xna {
&& normal == other.normal
&& textureCoodinate == other.textureCoodinate;
}
VertexPositionNormalTexture(const Vector3& position, const Vector3& normal, const Vector2& textureCoodinate)
: position(position), normal(normal), textureCoodinate(textureCoodinate)
{
}
};
}

View File

@ -1,47 +1,5 @@
#include "indexbuffer-dx.hpp"
#include "device-dx.hpp"
namespace xna {
bool IndexBuffer::Initialize(GraphicsDevice& device, xna_error_ptr_arg)
{
if (!device._device) {
xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL);
return false;
}
if (_buffer) {
_buffer->Release();
_buffer = nullptr;
}
//TODO: refatorar isso aqui
int IndicesPerSprite = 4;
int MaxBatchSize = 4096;
int VerticesPerSprite = 4;
std::vector<short> indices;
indices.reserve(MaxBatchSize * IndicesPerSprite);
for (short i = 0; i < (MaxBatchSize * VerticesPerSprite); i += VerticesPerSprite)
{
indices.push_back(i);
indices.push_back(i + 1);
indices.push_back(i + 2);
indices.push_back(i + 1);
indices.push_back(i + 3);
indices.push_back(i + 2);
}
_subResource.pSysMem = &indices.front();
const auto hr = device._device->CreateBuffer(&_description, &_subResource, &_buffer);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
return true;
}
namespace xna {
}

View File

@ -2,37 +2,50 @@
#define XNA_PLATFORM_INDEXBUFFER_DX_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:
IndexBuffer() {
_description.Usage = D3D11_USAGE_DEFAULT;
_description.BindFlags = D3D11_BIND_INDEX_BUFFER;
}
IndexBuffer(size_t size) {
_description.ByteWidth = static_cast<UINT>(size);
_description.Usage = D3D11_USAGE_DEFAULT;
_description.BindFlags = D3D11_BIND_INDEX_BUFFER;
}
constexpr IndexBuffer() = default;
IndexBuffer(D3D11_BUFFER_DESC desc) : _description(desc){}
constexpr IndexBuffer(std::vector<T> const& vertices) : data(vertices) {}
virtual ~IndexBuffer() override {
if (_buffer) {
_buffer->Release();
_buffer = nullptr;
if (dxBuffer) {
dxBuffer->Release();
dxBuffer = nullptr;
}
}
virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override;
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()) {
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);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
return true;
}
public:
D3D11_BUFFER_DESC _description;
ID3D11Buffer* _buffer = nullptr;
D3D11_SUBRESOURCE_DATA _subResource{};
ID3D11Buffer* dxBuffer = nullptr;
std::vector<T> data;
};
}

View File

@ -1,32 +1,4 @@
#include "vertexbuffer-dx.hpp"
#include "device-dx.hpp"
#include <BufferHelpers.h>
namespace xna {
bool VertexBuffer::Initialize(GraphicsDevice& device, xna_error_ptr_arg)
{
if (!device._device) {
xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL);
return false;
}
if (_buffer) {
_buffer->Release();
_buffer = nullptr;
}
const auto hr = device._device->CreateBuffer(
&_description,
&_subResource,
&_buffer);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
return true;
}
}

View File

@ -4,38 +4,49 @@
#include "../graphics/vertexbuffer.hpp"
#include "../graphics/vertexposition.hpp"
#include "dxheaders.hpp"
#include <BufferHelpers.h>
#include <VertexTypes.h>
#include "device-dx.hpp"
namespace xna {
template <typename T>
class VertexBuffer : public IVertexBuffer {
public:
VertexBuffer() {
_description.Usage = D3D11_USAGE_DYNAMIC;
_description.BindFlags = D3D11_BIND_VERTEX_BUFFER;
_description.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
}
constexpr VertexBuffer() = default;
VertexBuffer(size_t size) {
_description.ByteWidth = static_cast<UINT>(size);
_description.Usage = D3D11_USAGE_DYNAMIC;
_description.BindFlags = D3D11_BIND_VERTEX_BUFFER;
_description.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
}
VertexBuffer(D3D11_BUFFER_DESC desc) : _description(desc){}
constexpr VertexBuffer(std::vector<T> const& vertices) : data(vertices) {}
virtual ~VertexBuffer() override {
if (_buffer) {
_buffer->Release();
_buffer = nullptr;
if (dxBuffer) {
dxBuffer->Release();
dxBuffer = nullptr;
}
}
virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override;
virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override {
if (!device._device) {
xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL);
return false;
}
public:
D3D11_BUFFER_DESC _description{};
D3D11_SUBRESOURCE_DATA _subResource{};
ID3D11Buffer* _buffer = nullptr;
if (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);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
return true;
}
public:
ID3D11Buffer* dxBuffer = nullptr;
std::vector<T> data;
};
}

View File

@ -18,7 +18,6 @@ namespace xna {
void Initialize() override {
graphics->Initialize();
const auto modes = _graphicsDevice->_adapter->SupportedDisplayModes();
Game::Initialize();
}
@ -27,9 +26,19 @@ namespace xna {
XnaErrorCode err;
texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err);
auto audio = AudioEngine();
std::vector<VertexPositionColor> data(3);
data[0] = VertexPositionColor(Vector3(0.0F, 0.5F, 0.5F), Colors::AliceBlue);
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<VertexPositionColor> vbuffer(data);
vbuffer.Initialize(*_graphicsDevice, &err);
D3D11_BUFFER_DESC desc;
vbuffer.dxBuffer->GetDesc(&desc);
Game::LoadContent();
}

View File

@ -18,6 +18,8 @@
#include "platform/spritebatch-dx.hpp"
#include "platform/texture-dx.hpp"
#include "platform/window-dx.hpp"
#include "graphics/vertexposition.hpp"
#include "platform/vertexbuffer-dx.hpp"
#include "Windows.h"
#include <iostream>