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

Implementa SetData de Texture2D

This commit is contained in:
Danilo 2024-05-04 21:07:39 -03:00
parent acef4d9787
commit 7ebfa86809
13 changed files with 259 additions and 82 deletions

View File

@ -189,6 +189,10 @@ namespace xna {
return Color::Multiply(value, scale);
}
constexpr operator Uint() const {
return _packedValue;
}
private:
Uint _packedValue{ 0 };

View File

@ -10,7 +10,12 @@ namespace xna {
template <typename T>
static void BlockCopy(T const* src, rsize_t srcOffset, T* dst, rsize_t dstOffset, rsize_t byteCount) {
memmove_s(dst + dstOffset, byteCount, src + srcOffset, byteCount);
}
}
template <typename TSOURCE, typename TDEST>
static void BlockCopy(TSOURCE const* src, rsize_t srcOffset, TDEST* dst, rsize_t dstOffset, rsize_t byteCount) {
memmove_s(dst + dstOffset, byteCount, src + srcOffset, byteCount);
}
private:
constexpr Buffer() = default;

View File

@ -1,7 +1,7 @@
#ifndef XNA_GRAPHICS_RENDERTARGET_HPP
#define XNA_GRAPHICS_RENDERTARGET_HPP
#include "texture.hpp"
#include "../default.hpp"
namespace xna {
@ -9,8 +9,8 @@ namespace xna {
public:
virtual ~IRenderTarget2D(){}
virtual bool Initialize(GraphicsDevice& device) = 0;
virtual bool Apply(GraphicsDevice& device) = 0;
virtual bool Initialize(xna_error_nullarg) = 0;
virtual bool Apply(xna_error_nullarg) = 0;
};
}

View File

