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:
parent
da12db2c47
commit
3f0e294bbd
@ -68,10 +68,10 @@ namespace xna {
|
|||||||
using BlendOperation = BlendFunction;
|
using BlendOperation = BlendFunction;
|
||||||
|
|
||||||
enum class BufferUsage {
|
enum class BufferUsage {
|
||||||
Static,
|
Default,
|
||||||
Dynamic,
|
|
||||||
Immutable,
|
Immutable,
|
||||||
Staging
|
Dynamic,
|
||||||
|
Static,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Buttons {
|
enum class Buttons {
|
||||||
|
@ -128,11 +128,7 @@ namespace xna {
|
|||||||
class SpriteBatch;
|
class SpriteBatch;
|
||||||
using PSpriteBatch = std::shared_ptr<SpriteBatch>;
|
using PSpriteBatch = std::shared_ptr<SpriteBatch>;
|
||||||
class SpriteFont;
|
class SpriteFont;
|
||||||
using PSpriteFont = std::shared_ptr<SpriteFont>;
|
using PSpriteFont = std::shared_ptr<SpriteFont>;
|
||||||
class VertexBuffer;
|
|
||||||
using PVertexBuffer = std::shared_ptr<VertexBuffer>;
|
|
||||||
class VertexInputLayout;
|
|
||||||
using PVertexInputLayout = std::shared_ptr<VertexInputLayout>;
|
|
||||||
struct VertexPositionColor;
|
struct VertexPositionColor;
|
||||||
using PVertexPositionColor = std::shared_ptr<VertexPositionColor>;
|
using PVertexPositionColor = std::shared_ptr<VertexPositionColor>;
|
||||||
class VertexShader;
|
class VertexShader;
|
||||||
@ -140,25 +136,15 @@ namespace xna {
|
|||||||
struct Viewport;
|
struct Viewport;
|
||||||
using PViewport = std::shared_ptr<Viewport>;
|
using PViewport = std::shared_ptr<Viewport>;
|
||||||
|
|
||||||
//Input
|
//Input
|
||||||
class GamePad;
|
|
||||||
using PGamePad = std::shared_ptr<GamePad>;
|
|
||||||
struct GamePadTriggers;
|
struct GamePadTriggers;
|
||||||
using PGamePagTriggers = std::shared_ptr<GamePadTriggers>;
|
|
||||||
struct GamePadThumbSticks;
|
struct GamePadThumbSticks;
|
||||||
using PGamePadThumbSticks = std::shared_ptr<GamePadThumbSticks>;
|
|
||||||
struct GamePadDPad;
|
struct GamePadDPad;
|
||||||
using PGamePadDPad = std::shared_ptr<GamePadDPad>;
|
|
||||||
struct GamePadCapabilities;
|
struct GamePadCapabilities;
|
||||||
using PGamePadCapabilities = std::shared_ptr<GamePadCapabilities>;
|
|
||||||
struct GamePadButtons;
|
struct GamePadButtons;
|
||||||
using PGamePadButtons = std::shared_ptr<GamePadButtons>;
|
|
||||||
struct GamePadState;
|
struct GamePadState;
|
||||||
using PGamePadState = std::shared_ptr<GamePadState>;
|
|
||||||
struct KeyboardState;
|
struct KeyboardState;
|
||||||
using PKeyboardState = std::shared_ptr<KeyboardState>;
|
|
||||||
struct MouseState;
|
struct MouseState;
|
||||||
using PMouseState = std::shared_ptr<MouseState>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -11,6 +11,8 @@ namespace xna {
|
|||||||
Color color{};
|
Color color{};
|
||||||
|
|
||||||
constexpr VertexPositionColor() = default;
|
constexpr VertexPositionColor() = default;
|
||||||
|
constexpr VertexPositionColor(Vector3 const& position, Color const& color):
|
||||||
|
position(position), color(color){}
|
||||||
|
|
||||||
constexpr bool operator==(const VertexPositionColor& other) const {
|
constexpr bool operator==(const VertexPositionColor& other) const {
|
||||||
return position == other.position && color == other.color;
|
return position == other.position && color == other.color;
|
||||||
@ -27,6 +29,11 @@ namespace xna {
|
|||||||
return position == other.position
|
return position == other.position
|
||||||
&& textureCoordinate == other.textureCoordinate;
|
&& textureCoordinate == other.textureCoordinate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VertexPositionTexture(const Vector3& position, const Vector2& textureCoordinate)
|
||||||
|
: position(position), textureCoordinate(textureCoordinate)
|
||||||
|
{
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct VertexPositionColorTexture {
|
struct VertexPositionColorTexture {
|
||||||
@ -41,6 +48,11 @@ namespace xna {
|
|||||||
&& textureCoodinate == other.textureCoodinate
|
&& textureCoodinate == other.textureCoodinate
|
||||||
&& color == other.color;
|
&& color == other.color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VertexPositionColorTexture(const Vector3& position, const Vector2& textureCoodinate, const Color& color)
|
||||||
|
: position(position), textureCoodinate(textureCoodinate), color(color)
|
||||||
|
{
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct VertexPositionNormalTexture {
|
struct VertexPositionNormalTexture {
|
||||||
@ -55,6 +67,11 @@ namespace xna {
|
|||||||
&& normal == other.normal
|
&& normal == other.normal
|
||||||
&& textureCoodinate == other.textureCoodinate;
|
&& textureCoodinate == other.textureCoodinate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VertexPositionNormalTexture(const Vector3& position, const Vector3& normal, const Vector2& textureCoodinate)
|
||||||
|
: position(position), normal(normal), textureCoodinate(textureCoodinate)
|
||||||
|
{
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,47 +1,5 @@
|
|||||||
#include "indexbuffer-dx.hpp"
|
#include "indexbuffer-dx.hpp"
|
||||||
#include "device-dx.hpp"
|
#include "device-dx.hpp"
|
||||||
|
|
||||||
namespace xna {
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -2,37 +2,50 @@
|
|||||||
#define XNA_PLATFORM_INDEXBUFFER_DX_HPP
|
#define XNA_PLATFORM_INDEXBUFFER_DX_HPP
|
||||||
|
|
||||||
#include "../graphics/indexbuffer.hpp"
|
#include "../graphics/indexbuffer.hpp"
|
||||||
|
#include "device-dx.hpp"
|
||||||
#include "dxheaders.hpp"
|
#include "dxheaders.hpp"
|
||||||
|
#include <BufferHelpers.h>
|
||||||
|
#include <VertexTypes.h>
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
|
template <typename T>
|
||||||
class IndexBuffer : public IIndexBuffer {
|
class IndexBuffer : public IIndexBuffer {
|
||||||
public:
|
public:
|
||||||
IndexBuffer() {
|
constexpr IndexBuffer() = default;
|
||||||
_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;
|
|
||||||
}
|
|
||||||
|
|
||||||
IndexBuffer(D3D11_BUFFER_DESC desc) : _description(desc){}
|
constexpr IndexBuffer(std::vector<T> const& vertices) : data(vertices) {}
|
||||||
|
|
||||||
virtual ~IndexBuffer() override {
|
virtual ~IndexBuffer() override {
|
||||||
if (_buffer) {
|
if (dxBuffer) {
|
||||||
_buffer->Release();
|
dxBuffer->Release();
|
||||||
_buffer = nullptr;
|
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:
|
public:
|
||||||
D3D11_BUFFER_DESC _description;
|
ID3D11Buffer* dxBuffer = nullptr;
|
||||||
ID3D11Buffer* _buffer = nullptr;
|
std::vector<T> data;
|
||||||
D3D11_SUBRESOURCE_DATA _subResource{};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,32 +1,4 @@
|
|||||||
#include "vertexbuffer-dx.hpp"
|
#include "vertexbuffer-dx.hpp"
|
||||||
#include "device-dx.hpp"
|
|
||||||
#include <BufferHelpers.h>
|
|
||||||
|
|
||||||
namespace xna {
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -4,38 +4,49 @@
|
|||||||
#include "../graphics/vertexbuffer.hpp"
|
#include "../graphics/vertexbuffer.hpp"
|
||||||
#include "../graphics/vertexposition.hpp"
|
#include "../graphics/vertexposition.hpp"
|
||||||
#include "dxheaders.hpp"
|
#include "dxheaders.hpp"
|
||||||
|
#include <BufferHelpers.h>
|
||||||
|
#include <VertexTypes.h>
|
||||||
|
#include "device-dx.hpp"
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
|
template <typename T>
|
||||||
class VertexBuffer : public IVertexBuffer {
|
class VertexBuffer : public IVertexBuffer {
|
||||||
public:
|
public:
|
||||||
VertexBuffer() {
|
constexpr VertexBuffer() = default;
|
||||||
_description.Usage = D3D11_USAGE_DYNAMIC;
|
|
||||||
_description.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
||||||
_description.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexBuffer(size_t size) {
|
constexpr VertexBuffer(std::vector<T> const& vertices) : data(vertices) {}
|
||||||
_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){}
|
|
||||||
|
|
||||||
virtual ~VertexBuffer() override {
|
virtual ~VertexBuffer() override {
|
||||||
if (_buffer) {
|
if (dxBuffer) {
|
||||||
_buffer->Release();
|
dxBuffer->Release();
|
||||||
_buffer = nullptr;
|
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:
|
if (data.empty()) {
|
||||||
D3D11_BUFFER_DESC _description{};
|
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||||
D3D11_SUBRESOURCE_DATA _subResource{};
|
return false;
|
||||||
ID3D11Buffer* _buffer = nullptr;
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@ namespace xna {
|
|||||||
|
|
||||||
void Initialize() override {
|
void Initialize() override {
|
||||||
graphics->Initialize();
|
graphics->Initialize();
|
||||||
const auto modes = _graphicsDevice->_adapter->SupportedDisplayModes();
|
|
||||||
Game::Initialize();
|
Game::Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,9 +26,19 @@ namespace xna {
|
|||||||
|
|
||||||
XnaErrorCode err;
|
XnaErrorCode err;
|
||||||
texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &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();
|
Game::LoadContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
#include "platform/spritebatch-dx.hpp"
|
#include "platform/spritebatch-dx.hpp"
|
||||||
#include "platform/texture-dx.hpp"
|
#include "platform/texture-dx.hpp"
|
||||||
#include "platform/window-dx.hpp"
|
#include "platform/window-dx.hpp"
|
||||||
|
#include "graphics/vertexposition.hpp"
|
||||||
|
#include "platform/vertexbuffer-dx.hpp"
|
||||||
#include "Windows.h"
|
#include "Windows.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user