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:
parent
acef4d9787
commit
7ebfa86809
@ -189,6 +189,10 @@ namespace xna {
|
|||||||
return Color::Multiply(value, scale);
|
return Color::Multiply(value, scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr operator Uint() const {
|
||||||
|
return _packedValue;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Uint _packedValue{ 0 };
|
Uint _packedValue{ 0 };
|
||||||
|
|
||||||
|
@ -10,7 +10,12 @@ namespace xna {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
static void BlockCopy(T const* src, rsize_t srcOffset, T* dst, rsize_t dstOffset, rsize_t byteCount) {
|
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);
|
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:
|
private:
|
||||||
constexpr Buffer() = default;
|
constexpr Buffer() = default;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef XNA_GRAPHICS_RENDERTARGET_HPP
|
#ifndef XNA_GRAPHICS_RENDERTARGET_HPP
|
||||||
#define XNA_GRAPHICS_RENDERTARGET_HPP
|
#define XNA_GRAPHICS_RENDERTARGET_HPP
|
||||||
|
|
||||||
#include "texture.hpp"
|
#include "../default.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
@ -9,8 +9,8 @@ namespace xna {
|
|||||||
public:
|
public:
|
||||||
virtual ~IRenderTarget2D(){}
|
virtual ~IRenderTarget2D(){}
|
||||||
|
|
||||||
virtual bool Initialize(GraphicsDevice& device) = 0;
|
virtual bool Initialize(xna_error_nullarg) = 0;
|
||||||
virtual bool Apply(GraphicsDevice& device) = 0;
|
virtual bool Apply(xna_error_nullarg) = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
#ifndef XNA_GRAPHICS_TEXTURE_HPP
|
#ifndef XNA_GRAPHICS_TEXTURE_HPP
|
||||||
#define XNA_GRAPHICS_TEXTURE_HPP
|
#define XNA_GRAPHICS_TEXTURE_HPP
|
||||||
|
|
||||||
#include "../forward.hpp"
|
#include "../default.hpp"
|
||||||
#include "../types.hpp"
|
|
||||||
#include "../enums.hpp"
|
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
class Texture {
|
class Texture {
|
||||||
@ -14,6 +12,8 @@ namespace xna {
|
|||||||
virtual ~ITexture2D(){}
|
virtual ~ITexture2D(){}
|
||||||
virtual Int Width() const = 0;
|
virtual Int Width() const = 0;
|
||||||
virtual Int Height() const = 0;
|
virtual Int Height() const = 0;
|
||||||
|
virtual Rectangle Bounds() const = 0;
|
||||||
|
virtual bool Initialize(xna_error_nullarg) = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,10 +47,11 @@ namespace xna {
|
|||||||
hr = _factory->MakeWindowAssociation(gameWindow.WindowHandle(), DXGI_MWA_NO_ALT_ENTER);
|
hr = _factory->MakeWindowAssociation(gameWindow.WindowHandle(), DXGI_MWA_NO_ALT_ENTER);
|
||||||
if (FAILED(hr)) return false;
|
if (FAILED(hr)) return false;
|
||||||
|
|
||||||
_renderTarget2D = New<RenderTarget2D>();
|
_renderTarget2D = New<RenderTarget2D>(this);
|
||||||
if (!_renderTarget2D->Initialize(*this)) return false;
|
if (!_renderTarget2D->Initialize())
|
||||||
|
return false;
|
||||||
|
|
||||||
_renderTarget2D->Apply(*this);
|
_renderTarget2D->Apply();
|
||||||
|
|
||||||
D3D11_VIEWPORT view{};
|
D3D11_VIEWPORT view{};
|
||||||
view.TopLeftX = _viewport.X;
|
view.TopLeftX = _viewport.X;
|
||||||
|
@ -42,19 +42,33 @@ namespace xna {
|
|||||||
Keyboard::Initialize();
|
Keyboard::Initialize();
|
||||||
Mouse::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);
|
Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
|
||||||
|
|
||||||
if (FAILED(initialize))
|
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))
|
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>();
|
_audioEngine = New<AudioEngine>();
|
||||||
|
|
||||||
|
@ -4,30 +4,39 @@
|
|||||||
#include "device-dx.hpp"
|
#include "device-dx.hpp"
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
bool RenderTarget2D::Initialize(GraphicsDevice& device) {
|
bool RenderTarget2D::Initialize(xna_error_ptr_arg) {
|
||||||
if (!device._device)
|
if (!m_device || !m_device->_device) {
|
||||||
|
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||||
return false;
|
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;
|
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 false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RenderTarget2D::Apply(GraphicsDevice& device) {
|
bool RenderTarget2D::Apply(xna_error_ptr_arg) {
|
||||||
auto& context = device._context;
|
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);
|
context->OMSetRenderTargets(1, &_renderTargetView, nullptr);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
namespace xna {
|
namespace xna {
|
||||||
class RenderTarget2D : public IRenderTarget2D, public Texture2D {
|
class RenderTarget2D : public IRenderTarget2D, public Texture2D {
|
||||||
public:
|
public:
|
||||||
|
RenderTarget2D(GraphicsDevice* device) : Texture2D(device){}
|
||||||
|
|
||||||
virtual ~RenderTarget2D() override {
|
virtual ~RenderTarget2D() override {
|
||||||
if (_renderTargetView) {
|
if (_renderTargetView) {
|
||||||
_renderTargetView->Release();
|
_renderTargetView->Release();
|
||||||
@ -16,8 +18,8 @@ namespace xna {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool Initialize(GraphicsDevice& device) override;
|
virtual bool Initialize(xna_error_nullarg) override;
|
||||||
virtual bool Apply(GraphicsDevice& device) override;
|
virtual bool Apply(xna_error_nullarg) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ID3D11RenderTargetView* _renderTargetView = nullptr;
|
ID3D11RenderTargetView* _renderTargetView = nullptr;
|
||||||
|
@ -63,7 +63,7 @@ namespace xna {
|
|||||||
if (!_dxspriteBatch)
|
if (!_dxspriteBatch)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!texture._textureView)
|
if (!texture.dxShaderResourceView)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const auto _position = XMFLOAT2(position.X, position.Y);
|
const auto _position = XMFLOAT2(position.X, position.Y);
|
||||||
@ -71,7 +71,7 @@ namespace xna {
|
|||||||
XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
|
XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
|
||||||
|
|
||||||
_dxspriteBatch->Draw(
|
_dxspriteBatch->Draw(
|
||||||
texture._textureView,
|
texture.dxShaderResourceView,
|
||||||
_position,
|
_position,
|
||||||
_color
|
_color
|
||||||
);
|
);
|
||||||
@ -81,7 +81,7 @@ namespace xna {
|
|||||||
if (!_dxspriteBatch)
|
if (!_dxspriteBatch)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!texture._textureView)
|
if (!texture.dxShaderResourceView)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const auto _position = XMFLOAT2(position.X, position.Y);
|
const auto _position = XMFLOAT2(position.X, position.Y);
|
||||||
@ -98,7 +98,7 @@ namespace xna {
|
|||||||
};
|
};
|
||||||
|
|
||||||
_dxspriteBatch->Draw(
|
_dxspriteBatch->Draw(
|
||||||
texture._textureView,
|
texture.dxShaderResourceView,
|
||||||
_position,
|
_position,
|
||||||
sourceRectangle ? &_sourceRect : nullptr,
|
sourceRectangle ? &_sourceRect : nullptr,
|
||||||
_color);
|
_color);
|
||||||
@ -108,7 +108,7 @@ namespace xna {
|
|||||||
if (!_dxspriteBatch)
|
if (!_dxspriteBatch)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!texture._textureView)
|
if (!texture.dxShaderResourceView)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const auto _position = XMFLOAT2(position.X, position.Y);
|
const auto _position = XMFLOAT2(position.X, position.Y);
|
||||||
@ -128,7 +128,7 @@ namespace xna {
|
|||||||
const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects);
|
const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects);
|
||||||
|
|
||||||
_dxspriteBatch->Draw(
|
_dxspriteBatch->Draw(
|
||||||
texture._textureView,
|
texture.dxShaderResourceView,
|
||||||
_position,
|
_position,
|
||||||
sourceRectangle ? &_sourceRect : nullptr,
|
sourceRectangle ? &_sourceRect : nullptr,
|
||||||
_color,
|
_color,
|
||||||
@ -143,7 +143,7 @@ namespace xna {
|
|||||||
if (!_dxspriteBatch)
|
if (!_dxspriteBatch)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!texture._textureView)
|
if (!texture.dxShaderResourceView)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const auto _position = XMFLOAT2(position.X, position.Y);
|
const auto _position = XMFLOAT2(position.X, position.Y);
|
||||||
@ -164,7 +164,7 @@ namespace xna {
|
|||||||
const XMFLOAT2 _scale = { scale.X, scale.Y };
|
const XMFLOAT2 _scale = { scale.X, scale.Y };
|
||||||
|
|
||||||
_dxspriteBatch->Draw(
|
_dxspriteBatch->Draw(
|
||||||
texture._textureView,
|
texture.dxShaderResourceView,
|
||||||
_position,
|
_position,
|
||||||
sourceRectangle ? &_sourceRect : nullptr,
|
sourceRectangle ? &_sourceRect : nullptr,
|
||||||
_color,
|
_color,
|
||||||
@ -179,7 +179,7 @@ namespace xna {
|
|||||||
if (!_dxspriteBatch)
|
if (!_dxspriteBatch)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!texture._textureView)
|
if (!texture.dxShaderResourceView)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
RECT _destinationRect{};
|
RECT _destinationRect{};
|
||||||
@ -191,14 +191,14 @@ namespace xna {
|
|||||||
const auto v4 = color.ToVector4();
|
const auto v4 = color.ToVector4();
|
||||||
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
|
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) {
|
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color) {
|
||||||
if (!_dxspriteBatch)
|
if (!_dxspriteBatch)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!texture._textureView)
|
if (!texture.dxShaderResourceView)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
RECT _destinationRect{};
|
RECT _destinationRect{};
|
||||||
@ -219,14 +219,14 @@ namespace xna {
|
|||||||
_sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height;
|
_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) {
|
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)
|
if (!_dxspriteBatch)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!texture._textureView)
|
if (!texture.dxShaderResourceView)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
RECT _destinationRect{};
|
RECT _destinationRect{};
|
||||||
@ -251,7 +251,7 @@ namespace xna {
|
|||||||
const auto _effects = static_cast<DxSpriteEffects>(effects);
|
const auto _effects = static_cast<DxSpriteEffects>(effects);
|
||||||
|
|
||||||
_dxspriteBatch->Draw(
|
_dxspriteBatch->Draw(
|
||||||
texture._textureView,
|
texture.dxShaderResourceView,
|
||||||
_destinationRect,
|
_destinationRect,
|
||||||
sourceRectangle ? &_sourceRect : nullptr,
|
sourceRectangle ? &_sourceRect : nullptr,
|
||||||
_color,
|
_color,
|
||||||
|
@ -3,13 +3,10 @@
|
|||||||
#include "device-dx.hpp"
|
#include "device-dx.hpp"
|
||||||
#include "../helpers.hpp"
|
#include "../helpers.hpp"
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
Texture2D::Texture2D() {
|
|
||||||
}
|
|
||||||
|
|
||||||
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName, xna_error_ptr_arg)
|
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;
|
ID3D11Resource* resource = nullptr;
|
||||||
auto wstr = XnaHToWString(fileName);
|
auto wstr = XnaHToWString(fileName);
|
||||||
|
|
||||||
@ -18,7 +15,7 @@ namespace xna {
|
|||||||
device._context,
|
device._context,
|
||||||
wstr.c_str(),
|
wstr.c_str(),
|
||||||
&resource,
|
&resource,
|
||||||
&texture2d->_textureView,
|
&texture2d->dxShaderResourceView,
|
||||||
0U);
|
0U);
|
||||||
|
|
||||||
if (FAILED(result))
|
if (FAILED(result))
|
||||||
@ -33,7 +30,7 @@ namespace xna {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->_texture2D);
|
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->dxTexture2D);
|
||||||
|
|
||||||
if (FAILED(result)) {
|
if (FAILED(result)) {
|
||||||
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
|
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
|
||||||
@ -47,12 +44,54 @@ namespace xna {
|
|||||||
}
|
}
|
||||||
|
|
||||||
D3D11_TEXTURE2D_DESC desc;
|
D3D11_TEXTURE2D_DESC desc;
|
||||||
texture2d->_texture2D->GetDesc(&desc);
|
texture2d->dxTexture2D->GetDesc(&desc);
|
||||||
texture2d->_description = desc;
|
texture2d->dxDescription = desc;
|
||||||
|
|
||||||
resource->Release();
|
resource->Release();
|
||||||
resource = nullptr;
|
resource = nullptr;
|
||||||
|
|
||||||
return texture2d;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,44 +1,147 @@
|
|||||||
#ifndef XNA_PLATFORM_TEXTURE_DX_HPP
|
#ifndef XNA_PLATFORM_TEXTURE_DX_HPP
|
||||||
#define XNA_PLATFORM_TEXTURE_DX_HPP
|
#define XNA_PLATFORM_TEXTURE_DX_HPP
|
||||||
|
|
||||||
|
#include "../common/rectangle.hpp"
|
||||||
|
#include "../csharp/buffer.hpp"
|
||||||
#include "../graphics/texture.hpp"
|
#include "../graphics/texture.hpp"
|
||||||
#include "dxgi.h"
|
|
||||||
#include "d3d11.h"
|
|
||||||
#include "../xnaerror.hpp"
|
#include "../xnaerror.hpp"
|
||||||
|
#include "dxheaders.hpp"
|
||||||
|
#include <WICTextureLoader.h>
|
||||||
|
#include "../graphics/gresource.hpp"
|
||||||
|
#include "device-dx.hpp"
|
||||||
|
#include <SimpleMath.h>
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
class Texture2D : public ITexture2D {
|
class Texture2D : public ITexture2D, public GraphicsResource {
|
||||||
public:
|
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 {
|
virtual ~Texture2D() override {
|
||||||
if (_texture2D) {
|
if (dxTexture2D) {
|
||||||
_texture2D->Release();
|
dxTexture2D->Release();
|
||||||
_texture2D = nullptr;
|
dxTexture2D = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_textureView) {
|
if (dxShaderResourceView) {
|
||||||
_textureView->Release();
|
dxShaderResourceView->Release();
|
||||||
_textureView = nullptr;
|
dxShaderResourceView = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual constexpr Int Width() const override {
|
virtual constexpr Int Width() const override {
|
||||||
return _description.Width;
|
return dxDescription.Width;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual constexpr Int Height() const override {
|
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:
|
public:
|
||||||
ID3D11Texture2D* _texture2D{nullptr};
|
ID3D11Texture2D* dxTexture2D{ nullptr };
|
||||||
ID3D11ShaderResourceView* _textureView{ nullptr };
|
ID3D11ShaderResourceView* dxShaderResourceView{ nullptr };
|
||||||
D3D11_TEXTURE2D_DESC _description{};
|
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
|
#endif
|
@ -14,12 +14,6 @@ namespace xna {
|
|||||||
graphics = New<GraphicsDeviceManager>(_game);
|
graphics = New<GraphicsDeviceManager>(_game);
|
||||||
graphics->PreferredBackBufferWidth(1280);
|
graphics->PreferredBackBufferWidth(1280);
|
||||||
graphics->PreferredBackBufferHeight(720);
|
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 {
|
void Initialize() override {
|
||||||
@ -30,8 +24,13 @@ namespace xna {
|
|||||||
void LoadContent() override {
|
void LoadContent() override {
|
||||||
spriteBatch = New<SpriteBatch>(*_graphicsDevice);
|
spriteBatch = New<SpriteBatch>(*_graphicsDevice);
|
||||||
|
|
||||||
XnaErrorCode err;
|
XnaErrorCode err{0};
|
||||||
texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err);
|
//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();
|
Game::LoadContent();
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,8 @@ namespace xna {
|
|||||||
STREAM_ERROR,
|
STREAM_ERROR,
|
||||||
UNINTIALIZED_RESOURCE,
|
UNINTIALIZED_RESOURCE,
|
||||||
END_OF_FILE,
|
END_OF_FILE,
|
||||||
BAD_TYPE
|
BAD_TYPE,
|
||||||
|
WARNING_INITIALIZED_RESOURCE
|
||||||
};
|
};
|
||||||
|
|
||||||
inline void xna_error_apply(XnaErrorCode* source, XnaErrorCode const& value) {
|
inline void xna_error_apply(XnaErrorCode* source, XnaErrorCode const& value) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user