@ -1,9 +1,7 @@
#ifndef XNA_GRAPHICS_TEXTURE_HPP
#define XNA_GRAPHICS_TEXTURE_HPP
#include "../forward.hpp"
#include "../types.hpp"
#include "../enums.hpp"
#include "../default.hpp"
namespace xna {
class Texture {
@ -14,6 +12,8 @@ namespace xna {
virtual ~ITexture2D(){}
virtual Int Width() const = 0;
virtual Int Height() const = 0;
virtual Rectangle Bounds() const = 0;
virtual bool Initialize(xna_error_nullarg) = 0;
};
}

View File

@ -47,10 +47,11 @@ namespace xna {
hr = _factory->MakeWindowAssociation(gameWindow.WindowHandle(), DXGI_MWA_NO_ALT_ENTER);
if (FAILED(hr)) return false;
_renderTarget2D = New<RenderTarget2D>();
if (!_renderTarget2D->Initialize(*this)) return false;
_renderTarget2D = New<RenderTarget2D>(this);
if (!_renderTarget2D->Initialize())
return false;
_renderTarget2D->Apply(*this);
_renderTarget2D->Apply();
D3D11_VIEWPORT view{};
view.TopLeftX = _viewport.X;

View File

@ -42,19 +42,33 @@ namespace xna {
Keyboard::Initialize();
Mouse::Initialize();
//initialize é requisito para GamePad
////initialize é requisito para GamePad
//Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
//if (FAILED(initialize))
// MessageBox(nullptr, "Ocorreu um erro ao executar Microsoft::WRL::Wrappers::RoInitializeWrapper. O GamePad não foi inicializado corretamente.", "XN65", MB_OK);
//GamePad.Initialize();
////CoInitializeEx é requisito para biblioteca DirectXTK
//const auto hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
//if (FAILED(hr))
// MessageBox(nullptr, "Ocorreu um erro ao executar CoInitializeEx. O AudioEngine não foi inicializado corretamente.", "XN65", MB_OK);
#if (_WIN32_WINNT >= 0x0A00 /*_WIN32_WINNT_WIN10*/)
Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
if (FAILED(initialize))
MessageBox(nullptr, "Ocorreu um erro ao executar Microsoft::WRL::Wrappers::RoInitializeWrapper. O GamePad não foi inicializado corretamente.", "XN65", MB_OK);
GamePad.Initialize();
//CoInitializeEx é requisito para AudioEngine
const auto hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
{
}
#else
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr))
MessageBox(nullptr, "Ocorreu um erro ao executar CoInitializeEx. O AudioEngine não foi inicializado corretamente.", "XN65", MB_OK);
{
}
#endif
_audioEngine = New<AudioEngine>();

View File

@ -4,30 +4,39 @@
#include "device-dx.hpp"
namespace xna {
bool RenderTarget2D::Initialize(GraphicsDevice& device) {
if (!device._device)
bool RenderTarget2D::Initialize(xna_error_ptr_arg) {
if (!m_device || !m_device->_device) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
if (_texture2D) {
_texture2D->Release();
_texture2D = nullptr;
}
if (!device._swapChain->GetBackBuffer(_texture2D))
if (dxTexture2D) {
dxTexture2D->Release();
dxTexture2D = nullptr;
}
if (!m_device->_swapChain->GetBackBuffer(dxTexture2D))
return false;
auto& dxdevice = device._device;
auto& dxdevice = m_device->_device;
const auto hr = dxdevice->CreateRenderTargetView(_texture2D, NULL, &_renderTargetView);
const auto hr = dxdevice->CreateRenderTargetView(dxTexture2D, NULL, &_renderTargetView);
if (FAILED(hr))
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
return true;
}
bool RenderTarget2D::Apply(GraphicsDevice& device) {
auto& context = device._context;
bool RenderTarget2D::Apply(xna_error_ptr_arg) {
if (!m_device || !m_device->_context) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
auto& context = m_device->_context;
context->OMSetRenderTargets(1, &_renderTargetView, nullptr);
return true;
}

View File

@ -9,6 +9,8 @@
namespace xna {
class RenderTarget2D : public IRenderTarget2D, public Texture2D {
public:
RenderTarget2D(GraphicsDevice* device) : Texture2D(device){}
virtual ~RenderTarget2D() override {
if (_renderTargetView) {
_renderTargetView->Release();
@ -16,8 +18,8 @@ namespace xna {
}
}
virtual bool Initialize(GraphicsDevice& device) override;
virtual bool Apply(GraphicsDevice& device) override;
virtual bool Initialize(xna_error_nullarg) override;
virtual bool Apply(xna_error_nullarg) override;
public:
ID3D11RenderTargetView* _renderTargetView = nullptr;

View File

@ -63,7 +63,7 @@ namespace xna {
if (!_dxspriteBatch)
return;
if (!texture._textureView)
if (!texture.dxShaderResourceView)
return;
const auto _position = XMFLOAT2(position.X, position.Y);
@ -71,7 +71,7 @@ namespace xna {
XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
_dxspriteBatch->Draw(
texture._textureView,
texture.dxShaderResourceView,
_position,
_color
);
@ -81,7 +81,7 @@ namespace xna {
if (!_dxspriteBatch)
return;
if (!texture._textureView)
if (!texture.dxShaderResourceView)
return;
const auto _position = XMFLOAT2(position.X, position.Y);
@ -98,7 +98,7 @@ namespace xna {
};
_dxspriteBatch->Draw(
texture._textureView,
texture.dxShaderResourceView,
_position,
sourceRectangle ? &_sourceRect : nullptr,
_color);
@ -108,7 +108,7 @@ namespace xna {
if (!_dxspriteBatch)
return;
if (!texture._textureView)
if (!texture.dxShaderResourceView)
return;
const auto _position = XMFLOAT2(position.X, position.Y);
@ -128,7 +128,7 @@ namespace xna {
const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects);
_dxspriteBatch->Draw(
texture._textureView,
texture.dxShaderResourceView,
_position,
sourceRectangle ? &_sourceRect : nullptr,
_color,
@ -143,7 +143,7 @@ namespace xna {
if (!_dxspriteBatch)
return;
if (!texture._textureView)
if (!texture.dxShaderResourceView)
return;
const auto _position = XMFLOAT2(position.X, position.Y);
@ -164,7 +164,7 @@ namespace xna {
const XMFLOAT2 _scale = { scale.X, scale.Y };
_dxspriteBatch->Draw(
texture._textureView,
texture.dxShaderResourceView,
_position,
sourceRectangle ? &_sourceRect : nullptr,
_color,
@ -179,7 +179,7 @@ namespace xna {
if (!_dxspriteBatch)
return;
if (!texture._textureView)
if (!texture.dxShaderResourceView)
return;
RECT _destinationRect{};
@ -191,14 +191,14 @@ namespace xna {
const auto v4 = color.ToVector4();
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
_dxspriteBatch->Draw(texture._textureView, _destinationRect, _color);
_dxspriteBatch->Draw(texture.dxShaderResourceView, _destinationRect, _color);
}
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color) {
if (!_dxspriteBatch)
return;
if (!texture._textureView)
if (!texture.dxShaderResourceView)
return;
RECT _destinationRect{};
@ -219,14 +219,14 @@ namespace xna {
_sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height;
};
_dxspriteBatch->Draw(texture._textureView, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color);
_dxspriteBatch->Draw(texture.dxShaderResourceView, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color);
}
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) {
if (!_dxspriteBatch)
return;
if (!texture._textureView)
if (!texture.dxShaderResourceView)
return;
RECT _destinationRect{};
@ -251,7 +251,7 @@ namespace xna {
const auto _effects = static_cast<DxSpriteEffects>(effects);
_dxspriteBatch->Draw(
texture._textureView,
texture.dxShaderResourceView,
_destinationRect,
sourceRectangle ? &_sourceRect : nullptr,
_color,

View File

@ -3,13 +3,10 @@
#include "device-dx.hpp"
#include "../helpers.hpp"
namespace xna {
Texture2D::Texture2D() {
}
namespace xna {
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName, xna_error_ptr_arg)
{
auto texture2d = New<Texture2D>();
auto texture2d = New<Texture2D>(&device);
ID3D11Resource* resource = nullptr;
auto wstr = XnaHToWString(fileName);
@ -18,7 +15,7 @@ namespace xna {
device._context,
wstr.c_str(),
&resource,
&texture2d->_textureView,
&texture2d->dxShaderResourceView,
0U);
if (FAILED(result))
@ -33,7 +30,7 @@ namespace xna {
return nullptr;
}
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->_texture2D);
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->dxTexture2D);
if (FAILED(result)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
@ -47,12 +44,54 @@ namespace xna {
}
D3D11_TEXTURE2D_DESC desc;
texture2d->_texture2D->GetDesc(&desc);
texture2d->_description = desc;
texture2d->dxTexture2D->GetDesc(&desc);
texture2d->dxDescription = desc;
resource->Release();
resource = nullptr;
return texture2d;
}
bool Texture2D::Initialize(xna_error_ptr_arg)
{
if (dxTexture2D) {
xna_error_apply(err, XnaErrorCode::WARNING_INITIALIZED_RESOURCE);
return false;
}
if (!m_device || !m_device->_device) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
auto hr = m_device->_device->CreateTexture2D(&dxDescription, nullptr, &dxTexture2D);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
ID3D11Resource* resource = nullptr;
hr = dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
hr = m_device->_device->CreateShaderResourceView(resource, &dxShaderDecription, &dxShaderResourceView);
if (resource) {
resource->Release();
resource = nullptr;
}
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
return true;
}
}

View File

@ -1,44 +1,147 @@
#ifndef XNA_PLATFORM_TEXTURE_DX_HPP
#define XNA_PLATFORM_TEXTURE_DX_HPP
#include "../common/rectangle.hpp"
#include "../csharp/buffer.hpp"
#include "../graphics/texture.hpp"
#include "dxgi.h"
#include "d3d11.h"
#include "../xnaerror.hpp"
#include "dxheaders.hpp"
#include <WICTextureLoader.h>
#include "../graphics/gresource.hpp"
#include "device-dx.hpp"
#include <SimpleMath.h>
namespace xna {
class Texture2D : public ITexture2D {
class Texture2D : public ITexture2D, public GraphicsResource {
public:
Texture2D();
Texture2D(GraphicsDevice* device, size_t width, size_t height) : GraphicsResource(device) {
dxDescription.Width = static_cast<UINT>(width);
dxDescription.Height = static_cast<UINT>(height);
setDefaultDesc();
}
Texture2D(GraphicsDevice* device) : GraphicsResource(device) {
setDefaultDesc();
}
virtual ~Texture2D() override {
if (_texture2D) {
_texture2D->Release();
_texture2D = nullptr;
if (dxTexture2D) {
dxTexture2D->Release();
dxTexture2D = nullptr;
}
if (_textureView) {
_textureView->Release();
_textureView = nullptr;
if (dxShaderResourceView) {
dxShaderResourceView->Release();
dxShaderResourceView = nullptr;
}
}
virtual constexpr Int Width() const override {
return _description.Width;
return dxDescription.Width;
}
virtual constexpr Int Height() const override {
return _description.Height;
return dxDescription.Height;
}
static sptr<Texture2D> FromStream(GraphicsDevice& device, String const& fileName, xna_error_nullarg);
constexpr Rectangle Bounds() const override {
return { 0, 0, static_cast<Int>(dxDescription.Width), static_cast<Int>(dxDescription.Height) };
}
bool Initialize(xna_error_nullarg) override;
template <typename T>
void SetData(std::vector<T> const& data, xna_error_ptr_arg);
template <typename T>
void SetData(std::vector<T> const& data, size_t startIndex, size_t elementCount, xna_error_nullarg);
static sptr<Texture2D> FromStream(GraphicsDevice& device, String const& fileName, xna_error_nullarg);
public:
ID3D11Texture2D* _texture2D{nullptr};
ID3D11ShaderResourceView* _textureView{ nullptr };
D3D11_TEXTURE2D_DESC _description{};
ID3D11Texture2D* dxTexture2D{ nullptr };
ID3D11ShaderResourceView* dxShaderResourceView{ nullptr };
D3D11_SUBRESOURCE_DATA dxSubResource{};
D3D11_TEXTURE2D_DESC dxDescription{};
D3D11_SHADER_RESOURCE_VIEW_DESC dxShaderDecription{};
private:
static constexpr int R8G8B8A8U_BYTE_SIZE = 4;
void setDefaultDesc() {
dxDescription.MipLevels = 1;
dxDescription.ArraySize = 1;
dxDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
dxDescription.SampleDesc.Count = 1;
dxDescription.Usage = D3D11_USAGE_DEFAULT;
dxDescription.BindFlags = D3D11_BIND_SHADER_RESOURCE;
dxShaderDecription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
dxShaderDecription.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
dxShaderDecription.Texture2D.MipLevels = dxDescription.MipLevels;
dxShaderDecription.Texture2D.MostDetailedMip = 0;
}
};
template <typename T>
inline void Texture2D::SetData(std::vector<T> const& data, xna_error_ptr_arg) {
SetData(data, 0, data.size(), err);
}
template <typename T>
inline void Texture2D::SetData(std::vector<T> const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) {
if (!m_device || !m_device->_device || !m_device->_context) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return;
}
std::vector<UINT> finalData(elementCount);
auto finalDataIndex = 0;
for (size_t i = startIndex; i < elementCount; ++i) {
finalData[finalDataIndex] = static_cast<UINT>(data[i]);
++finalDataIndex;
}
if (!dxTexture2D) {
auto hr = m_device->_device->CreateTexture2D(&dxDescription, nullptr, &dxTexture2D);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return;
}
}
ID3D11Resource* resource = nullptr;
auto hr = dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return;
}
dxShaderDecription.Texture2D.MipLevels = dxDescription.MipLevels;
m_device->_context->UpdateSubresource(resource, 0, nullptr, finalData.data(), dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
if (dxShaderResourceView) {
dxShaderResourceView->Release();
dxShaderResourceView = nullptr;
}
hr = m_device->_device->CreateShaderResourceView(resource, &dxShaderDecription, &dxShaderResourceView);
if (resource) {
resource->Release();
resource = nullptr;
}
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return;
}
dxTexture2D->GetDesc(&dxDescription);
}
}
#endif

View File

@ -14,12 +14,6 @@ namespace xna {
graphics = New<GraphicsDeviceManager>(_game);
graphics->PreferredBackBufferWidth(1280);
graphics->PreferredBackBufferHeight(720);
contentManager = New<ContentManager>("Content");
//const auto s = contentManager->_path.string();
// const auto current = std::filesystem::current_path();
//auto s = contentManager->OpenStream("file");
//DecompressStream::Decompress();
}
void Initialize() override {
@ -30,8 +24,13 @@ namespace xna {
void LoadContent() override {
spriteBatch = New<SpriteBatch>(*_graphicsDevice);
XnaErrorCode err;
texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err);
XnaErrorCode err{0};
//texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err);
texture = New<Texture2D>(_graphicsDevice.get(), 256, 256);
std::vector<Color> data(256 * 256, 4278190080U);
//std::vector<UINT> data(256 * 256, 0xffffffff);
//std::vector<Uint> data(256 * 256, 4278190080U);
texture->SetData(data, 0, data.size());
Game::LoadContent();
}

View File

@ -14,7 +14,8 @@ namespace xna {
STREAM_ERROR,
UNINTIALIZED_RESOURCE,
END_OF_FILE,
BAD_TYPE
BAD_TYPE,
WARNING_INITIALIZED_RESOURCE
};
inline void xna_error_apply(XnaErrorCode* source, XnaErrorCode const& value) {