diff --git a/framework/common/color.hpp b/framework/common/color.hpp index a366598..40f52d1 100644 --- a/framework/common/color.hpp +++ b/framework/common/color.hpp @@ -189,6 +189,10 @@ namespace xna { return Color::Multiply(value, scale); } + constexpr operator Uint() const { + return _packedValue; + } + private: Uint _packedValue{ 0 }; diff --git a/framework/csharp/buffer.hpp b/framework/csharp/buffer.hpp index e2a8e75..9810c1f 100644 --- a/framework/csharp/buffer.hpp +++ b/framework/csharp/buffer.hpp @@ -10,7 +10,12 @@ namespace xna { template 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 + 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; diff --git a/framework/graphics/rendertarget.hpp b/framework/graphics/rendertarget.hpp index 1e8ca1c..90361db 100644 --- a/framework/graphics/rendertarget.hpp +++ b/framework/graphics/rendertarget.hpp @@ -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; }; } diff --git a/framework/graphics/texture.hpp b/framework/graphics/texture.hpp index bbf9bba..ec7dbf4 100644 --- a/framework/graphics/texture.hpp +++ b/framework/graphics/texture.hpp @@ -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; }; } diff --git a/framework/platform/device-dx.cpp b/framework/platform/device-dx.cpp index 86bd563..8e491dd 100644 --- a/framework/platform/device-dx.cpp +++ b/framework/platform/device-dx.cpp @@ -47,10 +47,11 @@ namespace xna { hr = _factory->MakeWindowAssociation(gameWindow.WindowHandle(), DXGI_MWA_NO_ALT_ENTER); if (FAILED(hr)) return false; - _renderTarget2D = New(); - if (!_renderTarget2D->Initialize(*this)) return false; + _renderTarget2D = New(this); + if (!_renderTarget2D->Initialize()) + return false; - _renderTarget2D->Apply(*this); + _renderTarget2D->Apply(); D3D11_VIEWPORT view{}; view.TopLeftX = _viewport.X; diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index 3121df2..d342ca3 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -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(); diff --git a/framework/platform/rendertarget-dx.cpp b/framework/platform/rendertarget-dx.cpp index 564a58d..32820ac 100644 --- a/framework/platform/rendertarget-dx.cpp +++ b/framework/platform/rendertarget-dx.cpp @@ -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; } diff --git a/framework/platform/rendertarget-dx.hpp b/framework/platform/rendertarget-dx.hpp index 81493c3..d7430c0 100644 --- a/framework/platform/rendertarget-dx.hpp +++ b/framework/platform/rendertarget-dx.hpp @@ -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; diff --git a/framework/platform/spritebatch-dx.cpp b/framework/platform/spritebatch-dx.cpp index f4d0fbb..e01457c 100644 --- a/framework/platform/spritebatch-dx.cpp +++ b/framework/platform/spritebatch-dx.cpp @@ -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(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(effects); _dxspriteBatch->Draw( - texture._textureView, + texture.dxShaderResourceView, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color, diff --git a/framework/platform/texture-dx.cpp b/framework/platform/texture-dx.cpp index 63757e1..8e2a5e0 100644 --- a/framework/platform/texture-dx.cpp +++ b/framework/platform/texture-dx.cpp @@ -3,13 +3,10 @@ #include "device-dx.hpp" #include "../helpers.hpp" -namespace xna { - Texture2D::Texture2D() { - } - +namespace xna { sptr Texture2D::FromStream(GraphicsDevice& device, String const& fileName, xna_error_ptr_arg) { - auto texture2d = New(); + auto texture2d = New(&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; + } } \ No newline at end of file diff --git a/framework/platform/texture-dx.hpp b/framework/platform/texture-dx.hpp index dbd5b16..1cbe9e0 100644 --- a/framework/platform/texture-dx.hpp +++ b/framework/platform/texture-dx.hpp @@ -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 +#include "../graphics/gresource.hpp" +#include "device-dx.hpp" +#include 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(width); + dxDescription.Height = static_cast(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 FromStream(GraphicsDevice& device, String const& fileName, xna_error_nullarg); + constexpr Rectangle Bounds() const override { + return { 0, 0, static_cast(dxDescription.Width), static_cast(dxDescription.Height) }; + } + + bool Initialize(xna_error_nullarg) override; + + template + void SetData(std::vector const& data, xna_error_ptr_arg); + + template + void SetData(std::vector const& data, size_t startIndex, size_t elementCount, xna_error_nullarg); + + static sptr 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 + inline void Texture2D::SetData(std::vector const& data, xna_error_ptr_arg) { + SetData(data, 0, data.size(), err); + } + + template + inline void Texture2D::SetData(std::vector 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 finalData(elementCount); + auto finalDataIndex = 0; + + for (size_t i = startIndex; i < elementCount; ++i) { + finalData[finalDataIndex] = static_cast(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 \ No newline at end of file diff --git a/framework/xna.cpp b/framework/xna.cpp index 5cbd3f5..2746e92 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -14,12 +14,6 @@ namespace xna { graphics = New(_game); graphics->PreferredBackBufferWidth(1280); graphics->PreferredBackBufferHeight(720); - - contentManager = New("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(*_graphicsDevice); - XnaErrorCode err; - texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); + XnaErrorCode err{0}; + //texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); + texture = New(_graphicsDevice.get(), 256, 256); + std::vector data(256 * 256, 4278190080U); + //std::vector data(256 * 256, 0xffffffff); + //std::vector data(256 * 256, 4278190080U); + texture->SetData(data, 0, data.size()); Game::LoadContent(); } diff --git a/framework/xnaerror.hpp b/framework/xnaerror.hpp index a89e32c..dcdf274 100644 --- a/framework/xnaerror.hpp +++ b/framework/xnaerror.hpp @@ -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) {