From 3f0e294bbdd3c761d98b90286afc82d4d1a08753 Mon Sep 17 00:00:00 2001 From: Danilo Date: Wed, 24 Apr 2024 21:19:39 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20Vertex=20e=20In?= =?UTF-8?q?dexBuffer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/enums.hpp | 6 +-- framework/forward.hpp | 18 +-------- framework/graphics/vertexposition.hpp | 17 +++++++++ framework/platform/indexbuffer-dx.cpp | 44 +-------------------- framework/platform/indexbuffer-dx.hpp | 49 +++++++++++++++--------- framework/platform/vertexbuffer-dx.cpp | 28 -------------- framework/platform/vertexbuffer-dx.hpp | 53 ++++++++++++++++---------- framework/xna.cpp | 15 ++++++-- framework/xna.h | 2 + 9 files changed, 100 insertions(+), 132 deletions(-) diff --git a/framework/enums.hpp b/framework/enums.hpp index 720756f..9a0a85b 100644 --- a/framework/enums.hpp +++ b/framework/enums.hpp @@ -68,10 +68,10 @@ namespace xna { using BlendOperation = BlendFunction; enum class BufferUsage { - Static, - Dynamic, + Default, Immutable, - Staging + Dynamic, + Static, }; enum class Buttons { diff --git a/framework/forward.hpp b/framework/forward.hpp index b659d89..17686b9 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -128,11 +128,7 @@ namespace xna { class SpriteBatch; using PSpriteBatch = std::shared_ptr; class SpriteFont; - using PSpriteFont = std::shared_ptr; - class VertexBuffer; - using PVertexBuffer = std::shared_ptr; - class VertexInputLayout; - using PVertexInputLayout = std::shared_ptr; + using PSpriteFont = std::shared_ptr; struct VertexPositionColor; using PVertexPositionColor = std::shared_ptr; class VertexShader; @@ -140,25 +136,15 @@ namespace xna { struct Viewport; using PViewport = std::shared_ptr; - //Input - class GamePad; - using PGamePad = std::shared_ptr; + //Input struct GamePadTriggers; - using PGamePagTriggers = std::shared_ptr; struct GamePadThumbSticks; - using PGamePadThumbSticks = std::shared_ptr; struct GamePadDPad; - using PGamePadDPad = std::shared_ptr; struct GamePadCapabilities; - using PGamePadCapabilities = std::shared_ptr; struct GamePadButtons; - using PGamePadButtons = std::shared_ptr; struct GamePadState; - using PGamePadState = std::shared_ptr; struct KeyboardState; - using PKeyboardState = std::shared_ptr; struct MouseState; - using PMouseState = std::shared_ptr; } #endif \ No newline at end of file diff --git a/framework/graphics/vertexposition.hpp b/framework/graphics/vertexposition.hpp index c6c301e..5594702 100644 --- a/framework/graphics/vertexposition.hpp +++ b/framework/graphics/vertexposition.hpp @@ -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) + { + } }; } diff --git a/framework/platform/indexbuffer-dx.cpp b/framework/platform/indexbuffer-dx.cpp index 67d5d2a..0c6a893 100644 --- a/framework/platform/indexbuffer-dx.cpp +++ b/framework/platform/indexbuffer-dx.cpp @@ -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 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 { } \ No newline at end of file diff --git a/framework/platform/indexbuffer-dx.hpp b/framework/platform/indexbuffer-dx.hpp index e105270..c272376 100644 --- a/framework/platform/indexbuffer-dx.hpp +++ b/framework/platform/indexbuffer-dx.hpp @@ -2,37 +2,50 @@ #define XNA_PLATFORM_INDEXBUFFER_DX_HPP #include "../graphics/indexbuffer.hpp" +#include "device-dx.hpp" #include "dxheaders.hpp" +#include +#include namespace xna { + template 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(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 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 data; }; } diff --git a/framework/platform/vertexbuffer-dx.cpp b/framework/platform/vertexbuffer-dx.cpp index 66171b7..f866aa2 100644 --- a/framework/platform/vertexbuffer-dx.cpp +++ b/framework/platform/vertexbuffer-dx.cpp @@ -1,32 +1,4 @@ #include "vertexbuffer-dx.hpp" -#include "device-dx.hpp" -#include 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; - } } \ No newline at end of file diff --git a/framework/platform/vertexbuffer-dx.hpp b/framework/platform/vertexbuffer-dx.hpp index fb4afcd..8a1b339 100644 --- a/framework/platform/vertexbuffer-dx.hpp +++ b/framework/platform/vertexbuffer-dx.hpp @@ -4,38 +4,49 @@ #include "../graphics/vertexbuffer.hpp" #include "../graphics/vertexposition.hpp" #include "dxheaders.hpp" +#include +#include +#include "device-dx.hpp" namespace xna { + template 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(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 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 data; }; } diff --git a/framework/xna.cpp b/framework/xna.cpp index ed6d1ec..6c5f80a 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -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 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 vbuffer(data); + vbuffer.Initialize(*_graphicsDevice, &err); + + D3D11_BUFFER_DESC desc; + vbuffer.dxBuffer->GetDesc(&desc); + + Game::LoadContent(); } diff --git a/framework/xna.h b/framework/xna.h index e0935bb..aebe77f 100644 --- a/framework/xna.h +++ b/framework/xna.h @@ -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