From 3fcb87768f584d7c9a003718047be458ad695187 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 16 Jul 2024 09:33:57 -0300 Subject: [PATCH 01/30] =?UTF-8?q?Corre=C3=A7=C3=B5es=20em=20SpriteBatch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/effect.cpp | 2 +- framework/platform-dx/rasterizerstate.cpp | 2 +- framework/platform-dx/sprite.cpp | 231 ++++++---------------- inc/xna/graphics/sprite.hpp | 2 +- inc/xna/xna-dx.hpp | 28 ++- 5 files changed, 88 insertions(+), 177 deletions(-) diff --git a/framework/platform-dx/effect.cpp b/framework/platform-dx/effect.cpp index 28c3311..5e1d83c 100644 --- a/framework/platform-dx/effect.cpp +++ b/framework/platform-dx/effect.cpp @@ -116,7 +116,7 @@ namespace xna { void BasicEffect::SetDirectionalLight(Int index, DirectionalLight const& direction) { DxVec vec3 = DxHelpers::VectorToDx(direction.Direction); - const auto value = (int)MathHelper::Clamp(index, 0, impl->dxBasicEffect->MaxDirectionalLights); + const auto value = static_cast(MathHelper::Clamp(static_cast(index), 0, impl->dxBasicEffect->MaxDirectionalLights)); impl->dxBasicEffect->SetLightDirection(value, vec3); } diff --git a/framework/platform-dx/rasterizerstate.cpp b/framework/platform-dx/rasterizerstate.cpp index 039ae67..2e61e8f 100644 --- a/framework/platform-dx/rasterizerstate.cpp +++ b/framework/platform-dx/rasterizerstate.cpp @@ -72,7 +72,7 @@ namespace xna { } void RasterizerState::DepthBias(float value) { - impl->dxDescription.DepthBias = value; + impl->dxDescription.DepthBias = static_cast(value); } float RasterizerState::SlopeScaleDepthBias() const { diff --git a/framework/platform-dx/sprite.cpp b/framework/platform-dx/sprite.cpp index 26a2461..866eb37 100644 --- a/framework/platform-dx/sprite.cpp +++ b/framework/platform-dx/sprite.cpp @@ -1,11 +1,3 @@ -#include "xna/graphics/rasterizerstate.hpp" -#include "xna/graphics/samplerstate.hpp" -#include "xna/common/color.hpp" -#include "xna/common/numerics.hpp" -#include "xna/graphics/sprite.hpp" -#include "xna/graphics/viewport.hpp" -#include "xna/graphics/blendstate.hpp" -#include "xna/graphics/depthstencilstate.hpp" #include "xna/xna-dx.hpp" #include @@ -31,11 +23,11 @@ namespace xna { std::vector const& kerning, std::optional const& defaultCharacter) { - if (!texture || !texture->impl->dxShaderResource) - throw std::invalid_argument("SpriteFont: texture is null."); + Exception::ThrowIfNull(texture, nameof(texture)); + Exception::ThrowIfNull(texture->impl->dxShaderResource.Get(), nameof(texture->impl->dxShaderResource)); if(cropping.size() != glyphs.size() || charMap.size() != glyphs.size() || (!kerning.empty() && kerning.size() != glyphs.size())) - throw std::invalid_argument("SpriteFont: cropping, charmap and kerning (if not empty) must all be the same size."); + Exception::Throw("Cropping, charmap and kerning (if not empty) must all be the same size."); std::vector dxGlyps(glyphs.size()); @@ -62,7 +54,7 @@ namespace xna { } impl = unew(); - impl->_dxSpriteFont = unew( + impl->dxSpriteFont = unew( //ID3D11ShaderResourceView* texture texture->impl->dxShaderResource.Get(), //Glyph const* glyphs @@ -75,16 +67,12 @@ namespace xna { if (defaultCharacter.has_value()) { const auto defChar = static_cast(defaultCharacter.value()); - impl->_dxSpriteFont->SetDefaultCharacter(defChar); + impl->dxSpriteFont->SetDefaultCharacter(defChar); } } - Vector2 SpriteFont::MeasureString(String const& text, bool ignoreWhiteSpace) - { - if (!impl->_dxSpriteFont) - return Vector2(); - - const auto size = impl->_dxSpriteFont->MeasureString(text.c_str(), ignoreWhiteSpace); + Vector2 SpriteFont::MeasureString(String const& text, bool ignoreWhiteSpace) { + const auto size = impl->dxSpriteFont->MeasureString(text.c_str(), ignoreWhiteSpace); Vector2 vec2{}; vec2.X = size.m128_f32[0]; vec2.Y = size.m128_f32[1]; @@ -92,12 +80,8 @@ namespace xna { return vec2; } - Vector2 SpriteFont::MeasureString(WString const& text, bool ignoreWhiteSpace) - { - if (!impl->_dxSpriteFont) - return Vector2(); - - const auto size = impl->_dxSpriteFont->MeasureString(text.c_str(), ignoreWhiteSpace); + Vector2 SpriteFont::MeasureString(WString const& text, bool ignoreWhiteSpace){ + const auto size = impl->dxSpriteFont->MeasureString(text.c_str(), ignoreWhiteSpace); Vector2 vec2{}; vec2.X = size.m128_f32[0]; vec2.Y = size.m128_f32[1]; @@ -106,30 +90,30 @@ namespace xna { } Char SpriteFont::DefaultCharacter() const { - const auto defChar = impl->_dxSpriteFont->GetDefaultCharacter(); + const auto defChar = impl->dxSpriteFont->GetDefaultCharacter(); return static_cast(defChar); } void SpriteFont::DefaultCharacter(Char value) { const auto defChar = static_cast(value); - impl->_dxSpriteFont->SetDefaultCharacter(defChar); + impl->dxSpriteFont->SetDefaultCharacter(defChar); } Int SpriteFont::LineSpacing() const { - const auto space = impl->_dxSpriteFont->GetLineSpacing(); + const auto space = impl->dxSpriteFont->GetLineSpacing(); return static_cast(space); } - void SpriteFont::LineSpacing(Int value) { - impl->_dxSpriteFont->SetLineSpacing(static_cast(value)); + void SpriteFont::LineSpacing(float value) { + impl->dxSpriteFont->SetLineSpacing(value); } SpriteBatch::SpriteBatch(sptr const& device) : GraphicsResource(device) { - if (!device->impl->_context) - return; + Exception::ThrowIfNull(device, nameof(device)); + Exception::ThrowIfNull(device->impl->_context.Get(), nameof(device->impl->_context)); impl = unew(); - impl->_dxspriteBatch = snew( + impl->dxSpriteBatch = snew( //ID3D11DeviceContext* deviceContext device->impl->_context.Get() ); @@ -138,31 +122,18 @@ namespace xna { } void SpriteBatch::Begin(SpriteSortMode sortMode, BlendState* blendState, SamplerState* samplerState, DepthStencilState* depthStencil, RasterizerState* rasterizerState, Effect* effect, Matrix const& transformMatrix) { - - if (!impl->_dxspriteBatch) - return; - - DxSpriteSortMode sort = DxHelpers::SpriteSortToDx(sortMode); - - const auto& t = transformMatrix; - DxMatrix matrix = DxMatrix( - t.M11, t.M12, t.M13, t.M14, - t.M21, t.M22, t.M23, t.M24, - t.M31, t.M32, t.M33, t.M34, - t.M41, t.M42, t.M43, t.M44); - std::function effectFunc = nullptr; - //if Effect is not null set effectBuffer and inputLayout - if (effect && effect->impl) { - bool effectBufferChanged = false; + //if Effect is not null set dxEffectBuffer and inputLayout + if (effect) { + bool dxEffectBufferChanged = false; - if (!impl->effectBuffer || impl->effectBuffer != effect->impl->dxEffect) { - impl->effectBuffer = effect->impl->dxEffect; - effectBufferChanged = true; + if (!impl->dxEffectBuffer || impl->dxEffectBuffer != effect->impl->dxEffect) { + impl->dxEffectBuffer = effect->impl->dxEffect; + dxEffectBufferChanged = true; } - if (effectBufferChanged) { + if (!impl->dxInputLayout || dxEffectBufferChanged) { void const* shaderByteCode; size_t byteCodeLength; @@ -178,68 +149,53 @@ namespace xna { auto& context = m_device->impl->_context; effectFunc = [=] { - impl->effectBuffer->Apply(context.Get()); + impl->dxEffectBuffer->Apply(context.Get()); context->IASetInputLayout(impl->dxInputLayout.Get()); }; } - impl->_dxspriteBatch->Begin( - sort, + auto _sortMode = DxHelpers::SpriteSortToDx(sortMode); + auto _transformMatrix = DxHelpers::MatrixToDx(transformMatrix); + + impl->dxSpriteBatch->Begin( + _sortMode, blendState ? blendState->impl->dxBlendState.Get() : nullptr, samplerState ? samplerState->impl->_samplerState.Get() : nullptr, depthStencil ? depthStencil->impl->dxDepthStencil.Get() : nullptr, rasterizerState ? rasterizerState->impl->dxRasterizerState.Get() : nullptr, effectFunc, - matrix + _transformMatrix ); } void SpriteBatch::End() { - if (!impl->_dxspriteBatch) - return; - - impl->_dxspriteBatch->End(); + impl->dxSpriteBatch->End(); } void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, Color const& color) { - if (!impl->_dxspriteBatch) - return; - - if (!texture.impl->dxShaderResource) - return; - - const auto _position = XMFLOAT2(position.X, position.Y); + const auto _position = DxHelpers::VectorToDx(position); const auto v4 = color.ToVector4(); - XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; + const auto _color = DxHelpers::VectorToDx(v4); - impl->_dxspriteBatch->Draw( + impl->dxSpriteBatch->Draw( texture.impl->dxShaderResource.Get(), _position, _color ); } - void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional const& sourceRectangle, Color const& color) { - if (!impl->_dxspriteBatch) - return; - - if (!texture.impl->dxShaderResource) - return; - - const auto _position = XMFLOAT2(position.X, position.Y); + void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional const& sourceRectangle, Color const& color) { + const auto _position = DxHelpers::VectorToDx(position); const auto v4 = color.ToVector4(); - const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; + const auto _color = DxHelpers::VectorToDx(v4); RECT _sourceRect{}; if (sourceRectangle.has_value()) { - _sourceRect.top = sourceRectangle->Y; - _sourceRect.left = sourceRectangle->X; - _sourceRect.right = sourceRectangle->X + sourceRectangle->Width; - _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; + _sourceRect = DxHelpers::RectangleToDx(sourceRectangle.value()); }; - impl->_dxspriteBatch->Draw( + impl->dxSpriteBatch->Draw( texture.impl->dxShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, @@ -247,29 +203,20 @@ namespace xna { } void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) { - if (!impl->_dxspriteBatch) - return; - - if (!texture.impl->dxShaderResource) - return; - - const auto _position = XMFLOAT2(position.X, position.Y); - const auto _origin = XMFLOAT2(origin.X, origin.Y); + const auto _position = DxHelpers::VectorToDx(position); + const auto _origin = DxHelpers::VectorToDx(origin); const auto v4 = color.ToVector4(); - const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; + const auto _color = DxHelpers::VectorToDx(v4); RECT _sourceRect{}; if (sourceRectangle.has_value()) { - _sourceRect.top = sourceRectangle->Y; - _sourceRect.left = sourceRectangle->X; - _sourceRect.right = sourceRectangle->X + sourceRectangle->Width; - _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; + _sourceRect = DxHelpers::RectangleToDx(sourceRectangle.value()); }; const DxSpriteEffects _effects = static_cast(effects); - impl->_dxspriteBatch->Draw( + impl->dxSpriteBatch->Draw( texture.impl->dxShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, @@ -282,12 +229,6 @@ namespace xna { } void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth) { - if (!impl->_dxspriteBatch) - return; - - if (!texture.impl->dxShaderResource) - return; - const auto _position = XMFLOAT2(position.X, position.Y); const auto _origin = XMFLOAT2(origin.X, origin.Y); const auto v4 = color.ToVector4(); @@ -296,16 +237,13 @@ namespace xna { RECT _sourceRect{}; if (sourceRectangle.has_value()) { - _sourceRect.top = sourceRectangle->Y; - _sourceRect.left = sourceRectangle->X; - _sourceRect.right = sourceRectangle->X + sourceRectangle->Width; - _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; + _sourceRect = DxHelpers::RectangleToDx(sourceRectangle.value()); }; const auto _effects = static_cast(effects); const XMFLOAT2 _scale = { scale.X, scale.Y }; - impl->_dxspriteBatch->Draw( + impl->dxSpriteBatch->Draw( texture.impl->dxShaderResource.Get(), _position, sourceRectangle ? &_sourceRect : nullptr, @@ -318,36 +256,16 @@ namespace xna { } void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color) { - if (!impl->_dxspriteBatch) - return; - - if (!texture.impl->dxShaderResource) - return; - - RECT _destinationRect{}; - _destinationRect.left = destinationRectangle.X; - _destinationRect.top = destinationRectangle.Y; - _destinationRect.right = destinationRectangle.X + destinationRectangle.Width; - _destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height; + RECT _destinationRect = DxHelpers::RectangleToDx(destinationRectangle); const auto v4 = color.ToVector4(); const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; - impl->_dxspriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, _color); + impl->dxSpriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, _color); } void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional const& sourceRectangle, Color const& color) { - if (!impl->_dxspriteBatch) - return; - - if (!texture.impl->dxShaderResource) - return; - - RECT _destinationRect{}; - _destinationRect.left = destinationRectangle.X; - _destinationRect.top = destinationRectangle.Y; - _destinationRect.right = destinationRectangle.X + destinationRectangle.Width; - _destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height; + RECT _destinationRect = DxHelpers::RectangleToDx(destinationRectangle); const auto v4 = color.ToVector4(); const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; @@ -361,21 +279,11 @@ namespace xna { _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; }; - impl->_dxspriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); + impl->dxSpriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); } void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) { - if (!impl->_dxspriteBatch) - return; - - if (!texture.impl->dxShaderResource) - return; - - RECT _destinationRect{}; - _destinationRect.left = destinationRectangle.X; - _destinationRect.top = destinationRectangle.Y; - _destinationRect.right = destinationRectangle.X + destinationRectangle.Width; - _destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height; + RECT _destinationRect = DxHelpers::RectangleToDx(destinationRectangle); const auto v4 = color.ToVector4(); const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; @@ -383,16 +291,13 @@ namespace xna { RECT _sourceRect{}; if (sourceRectangle.has_value()) { - _sourceRect.top = sourceRectangle->Y; - _sourceRect.left = sourceRectangle->X; - _sourceRect.right = sourceRectangle->X + sourceRectangle->Width; - _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; + _sourceRect = DxHelpers::RectangleToDx(sourceRectangle.value()); }; auto _origin = XMFLOAT2(origin.X, origin.Y); const auto _effects = static_cast(effects); - impl->_dxspriteBatch->Draw( + impl->dxSpriteBatch->Draw( texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, @@ -404,30 +309,17 @@ namespace xna { } void SpriteBatch::Viewport(xna::Viewport const& value) { - if (!impl->_dxspriteBatch) - return; - - D3D11_VIEWPORT _view{}; - _view.TopLeftX = value.X; - _view.TopLeftY = value.Y; - _view.Width = value.Width; - _view.Height = value.Height; - _view.MinDepth = value.MinDetph; - _view.MaxDepth = value.MaxDepth; - - impl->_dxspriteBatch->SetViewport(_view); + const auto _view = DxHelpers::ViewportToDx(value); + impl->dxSpriteBatch->SetViewport(_view); } void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color) { - if (!impl->_dxspriteBatch || !spriteFont.impl->_dxSpriteFont) - return; - const auto _position = XMFLOAT2(position.X, position.Y); const auto v4 = color.ToVector4(); const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; - spriteFont.impl->_dxSpriteFont->DrawString( - impl->_dxspriteBatch.get(), + spriteFont.impl->dxSpriteFont->DrawString( + impl->dxSpriteBatch.get(), text.c_str(), _position, _color @@ -436,17 +328,14 @@ namespace xna { void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) { - if (!impl->_dxspriteBatch || !spriteFont.impl->_dxSpriteFont) - return; - const auto _position = XMFLOAT2(position.X, position.Y); const auto _origin = XMFLOAT2(origin.X, origin.Y); const auto v4 = color.ToVector4(); const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; const auto _effects = static_cast(effects); - spriteFont.impl->_dxSpriteFont->DrawString( - impl->_dxspriteBatch.get(), + spriteFont.impl->dxSpriteFont->DrawString( + impl->dxSpriteBatch.get(), text.c_str(), _position, _color, diff --git a/inc/xna/graphics/sprite.hpp b/inc/xna/graphics/sprite.hpp index f25cf66..90a2964 100644 --- a/inc/xna/graphics/sprite.hpp +++ b/inc/xna/graphics/sprite.hpp @@ -154,7 +154,7 @@ namespace xna { //Gets or sets the vertical distance (in pixels) between the base lines of two consecutive lines of text Int LineSpacing() const; //Gets or sets the vertical distance (in pixels) between the base lines of two consecutive lines of text - void LineSpacing(Int value); + void LineSpacing(float value); public: struct PlatformImplementation; diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index a3bf2f3..e0c326b 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -64,6 +64,28 @@ namespace xna { //---------------- HELPERS ----------------// struct DxHelpers { + static constexpr RECT RectangleToDx(Rectangle const& value) { + RECT rect{}; + rect.top = value.Top(); + rect.left = value.Left(); + rect.right = value.Right(); + rect.bottom = value.Bottom(); + + return rect; + } + + static constexpr D3D11_VIEWPORT ViewportToDx(Viewport const& value) { + D3D11_VIEWPORT _view{}; + _view.TopLeftX = value.X; + _view.TopLeftY = value.Y; + _view.Width = value.Width; + _view.Height = value.Height; + _view.MinDepth = value.MinDetph; + _view.MaxDepth = value.MaxDepth; + + return _view; + } + static constexpr DirectX::XMVECTOR VectorToDx(Vector2 const& value) { DirectX::XMVECTOR v{}; @@ -560,13 +582,13 @@ namespace xna { //---------------- IMPLEMENTATIONS ----------------// struct SpriteFont::PlatformImplementation { - uptr _dxSpriteFont{ nullptr }; + uptr dxSpriteFont{ nullptr }; }; struct SpriteBatch::PlatformImplementation { - sptr _dxspriteBatch = nullptr; + sptr dxSpriteBatch = nullptr; comptr dxInputLayout = nullptr; - sptr effectBuffer = nullptr; + sptr dxEffectBuffer = nullptr; }; struct GraphicsAdapter::PlatformImplementation { From 80a6e2d3894103211996096f6d9297533f4b57b1 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 16 Jul 2024 09:50:53 -0300 Subject: [PATCH 02/30] =?UTF-8?q?Muda=20a=20assinatura=20de=20Clamp=20e=20?= =?UTF-8?q?corre=C3=A7=C3=B5es=20de=20avisos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/effect.cpp | 4 ++-- framework/platform-dx/rasterizerstate.cpp | 2 +- framework/platform-dx/samplerstate.cpp | 5 ++++- inc/xna/common/math.hpp | 5 +++-- inc/xna/content/reader.hpp | 2 +- inc/xna/csharp/stream.hpp | 3 ++- 6 files changed, 13 insertions(+), 8 deletions(-) diff --git a/framework/platform-dx/effect.cpp b/framework/platform-dx/effect.cpp index 5e1d83c..e67ed90 100644 --- a/framework/platform-dx/effect.cpp +++ b/framework/platform-dx/effect.cpp @@ -30,7 +30,7 @@ namespace xna { } void BasicEffect::Alpha(float value) { - const auto a = MathHelper::Clamp(value, 0.0f, 1.0); + const auto a = MathHelper::Clamp(value, 0.0F, 1.0F); impl->dxBasicEffect->SetAlpha(a); } @@ -116,7 +116,7 @@ namespace xna { void BasicEffect::SetDirectionalLight(Int index, DirectionalLight const& direction) { DxVec vec3 = DxHelpers::VectorToDx(direction.Direction); - const auto value = static_cast(MathHelper::Clamp(static_cast(index), 0, impl->dxBasicEffect->MaxDirectionalLights)); + const auto value = MathHelper::Clamp(index, 0, impl->dxBasicEffect->MaxDirectionalLights); impl->dxBasicEffect->SetLightDirection(value, vec3); } diff --git a/framework/platform-dx/rasterizerstate.cpp b/framework/platform-dx/rasterizerstate.cpp index 2e61e8f..777963f 100644 --- a/framework/platform-dx/rasterizerstate.cpp +++ b/framework/platform-dx/rasterizerstate.cpp @@ -68,7 +68,7 @@ namespace xna { } float RasterizerState::DepthBias() const { - return impl->dxDescription.DepthBias; + return static_cast(impl->dxDescription.DepthBias); } void RasterizerState::DepthBias(float value) { diff --git a/framework/platform-dx/samplerstate.cpp b/framework/platform-dx/samplerstate.cpp index f624144..5209173 100644 --- a/framework/platform-dx/samplerstate.cpp +++ b/framework/platform-dx/samplerstate.cpp @@ -65,7 +65,10 @@ namespace xna { states[i]->AddRef(); } - device.impl->_context->PSSetSamplers(0, states.size(), states.data()); + device.impl->_context->PSSetSamplers( + 0, + static_cast(states.size()), + states.data()); for (size_t i = 0; i < samplers.size(); ++i) { states[i]->Release(); diff --git a/inc/xna/common/math.hpp b/inc/xna/common/math.hpp index afaec53..e677068 100644 --- a/inc/xna/common/math.hpp +++ b/inc/xna/common/math.hpp @@ -17,11 +17,12 @@ namespace xna { static constexpr float Min(float value1, float value2) { return (std::min)(value1, value2); } static constexpr float Max(float value1, float value2) { return (std::max)(value1, value2); } - static constexpr float Clamp(float value, float min, float max) { + template + static constexpr T Clamp(T value, T min, T max) { value = value > max ? max : value; value = value < min ? min : value; return value; - } + } static constexpr float Lerp(float value1, float value2, float amount) { return value1 + (value2 - value1) * amount; diff --git a/inc/xna/content/reader.hpp b/inc/xna/content/reader.hpp index a96f6d1..ff4b3f7 100644 --- a/inc/xna/content/reader.hpp +++ b/inc/xna/content/reader.hpp @@ -176,7 +176,7 @@ namespace xna { if (typeReader.TargetIsValueType) return InvokeReader(typeReader, existingInstance); - ReadObjectInternal(existingInstance); + return ReadObjectInternal(existingInstance); } } diff --git a/inc/xna/csharp/stream.hpp b/inc/xna/csharp/stream.hpp index 4e33bfe..103f881 100644 --- a/inc/xna/csharp/stream.hpp +++ b/inc/xna/csharp/stream.hpp @@ -47,7 +47,8 @@ namespace xna { class MemoryStream : public Stream { public: constexpr MemoryStream(std::vector const& bytes): - _buffer(bytes), _length(bytes.size()){} + _buffer(bytes), + _length(static_cast(bytes.size())){} ~MemoryStream() override { Close(); From 64497afdf3b53974be62a81b8e9462ab3af4d39e Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 16 Jul 2024 12:28:03 -0300 Subject: [PATCH 03/30] =?UTF-8?q?Corre=C3=A7=C3=B5es=20em=20Adapter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/adapter.cpp | 95 +++++++++++++---------------- framework/platform-dx/device.cpp | 6 +- framework/platform-dx/swapchain.cpp | 16 ++--- inc/xna/xna-dx.hpp | 11 ++-- 4 files changed, 55 insertions(+), 73 deletions(-) diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index a5e4132..9a38799 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -13,7 +13,7 @@ namespace xna { uptr GraphicsAdapter::DefaultAdapter() { comptr pFactory = nullptr; - + if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)pFactory.GetAddressOf())) Exception::Throw(Exception::FAILED_TO_CREATE); @@ -23,7 +23,8 @@ namespace xna { auto adp = unew(); adp->impl->_index = 0; - adp->impl->dxadapter = pAdapter; + adp->impl->dxAdapter = pAdapter; + adp->impl->dxFactory = pFactory; return adp; } @@ -37,49 +38,47 @@ namespace xna { if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)pFactory.GetAddressOf())) Exception::Throw(Exception::FAILED_TO_CREATE); - comptr pAdapter = nullptr; - UINT count = 0; + comptr pAdapter = nullptr; - for (; pFactory->EnumAdapters1(count, pAdapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND; ++count) { + for (UINT count = 0; pFactory->EnumAdapters1(count, pAdapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND; ++count) { auto adp = unew(); adp->impl->_index = count; - adp->impl->dxadapter = pAdapter; + adp->impl->dxAdapter = pAdapter; + adp->impl->dxFactory = pFactory; adapters.push_back(std::move(adp)); } } String GraphicsAdapter::Description() const { - if (!impl->dxadapter) return String(); + if (!impl->dxAdapter) return String(); DXGI_ADAPTER_DESC1 desc; - impl->dxadapter->GetDesc1(&desc); + impl->dxAdapter->GetDesc1(&desc); String description = XnaHelper::ToString(desc.Description); return description; } Uint GraphicsAdapter::DeviceId() const { - if (!impl->dxadapter) return 0; + if (!impl->dxAdapter) return 0; DXGI_ADAPTER_DESC1 desc; - impl->dxadapter->GetDesc1(&desc); + impl->dxAdapter->GetDesc1(&desc); return static_cast(desc.DeviceId); } String GraphicsAdapter::DeviceName() const { - if (!impl->dxadapter) return String(); + if (!impl->dxAdapter) return String(); - IDXGIOutput* pOutput = nullptr; - DXGI_OUTPUT_DESC outputDesc; + comptr pOutput = nullptr; - if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { + if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { + DXGI_OUTPUT_DESC outputDesc{}; pOutput->GetDesc(&outputDesc); + String deviceName = XnaHelper::ToString(outputDesc.DeviceName); - - pOutput->Release(); - pOutput = nullptr; return deviceName; } @@ -88,16 +87,13 @@ namespace xna { } intptr_t GraphicsAdapter::MonitorHandle() const { - if (!impl->dxadapter) return 0; + if (!impl->dxAdapter) return 0; - IDXGIOutput* pOutput = nullptr; - DXGI_OUTPUT_DESC outputDesc; + comptr pOutput = nullptr; - if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { - pOutput->GetDesc(&outputDesc); - - pOutput->Release(); - pOutput = nullptr; + if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { + DXGI_OUTPUT_DESC outputDesc; + pOutput->GetDesc(&outputDesc); return reinterpret_cast(outputDesc.Monitor); } @@ -106,46 +102,46 @@ namespace xna { } Uint GraphicsAdapter::Revision() const { - if (!impl->dxadapter) return 0; + if (!impl->dxAdapter) return 0; DXGI_ADAPTER_DESC1 desc; - impl->dxadapter->GetDesc1(&desc); + impl->dxAdapter->GetDesc1(&desc); return static_cast(desc.Revision); } Uint GraphicsAdapter::SubSystemId() const { - if (!impl->dxadapter) return 0; + if (!impl->dxAdapter) return 0; DXGI_ADAPTER_DESC1 desc; - impl->dxadapter->GetDesc1(&desc); + impl->dxAdapter->GetDesc1(&desc); return static_cast(desc.SubSysId); } Uint GraphicsAdapter::VendorId() const { - if (!impl->dxadapter) return 0; + if (!impl->dxAdapter) return 0; DXGI_ADAPTER_DESC1 desc; - impl->dxadapter->GetDesc1(&desc); + impl->dxAdapter->GetDesc1(&desc); return static_cast(desc.VendorId); } uptr GraphicsAdapter::SupportedDisplayModes() const { - if (!impl->dxadapter) return nullptr; + if (!impl->dxAdapter) return nullptr; - const auto totalDisplay = getDisplayModesCount(impl->dxadapter.Get()); + const auto totalDisplay = getDisplayModesCount(impl->dxAdapter.Get()); if (totalDisplay == 0) return nullptr; - IDXGIOutput* pOutput = nullptr; + comptr pOutput = nullptr; UINT bufferOffset = 0; std::vector buffer(totalDisplay); - if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { + if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) { const auto currentSurface = static_cast(f); DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(currentSurface); @@ -165,20 +161,17 @@ namespace xna { if (!pOutput) return nullptr; - pOutput->Release(); - pOutput = nullptr; - return createDisplayModeCollection(buffer); } uptr GraphicsAdapter::SupportedDisplayModes(SurfaceFormat surfaceFormat) const { - if (!impl->dxadapter) return nullptr; + if (!impl->dxAdapter) return nullptr; - IDXGIOutput* pOutput = nullptr; + comptr pOutput = nullptr; UINT bufferOffset = 0; - if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { + if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(surfaceFormat); UINT numModes = 0; @@ -189,10 +182,7 @@ namespace xna { return unew(); std::vector buffer(numModes); - pOutput->GetDisplayModeList(format, 0, &numModes, buffer.data()); - - pOutput->Release(); - pOutput = nullptr; + pOutput->GetDisplayModeList(format, 0, &numModes, buffer.data()); return createDisplayModeCollection(buffer); } @@ -223,10 +213,10 @@ namespace xna { } } - bool GraphicsAdapter::PlatformImplementation::GetOutput(UINT slot, IDXGIOutput*& output) { - if (!dxadapter) return false; + bool GraphicsAdapter::PlatformImplementation::GetOutput(UINT slot, IDXGIOutput*& output) const { + if (!dxAdapter) return false; - if (dxadapter->EnumOutputs(slot, &output) != DXGI_ERROR_NOT_FOUND) + if (dxAdapter->EnumOutputs(slot, &output) != DXGI_ERROR_NOT_FOUND) return true; return false; @@ -235,10 +225,10 @@ namespace xna { //INTERNAL FUNCTIONS static size_t getDisplayModesCount(IDXGIAdapter* adapter) { - IDXGIOutput* pOutput = nullptr; + comptr pOutput = nullptr; size_t numModes = 0; - if (adapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { + if (adapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) { const auto currentSurface = static_cast(f); DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(currentSurface); @@ -247,10 +237,7 @@ namespace xna { pOutput->GetDisplayModeList(format, 0, &num, nullptr); numModes += num; - } - - pOutput->Release(); - pOutput = nullptr; + } } return numModes; diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 2ee9001..50d839e 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -25,8 +25,10 @@ namespace xna { #if _DEBUG createDeviceFlags = D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG; #endif + const auto& currentAdapter = impl._adapter; + auto hr = D3D11CreateDevice( - impl._adapter->impl->dxadapter.Get(), + currentAdapter ? currentAdapter->impl->dxAdapter.Get() : NULL, D3D_DRIVER_TYPE_UNKNOWN, NULL, createDeviceFlags, @@ -57,7 +59,7 @@ namespace xna { } } - void initAndApplyState(GraphicsDevice::PlatformImplementation& impl, PGraphicsDevice const& device) { + static void initAndApplyState(GraphicsDevice::PlatformImplementation& impl, PGraphicsDevice const& device) { impl._blendState->Bind(device); impl._blendState->Initialize(); impl._blendState->Apply(); diff --git a/framework/platform-dx/swapchain.cpp b/framework/platform-dx/swapchain.cpp index 19aacb3..c440bb4 100644 --- a/framework/platform-dx/swapchain.cpp +++ b/framework/platform-dx/swapchain.cpp @@ -24,18 +24,13 @@ namespace xna { swapChain.ReleaseAndGetAddressOf(); } - auto adapter = device.Adapter(); - auto dxAdapter = adapter->impl->dxadapter; + auto adapter = device.Adapter(); - IDXGIFactory1* dxFactory1 = nullptr; - auto hr = dxAdapter->GetParent(IID_IDXGIFactory1, (void**)&dxFactory1); + comptr dxFactory2 = nullptr; + const auto hr = adapter->impl->dxFactory->QueryInterface(IID_IDXGIFactory2, (void**)&dxFactory2); - if (FAILED(hr)) return false; - - IDXGIFactory2* dxFactory2 = nullptr; - hr = dxFactory1->QueryInterface(IID_IDXGIFactory2, (void**)&dxFactory2); - - if (FAILED(hr)) return false; + if (FAILED(hr)) + return false; dxFactory2->CreateSwapChainForHwnd( device.impl->_device.Get(), @@ -45,7 +40,6 @@ namespace xna { nullptr, swapChain.GetAddressOf()); - return true; } diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index e0c326b..685213d 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -1,5 +1,5 @@ -#ifndef XNA_XNA_DX_HPP -#define XNA_XNA_DX_HPP +#ifndef XNA_XNADX_HPP +#define XNA_XNADX_HPP #define NOMINMAX @@ -95,7 +95,6 @@ namespace xna { return v; } - static constexpr DirectX::XMVECTOR VectorToDx(Vector3 const& value) { DirectX::XMVECTOR v{}; @@ -150,7 +149,6 @@ namespace xna { return m; } - static constexpr DirectX::SpriteSortMode SpriteSortToDx(SpriteSortMode value) { return static_cast(static_cast(value)); } @@ -592,7 +590,8 @@ namespace xna { }; struct GraphicsAdapter::PlatformImplementation { - comptr dxadapter = nullptr; + comptr dxAdapter = nullptr; + comptr dxFactory = nullptr; private: friend class GraphicsAdapter; @@ -600,7 +599,7 @@ namespace xna { sptr _currentDisplayMode = nullptr; public: - bool GetOutput(UINT slot, IDXGIOutput*& output); + bool GetOutput(UINT slot, IDXGIOutput*& output) const; }; struct BlendRenderTarget { From 6e10a3a6699bc2bc3469d7a7d92506ab80283a12 Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 19 Jul 2024 22:11:57 -0300 Subject: [PATCH 04/30] =?UTF-8?q?Corre=C3=A7=C3=B5es=20em=20Adapter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/adapter.cpp | 223 ++++++++++++++++-------------- framework/platform-dx/device.cpp | 12 +- inc/xna/graphics/adapter.hpp | 103 ++++++++++---- inc/xna/xna-dx.hpp | 9 +- 4 files changed, 200 insertions(+), 147 deletions(-) diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index 9a38799..fe0e00c 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -4,8 +4,17 @@ #include "xna/xna-dx.hpp" namespace xna { + static String getDescription(comptr const& adapter); + static Uint getDeviceId(comptr const& adapter); + static String getDeviceName(comptr const& adapter); + static intptr_t getMonitorHandle(comptr const& adapter); + static Uint getRevision(comptr const& adapter); + static Uint getRevision(comptr const& adapter); + static Uint getSubSystemId(comptr const& adapter); + static Uint getVendorId(comptr const& adapter); static size_t getDisplayModesCount(IDXGIAdapter* adapter); - static uptr createDisplayModeCollection(std::vector const& source); + static uptr createDisplayModeCollection(std::vector const& source); + static void setCurrentDisplayMode(GraphicsAdapter& adapter, SurfaceFormat surfaceFormat, Uint width, Uint height, sptr& currentDisplayMode); GraphicsAdapter::GraphicsAdapter() { impl = unew(); @@ -21,10 +30,18 @@ namespace xna { if (pFactory->EnumAdapters1(0, pAdapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { auto adp = unew(); - - adp->impl->_index = 0; + adp->impl->dxAdapter = pAdapter; - adp->impl->dxFactory = pFactory; + adp->impl->dxFactory = pFactory; + + adp->description = getDescription(pAdapter); + adp->deviceId = getDeviceId(pAdapter); + adp->deviceName = getDeviceName(pAdapter); + adp->isDefault = true; + adp->monitorHandle = getMonitorHandle(pAdapter); + adp->revision = getRevision(pAdapter); + adp->subSystemId = getSubSystemId(pAdapter); + adp->vendorId = getVendorId(pAdapter); return adp; } @@ -43,90 +60,21 @@ namespace xna { for (UINT count = 0; pFactory->EnumAdapters1(count, pAdapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND; ++count) { auto adp = unew(); - adp->impl->_index = count; adp->impl->dxAdapter = pAdapter; adp->impl->dxFactory = pFactory; + adp->description = getDescription(pAdapter); + adp->deviceId = getDeviceId(pAdapter); + adp->deviceName = getDeviceName(pAdapter); + adp->isDefault = count == 0; + adp->monitorHandle = getMonitorHandle(pAdapter); + adp->revision = getRevision(pAdapter); + adp->subSystemId = getSubSystemId(pAdapter); + adp->vendorId = getVendorId(pAdapter); + adapters.push_back(std::move(adp)); } - } - - String GraphicsAdapter::Description() const { - if (!impl->dxAdapter) return String(); - - DXGI_ADAPTER_DESC1 desc; - impl->dxAdapter->GetDesc1(&desc); - String description = XnaHelper::ToString(desc.Description); - return description; - } - - Uint GraphicsAdapter::DeviceId() const { - if (!impl->dxAdapter) return 0; - - DXGI_ADAPTER_DESC1 desc; - impl->dxAdapter->GetDesc1(&desc); - - return static_cast(desc.DeviceId); - } - - String GraphicsAdapter::DeviceName() const { - if (!impl->dxAdapter) return String(); - - comptr pOutput = nullptr; - - if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { - DXGI_OUTPUT_DESC outputDesc{}; - pOutput->GetDesc(&outputDesc); - - String deviceName = XnaHelper::ToString(outputDesc.DeviceName); - - return deviceName; - } - - return String(); - } - - intptr_t GraphicsAdapter::MonitorHandle() const { - if (!impl->dxAdapter) return 0; - - comptr pOutput = nullptr; - - if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { - DXGI_OUTPUT_DESC outputDesc; - pOutput->GetDesc(&outputDesc); - - return reinterpret_cast(outputDesc.Monitor); - } - - return 0; - } - - Uint GraphicsAdapter::Revision() const { - if (!impl->dxAdapter) return 0; - - DXGI_ADAPTER_DESC1 desc; - impl->dxAdapter->GetDesc1(&desc); - - return static_cast(desc.Revision); - } - - Uint GraphicsAdapter::SubSystemId() const { - if (!impl->dxAdapter) return 0; - - DXGI_ADAPTER_DESC1 desc; - impl->dxAdapter->GetDesc1(&desc); - - return static_cast(desc.SubSysId); - } - - Uint GraphicsAdapter::VendorId() const { - if (!impl->dxAdapter) return 0; - - DXGI_ADAPTER_DESC1 desc; - impl->dxAdapter->GetDesc1(&desc); - - return static_cast(desc.VendorId); - } + } uptr GraphicsAdapter::SupportedDisplayModes() const { if (!impl->dxAdapter) return nullptr; @@ -191,27 +139,16 @@ namespace xna { } sptr GraphicsAdapter::CurrentDisplayMode() { - if (!impl->_currentDisplayMode) { - CurrentDisplayMode(SurfaceFormat::Color, GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight); + if (!currentDisplayMode) { + setCurrentDisplayMode(*this, + SurfaceFormat::Color, + GraphicsDeviceManager::DefaultBackBufferWidth, + GraphicsDeviceManager::DefaultBackBufferHeight, + currentDisplayMode); } - return impl->_currentDisplayMode; - } - - void GraphicsAdapter::CurrentDisplayMode(SurfaceFormat surfaceFormat, Uint width, Uint height) { - const auto modes = SupportedDisplayModes(surfaceFormat); - - for (size_t i = 0; i < modes->DisplayModes.size(); ++i) { - auto& m = modes->DisplayModes[i]; - - if (m->Format == surfaceFormat && m->Width == width && m->Height == height) { - impl->_currentDisplayMode = m; - } - else if(i + 1 == modes->DisplayModes.size()) { - impl->_currentDisplayMode = m; - } - } - } + return currentDisplayMode; + } bool GraphicsAdapter::PlatformImplementation::GetOutput(UINT slot, IDXGIOutput*& output) const { if (!dxAdapter) return false; @@ -224,7 +161,22 @@ namespace xna { //INTERNAL FUNCTIONS - static size_t getDisplayModesCount(IDXGIAdapter* adapter) { + void setCurrentDisplayMode(GraphicsAdapter& adapter, SurfaceFormat surfaceFormat, Uint width, Uint height, sptr& currentDisplayMode) { + const auto modes = adapter.SupportedDisplayModes(surfaceFormat); + + for (size_t i = 0; i < modes->DisplayModes.size(); ++i) { + auto& m = modes->DisplayModes[i]; + + if (m->Format == surfaceFormat && m->Width == width && m->Height == height) { + currentDisplayMode = m; + } + else if (i + 1 == modes->DisplayModes.size()) { + currentDisplayMode = m; + } + } + } + + size_t getDisplayModesCount(IDXGIAdapter* adapter) { comptr pOutput = nullptr; size_t numModes = 0; @@ -243,7 +195,7 @@ namespace xna { return numModes; } - static uptr createDisplayModeCollection(std::vector const& source) { + uptr createDisplayModeCollection(std::vector const& source) { auto collection = unew(); DisplayMode currentDisplayMode; std::vector> displayList; @@ -274,4 +226,67 @@ namespace xna { return collection; } + + String getDescription(comptr const& adapter) { + DXGI_ADAPTER_DESC1 desc; + adapter->GetDesc1(&desc); + const auto description = XnaHelper::ToString(desc.Description); + return description; + } + + Uint getDeviceId(comptr const& adapter) { + DXGI_ADAPTER_DESC1 desc; + adapter->GetDesc1(&desc); + + return static_cast(desc.DeviceId); + } + + String getDeviceName(comptr const& adapter) { + comptr pOutput = nullptr; + + if (adapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { + DXGI_OUTPUT_DESC outputDesc{}; + pOutput->GetDesc(&outputDesc); + + String deviceName = XnaHelper::ToString(outputDesc.DeviceName); + + return deviceName; + } + + return String(); + } + + intptr_t getMonitorHandle(comptr const& adapter) { + comptr pOutput = nullptr; + + if (adapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { + DXGI_OUTPUT_DESC outputDesc; + pOutput->GetDesc(&outputDesc); + + return reinterpret_cast(outputDesc.Monitor); + } + + return 0; + } + + Uint getRevision(comptr const& adapter) { + DXGI_ADAPTER_DESC1 desc; + adapter->GetDesc1(&desc); + + return static_cast(desc.Revision); + } + + Uint getSubSystemId(comptr const& adapter) { + DXGI_ADAPTER_DESC1 desc; + adapter->GetDesc1(&desc); + + return static_cast(desc.SubSysId); + } + + Uint getVendorId(comptr const& adapter) { + DXGI_ADAPTER_DESC1 desc; + adapter->GetDesc1(&desc); + + return static_cast(desc.VendorId); + } } \ No newline at end of file diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 50d839e..dfa1e50 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -77,11 +77,7 @@ namespace xna { GraphicsDevice::GraphicsDevice() { impl = unew(); - impl->_adapter = GraphicsAdapter::DefaultAdapter(); - impl->_adapter->CurrentDisplayMode( - SurfaceFormat::Color, - GraphicsDeviceManager::DefaultBackBufferWidth, - GraphicsDeviceManager::DefaultBackBufferHeight); + impl->_adapter = GraphicsAdapter::DefaultAdapter(); } GraphicsDevice::GraphicsDevice(GraphicsDeviceInformation const& info) { @@ -89,11 +85,7 @@ namespace xna { impl->_adapter = info.Adapter; impl->_gameWindow = info.Window; - impl->_presentationParameters = info.Parameters; - impl->_adapter->CurrentDisplayMode( - impl->_presentationParameters->BackBufferFormat, - impl->_presentationParameters->BackBufferWidth, - impl->_presentationParameters->BackBufferHeight); + impl->_presentationParameters = info.Parameters; } bool GraphicsDevice::Initialize() { diff --git a/inc/xna/graphics/adapter.hpp b/inc/xna/graphics/adapter.hpp index c0dad93..ae04431 100644 --- a/inc/xna/graphics/adapter.hpp +++ b/inc/xna/graphics/adapter.hpp @@ -2,6 +2,7 @@ #define XNA_GRAPHICS_ADAPTER_HPP #include "../default.hpp" +#include "displaymode.hpp" namespace xna { //Provides methods to retrieve and manipulate graphics adapters. @@ -9,38 +10,88 @@ namespace xna { public: GraphicsAdapter(); - //Retrieves a string used for presentation to the user. - String Description() const; - //Retrieves a value that is used to help identify a particular chip set. - Uint DeviceId() const; - //Retrieves a string that contains the device name. - String DeviceName() const; - //Determines if this instance of GraphicsAdapter is the default adapter. - bool IsDefaultAdapter() const; - //Retrieves the handle of the monitor - intptr_t MonitorHandle() const; - //Retrieves a value used to help identify the revision level of a particular chip set. - Uint Revision() const; - //Retrieves a value used to identify the subsystem. - Uint SubSystemId() const; - //Retrieves a value used to identify the manufacturer. - Uint VendorId() const; + //Collection of available adapters on the system. + static void Adapters(std::vector>& adapters); + //Gets the current display mode. + sptr CurrentDisplayMode(); + + //Gets the default adapter. + static uptr DefaultAdapter(); + + //Retrieves a string used for presentation to the user. + constexpr String Description() const { return description; } + + //Retrieves a value that is used to help identify a particular chip set. + constexpr Uint DeviceId() const { return deviceId; } + + //Retrieves a string that contains the device name. + constexpr String DeviceName() const { return deviceName; } + + //Determines if this instance of GraphicsAdapter is the default adapter. + constexpr bool IsDefaultAdapter() const { return isDefault; } + + //Determines if the graphics adapter is in widescreen mode. + constexpr bool IsWideScreen() const { return false; } + + //Retrieves the handle of the monitor + constexpr intptr_t MonitorHandle() const { return monitorHandle; } + + //Retrieves a value used to help identify the revision level of a particular chip set. + constexpr Uint Revision() const { return revision; } + + //Retrieves a value used to identify the subsystem. + constexpr Uint SubSystemId() const { return subSystemId; } + //Returns a collection of supported display modes for the current adapter. uptr SupportedDisplayModes() const; //Returns a collection of supported display modes for the current adapter. uptr SupportedDisplayModes(SurfaceFormat surfaceFormat) const; - - //Gets the current display mode. - sptr CurrentDisplayMode(); - //Gets the current display mode. - void CurrentDisplayMode(SurfaceFormat surfaceFormat, Uint width, Uint height); - //Gets the default adapter. - static uptr DefaultAdapter(); - - //Collection of available adapters on the system. - static void Adapters(std::vector>& adapters); + //Retrieves a value used to identify the manufacturer. + constexpr Uint VendorId() const { return vendorId; } + + //Tests to see if the adapter supports the requested profile. + bool IsProfileSupported(GraphicsProfile graphicsProfile) { + return false; + } + + //Queries the adapter for support for the requested back buffer format. + bool QueryBackBufferFormat( + GraphicsProfile graphicsProfile, + SurfaceFormat format, + DepthFormat depthFormat, + Int multiSampleCount, + SurfaceFormat& selectedFormat, + DepthFormat& selectedDepthFormat, + Int& selectedMultiSampleCount + ) { + return false; + } + + //Queries the adapter for support for the requested render target format. + bool QueryRenderTargetFormat( + GraphicsProfile graphicsProfile, + SurfaceFormat format, + DepthFormat depthFormat, + Int multiSampleCount, + SurfaceFormat& selectedFormat, + DepthFormat& selectedDepthFormat, + int& selectedMultiSampleCount + ) { + return false; + } + + private: + String description; + Uint deviceId{0}; + String deviceName; + bool isDefault{ false }; + intptr_t monitorHandle{ 0 }; + Uint revision{ 0 }; + Uint subSystemId{ 0 }; + Uint vendorId{ 0 }; + sptr currentDisplayMode{ nullptr }; public: struct PlatformImplementation; diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index 685213d..0226b17 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -591,15 +591,10 @@ namespace xna { struct GraphicsAdapter::PlatformImplementation { comptr dxAdapter = nullptr; - comptr dxFactory = nullptr; - - private: - friend class GraphicsAdapter; - Uint _index{ 0 }; - sptr _currentDisplayMode = nullptr; + comptr dxFactory = nullptr; public: - bool GetOutput(UINT slot, IDXGIOutput*& output) const; + bool GetOutput(UINT slot, IDXGIOutput*& output) const; }; struct BlendRenderTarget { From 818e694da56e7b7d5bd1f4c4e2b506c82187e425 Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 19 Jul 2024 23:21:25 -0300 Subject: [PATCH 05/30] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20DisplayMo?= =?UTF-8?q?de?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/adapter.cpp | 28 ++++++----- framework/platform-dx/displaymode.cpp | 12 ++--- inc/xna/common/numerics.hpp | 17 +++++++ inc/xna/enumerations.hpp | 13 ----- inc/xna/graphics/displaymode.hpp | 72 ++++++++++++++++++++------- inc/xna/xna-dx.hpp | 34 ------------- 6 files changed, 91 insertions(+), 85 deletions(-) diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index fe0e00c..bc9e242 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -167,7 +167,7 @@ namespace xna { for (size_t i = 0; i < modes->DisplayModes.size(); ++i) { auto& m = modes->DisplayModes[i]; - if (m->Format == surfaceFormat && m->Width == width && m->Height == height) { + if (m->Format() == surfaceFormat && m->Width() == width && m->Height() == height) { currentDisplayMode = m; } else if (i + 1 == modes->DisplayModes.size()) { @@ -197,27 +197,29 @@ namespace xna { uptr createDisplayModeCollection(std::vector const& source) { auto collection = unew(); - DisplayMode currentDisplayMode; + std::vector> displayList; sptr pDisplay = nullptr; for (size_t i = 0; i < source.size(); ++i) { auto& modedesc = source[i]; - DisplayModeDescription description; - description._refreshRate = modedesc.RefreshRate; - description._scaling = static_cast(modedesc.Scaling); - description._scanlineOrdering = static_cast(modedesc.ScanlineOrdering); + DisplayModeRate rate; + rate.RefreshRate.Denominator = modedesc.RefreshRate.Denominator; + rate.RefreshRate.Numerator = modedesc.RefreshRate.Numerator; + rate.Scaling = static_cast(modedesc.Scaling); + rate.ScanlineOrdering = static_cast(modedesc.ScanlineOrdering); - if (pDisplay && pDisplay->Width == modedesc.Width && pDisplay->Height == modedesc.Height && pDisplay->Format == DxHelpers::SurfaceFormatToXna(modedesc.Format)) { - pDisplay->impl->Descriptions.push_back(description); + if (pDisplay && pDisplay->Width() == modedesc.Width && pDisplay->Height() == modedesc.Height && pDisplay->Format() == DxHelpers::SurfaceFormatToXna(modedesc.Format)) { + pDisplay->Rates.push_back(rate); } else { - pDisplay = snew(); - pDisplay->Width = modedesc.Width; - pDisplay->Height = modedesc.Height; - pDisplay->Format = DxHelpers::SurfaceFormatToXna(modedesc.Format); - pDisplay->impl->Descriptions.push_back(description); + pDisplay = snew( + modedesc.Width, + modedesc.Height, + DxHelpers::SurfaceFormatToXna(modedesc.Format)); + + pDisplay->Rates.push_back(rate); displayList.push_back(pDisplay); } } diff --git a/framework/platform-dx/displaymode.cpp b/framework/platform-dx/displaymode.cpp index fbebf57..80a56c4 100644 --- a/framework/platform-dx/displaymode.cpp +++ b/framework/platform-dx/displaymode.cpp @@ -2,16 +2,12 @@ #include "xna/graphics/displaymode.hpp" namespace xna { - DisplayMode::DisplayMode() { - impl = unew(); - } - size_t DisplayModeCollection::SurfaceCount(SurfaceFormat format) const { size_t counter = 0; for (size_t i = 0; i < DisplayModes.size(); ++i) { - if (DisplayModes[i]->Format == format) { + if (DisplayModes[i]->Format() == format) { ++counter; } } @@ -27,7 +23,7 @@ namespace xna { std::vector> modes(count); for (size_t i = 0; i < DisplayModes.size(); ++i) { - if (DisplayModes[i]->Format == format) { + if (DisplayModes[i]->Format() == format) { modes[index] = DisplayModes[i]; ++index; } @@ -46,10 +42,10 @@ namespace xna { for (size_t i = 0; i < DisplayModes.size(); ++i) { const auto& mode = DisplayModes[i]; - if (mode->Format == format && mode->Width == width && mode->Height == height) { + if (mode->Format() == format && mode->Width() == width && mode->Height() == height) { return DisplayModes[i]; } - else if(mode->Format == format && mode->Width == width) { + else if(mode->Format() == format && mode->Width() == width) { matched = mode; } } diff --git a/inc/xna/common/numerics.hpp b/inc/xna/common/numerics.hpp index d637230..bd1d63f 100644 --- a/inc/xna/common/numerics.hpp +++ b/inc/xna/common/numerics.hpp @@ -6,6 +6,23 @@ #include namespace xna { + //Represents a rational number. + struct RationalNumber { + constexpr RationalNumber() = default; + + constexpr RationalNumber(Uint numerator, Uint denominator) + : Numerator(numerator), Denominator(denominator) {} + + constexpr bool operator==(const RationalNumber& other) const { + return Numerator == other.Numerator && Denominator == other.Denominator; + } + + //An unsigned integer value representing the top of the rational number. + Uint Numerator{ 0 }; + //An unsigned integer value representing the bottom of the rational number. + Uint Denominator{ 0 }; + }; + struct Point { Int X{ 0 }; Int Y{ 0 }; diff --git a/inc/xna/enumerations.hpp b/inc/xna/enumerations.hpp index 51bf886..bb007e0 100644 --- a/inc/xna/enumerations.hpp +++ b/inc/xna/enumerations.hpp @@ -193,19 +193,6 @@ namespace xna { Portrait = 4, }; - enum class DisplayModeScanlineOrder { - Unspecified = 0, - Progressive = 1, - UpperFieldFirst = 2, - LowerFieldFirst = 3 - }; - - enum class DisplayModeScaling { - Unspecified = 0, - Centered = 1, - Stretched = 2 - }; - enum class EffectParameterClass { Matrix, Object, diff --git a/inc/xna/graphics/displaymode.hpp b/inc/xna/graphics/displaymode.hpp index 8861a7f..6385008 100644 --- a/inc/xna/graphics/displaymode.hpp +++ b/inc/xna/graphics/displaymode.hpp @@ -2,40 +2,78 @@ #define XNA_GRAPHICS_DISPLAYMODE_HPP #include "../default.hpp" +#include "../common/numerics.hpp" namespace xna { - struct DisplayModeDescription; + //Flags indicating the method the raster uses to create an image on a surface + enum class DisplayModeScanlineOrder { + Unspecified = 0, + Progressive = 1, + UpperFieldFirst = 2, + LowerFieldFirst = 3 + }; + + //Flags indicating how an image is stretched to fit a given monitor's resolution + enum class DisplayModeScaling { + Unspecified = 0, + Centered = 1, + Stretched = 2 + }; + + struct DisplayModeRate { + constexpr DisplayModeRate() = default; + + constexpr DisplayModeRate(DisplayModeScanlineOrder scanlineOrdering, DisplayModeScaling scaling, RationalNumber refreshRate) : + ScanlineOrdering(scanlineOrdering), Scaling(scaling), RefreshRate(refreshRate){} + + constexpr bool operator==(const DisplayModeRate& other) const { + return ScanlineOrdering == other.ScanlineOrdering && Scaling == other.Scaling && RefreshRate == other.RefreshRate; + } + + //Gets the method the raster uses to create an image on a surface + DisplayModeScanlineOrder ScanlineOrdering{ DisplayModeScanlineOrder::Unspecified }; + //Gets how an image is stretched to fit a given monitor's resolution + DisplayModeScaling Scaling{ DisplayModeScaling::Unspecified }; + //Describing the refresh rate in hertz. + RationalNumber RefreshRate{}; + }; //Describes the display mode. class DisplayMode { public: - DisplayMode(); + constexpr DisplayMode(); + + constexpr DisplayMode(Int width, Int height, SurfaceFormat format): + width(width), height(height), format(format){} //Gets the aspect ratio used by the graphics device. constexpr float AspectRatio() const { - if (Height == 0 || Width == 0) + if (height == 0 || width == 0) return 0; - return static_cast(Width) / static_cast(Height); + return static_cast(width) / static_cast(height); } + //Gets a value indicating the screen width, in pixels. + constexpr Int Width() const { return width; } + //Gets a value indicating the screen height, in pixels. + constexpr Int Height() const { return height; } + //Gets a value indicating the surface format of the display mode. + constexpr SurfaceFormat Format() const { return format; } + constexpr bool operator==(const DisplayMode& other) const { - return Width == other.Width - && Height == other.Height - && Format == other.Format; + return width == other.width + && height == other.height + && format == other.format; } + private: + Int width{ 0 }; + Int height{ 0 }; + SurfaceFormat format{ SurfaceFormat::Color }; + public: - //Gets a value indicating the screen width, in pixels. - Int Width{ 0 }; - //Gets a value indicating the screen height, in pixels. - Int Height{ 0 }; - //Gets a value indicating the surface format of the display mode. - SurfaceFormat Format{ SurfaceFormat::Color }; - - public: - struct PlatformImplementation; - uptr impl; + std::vector Rates; }; //Manipulates a collection of DisplayMode structures. diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index 0226b17..959d3fb 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -622,40 +622,6 @@ namespace xna { D3D11_DEPTH_STENCIL_DESC dxDescription{}; }; - struct DisplayModeRefreshRate { - constexpr DisplayModeRefreshRate() = default; - - constexpr DisplayModeRefreshRate(DXGI_RATIONAL const& dxrational) { - Numerator = dxrational.Numerator; - Denominator = dxrational.Denominator; - } - constexpr DisplayModeRefreshRate(Uint numerator, Uint denominator) - : Numerator(numerator), Denominator(denominator) {} - - Uint Numerator{ 0 }; - Uint Denominator{ 0 }; - - constexpr bool operator==(const DisplayModeRefreshRate& other) const - { - return Numerator == other.Numerator && Denominator == other.Denominator; - } - }; - - struct DisplayModeDescription { - DisplayModeScanlineOrder _scanlineOrdering{ DisplayModeScanlineOrder::Unspecified }; - DisplayModeScaling _scaling{ DisplayModeScaling::Unspecified }; - DisplayModeRefreshRate _refreshRate{}; - - constexpr bool operator==(const DisplayModeDescription& other) const - { - return _scanlineOrdering == other._scanlineOrdering && _scaling == other._scaling && _refreshRate == other._refreshRate; - } - }; - - struct DisplayMode::PlatformImplementation { - std::vector Descriptions; - }; - struct GamePad::PlatformImplementation { uptr _dxGamePad = unew(); From b86d7adf558f93a9bc69b937dbce0c0aecc712dd Mon Sep 17 00:00:00 2001 From: Danilo Date: Sat, 20 Jul 2024 19:08:40 -0300 Subject: [PATCH 06/30] Melhorias em GraphicsAdapter --- framework/platform-dx/adapter.cpp | 172 ++++++++++-------------------- inc/xna/graphics/adapter.hpp | 10 +- inc/xna/graphics/displaymode.hpp | 2 + 3 files changed, 66 insertions(+), 118 deletions(-) diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index bc9e242..efdc0c4 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -3,17 +3,10 @@ #include "xna/game/gdevicemanager.hpp" #include "xna/xna-dx.hpp" -namespace xna { - static String getDescription(comptr const& adapter); - static Uint getDeviceId(comptr const& adapter); - static String getDeviceName(comptr const& adapter); - static intptr_t getMonitorHandle(comptr const& adapter); - static Uint getRevision(comptr const& adapter); - static Uint getRevision(comptr const& adapter); - static Uint getSubSystemId(comptr const& adapter); - static Uint getVendorId(comptr const& adapter); +namespace xna { + static void setOutputVars(comptr const& adapter, String& deviceName, intptr_t& monitorHandle); static size_t getDisplayModesCount(IDXGIAdapter* adapter); - static uptr createDisplayModeCollection(std::vector const& source); + static uptr createDisplayModeCollection(std::vector const& source); static void setCurrentDisplayMode(GraphicsAdapter& adapter, SurfaceFormat surfaceFormat, Uint width, Uint height, sptr& currentDisplayMode); GraphicsAdapter::GraphicsAdapter() { @@ -22,26 +15,33 @@ namespace xna { uptr GraphicsAdapter::DefaultAdapter() { comptr pFactory = nullptr; - + if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)pFactory.GetAddressOf())) Exception::Throw(Exception::FAILED_TO_CREATE); comptr pAdapter = nullptr; - + if (pFactory->EnumAdapters1(0, pAdapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { - auto adp = unew(); - + auto adp = uptr(new GraphicsAdapter()); + adp->impl->dxAdapter = pAdapter; - adp->impl->dxFactory = pFactory; - - adp->description = getDescription(pAdapter); - adp->deviceId = getDeviceId(pAdapter); - adp->deviceName = getDeviceName(pAdapter); + adp->impl->dxFactory = pFactory; + + DXGI_ADAPTER_DESC1 desc{}; + pAdapter->GetDesc1(&desc); + + adp->description = XnaHelper::ToString(desc.Description); + adp->deviceId = static_cast(desc.DeviceId); adp->isDefault = true; - adp->monitorHandle = getMonitorHandle(pAdapter); - adp->revision = getRevision(pAdapter); - adp->subSystemId = getSubSystemId(pAdapter); - adp->vendorId = getVendorId(pAdapter); + adp->revision = static_cast(desc.Revision); + adp->subSystemId = static_cast(desc.SubSysId); + adp->vendorId = static_cast(desc.VendorId); + + setOutputVars(pAdapter, adp->deviceName, adp->monitorHandle); + + setCurrentDisplayMode(*adp, SurfaceFormat::Color, + GraphicsDeviceManager::DefaultBackBufferWidth, + GraphicsDeviceManager::DefaultBackBufferHeight, adp->currentDisplayMode); return adp; } @@ -55,26 +55,33 @@ namespace xna { if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)pFactory.GetAddressOf())) Exception::Throw(Exception::FAILED_TO_CREATE); - comptr pAdapter = nullptr; + comptr pAdapter = nullptr; for (UINT count = 0; pFactory->EnumAdapters1(count, pAdapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND; ++count) { - auto adp = unew(); + auto adp = uptr(new GraphicsAdapter()); adp->impl->dxAdapter = pAdapter; adp->impl->dxFactory = pFactory; - adp->description = getDescription(pAdapter); - adp->deviceId = getDeviceId(pAdapter); - adp->deviceName = getDeviceName(pAdapter); + DXGI_ADAPTER_DESC1 desc{}; + pAdapter->GetDesc1(&desc); + + adp->description = XnaHelper::ToString(desc.Description); + adp->deviceId = static_cast(desc.DeviceId); adp->isDefault = count == 0; - adp->monitorHandle = getMonitorHandle(pAdapter); - adp->revision = getRevision(pAdapter); - adp->subSystemId = getSubSystemId(pAdapter); - adp->vendorId = getVendorId(pAdapter); + adp->revision = static_cast(desc.Revision); + adp->subSystemId = static_cast(desc.SubSysId); + adp->vendorId = static_cast(desc.VendorId); + + setOutputVars(pAdapter, adp->deviceName, adp->monitorHandle); + + setCurrentDisplayMode(*adp, SurfaceFormat::Color, + GraphicsDeviceManager::DefaultBackBufferWidth, + GraphicsDeviceManager::DefaultBackBufferHeight, adp->currentDisplayMode); adapters.push_back(std::move(adp)); } - } + } uptr GraphicsAdapter::SupportedDisplayModes() const { if (!impl->dxAdapter) return nullptr; @@ -86,9 +93,9 @@ namespace xna { comptr pOutput = nullptr; UINT bufferOffset = 0; - - std::vector buffer(totalDisplay); - + + std::vector buffer(totalDisplay); + if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) { const auto currentSurface = static_cast(f); @@ -98,12 +105,12 @@ namespace xna { pOutput->GetDisplayModeList(format, 0, &numModes, nullptr); if (numModes == 0) - continue; + continue; pOutput->GetDisplayModeList(format, 0, &numModes, buffer.data() + bufferOffset); bufferOffset += numModes; - } + } } if (!pOutput) @@ -114,12 +121,12 @@ namespace xna { uptr GraphicsAdapter::SupportedDisplayModes(SurfaceFormat surfaceFormat) const { - if (!impl->dxAdapter) return nullptr; + if (!impl->dxAdapter) return nullptr; comptr pOutput = nullptr; - UINT bufferOffset = 0; + UINT bufferOffset = 0; - if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { + if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(surfaceFormat); UINT numModes = 0; @@ -130,26 +137,14 @@ namespace xna { return unew(); std::vector buffer(numModes); - pOutput->GetDisplayModeList(format, 0, &numModes, buffer.data()); + pOutput->GetDisplayModeList(format, 0, &numModes, buffer.data()); return createDisplayModeCollection(buffer); - } + } return unew(); } - sptr GraphicsAdapter::CurrentDisplayMode() { - if (!currentDisplayMode) { - setCurrentDisplayMode(*this, - SurfaceFormat::Color, - GraphicsDeviceManager::DefaultBackBufferWidth, - GraphicsDeviceManager::DefaultBackBufferHeight, - currentDisplayMode); - } - - return currentDisplayMode; - } - bool GraphicsAdapter::PlatformImplementation::GetOutput(UINT slot, IDXGIOutput*& output) const { if (!dxAdapter) return false; @@ -189,7 +184,7 @@ namespace xna { pOutput->GetDisplayModeList(format, 0, &num, nullptr); numModes += num; - } + } } return numModes; @@ -197,7 +192,7 @@ namespace xna { uptr createDisplayModeCollection(std::vector const& source) { auto collection = unew(); - + std::vector> displayList; sptr pDisplay = nullptr; @@ -216,9 +211,9 @@ namespace xna { else { pDisplay = snew( modedesc.Width, - modedesc.Height, + modedesc.Height, DxHelpers::SurfaceFormatToXna(modedesc.Format)); - + pDisplay->Rates.push_back(rate); displayList.push_back(pDisplay); } @@ -227,68 +222,17 @@ namespace xna { collection->DisplayModes = displayList; return collection; - } + } - String getDescription(comptr const& adapter) { - DXGI_ADAPTER_DESC1 desc; - adapter->GetDesc1(&desc); - const auto description = XnaHelper::ToString(desc.Description); - return description; - } - - Uint getDeviceId(comptr const& adapter) { - DXGI_ADAPTER_DESC1 desc; - adapter->GetDesc1(&desc); - - return static_cast(desc.DeviceId); - } - - String getDeviceName(comptr const& adapter) { - comptr pOutput = nullptr; - - if (adapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { - DXGI_OUTPUT_DESC outputDesc{}; - pOutput->GetDesc(&outputDesc); - - String deviceName = XnaHelper::ToString(outputDesc.DeviceName); - - return deviceName; - } - - return String(); - } - - intptr_t getMonitorHandle(comptr const& adapter) { + static void setOutputVars(comptr const& adapter, String& deviceName, intptr_t& monitorHandle) { comptr pOutput = nullptr; if (adapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { DXGI_OUTPUT_DESC outputDesc; pOutput->GetDesc(&outputDesc); - return reinterpret_cast(outputDesc.Monitor); + deviceName = XnaHelper::ToString(outputDesc.DeviceName); + monitorHandle = reinterpret_cast(outputDesc.Monitor); } - - return 0; - } - - Uint getRevision(comptr const& adapter) { - DXGI_ADAPTER_DESC1 desc; - adapter->GetDesc1(&desc); - - return static_cast(desc.Revision); - } - - Uint getSubSystemId(comptr const& adapter) { - DXGI_ADAPTER_DESC1 desc; - adapter->GetDesc1(&desc); - - return static_cast(desc.SubSysId); - } - - Uint getVendorId(comptr const& adapter) { - DXGI_ADAPTER_DESC1 desc; - adapter->GetDesc1(&desc); - - return static_cast(desc.VendorId); } } \ No newline at end of file diff --git a/inc/xna/graphics/adapter.hpp b/inc/xna/graphics/adapter.hpp index ae04431..27c5bcd 100644 --- a/inc/xna/graphics/adapter.hpp +++ b/inc/xna/graphics/adapter.hpp @@ -8,13 +8,11 @@ namespace xna { //Provides methods to retrieve and manipulate graphics adapters. class GraphicsAdapter { public: - GraphicsAdapter(); - //Collection of available adapters on the system. static void Adapters(std::vector>& adapters); //Gets the current display mode. - sptr CurrentDisplayMode(); + inline sptr CurrentDisplayMode() const { return currentDisplayMode; } //Gets the default adapter. static uptr DefaultAdapter(); @@ -32,7 +30,9 @@ namespace xna { constexpr bool IsDefaultAdapter() const { return isDefault; } //Determines if the graphics adapter is in widescreen mode. - constexpr bool IsWideScreen() const { return false; } + inline bool IsWideScreen() const { + return currentDisplayMode->AspectRatio() > 1.6000000238418579; + } //Retrieves the handle of the monitor constexpr intptr_t MonitorHandle() const { return monitorHandle; } @@ -93,6 +93,8 @@ namespace xna { Uint vendorId{ 0 }; sptr currentDisplayMode{ nullptr }; + GraphicsAdapter(); + public: struct PlatformImplementation; uptr impl = nullptr; diff --git a/inc/xna/graphics/displaymode.hpp b/inc/xna/graphics/displaymode.hpp index 6385008..9d39c7d 100644 --- a/inc/xna/graphics/displaymode.hpp +++ b/inc/xna/graphics/displaymode.hpp @@ -68,6 +68,8 @@ namespace xna { } private: + friend class GraphicsAdapter; + Int width{ 0 }; Int height{ 0 }; SurfaceFormat format{ SurfaceFormat::Color }; From 8dec213fc31608b7344b68551112e8069dca2435 Mon Sep 17 00:00:00 2001 From: Danilo Date: Sat, 20 Jul 2024 19:10:28 -0300 Subject: [PATCH 07/30] Remove GetOutput --- framework/platform-dx/adapter.cpp | 11 +---------- inc/xna/xna-dx.hpp | 5 +---- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index efdc0c4..01c71cb 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -143,16 +143,7 @@ namespace xna { } return unew(); - } - - bool GraphicsAdapter::PlatformImplementation::GetOutput(UINT slot, IDXGIOutput*& output) const { - if (!dxAdapter) return false; - - if (dxAdapter->EnumOutputs(slot, &output) != DXGI_ERROR_NOT_FOUND) - return true; - - return false; - } + } //INTERNAL FUNCTIONS diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index 959d3fb..7570f67 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -591,10 +591,7 @@ namespace xna { struct GraphicsAdapter::PlatformImplementation { comptr dxAdapter = nullptr; - comptr dxFactory = nullptr; - - public: - bool GetOutput(UINT slot, IDXGIOutput*& output) const; + comptr dxFactory = nullptr; }; struct BlendRenderTarget { From 8ffbabc2d1a6c343e944befadc9740d7bfd18222 Mon Sep 17 00:00:00 2001 From: Danilo Date: Sat, 20 Jul 2024 22:20:18 -0300 Subject: [PATCH 08/30] Melhorias em GraphicsAdapter --- framework/platform-dx/adapter.cpp | 90 ++++++++++++++----------------- inc/xna/graphics/adapter.hpp | 5 +- inc/xna/graphics/displaymode.hpp | 8 +-- inc/xna/xna-dx.hpp | 2 +- 4 files changed, 48 insertions(+), 57 deletions(-) diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index 01c71cb..d74932b 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -6,8 +6,9 @@ namespace xna { static void setOutputVars(comptr const& adapter, String& deviceName, intptr_t& monitorHandle); static size_t getDisplayModesCount(IDXGIAdapter* adapter); - static uptr createDisplayModeCollection(std::vector const& source); - static void setCurrentDisplayMode(GraphicsAdapter& adapter, SurfaceFormat surfaceFormat, Uint width, Uint height, sptr& currentDisplayMode); + static sptr createDisplayModeCollection(std::vector const& source); + static void setCurrentDisplayMode(DisplayModeCollection& displayModes, SurfaceFormat surfaceFormat, Uint width, Uint height, sptr& currentDisplayMode); + static sptr getSupportedDisplayModes(comptr& dxAdapter); GraphicsAdapter::GraphicsAdapter() { impl = unew(); @@ -39,7 +40,7 @@ namespace xna { setOutputVars(pAdapter, adp->deviceName, adp->monitorHandle); - setCurrentDisplayMode(*adp, SurfaceFormat::Color, + setCurrentDisplayMode(*adp->supportedDisplayModes, SurfaceFormat::Color, GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight, adp->currentDisplayMode); @@ -75,39 +76,48 @@ namespace xna { setOutputVars(pAdapter, adp->deviceName, adp->monitorHandle); - setCurrentDisplayMode(*adp, SurfaceFormat::Color, + setCurrentDisplayMode(*adp->supportedDisplayModes, SurfaceFormat::Color, GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight, adp->currentDisplayMode); + adp->supportedDisplayModes = getSupportedDisplayModes(pAdapter); + adapters.push_back(std::move(adp)); } } - uptr GraphicsAdapter::SupportedDisplayModes() const { - if (!impl->dxAdapter) return nullptr; + //INTERNAL FUNCTIONS - const auto totalDisplay = getDisplayModesCount(impl->dxAdapter.Get()); + sptr getSupportedDisplayModes(comptr& dxAdapter) { + const auto totalDisplay = getDisplayModesCount(dxAdapter.Get()); if (totalDisplay == 0) return nullptr; comptr pOutput = nullptr; + comptr pOutput1 = nullptr; UINT bufferOffset = 0; - std::vector buffer(totalDisplay); + std::vector buffer(totalDisplay); - if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { + if (dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) { const auto currentSurface = static_cast(f); DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(currentSurface); UINT numModes = 0; - pOutput->GetDisplayModeList(format, 0, &numModes, nullptr); + + if (!pOutput1) { + pOutput->QueryInterface(IID_IDXGIOutput1, (void**)pOutput1.GetAddressOf()); + } + + //See ref: https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-getdisplaymodelist?redirectedfrom=MSDN + pOutput1->GetDisplayModeList1(format, 0, &numModes, nullptr); if (numModes == 0) continue; - pOutput->GetDisplayModeList(format, 0, &numModes, buffer.data() + bufferOffset); + pOutput1->GetDisplayModeList1(format, 0, &numModes, buffer.data() + bufferOffset); bufferOffset += numModes; } @@ -117,62 +127,42 @@ namespace xna { return nullptr; return createDisplayModeCollection(buffer); - } + } - uptr GraphicsAdapter::SupportedDisplayModes(SurfaceFormat surfaceFormat) const - { - if (!impl->dxAdapter) return nullptr; + void setCurrentDisplayMode(DisplayModeCollection& displayModes, SurfaceFormat surfaceFormat, Uint width, Uint height, sptr& currentDisplayMode) { + auto modes = displayModes.Query(surfaceFormat); - comptr pOutput = nullptr; - UINT bufferOffset = 0; - - if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { - DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(surfaceFormat); - - UINT numModes = 0; - - pOutput->GetDisplayModeList(format, 0, &numModes, nullptr); - - if (numModes == 0) - return unew(); - - std::vector buffer(numModes); - pOutput->GetDisplayModeList(format, 0, &numModes, buffer.data()); - - return createDisplayModeCollection(buffer); - } - - return unew(); - } - - //INTERNAL FUNCTIONS - - void setCurrentDisplayMode(GraphicsAdapter& adapter, SurfaceFormat surfaceFormat, Uint width, Uint height, sptr& currentDisplayMode) { - const auto modes = adapter.SupportedDisplayModes(surfaceFormat); - - for (size_t i = 0; i < modes->DisplayModes.size(); ++i) { - auto& m = modes->DisplayModes[i]; + for (size_t i = 0; i < modes.size(); ++i) { + auto& m = modes[i]; if (m->Format() == surfaceFormat && m->Width() == width && m->Height() == height) { currentDisplayMode = m; } - else if (i + 1 == modes->DisplayModes.size()) { + else if (i + 1 == modes.size()) { currentDisplayMode = m; } } } size_t getDisplayModesCount(IDXGIAdapter* adapter) { - comptr pOutput = nullptr; - size_t numModes = 0; + comptr pOutput = nullptr; + comptr pOutput1 = nullptr; + size_t numModes = 0; + if (adapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) { for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) { const auto currentSurface = static_cast(f); DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(currentSurface); UINT num = 0; - pOutput->GetDisplayModeList(format, 0, &num, nullptr); + + if (!pOutput1) { + pOutput->QueryInterface(IID_IDXGIOutput1, (void**)pOutput1.GetAddressOf()); + } + + //See ref: https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgioutput-getdisplaymodelist?redirectedfrom=MSDN + pOutput1->GetDisplayModeList1(format, 0, &num, nullptr); numModes += num; } @@ -181,8 +171,8 @@ namespace xna { return numModes; } - uptr createDisplayModeCollection(std::vector const& source) { - auto collection = unew(); + sptr createDisplayModeCollection(std::vector const& source) { + auto collection = snew(); std::vector> displayList; sptr pDisplay = nullptr; diff --git a/inc/xna/graphics/adapter.hpp b/inc/xna/graphics/adapter.hpp index 27c5bcd..b44d678 100644 --- a/inc/xna/graphics/adapter.hpp +++ b/inc/xna/graphics/adapter.hpp @@ -44,9 +44,7 @@ namespace xna { constexpr Uint SubSystemId() const { return subSystemId; } //Returns a collection of supported display modes for the current adapter. - uptr SupportedDisplayModes() const; - //Returns a collection of supported display modes for the current adapter. - uptr SupportedDisplayModes(SurfaceFormat surfaceFormat) const; + inline sptr SupportedDisplayModes() const { return supportedDisplayModes; } //Retrieves a value used to identify the manufacturer. constexpr Uint VendorId() const { return vendorId; } @@ -92,6 +90,7 @@ namespace xna { Uint subSystemId{ 0 }; Uint vendorId{ 0 }; sptr currentDisplayMode{ nullptr }; + sptr supportedDisplayModes{ nullptr }; GraphicsAdapter(); diff --git a/inc/xna/graphics/displaymode.hpp b/inc/xna/graphics/displaymode.hpp index 9d39c7d..440ea70 100644 --- a/inc/xna/graphics/displaymode.hpp +++ b/inc/xna/graphics/displaymode.hpp @@ -23,8 +23,8 @@ namespace xna { struct DisplayModeRate { constexpr DisplayModeRate() = default; - constexpr DisplayModeRate(DisplayModeScanlineOrder scanlineOrdering, DisplayModeScaling scaling, RationalNumber refreshRate) : - ScanlineOrdering(scanlineOrdering), Scaling(scaling), RefreshRate(refreshRate){} + constexpr DisplayModeRate(DisplayModeScanlineOrder scanlineOrdering, DisplayModeScaling scaling, RationalNumber refreshRate, bool stereo) : + ScanlineOrdering(scanlineOrdering), Scaling(scaling), RefreshRate(refreshRate), Stereo(stereo){} constexpr bool operator==(const DisplayModeRate& other) const { return ScanlineOrdering == other.ScanlineOrdering && Scaling == other.Scaling && RefreshRate == other.RefreshRate; @@ -35,7 +35,9 @@ namespace xna { //Gets how an image is stretched to fit a given monitor's resolution DisplayModeScaling Scaling{ DisplayModeScaling::Unspecified }; //Describing the refresh rate in hertz. - RationalNumber RefreshRate{}; + RationalNumber RefreshRate{}; + //Specifies whether the full-screen display mode is stereo. TRUE if stereo; otherwise, FALSE. + bool Stereo{ false }; }; //Describes the display mode. diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index 7570f67..7a108dc 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -362,7 +362,7 @@ namespace xna { static constexpr TextureAddressMode TextureAddresModeToXna(D3D11_TEXTURE_ADDRESS_MODE value) { return static_cast(value - 1); - } + } }; struct PlatformInit { From f084bf54f93c7c10a7bf149503ab791449e69934 Mon Sep 17 00:00:00 2001 From: Danilo Date: Sat, 20 Jul 2024 23:44:10 -0300 Subject: [PATCH 09/30] Implementa QueryBackBufferFormat --- framework/platform-dx/adapter.cpp | 29 +++++++++++++++++++++++++++++ inc/xna/enumerations.hpp | 4 ++++ inc/xna/graphics/adapter.hpp | 28 ++++++++++++++++++++-------- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index d74932b..a736e4c 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -86,6 +86,35 @@ namespace xna { } } + bool GraphicsAdapter::QueryBackBufferFormat( + GraphicsProfile graphicsProfile, SurfaceFormat format, + DepthFormat depthFormat, Int multiSampleCount, + SurfaceFormat& selectedFormat, DepthFormat& selectedDepthFormat, + Int& selectedMultiSampleCount) const + { + comptr pOutput = nullptr; + + if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND){ + comptr pOutput1 = nullptr; + + pOutput->QueryInterface(IID_IDXGIOutput1, (void**)pOutput1.GetAddressOf()); + + DXGI_MODE_DESC1 modeToMath{}; + modeToMath.Format = DxHelpers::SurfaceFormatToDx(format); + + DXGI_MODE_DESC1 closestMath; + pOutput1->FindClosestMatchingMode1(&modeToMath, &closestMath, nullptr); + + selectedFormat = DxHelpers::SurfaceFormatToXna(closestMath.Format); + selectedDepthFormat = depthFormat; + selectedMultiSampleCount = multiSampleCount; + + return selectedFormat == format; + } + + return false; + } + //INTERNAL FUNCTIONS sptr getSupportedDisplayModes(comptr& dxAdapter) { diff --git a/inc/xna/enumerations.hpp b/inc/xna/enumerations.hpp index bb007e0..17f1471 100644 --- a/inc/xna/enumerations.hpp +++ b/inc/xna/enumerations.hpp @@ -255,8 +255,12 @@ namespace xna { None, }; + //Identifies the set of supported devices for the game based on device capabilities. enum class GraphicsProfile { + //Use a limited set of graphic features and capabilities, allowing the game to support the widest variety of devices, including all Windows-based computers. Reach, + //Use the largest available set of graphic features and capabilities to target devices, + //such as an Xbox 360 console and a Windows-based computer, that have more enhanced graphic capabilities. HiDef }; diff --git a/inc/xna/graphics/adapter.hpp b/inc/xna/graphics/adapter.hpp index b44d678..30c9220 100644 --- a/inc/xna/graphics/adapter.hpp +++ b/inc/xna/graphics/adapter.hpp @@ -48,11 +48,21 @@ namespace xna { //Retrieves a value used to identify the manufacturer. constexpr Uint VendorId() const { return vendorId; } + + //Gets or sets a NULL device. + static bool UseNullDevice() { return useNullDevice; } + //Gets or sets a NULL device. + static void UseNullDevice(bool value) { useNullDevice = value; } + + //Gets or sets a reference device. + constexpr static bool UseReferenceDevice() { return useReferenceDevice; } + //Gets or sets a reference device. + constexpr static void UseReferenceDevice(bool value) { useReferenceDevice = value; } //Tests to see if the adapter supports the requested profile. bool IsProfileSupported(GraphicsProfile graphicsProfile) { - return false; - } + return true; + } //Queries the adapter for support for the requested back buffer format. bool QueryBackBufferFormat( @@ -63,9 +73,7 @@ namespace xna { SurfaceFormat& selectedFormat, DepthFormat& selectedDepthFormat, Int& selectedMultiSampleCount - ) { - return false; - } + ) const; //Queries the adapter for support for the requested render target format. bool QueryRenderTargetFormat( @@ -75,9 +83,10 @@ namespace xna { Int multiSampleCount, SurfaceFormat& selectedFormat, DepthFormat& selectedDepthFormat, - int& selectedMultiSampleCount - ) { - return false; + Int& selectedMultiSampleCount + ) const { + return QueryBackBufferFormat(graphicsProfile, format, depthFormat, multiSampleCount, + selectedFormat, selectedDepthFormat, selectedMultiSampleCount); } private: @@ -92,6 +101,9 @@ namespace xna { sptr currentDisplayMode{ nullptr }; sptr supportedDisplayModes{ nullptr }; + inline static bool useNullDevice = false; + inline static bool useReferenceDevice = false; + GraphicsAdapter(); public: From fae2a4e28fc77f1d270ba0bd6ff6bc06c57d577e Mon Sep 17 00:00:00 2001 From: Danilo Date: Sun, 21 Jul 2024 20:37:16 -0300 Subject: [PATCH 10/30] Melhorias em GraphicsAdapter --- framework/platform-dx/adapter.cpp | 20 +++++++++++++------- inc/xna/graphics/adapter.hpp | 16 +--------------- inc/xna/xna-dx.hpp | 3 ++- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index a736e4c..0c962be 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -1,6 +1,3 @@ -#include "xna/graphics/adapter.hpp" -#include "xna/graphics/displaymode.hpp" -#include "xna/game/gdevicemanager.hpp" #include "xna/xna-dx.hpp" namespace xna { @@ -92,6 +89,10 @@ namespace xna { SurfaceFormat& selectedFormat, DepthFormat& selectedDepthFormat, Int& selectedMultiSampleCount) const { + selectedFormat = format; + selectedDepthFormat = depthFormat; + selectedMultiSampleCount = multiSampleCount; + comptr pOutput = nullptr; if (impl->dxAdapter->EnumOutputs(0, pOutput.GetAddressOf()) != DXGI_ERROR_NOT_FOUND){ @@ -100,14 +101,19 @@ namespace xna { pOutput->QueryInterface(IID_IDXGIOutput1, (void**)pOutput1.GetAddressOf()); DXGI_MODE_DESC1 modeToMath{}; - modeToMath.Format = DxHelpers::SurfaceFormatToDx(format); + modeToMath.Format = DxHelpers::SurfaceFormatToDx(format); + + //If pConcernedDevice is NULL, the Format member of DXGI_MODE_DESC1 cannot be DXGI_FORMAT_UNKNOWN. + if (modeToMath.Format == DXGI_FORMAT_UNKNOWN) + return false; DXGI_MODE_DESC1 closestMath; - pOutput1->FindClosestMatchingMode1(&modeToMath, &closestMath, nullptr); + const auto hresult = pOutput1->FindClosestMatchingMode1(&modeToMath, &closestMath, nullptr); + + if FAILED(hresult) + return false; selectedFormat = DxHelpers::SurfaceFormatToXna(closestMath.Format); - selectedDepthFormat = depthFormat; - selectedMultiSampleCount = multiSampleCount; return selectedFormat == format; } diff --git a/inc/xna/graphics/adapter.hpp b/inc/xna/graphics/adapter.hpp index 30c9220..55f978e 100644 --- a/inc/xna/graphics/adapter.hpp +++ b/inc/xna/graphics/adapter.hpp @@ -73,21 +73,7 @@ namespace xna { SurfaceFormat& selectedFormat, DepthFormat& selectedDepthFormat, Int& selectedMultiSampleCount - ) const; - - //Queries the adapter for support for the requested render target format. - bool QueryRenderTargetFormat( - GraphicsProfile graphicsProfile, - SurfaceFormat format, - DepthFormat depthFormat, - Int multiSampleCount, - SurfaceFormat& selectedFormat, - DepthFormat& selectedDepthFormat, - Int& selectedMultiSampleCount - ) const { - return QueryBackBufferFormat(graphicsProfile, format, depthFormat, multiSampleCount, - selectedFormat, selectedDepthFormat, selectedMultiSampleCount); - } + ) const; private: String description; diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index 7a108dc..59242bf 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -362,7 +362,8 @@ namespace xna { static constexpr TextureAddressMode TextureAddresModeToXna(D3D11_TEXTURE_ADDRESS_MODE value) { return static_cast(value - 1); - } + } + }; struct PlatformInit { From 2a26c5f5da9c3782277a73c5dd5009e6af41b7b8 Mon Sep 17 00:00:00 2001 From: Danilo Date: Sun, 21 Jul 2024 21:54:50 -0300 Subject: [PATCH 11/30] Melhorias em createDevice --- CMakeLists.txt | 2 +- framework/platform-dx/adapter.cpp | 2 + framework/platform-dx/device.cpp | 62 +++++++++++++++++++------------ inc/xna/xna-dx.hpp | 14 ++++++- samples/CMakeLists.txt | 2 +- 5 files changed, 56 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3757379..562ba32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,6 @@ project ("xna") include_directories(${PROJECT_INCLUDES_DIR}) add_subdirectory ("framework") -#add_subdirectory ("samples") +add_subdirectory ("samples") diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index 0c962be..102783e 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -37,6 +37,8 @@ namespace xna { setOutputVars(pAdapter, adp->deviceName, adp->monitorHandle); + adp->supportedDisplayModes = getSupportedDisplayModes(pAdapter); + setCurrentDisplayMode(*adp->supportedDisplayModes, SurfaceFormat::Color, GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight, adp->currentDisplayMode); diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index dfa1e50..2427add 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -21,42 +21,58 @@ namespace xna { } static void createDevice(GraphicsDevice::PlatformImplementation& impl) { + // + // See ref + // + // D3D_DRIVER_TYPE + // https://learn.microsoft.com/en-us/windows/win32/api/d3dcommon/ne-d3dcommon-d3d_driver_type + // + // D3D11CreateDevice function + // https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-d3d11createdevice + // + auto createDeviceFlags = 0; #if _DEBUG createDeviceFlags = D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG; #endif + const auto& currentAdapter = impl._adapter; + const auto& pAdapter = GraphicsAdapter::UseNullDevice() ? NULL : currentAdapter->impl->dxAdapter.Get(); + + // + // if pAdapter is not NULL driverType must be D3D_DRIVER_TYPE_UNKNOWN + // + auto driverType = D3D_DRIVER_TYPE_UNKNOWN; + + if (GraphicsAdapter::UseReferenceDevice()) + driverType = D3D_DRIVER_TYPE_WARP; + else if (GraphicsAdapter::UseNullDevice()) + driverType = D3D_DRIVER_TYPE_HARDWARE; auto hr = D3D11CreateDevice( - currentAdapter ? currentAdapter->impl->dxAdapter.Get() : NULL, - D3D_DRIVER_TYPE_UNKNOWN, + //_In_opt_ IDXGIAdapter* pAdapter, + pAdapter, + //D3D_DRIVER_TYPE DriverType, + driverType, + //HMODULE Software, NULL, + //UINT Flags, createDeviceFlags, - NULL, - 0, + //_In_reads_opt_( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels, + impl.featureLevels, + //UINT FeatureLevels, + 7, + //UINT SDKVersion, D3D11_SDK_VERSION, + //_COM_Outptr_opt_ ID3D11Device** ppDevice impl._device.GetAddressOf(), - &impl._featureLevel, + //_Out_opt_ D3D_FEATURE_LEVEL* pFeatureLevel, + &impl.currentFeatureLevel, + //_COM_Outptr_opt_ ID3D11DeviceContext** ppImmediateContext impl._context.GetAddressOf()); - if (FAILED(hr)) { - OutputDebugString("---> Usando Adaptador WARP: não há suporte ao D3D11\n"); - - hr = D3D11CreateDevice( - NULL, - D3D_DRIVER_TYPE_WARP, - NULL, - createDeviceFlags, - NULL, - 0, - D3D11_SDK_VERSION, - impl._device.GetAddressOf(), - &impl._featureLevel, - impl._context.GetAddressOf()); - - if FAILED(hr) - Exception::Throw(Exception::FAILED_TO_CREATE); - } + if FAILED(hr) + Exception::Throw(Exception::FAILED_TO_CREATE); } static void initAndApplyState(GraphicsDevice::PlatformImplementation& impl, PGraphicsDevice const& device) { diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index 59242bf..1f3433c 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -845,7 +845,19 @@ namespace xna { sptr _gameWindow = nullptr; xna::Viewport _viewport{}; sptr _presentationParameters; - D3D_FEATURE_LEVEL _featureLevel{ D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0 }; + + D3D_FEATURE_LEVEL featureLevels[7] = + { + D3D_FEATURE_LEVEL_11_1, + D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, + D3D_FEATURE_LEVEL_9_3, + D3D_FEATURE_LEVEL_9_2, + D3D_FEATURE_LEVEL_9_1, + }; + + D3D_FEATURE_LEVEL currentFeatureLevel; private: friend class GraphicsDevice; diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 45257e9..24ef843 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -4,4 +4,4 @@ # Add source to this project's executable. add_subdirectory ("01_blank") -add_subdirectory ("02_PlatfformerStarterKit") +#add_subdirectory ("02_PlatfformerStarterKit") From 989abcfa13028d9b0b75d2c7bf401f4b873e86e4 Mon Sep 17 00:00:00 2001 From: Danilo Date: Sat, 27 Jul 2024 14:20:04 -0300 Subject: [PATCH 12/30] Implementa EventHandler --- inc/xna/csharp/eventhandler.hpp | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 inc/xna/csharp/eventhandler.hpp diff --git a/inc/xna/csharp/eventhandler.hpp b/inc/xna/csharp/eventhandler.hpp new file mode 100644 index 0000000..eb696b2 --- /dev/null +++ b/inc/xna/csharp/eventhandler.hpp @@ -0,0 +1,44 @@ +#ifndef XNA_CSHARP_EVENTHANDLER_HPP +#define XNA_CSHARP_EVENTHANDLER_HPP + +#include +#include + +namespace xna { + struct EventArgs { + virtual ~EventArgs() { + sender = nullptr; + } + + void** sender = nullptr; + }; + + template + struct EventHandler { + + template + using HANDLER_CALLBACK = void(_Ptr::*&)(TEventArgs& args); + + template + void Add(HANDLER_CALLBACK<_Ptr> function, _Ptr* ptr) { + using std::placeholders::_1; + std::function func = std::bind(function, ptr, _1); + funcs.push_back(func); + } + + void operator()(TEventArgs& args) { + for (size_t i = 0; i < funcs.size(); ++i) + { + auto& func = funcs[i]; + + if (func) + func(args); + } + } + + private: + std::vector> funcs; + }; +} + +#endif \ No newline at end of file From 626bc01dad8b525fd8b7a0515bdd93413d85ded9 Mon Sep 17 00:00:00 2001 From: Danilo Date: Sun, 28 Jul 2024 17:03:13 -0300 Subject: [PATCH 13/30] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20iniciais=20em?= =?UTF-8?q?=20GraphicsDeviceManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/gdevicemanager.cpp | 72 ++++++----- inc/xna/game/gdevicemanager.hpp | 157 ++++++++++++++++++++--- inc/xna/game/window.hpp | 2 +- samples/01_blank/xna.cpp | 4 +- 4 files changed, 184 insertions(+), 51 deletions(-) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index 0c0f447..d3e5cb3 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -1,41 +1,44 @@ -#include "xna/game/gdevicemanager.hpp" -#include "xna/graphics/presentparams.hpp" -#include "xna/graphics/swapchain.hpp" #include "xna/xna-dx.hpp" namespace xna { - GraphicsDeviceManager::GraphicsDeviceManager(sptr const& game) : _game(game) + GraphicsDeviceManager::GraphicsDeviceManager(sptr const& game) : game(game) { sptr adp = GraphicsAdapter::DefaultAdapter(); _information.Adapter = adp; _information.Profile = xna::GraphicsProfile::HiDef; auto parameters = snew(); - parameters->BackBufferWidth = _backBufferWidth; - parameters->BackBufferHeight = _backBufferHeight; + parameters->BackBufferWidth = backBufferWidth; + parameters->BackBufferHeight = backBufferHeight; parameters->BackBufferFormat = SurfaceFormat::Color; parameters->Fullscreen = false; _information.Parameters = parameters; - if (_game) - _information.Window = _game->Window(); + if (game) + _information.Window = game->Window(); } bool GraphicsDeviceManager::Initialize() { - if (!_game) + if (!game) return false; - return CreateDevice(); + CreateDevice(); + + return true; } void GraphicsDeviceManager::ApplyChanges() { + if (device && !isDeviceDirty) + return; + + ChangeDevice(false); } bool GraphicsDeviceManager::ToggleFullScreen() { - if (!_game || !_game->graphicsDevice || !_game->graphicsDevice->impl->_swapChain) + if (!game || !game->graphicsDevice || !game->graphicsDevice->impl->_swapChain) return false; - auto& swap = _game->graphicsDevice->impl->_swapChain; + auto& swap = game->graphicsDevice->impl->_swapChain; BOOL state = false; auto hr = swap->impl->dxSwapChain->GetFullscreenState(&state, nullptr); @@ -46,20 +49,10 @@ namespace xna { if (FAILED(hr)) return false; - _isFullScreen = !state; + isFullScreen = !state; return true; - } - - void GraphicsDeviceManager::PreferredBackBufferWidth(Int value) { - _backBufferWidth = value; - _isDeviceDirty = true; - } - - void GraphicsDeviceManager::PreferredBackBufferHeight(Int value) { - _backBufferHeight = value; - _isDeviceDirty = true; - } + } bool initWindow(GraphicsDeviceInformation& info, Game& game, int backWidth, int backHeight) { @@ -98,21 +91,36 @@ namespace xna { } - bool GraphicsDeviceManager::CreateDevice() { - if (_isDeviceDirty) { - _information.Parameters->BackBufferWidth = _backBufferWidth; - _information.Parameters->BackBufferHeight = _backBufferHeight; + void GraphicsDeviceManager::CreateDevice() { + if (isDeviceDirty) { + _information.Parameters->BackBufferWidth = backBufferWidth; + _information.Parameters->BackBufferHeight = backBufferHeight; } - auto result = initWindow(_information, *_game, _backBufferWidth, _backBufferHeight); + auto result = initWindow(_information, *game, backBufferWidth, backBufferHeight); - if (!result) return false; + //if (!result) return false; - return initDevice(_information, *_game, _device); + initDevice(_information, *game, device); } void GraphicsDeviceManager::ChangeDevice() { } - + void GraphicsDeviceManager::AddDevice(bool anySuitableDevice, std::vector>& foundDevices) { + const auto handle = game->Window()->Handle(); + + std::vector> adapters; + GraphicsAdapter::Adapters(adapters); + + for (size_t i = 0; adapters.size(); ++i) { + auto& adapter = adapters[i]; + + if (!anySuitableDevice) { + //TODO + //if (!this.IsWindowOnAdapter(handle, adapter)) + //continue; + } + } + } } \ No newline at end of file diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index 4f00060..ab0779b 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -3,36 +3,161 @@ #include "../default.hpp" #include "gdeviceinfo.hpp" +#include "../csharp/eventhandler.hpp" namespace xna { - class GraphicsDeviceManager { + struct IGraphicsDeviceService { + virtual sptr GetGraphicsDevice() = 0; + + EventHandler DeviceDisposing; + EventHandler DeviceReset; + EventHandler DeviceResetting; + EventHandler DeviceCreated; + }; + + class IGraphicsDeviceManager { + virtual void CreateDevice() = 0; + virtual bool BeginDraw() = 0; + virtual void EndDraw() = 0; + }; + + class GraphicsDeviceManager : public IGraphicsDeviceService, public IGraphicsDeviceManager { public: + //Creates a new GraphicsDeviceManager and registers it to handle the configuration and management of the graphics device for the specified Game. GraphicsDeviceManager(sptr const& game); - ~GraphicsDeviceManager() {} + + public: + //Specifies the default minimum back-buffer width. + static constexpr int DefaultBackBufferWidth = 800; + //Specifies the default minimum back-buffer height. + static constexpr int DefaultBackBufferHeight = 480; + + public: + //Gets the GraphicsDevice associated with the GraphicsDeviceManager. + sptr GetGraphicsDevice() override { + return device; + } + + //Gets or sets the graphics profile, which determines the graphics feature set. + constexpr GraphicsProfile PreferredGraphicsProfile() const { + return graphicsProfile; + } + + //Gets or sets the graphics profile, which determines the graphics feature set. + constexpr void PreferredGraphicsProfile(xna::GraphicsProfile value) { + graphicsProfile = value; + isDeviceDirty = true; + } + + //Gets or sets a value that indicates whether the device should start in full-screen mode. + constexpr bool IsFullScreen() const { + return isFullScreen; + } + + //Gets or sets a value that indicates whether the device should start in full-screen mode. + constexpr void IsFullScreen(bool value) { + isFullScreen = value; + isDeviceDirty = true; + } + + //Gets or sets the format of the back buffer. + constexpr SurfaceFormat PreferredBackBufferFormat() const { + return backBufferFormat; + } + + //Gets or sets the format of the back buffer. + constexpr void PreferredBackBufferFormat(SurfaceFormat value) { + backBufferFormat = value; + isDeviceDirty = true; + } + + //Gets or sets the preferred back-buffer height. + constexpr Int PreferredBackBufferHeight() const { + return backBufferHeight; + } + + //Gets or sets the preferred back-buffer height. + constexpr void PreferredBackBufferHeight(Int value) { + backBufferHeight = value; + isDeviceDirty = true; + } + + //Gets or sets the preferred back-buffer width. + constexpr Int PreferredBackBufferWidth() const { + return backBufferWidth; + } + + //Gets or sets the preferred back-buffer width. + constexpr void PreferredBackBufferWidth(Int value) { + backBufferWidth = value; + isDeviceDirty = true; + } + + //Gets or sets the format of the depth stencil. + constexpr DepthFormat PreferredDepthStencilFormat() const { + return depthStencilFormat; + } + + //Gets or sets the format of the depth stencil. + constexpr void PreferredDepthStencilFormat(DepthFormat value) { + depthStencilFormat = value; + isDeviceDirty = true; + } + + //Gets or sets the display orientations that are available if automatic rotation and scaling is enabled. + constexpr DisplayOrientation SupportedOrientations() const { + return supportedOrientations; + } + + //Gets or sets the display orientations that are available if automatic rotation and scaling is enabled. + constexpr void SupportedOrientations(DisplayOrientation value) { + supportedOrientations = value; + isDeviceDirty = true; + } + + //Gets or sets a value that indicates whether to sync to the vertical trace (vsync) when presenting the back buffer. + constexpr bool SynchronizeWithVerticalRetrace() const { + return synchronizeWithVerticalRetrace; + } + + // Gets or sets a value that indicates whether to sync to the vertical trace(vsync) when presenting the back buffer. + constexpr void SynchronizeWithVerticalRetrace(bool value) { + synchronizeWithVerticalRetrace = value; + isDeviceDirty = true; + } + + public: + //Applies any changes to device-related properties, changing the graphics device as necessary. void ApplyChanges(); bool Initialize(); bool ToggleFullScreen(); - Int PreferredBackBufferWidth() const; - Int PreferredBackBufferHeight() const; - void PreferredBackBufferWidth(Int value); - void PreferredBackBufferHeight(Int value); - public: - static constexpr int DefaultBackBufferWidth = 800; - static constexpr int DefaultBackBufferHeight = 480; + private: + void ChangeDevice(bool forceCreate){} + void AddDevice(bool anySuitableDevice, std::vector>& foundDevices); protected: - bool CreateDevice(); + void CreateDevice(); void ChangeDevice(); private: - sptr _game = nullptr; - Int _backBufferWidth{ DefaultBackBufferWidth }; - Int _backBufferHeight{ DefaultBackBufferHeight }; - bool _isDeviceDirty{ false }; - sptr _device = nullptr; - bool _isFullScreen{ false }; + bool BeginDraw() override { return false; } + void EndDraw() override{ } + + private: + sptr game = nullptr; + bool isDeviceDirty{ false }; + sptr device = nullptr; GraphicsDeviceInformation _information{}; + + bool isFullScreen{ false }; + Int backBufferWidth{ DefaultBackBufferWidth }; + Int backBufferHeight{ DefaultBackBufferHeight }; + GraphicsProfile graphicsProfile; + DepthFormat depthStencilFormat{ DepthFormat::Depth24 }; + SurfaceFormat backBufferFormat; + DisplayOrientation supportedOrientations; + bool synchronizeWithVerticalRetrace{ true }; }; } diff --git a/inc/xna/game/window.hpp b/inc/xna/game/window.hpp index 7dc99eb..55f0aad 100644 --- a/inc/xna/game/window.hpp +++ b/inc/xna/game/window.hpp @@ -12,7 +12,7 @@ namespace xna { String Title() const; void Title(String const& title); Rectangle ClientBounds() const; - intptr_t Handle() const; + intptr_t Handle() const; public: struct PlatformImplementation; diff --git a/samples/01_blank/xna.cpp b/samples/01_blank/xna.cpp index dc97e81..c57eaa3 100644 --- a/samples/01_blank/xna.cpp +++ b/samples/01_blank/xna.cpp @@ -16,7 +16,7 @@ namespace xna { void Initialize() override { auto game = reinterpret_cast(this); - graphics = snew(game->shared_from_this()); + graphics = snew(game->shared_from_this()); graphics->Initialize(); std::any device = graphicsDevice; @@ -45,7 +45,7 @@ namespace xna { private: sptr graphics = nullptr; sptr spriteBatch = nullptr; - sptr texture = nullptr; + sptr texture = nullptr; }; } From e8af6af1c57f14253caaf6ea17de2e906812d1e0 Mon Sep 17 00:00:00 2001 From: Danilo Date: Mon, 29 Jul 2024 22:03:25 -0300 Subject: [PATCH 14/30] Implementa Screen::AllScreens --- framework/CMakeLists.txt | 2 +- framework/platform-dx/screen.cpp | 57 ++++++++++++++++++++++++++++++++ inc/xna/csharp/screen.hpp | 30 +++++++++++++++++ inc/xna/game/window.hpp | 5 ++- inc/xna/xna-dx.hpp | 4 +-- inc/xna/xna.hpp | 2 ++ 6 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 framework/platform-dx/screen.cpp create mode 100644 inc/xna/csharp/screen.hpp diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 9d2d114..a663821 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -40,7 +40,7 @@ add_library (Xn65 STATIC "platform-dx/audioengine.cpp" "graphics/gresource.cpp" "platform-dx/effect.cpp" - "exception.cpp") + "exception.cpp" "platform-dx/screen.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET Xn65 PROPERTY CXX_STANDARD 20) diff --git a/framework/platform-dx/screen.cpp b/framework/platform-dx/screen.cpp new file mode 100644 index 0000000..0b8ba1e --- /dev/null +++ b/framework/platform-dx/screen.cpp @@ -0,0 +1,57 @@ +#include "xna/xna-dx.hpp" + +namespace xna { + + //See ref + //https://learn.microsoft.com/pt-br/windows/win32/api/winuser/nf-winuser-getmonitorinfoa + //https://learn.microsoft.com/pt-br/windows/win32/api/winuser/ns-winuser-monitorinfoexa + //https://stackoverflow.com/questions/7767036/how-do-i-get-the-number-of-displays-in-windows + + static BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) + { + auto screens = (std::vector>*)dwData; + + MONITORINFOEX monitorInfo; + monitorInfo.cbSize = sizeof(MONITORINFOEX); + GetMonitorInfo(hMonitor, &monitorInfo); + + const auto hmonitor = reinterpret_cast(hMonitor); + + const auto primary = screens->size() == 0; + + Rectangle bounds; + bounds.X = monitorInfo.rcMonitor.left; + bounds.Y = monitorInfo.rcMonitor.top; + bounds.Width = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left; + bounds.Height = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; + + Rectangle workingArea; + workingArea.X = monitorInfo.rcWork.left; + workingArea.Y = monitorInfo.rcWork.top; + workingArea.Width = monitorInfo.rcWork.right - monitorInfo.rcWork.left; + workingArea.Height = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top; + + const auto deviceName = String(monitorInfo.szDevice); + + auto screen = unew( + hmonitor, + primary, + bounds, + workingArea, + deviceName + ); + + screens->push_back(std::move(screen)); + + return TRUE; + } + + + std::vector> Screen::AllScreens() { + std::vector> screens; + if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&screens)) + return screens; + + return std::vector>(); + } +} \ No newline at end of file diff --git a/inc/xna/csharp/screen.hpp b/inc/xna/csharp/screen.hpp new file mode 100644 index 0000000..72ca493 --- /dev/null +++ b/inc/xna/csharp/screen.hpp @@ -0,0 +1,30 @@ +#ifndef XNA_CSHARP_SCREEN_HPP +#define XNA_CSHARP_SCREEN_HPP + +#include "../default.hpp" +#include "../common/numerics.hpp" + +namespace xna { + class Screen { + public: + Screen(intptr_t hmonitor, bool primary, Rectangle bounds, Rectangle workingArea, String const& deviceName) : + hmonitor(hmonitor), primary(primary), bounds(bounds), workingArea(workingArea), deviceName(deviceName){} + + static std::vector> AllScreens(); + + constexpr Rectangle Bounds() const { return bounds; } + constexpr Rectangle WorkingArea() const { return workingArea; } + constexpr intptr_t HMonitor() const { return hmonitor; } + constexpr bool Primary() const { return primary; } + constexpr String DeviceName() const { return deviceName; } + + private: + intptr_t hmonitor{ 0 }; + bool primary{ false }; + Rectangle bounds{}; + Rectangle workingArea{}; + String deviceName; + }; +} + +#endif \ No newline at end of file diff --git a/inc/xna/game/window.hpp b/inc/xna/game/window.hpp index 55f0aad..3a3d356 100644 --- a/inc/xna/game/window.hpp +++ b/inc/xna/game/window.hpp @@ -3,6 +3,7 @@ #include "../default.hpp" #include "../common/numerics.hpp" +#include "../csharp/screen.hpp" namespace xna { class GameWindow { @@ -12,7 +13,9 @@ namespace xna { String Title() const; void Title(String const& title); Rectangle ClientBounds() const; - intptr_t Handle() const; + intptr_t Handle() const; + + static void ScreenFromAdapter(GraphicsAdapter const& adapter); public: struct PlatformImplementation; diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index 1f3433c..15d12bd 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -767,7 +767,7 @@ namespace xna { constexpr void Color(BYTE r, BYTE g, BYTE b) { _windowColor = RGB(r, g, b); - } + } bool Create(); bool Update(); @@ -857,7 +857,7 @@ namespace xna { D3D_FEATURE_LEVEL_9_1, }; - D3D_FEATURE_LEVEL currentFeatureLevel; + D3D_FEATURE_LEVEL currentFeatureLevel{ D3D_FEATURE_LEVEL_11_1 }; private: friend class GraphicsDevice; diff --git a/inc/xna/xna.hpp b/inc/xna/xna.hpp index 427eb79..fd43a6f 100644 --- a/inc/xna/xna.hpp +++ b/inc/xna/xna.hpp @@ -23,6 +23,8 @@ #include "csharp/stream.hpp" #include "csharp/timespan.hpp" #include "csharp/type.hpp" +#include "csharp/screen.hpp" +#include "csharp/eventhandler.hpp" #include "exception.hpp" #include "game/component.hpp" #include "game/game.hpp" From 73e8468906bb702af42ae996e5059691adb5b6f7 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 09:09:00 -0300 Subject: [PATCH 15/30] =?UTF-8?q?Corre=C3=A7=C3=B5es=20em=20MonitorEnumPro?= =?UTF-8?q?c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/screen.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/framework/platform-dx/screen.cpp b/framework/platform-dx/screen.cpp index 0b8ba1e..1af1482 100644 --- a/framework/platform-dx/screen.cpp +++ b/framework/platform-dx/screen.cpp @@ -3,21 +3,23 @@ namespace xna { //See ref + // //https://learn.microsoft.com/pt-br/windows/win32/api/winuser/nf-winuser-getmonitorinfoa //https://learn.microsoft.com/pt-br/windows/win32/api/winuser/ns-winuser-monitorinfoexa //https://stackoverflow.com/questions/7767036/how-do-i-get-the-number-of-displays-in-windows + // static BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { auto screens = (std::vector>*)dwData; - MONITORINFOEX monitorInfo; + MONITORINFOEX monitorInfo{}; monitorInfo.cbSize = sizeof(MONITORINFOEX); GetMonitorInfo(hMonitor, &monitorInfo); const auto hmonitor = reinterpret_cast(hMonitor); - const auto primary = screens->size() == 0; + const auto primary = monitorInfo.dwFlags == MONITORINFOF_PRIMARY; Rectangle bounds; bounds.X = monitorInfo.rcMonitor.left; From e69425d6c70a293e12cb1ada3e14fbe8c9b20f2e Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 09:26:27 -0300 Subject: [PATCH 16/30] GameWindow::ScreenFromAdapter --- framework/platform-dx/window.cpp | 15 +++++++++++++++ inc/xna/csharp/screen.hpp | 19 ++++++++++++++++--- inc/xna/game/window.hpp | 2 +- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/framework/platform-dx/window.cpp b/framework/platform-dx/window.cpp index 99ea44f..cc0467a 100644 --- a/framework/platform-dx/window.cpp +++ b/framework/platform-dx/window.cpp @@ -189,4 +189,19 @@ namespace xna { } return DefWindowProc(hWnd, msg, wParam, lParam); } + + uptr GameWindow::ScreenFromAdapter(GraphicsAdapter const& adapter) { + auto screens = Screen::AllScreens(); + + for (size_t i = 0; i < screens.size(); ++i) { + auto& screen = screens[i]; + + if (screen->DeviceName() == adapter.DeviceName()) + return std::move(screen); + } + + Exception::Throw("Invalid screen adapter."); + + return nullptr; + } } \ No newline at end of file diff --git a/inc/xna/csharp/screen.hpp b/inc/xna/csharp/screen.hpp index 72ca493..01059a2 100644 --- a/inc/xna/csharp/screen.hpp +++ b/inc/xna/csharp/screen.hpp @@ -5,17 +5,30 @@ #include "../common/numerics.hpp" namespace xna { + //A simplified port of System.Windows.Forms.Screen. + //Represents a display device or multiple display devices on a single system. class Screen { public: - Screen(intptr_t hmonitor, bool primary, Rectangle bounds, Rectangle workingArea, String const& deviceName) : - hmonitor(hmonitor), primary(primary), bounds(bounds), workingArea(workingArea), deviceName(deviceName){} + Screen(intptr_t hmonitor, bool primary, Rectangle bounds, Rectangle workingArea, String const& deviceName) : + hmonitor(hmonitor), primary(primary), bounds(bounds), workingArea(workingArea), deviceName(deviceName) {} + //Gets an array of all displays on the system. static std::vector> AllScreens(); + //Gets the bounds of the display. constexpr Rectangle Bounds() const { return bounds; } + + //Gets the working area of the display.The working area is the desktop area of + //the display, excluding taskbars, docked windows, and docked tool bars. constexpr Rectangle WorkingArea() const { return workingArea; } + + //Gets the monitor handler. constexpr intptr_t HMonitor() const { return hmonitor; } + + // Gets a value indicating whether a particular display is the primary device. constexpr bool Primary() const { return primary; } + + // Gets the device name associated with a display. constexpr String DeviceName() const { return deviceName; } private: @@ -24,7 +37,7 @@ namespace xna { Rectangle bounds{}; Rectangle workingArea{}; String deviceName; - }; + }; } #endif \ No newline at end of file diff --git a/inc/xna/game/window.hpp b/inc/xna/game/window.hpp index 3a3d356..422567c 100644 --- a/inc/xna/game/window.hpp +++ b/inc/xna/game/window.hpp @@ -15,7 +15,7 @@ namespace xna { Rectangle ClientBounds() const; intptr_t Handle() const; - static void ScreenFromAdapter(GraphicsAdapter const& adapter); + static uptr ScreenFromAdapter(GraphicsAdapter const& adapter); public: struct PlatformImplementation; From 7dfd3b3fbd508155b213c3fbcf2d06b79b8c4d29 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 09:41:31 -0300 Subject: [PATCH 17/30] Implementa IsWindowOnAdapter --- framework/platform-dx/gdevicemanager.cpp | 13 +++++--- framework/platform-dx/window.cpp | 38 ++++++++++++++++++++++++ inc/xna/csharp/screen.hpp | 4 +++ inc/xna/game/window.hpp | 3 +- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index d3e5cb3..dbc7204 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -1,6 +1,8 @@ #include "xna/xna-dx.hpp" namespace xna { + static bool IsWindowOnAdapter(intptr_t windowHandle, GraphicsAdapter const& adapter); + GraphicsDeviceManager::GraphicsDeviceManager(sptr const& game) : game(game) { sptr adp = GraphicsAdapter::DefaultAdapter(); @@ -116,11 +118,14 @@ namespace xna { for (size_t i = 0; adapters.size(); ++i) { auto& adapter = adapters[i]; - if (!anySuitableDevice) { - //TODO - //if (!this.IsWindowOnAdapter(handle, adapter)) - //continue; + if (!anySuitableDevice) { + if (!IsWindowOnAdapter(handle, *adapter)) + continue; } } } + + bool IsWindowOnAdapter(intptr_t windowHandle, GraphicsAdapter const& adapter) { + return GameWindow::ScreenFromAdapter(adapter) == GameWindow::ScreenFromHandle(windowHandle); + } } \ No newline at end of file diff --git a/framework/platform-dx/window.cpp b/framework/platform-dx/window.cpp index cc0467a..98f9506 100644 --- a/framework/platform-dx/window.cpp +++ b/framework/platform-dx/window.cpp @@ -204,4 +204,42 @@ namespace xna { return nullptr; } + + uptr GameWindow::ScreenFromHandle(intptr_t windowHandle) { + auto hMonitor = reinterpret_cast(windowHandle); + + if (!hMonitor) + return nullptr; + + MONITORINFOEX monitorInfo{}; + monitorInfo.cbSize = sizeof(MONITORINFOEX); + GetMonitorInfo(hMonitor, &monitorInfo); + + const auto hmonitor = reinterpret_cast(hMonitor); + const auto primary = monitorInfo.dwFlags == MONITORINFOF_PRIMARY; + + Rectangle bounds; + bounds.X = monitorInfo.rcMonitor.left; + bounds.Y = monitorInfo.rcMonitor.top; + bounds.Width = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left; + bounds.Height = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; + + Rectangle workingArea; + workingArea.X = monitorInfo.rcWork.left; + workingArea.Y = monitorInfo.rcWork.top; + workingArea.Width = monitorInfo.rcWork.right - monitorInfo.rcWork.left; + workingArea.Height = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top; + + const auto deviceName = String(monitorInfo.szDevice); + + auto screen = unew( + hmonitor, + primary, + bounds, + workingArea, + deviceName + ); + + return screen; + } } \ No newline at end of file diff --git a/inc/xna/csharp/screen.hpp b/inc/xna/csharp/screen.hpp index 01059a2..54b788b 100644 --- a/inc/xna/csharp/screen.hpp +++ b/inc/xna/csharp/screen.hpp @@ -31,6 +31,10 @@ namespace xna { // Gets the device name associated with a display. constexpr String DeviceName() const { return deviceName; } + constexpr bool operator==(Screen const& other) const { + return hmonitor == other.hmonitor; + } + private: intptr_t hmonitor{ 0 }; bool primary{ false }; diff --git a/inc/xna/game/window.hpp b/inc/xna/game/window.hpp index 422567c..1ae2066 100644 --- a/inc/xna/game/window.hpp +++ b/inc/xna/game/window.hpp @@ -15,7 +15,8 @@ namespace xna { Rectangle ClientBounds() const; intptr_t Handle() const; - static uptr ScreenFromAdapter(GraphicsAdapter const& adapter); + static uptr ScreenFromAdapter(GraphicsAdapter const& adapter); + static uptr ScreenFromHandle(intptr_t windowHandle); public: struct PlatformImplementation; From 38e6779e12630d6c179ca3b3990e2c42023875d1 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 10:43:39 -0300 Subject: [PATCH 18/30] Implementa segunda sobrecarga GraphicsDeviceManager::AddDevices --- framework/platform-dx/device.cpp | 2 +- framework/platform-dx/gdevicemanager.cpp | 63 +++++++++++++++++++++--- framework/platform-dx/swapchain.cpp | 2 +- inc/xna/game/gdeviceinfo.hpp | 4 +- inc/xna/game/gdevicemanager.hpp | 24 +++++++-- inc/xna/graphics/presentparams.hpp | 5 +- 6 files changed, 85 insertions(+), 15 deletions(-) diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 2427add..879c03b 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -101,7 +101,7 @@ namespace xna { impl->_adapter = info.Adapter; impl->_gameWindow = info.Window; - impl->_presentationParameters = info.Parameters; + impl->_presentationParameters = info.PresentParameters; } bool GraphicsDevice::Initialize() { diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index dbc7204..949447b 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -13,8 +13,8 @@ namespace xna { parameters->BackBufferWidth = backBufferWidth; parameters->BackBufferHeight = backBufferHeight; parameters->BackBufferFormat = SurfaceFormat::Color; - parameters->Fullscreen = false; - _information.Parameters = parameters; + parameters->IsFullscreen = false; + _information.PresentParameters = parameters; if (game) _information.Window = game->Window(); @@ -72,7 +72,7 @@ namespace xna { return false; } - info.Parameters->DeviceWindowHandle = reinterpret_cast(window->impl->WindowHandle()); + info.PresentParameters->DeviceWindowHandle = reinterpret_cast(window->impl->WindowHandle()); return true; } @@ -95,8 +95,8 @@ namespace xna { void GraphicsDeviceManager::CreateDevice() { if (isDeviceDirty) { - _information.Parameters->BackBufferWidth = backBufferWidth; - _information.Parameters->BackBufferHeight = backBufferHeight; + _information.PresentParameters->BackBufferWidth = backBufferWidth; + _information.PresentParameters->BackBufferHeight = backBufferHeight; } auto result = initWindow(_information, *game, backBufferWidth, backBufferHeight); @@ -109,7 +109,7 @@ namespace xna { void GraphicsDeviceManager::ChangeDevice() { } - void GraphicsDeviceManager::AddDevice(bool anySuitableDevice, std::vector>& foundDevices) { + void GraphicsDeviceManager::AddDevices(bool anySuitableDevice, std::vector>& foundDevices) { const auto handle = game->Window()->Handle(); std::vector> adapters; @@ -122,9 +122,60 @@ namespace xna { if (!IsWindowOnAdapter(handle, *adapter)) continue; } + + if (adapter->IsProfileSupported(graphicsProfile)) { + auto baseDeviceInfo = snew(); + baseDeviceInfo->Adapter = std::move(adapter); + baseDeviceInfo->Profile = graphicsProfile; + baseDeviceInfo->PresentParameters = snew(); + baseDeviceInfo->PresentParameters->DeviceWindowHandle = handle; + baseDeviceInfo->PresentParameters->MultiSampleCount = 0; + baseDeviceInfo->PresentParameters->IsFullscreen = isFullScreen; + baseDeviceInfo->PresentParameters->PresentationInterval = synchronizeWithVerticalRetrace ? PresentInterval::One : PresentInterval::Immediate; + + const auto& currentDisplayMode = baseDeviceInfo->Adapter->CurrentDisplayMode(); + AddDevices(*baseDeviceInfo->Adapter, *currentDisplayMode, baseDeviceInfo, foundDevices); + + if (isFullScreen) { + //TODO + } + } } } + void GraphicsDeviceManager::AddDevices(GraphicsAdapter const& adapter, DisplayMode const& mode, sptr& baseDeviceInfo, std::vector>& foundDevices) { + auto deviceInformation = snew(*baseDeviceInfo); + + if (isFullScreen) + { + deviceInformation->PresentParameters->BackBufferWidth = mode.Width(); + deviceInformation->PresentParameters->BackBufferHeight = mode.Height(); + } + else if (useResizedBackBuffer) { + deviceInformation->PresentParameters->BackBufferWidth = resizedBackBufferWidth; + deviceInformation->PresentParameters->BackBufferHeight = resizedBackBufferHeight; + } + else { + deviceInformation->PresentParameters->BackBufferWidth = backBufferWidth; + deviceInformation->PresentParameters->BackBufferHeight = backBufferHeight; + } + + SurfaceFormat selectedFormat; + DepthFormat selectedDepthFormat; + int selectedMultiSampleCount; + + adapter.QueryBackBufferFormat(deviceInformation->Profile, mode.Format(), depthStencilFormat, allowMultiSampling ? 16 : 0, selectedFormat, selectedDepthFormat, selectedMultiSampleCount); + + deviceInformation->PresentParameters->BackBufferFormat = selectedFormat; + deviceInformation->PresentParameters->DepthStencilFormat = selectedDepthFormat; + deviceInformation->PresentParameters->MultiSampleCount = selectedMultiSampleCount; + + if (std::find(foundDevices.begin(), foundDevices.end(), deviceInformation) != foundDevices.end()) + return; + + foundDevices.push_back(deviceInformation); + } + bool IsWindowOnAdapter(intptr_t windowHandle, GraphicsAdapter const& adapter) { return GameWindow::ScreenFromAdapter(adapter) == GameWindow::ScreenFromHandle(windowHandle); } diff --git a/framework/platform-dx/swapchain.cpp b/framework/platform-dx/swapchain.cpp index c440bb4..6ac7818 100644 --- a/framework/platform-dx/swapchain.cpp +++ b/framework/platform-dx/swapchain.cpp @@ -64,7 +64,7 @@ namespace xna { impl->dxFullScreenDescription.RefreshRate.Denominator = 1; impl->dxFullScreenDescription.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; impl->dxFullScreenDescription.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; - impl->dxFullScreenDescription.Windowed = !parameters->Fullscreen; + impl->dxFullScreenDescription.Windowed = !parameters->IsFullscreen; HWND hwnd = reinterpret_cast(parameters->DeviceWindowHandle); return internalInit(*m_device, hwnd, impl->dxSwapChain, impl->dxDescription, impl->dxFullScreenDescription); diff --git a/inc/xna/game/gdeviceinfo.hpp b/inc/xna/game/gdeviceinfo.hpp index f815e5f..00e6b2e 100644 --- a/inc/xna/game/gdeviceinfo.hpp +++ b/inc/xna/game/gdeviceinfo.hpp @@ -8,8 +8,8 @@ namespace xna { public: sptr Adapter = nullptr; xna::GraphicsProfile Profile{ xna::GraphicsProfile::Reach }; - sptr Parameters = nullptr; - sptr Window = nullptr; + sptr PresentParameters = nullptr; + sptr Window = nullptr; }; } diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index ab0779b..3d123ec 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -104,6 +104,15 @@ namespace xna { isDeviceDirty = true; } + constexpr bool PreferMultiSampling() const { + return allowMultiSampling; + } + + constexpr void PreferMultiSampling(bool value) { + allowMultiSampling = value; + isDeviceDirty = true; + } + //Gets or sets the display orientations that are available if automatic rotation and scaling is enabled. constexpr DisplayOrientation SupportedOrientations() const { return supportedOrientations; @@ -134,7 +143,8 @@ namespace xna { private: void ChangeDevice(bool forceCreate){} - void AddDevice(bool anySuitableDevice, std::vector>& foundDevices); + void AddDevices(bool anySuitableDevice, std::vector>& foundDevices); + void AddDevices(GraphicsAdapter const& adapter, DisplayMode const& mode, sptr& baseDeviceInfo, std::vector>& foundDevices); protected: void CreateDevice(); @@ -153,11 +163,17 @@ namespace xna { bool isFullScreen{ false }; Int backBufferWidth{ DefaultBackBufferWidth }; Int backBufferHeight{ DefaultBackBufferHeight }; - GraphicsProfile graphicsProfile; + GraphicsProfile graphicsProfile{GraphicsProfile::HiDef}; DepthFormat depthStencilFormat{ DepthFormat::Depth24 }; - SurfaceFormat backBufferFormat; - DisplayOrientation supportedOrientations; + SurfaceFormat backBufferFormat{SurfaceFormat::Color}; + DisplayOrientation supportedOrientations{DisplayOrientation::Default}; bool synchronizeWithVerticalRetrace{ true }; + bool useResizedBackBuffer{ false }; + Int resizedBackBufferWidth{ 0 }; + Int resizedBackBufferHeight{ 0 }; + bool allowMultiSampling{ false }; + + std::vector> foundDevices; }; } diff --git a/inc/xna/graphics/presentparams.hpp b/inc/xna/graphics/presentparams.hpp index 774fd97..3782c28 100644 --- a/inc/xna/graphics/presentparams.hpp +++ b/inc/xna/graphics/presentparams.hpp @@ -12,7 +12,10 @@ namespace xna { SurfaceFormat BackBufferFormat{ SurfaceFormat::Color }; SwapEffect PresentationSwapEffect{ SwapEffect::FlipDiscard }; intptr_t DeviceWindowHandle{ 0 }; - bool Fullscreen{ false }; + bool IsFullscreen{ false }; + Int MultiSampleCount{ 0 }; + PresentInterval PresentationInterval{ PresentInterval::Default }; + DepthFormat DepthStencilFormat{ DepthFormat::None }; }; } From 78a7264a0d787767576675e85c4f5fe2276c3688 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 11:02:18 -0300 Subject: [PATCH 19/30] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20GraphicsD?= =?UTF-8?q?eviceManager::AddDevices?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/gdevicemanager.cpp | 13 +++++++++++-- inc/xna/game/gdevicemanager.hpp | 2 +- inc/xna/graphics/displaymode.hpp | 4 ++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index 949447b..2df76c9 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -137,13 +137,22 @@ namespace xna { AddDevices(*baseDeviceInfo->Adapter, *currentDisplayMode, baseDeviceInfo, foundDevices); if (isFullScreen) { - //TODO + const auto& supportedDisplayModes = adapter->SupportedDisplayModes(); + const auto count = supportedDisplayModes->Count(); + + for (size_t i = 0; i < count; ++i) { + auto& supportedDisplayMode = supportedDisplayModes->DisplayModes[i]; + + if (supportedDisplayMode->Width() >= 640 && supportedDisplayMode->Height() >= 480) { + AddDevices(*baseDeviceInfo->Adapter, *supportedDisplayMode, baseDeviceInfo, foundDevices); + } + } } } } } - void GraphicsDeviceManager::AddDevices(GraphicsAdapter const& adapter, DisplayMode const& mode, sptr& baseDeviceInfo, std::vector>& foundDevices) { + void GraphicsDeviceManager::AddDevices(GraphicsAdapter const& adapter, DisplayMode const& mode, sptr& baseDeviceInfo, std::vector>& foundDevices) const { auto deviceInformation = snew(*baseDeviceInfo); if (isFullScreen) diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index 3d123ec..a59c221 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -144,7 +144,7 @@ namespace xna { private: void ChangeDevice(bool forceCreate){} void AddDevices(bool anySuitableDevice, std::vector>& foundDevices); - void AddDevices(GraphicsAdapter const& adapter, DisplayMode const& mode, sptr& baseDeviceInfo, std::vector>& foundDevices); + void AddDevices(GraphicsAdapter const& adapter, DisplayMode const& mode, sptr& baseDeviceInfo, std::vector>& foundDevices) const; protected: void CreateDevice(); diff --git a/inc/xna/graphics/displaymode.hpp b/inc/xna/graphics/displaymode.hpp index 440ea70..a5422d9 100644 --- a/inc/xna/graphics/displaymode.hpp +++ b/inc/xna/graphics/displaymode.hpp @@ -95,6 +95,10 @@ namespace xna { std::vector> Query(SurfaceFormat format) const; sptr Query(SurfaceFormat format, Uint width, Uint height) const; + constexpr size_t Count() const { + return DisplayModes.size(); + } + public: std::vector> DisplayModes; }; From a2e76c1eb8b7e82c5a90938fe54c9672bb2dfedd Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 14:21:35 -0300 Subject: [PATCH 20/30] Implementa RankDevicesPlatform --- framework/CMakeLists.txt | 2 +- framework/platform-dx/gdevicemanager.cpp | 111 +++++++++++++++++++++++ inc/xna/default.hpp | 2 +- inc/xna/game/gdeviceinfo.hpp | 9 +- inc/xna/game/gdevicemanager.hpp | 8 ++ inc/xna/graphics/presentparams.hpp | 4 +- 6 files changed, 129 insertions(+), 7 deletions(-) diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index a663821..0c82e8a 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -40,7 +40,7 @@ add_library (Xn65 STATIC "platform-dx/audioengine.cpp" "graphics/gresource.cpp" "platform-dx/effect.cpp" - "exception.cpp" "platform-dx/screen.cpp") + "exception.cpp" "platform-dx/screen.cpp" ) if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET Xn65 PROPERTY CXX_STANDARD 20) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index 2df76c9..d7764d5 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -185,6 +185,117 @@ namespace xna { foundDevices.push_back(deviceInformation); } + sptr GraphicsDeviceManager::FindBestPlatformDevice(bool anySuitableDevice) { + auto foundDevices = std::vector>(); + + AddDevices(anySuitableDevice, foundDevices); + + if (foundDevices.size() == 0 && allowMultiSampling) { + PreferMultiSampling(false); + AddDevices(anySuitableDevice, foundDevices); + } + + if (foundDevices.size() == 0) { + Exception::Throw("No Suitable Graphics Device"); + } + + RankDevices(foundDevices); + + if (foundDevices.size() == 0) + Exception::Throw("No Suitable Graphics Device"); + + return foundDevices[0]; + } + + struct GraphicsDeviceInformationComparer + { + GraphicsDeviceManager* graphics = nullptr; + + bool operator()(GraphicsDeviceInformation const& d1, GraphicsDeviceInformation const& d2) const { + return comparator(d1, d2); + } + + bool operator()(sptr const& a, sptr const& b) const { + return comparator(*a, *b); + } + + private: + bool comparator(GraphicsDeviceInformation const& d1, GraphicsDeviceInformation const& d2) const { + if (d1.Profile != d2.Profile) + return d1.Profile <= d2.Profile; + + auto& presentationParameters1 = d1.PresentParameters; + auto& presentationParameters2 = d2.PresentParameters; + + if (presentationParameters1 && presentationParameters2 && presentationParameters1->IsFullscreen != presentationParameters2->IsFullscreen) + return graphics->IsFullScreen() != presentationParameters1->IsFullscreen; + + const auto& backFormat1 = presentationParameters1->BackBufferFormat; + const auto& backFormat2 = presentationParameters2->BackBufferFormat; + + if (backFormat1 != backFormat2) + return static_cast(backFormat1) <= static_cast(backFormat2); + + if (presentationParameters1->MultiSampleCount != presentationParameters2->MultiSampleCount) + return presentationParameters1->MultiSampleCount <= presentationParameters2->MultiSampleCount; + + const auto num3 = graphics->PreferredBackBufferWidth() == 0 || graphics->PreferredBackBufferHeight() == 0 + ? GraphicsDeviceManager::DefaultBackBufferWidth / static_cast(GraphicsDeviceManager::DefaultBackBufferHeight) + : graphics->PreferredBackBufferWidth() / static_cast(graphics->PreferredBackBufferHeight()); + + const auto num4 = presentationParameters1->BackBufferWidth / static_cast(presentationParameters1->BackBufferHeight); + const auto num5 = presentationParameters2->BackBufferWidth / static_cast(presentationParameters2->BackBufferHeight); + + const auto num6 = std::abs(num4 - num3); + const auto num7 = std::abs(num5 - num3); + + if (std::abs(num6 - num7) > 0.20000000298023224) + return num6 <= num7; + + Int num8; + Int num9; + + if (graphics->IsFullScreen()) + { + if (graphics->PreferredBackBufferWidth() == 0 || graphics->PreferredBackBufferHeight() == 0) { + const auto& adapter1 = d1.Adapter; + num8 = adapter1->CurrentDisplayMode()->Width() * adapter1->CurrentDisplayMode()->Height(); + const auto& adapter2 = d2.Adapter; + num9 = adapter2->CurrentDisplayMode()->Width() * adapter2->CurrentDisplayMode()->Height(); + } + else + num8 = num9 = graphics->PreferredBackBufferWidth() * graphics->PreferredBackBufferHeight(); + } + else + num8 = graphics->PreferredBackBufferWidth() == 0 || graphics->PreferredBackBufferHeight() == 0 + ? (num9 = GraphicsDeviceManager::DefaultBackBufferWidth * GraphicsDeviceManager::DefaultBackBufferHeight) + : (num9 = graphics->PreferredBackBufferWidth() * graphics->PreferredBackBufferHeight()); + + const auto num10 = std::abs(presentationParameters1->BackBufferWidth * presentationParameters1->BackBufferHeight - num8); + const auto num11 = std::abs(presentationParameters2->BackBufferWidth * presentationParameters2->BackBufferHeight - num9); + + if (num10 != num11) + return num10 <= num11; + + if (d1.Adapter != d2.Adapter) { + if (d1.Adapter->IsDefaultAdapter()) + return false; + + if (d2.Adapter->IsDefaultAdapter()) + return true; + } + + return false; + } + }; + + void GraphicsDeviceManager::RankDevicesPlatform(std::vector>& foundDevices) { + GraphicsDeviceInformationComparer comparer; + comparer.graphics = this; + + std::sort(foundDevices.begin(), foundDevices.end(), comparer); + } + bool IsWindowOnAdapter(intptr_t windowHandle, GraphicsAdapter const& adapter) { return GameWindow::ScreenFromAdapter(adapter) == GameWindow::ScreenFromHandle(windowHandle); } diff --git a/inc/xna/default.hpp b/inc/xna/default.hpp index 86f50a1..f09f6a9 100644 --- a/inc/xna/default.hpp +++ b/inc/xna/default.hpp @@ -151,7 +151,7 @@ namespace xna { class GameClock; class GameTime; class GameWindow; - class GraphicsDeviceInformation; + struct GraphicsDeviceInformation; class GraphicsDeviceManager; class IGameTime; class IGameComponent; diff --git a/inc/xna/game/gdeviceinfo.hpp b/inc/xna/game/gdeviceinfo.hpp index 00e6b2e..a3a5d07 100644 --- a/inc/xna/game/gdeviceinfo.hpp +++ b/inc/xna/game/gdeviceinfo.hpp @@ -4,13 +4,16 @@ #include "../default.hpp" namespace xna { - class GraphicsDeviceInformation { - public: + struct GraphicsDeviceInformation { + GraphicsDeviceInformation() { + PresentParameters = snew(); + } + sptr Adapter = nullptr; xna::GraphicsProfile Profile{ xna::GraphicsProfile::Reach }; sptr PresentParameters = nullptr; sptr Window = nullptr; - }; + }; } #endif \ No newline at end of file diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index a59c221..ce57456 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -150,10 +150,18 @@ namespace xna { void CreateDevice(); void ChangeDevice(); + inline virtual void RankDevices(std::vector>& foundDevices) { + RankDevicesPlatform(foundDevices); + } + private: bool BeginDraw() override { return false; } void EndDraw() override{ } + sptr FindBestPlatformDevice(bool anySuitableDevice); + + void RankDevicesPlatform(std::vector>& foundDevices); + private: sptr game = nullptr; bool isDeviceDirty{ false }; diff --git a/inc/xna/graphics/presentparams.hpp b/inc/xna/graphics/presentparams.hpp index 3782c28..2306f12 100644 --- a/inc/xna/graphics/presentparams.hpp +++ b/inc/xna/graphics/presentparams.hpp @@ -7,8 +7,8 @@ namespace xna { struct PresentationParameters { constexpr PresentationParameters() = default; - Uint BackBufferWidth{ 0 }; - Uint BackBufferHeight{ 0 }; + Int BackBufferWidth{ 0 }; + Int BackBufferHeight{ 0 }; SurfaceFormat BackBufferFormat{ SurfaceFormat::Color }; SwapEffect PresentationSwapEffect{ SwapEffect::FlipDiscard }; intptr_t DeviceWindowHandle{ 0 }; From c686c38100a9d50d9487aa302352adfa8980f8b8 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 14:26:52 -0300 Subject: [PATCH 21/30] Implementa FindBestDevice --- inc/xna/game/gdevicemanager.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index ce57456..8406a64 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -154,6 +154,10 @@ namespace xna { RankDevicesPlatform(foundDevices); } + inline virtual sptr FindBestDevice(bool anySuitableDevice) { + return FindBestPlatformDevice(anySuitableDevice); + } + private: bool BeginDraw() override { return false; } void EndDraw() override{ } From 5316882cf592a13d07c972a3db0a374257a04b58 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 15:29:58 -0300 Subject: [PATCH 22/30] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20GameWindo?= =?UTF-8?q?w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/gdevicemanager.cpp | 13 +++++ framework/platform-dx/window.cpp | 73 +++++++++++++----------- inc/xna/default.hpp | 1 - inc/xna/game/gdevicemanager.hpp | 13 +++-- inc/xna/game/window.hpp | 43 ++++++++++++-- inc/xna/xna-dx.hpp | 3 + 6 files changed, 101 insertions(+), 45 deletions(-) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index d7764d5..2fb1e9e 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -109,6 +109,19 @@ namespace xna { void GraphicsDeviceManager::ChangeDevice() { } + void GraphicsDeviceManager::ChangeDevice(bool forceCreate) { + if (!game) + Exception::Throw(Exception::INVALID_OPERATION); + + inDeviceTransition = true; + auto screenDeviceName = game->Window()->ScreenDeviceName(); + int clientWidth = game->Window()->ClientBounds().Width; + int clientHeight = game->Window()->ClientBounds().Height; + bool flag1 = false; + + //TODO + } + void GraphicsDeviceManager::AddDevices(bool anySuitableDevice, std::vector>& foundDevices) { const auto handle = game->Window()->Handle(); diff --git a/framework/platform-dx/window.cpp b/framework/platform-dx/window.cpp index 98f9506..f9ce345 100644 --- a/framework/platform-dx/window.cpp +++ b/framework/platform-dx/window.cpp @@ -2,20 +2,17 @@ namespace xna { GameWindow::GameWindow() { - impl = unew(); + impl = unew(this); impl->_hInstance = GetModuleHandle(NULL); impl->_windowIcon = LoadIcon(NULL, IDI_APPLICATION); impl->_windowCursor = LoadCursor(NULL, IDC_ARROW); impl->_windowStyle = static_cast(GameWindowMode::Windowed); impl->_windowCenterX = impl->_windowWidth / 2.0F; - impl->_windowCenterY = impl->_windowHeight / 2.0F; - - } - - GameWindow::~GameWindow() { - impl = nullptr; - } + impl->_windowCenterY = impl->_windowHeight / 2.0F; + impl->_windowWidth = GameWindow::DefaultClientWidth; + impl->_windowHeight = GameWindow::DefaultClientHeight; + } void GameWindow::PlatformImplementation::Position(int width, int height, bool update) { _windowPosX = width; @@ -34,9 +31,8 @@ namespace xna { if(update) Update(); } - void GameWindow::Title(String const& title) { - if (!impl) return; - + void GameWindow::Title(String const& value) { + title = value; impl->_windowTitle = title; } @@ -92,6 +88,29 @@ namespace xna { return _windowHandle ? true : false; } + + // + // GameWindow + // + + gameWindow->handle = reinterpret_cast(_windowHandle); + gameWindow->title = _windowTitle; + gameWindow->clientBounds = { _windowPosX, _windowPosY, _windowWidth, _windowHeight }; + gameWindow->currentOrientation = DisplayOrientation::Default; + + auto screens = Screen::AllScreens(); + + if (screens.size() == 1) + gameWindow->screenDeviceName = screens[0]->DeviceName(); + else { + for (size_t i = 0; i < screens.size(); ++i) { + const auto& screen = screens[i]; + + if (screen->Primary()) + gameWindow->screenDeviceName = screen->DeviceName(); + } + } + return true; } @@ -118,30 +137,13 @@ namespace xna { return _windowHandle ? true : false; } + gameWindow->clientBounds = { _windowPosX, _windowPosY, _windowWidth, _windowHeight }; + return true; - } + } - String GameWindow::Title() const { - if (!impl) return String(); - - return impl->_windowTitle; - } - - Rectangle GameWindow::ClientBounds() const { - if (!impl) return {}; - - return Rectangle( - impl->_windowPosX, - impl->_windowPosY, - impl->_windowWidth, - impl->_windowHeight - ); - } - - intptr_t GameWindow::Handle() const { - if (!impl) return 0; - - return reinterpret_cast(impl->_windowHandle); + bool GameWindow::IsWindowMinimized() const { + return IsIconic(impl->_windowHandle); } LRESULT GameWindow::PlatformImplementation::WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) @@ -242,4 +244,9 @@ namespace xna { return screen; } + + String GameWindow::ScreenDeviceName() const { + //TODO + return std::string(); + } } \ No newline at end of file diff --git a/inc/xna/default.hpp b/inc/xna/default.hpp index f09f6a9..ad0327a 100644 --- a/inc/xna/default.hpp +++ b/inc/xna/default.hpp @@ -168,7 +168,6 @@ namespace xna { class Effect; class GraphicsAdapter; class GraphicsDevice; - class GraphicsDeviceInformation; struct PresentationParameters; class RenderTarget2D; class SwapChain; diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index 8406a64..3f68f8d 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -139,12 +139,8 @@ namespace xna { //Applies any changes to device-related properties, changing the graphics device as necessary. void ApplyChanges(); bool Initialize(); - bool ToggleFullScreen(); - - private: - void ChangeDevice(bool forceCreate){} - void AddDevices(bool anySuitableDevice, std::vector>& foundDevices); - void AddDevices(GraphicsAdapter const& adapter, DisplayMode const& mode, sptr& baseDeviceInfo, std::vector>& foundDevices) const; + bool ToggleFullScreen(); + protected: void CreateDevice(); @@ -159,6 +155,10 @@ namespace xna { } private: + void ChangeDevice(bool forceCreate); + void AddDevices(bool anySuitableDevice, std::vector>& foundDevices); + void AddDevices(GraphicsAdapter const& adapter, DisplayMode const& mode, sptr& baseDeviceInfo, std::vector>& foundDevices) const; + bool BeginDraw() override { return false; } void EndDraw() override{ } @@ -184,6 +184,7 @@ namespace xna { Int resizedBackBufferWidth{ 0 }; Int resizedBackBufferHeight{ 0 }; bool allowMultiSampling{ false }; + bool inDeviceTransition{ false }; std::vector> foundDevices; }; diff --git a/inc/xna/game/window.hpp b/inc/xna/game/window.hpp index 1ae2066..cbd6b13 100644 --- a/inc/xna/game/window.hpp +++ b/inc/xna/game/window.hpp @@ -9,14 +9,47 @@ namespace xna { class GameWindow { public: GameWindow(); - ~GameWindow(); - String Title() const; - void Title(String const& title); - Rectangle ClientBounds() const; - intptr_t Handle() const; + + //Gets the current display orientation, which reflects the physical orientation of the phone in the user's hand. + constexpr DisplayOrientation CurrentOrientation() const { + return currentOrientation; + } + + //Gets and sets the title of the system window. + constexpr String Title() const { + return title; + } + + //Gets and sets the title of the system window. + void Title(String const& value); + + //Gets the handle to the system window. + constexpr intptr_t Handle() const { + return handle; + } + + //The screen dimensions of the game window's client rectangle. + constexpr Rectangle ClientBounds() const { + return clientBounds; + } + + //Gets the device name of the screen the window is currently in. + String ScreenDeviceName() const; static uptr ScreenFromAdapter(GraphicsAdapter const& adapter); static uptr ScreenFromHandle(intptr_t windowHandle); + bool IsWindowMinimized() const; + + inline static constexpr Int DefaultClientWidth = 800; + inline static constexpr Int DefaultClientHeight = 600; + + private: + String title; + intptr_t handle{ 0 }; + Rectangle clientBounds{}; + String screenDeviceName; + DisplayOrientation currentOrientation{ DisplayOrientation::Default }; + public: struct PlatformImplementation; diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index 15d12bd..42486fa 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -698,6 +698,8 @@ namespace xna { struct GameWindow::PlatformImplementation { public: + PlatformImplementation(GameWindow* gameWindow): gameWindow(gameWindow){} + constexpr void Mode(GameWindowMode mode) { _windowStyle = static_cast(mode); } @@ -776,6 +778,7 @@ namespace xna { private: friend class GameWindow; + GameWindow*& gameWindow; HINSTANCE _hInstance{ nullptr }; HWND _windowHandle{ nullptr }; From 8006d1fad9e4cefcbb966474c3eb4d2d78eb9029 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 16:31:36 -0300 Subject: [PATCH 23/30] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20=20Graphi?= =?UTF-8?q?csDeviceManager::ChangeDevice(bool=20forceCreate)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/gdevicemanager.cpp | 40 +++++++++++++++++++++++- inc/xna/game/gdevicemanager.hpp | 5 +++ inc/xna/graphics/device.hpp | 9 ++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index 2fb1e9e..b5338f0 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -119,7 +119,41 @@ namespace xna { int clientHeight = game->Window()->ClientBounds().Height; bool flag1 = false; - //TODO + //this.game.Window.SetSupportedOrientations(Helpers.ChooseOrientation(this.supportedOrientations, this.PreferredBackBufferWidth, this.PreferredBackBufferHeight, true)); + auto bestDevice = FindBestDevice(forceCreate); + //this.game.Window.BeginScreenDeviceChange(bestDevice.PresentationParameters.IsFullScreen); + flag1 = true; + bool flag2 = true; + + if (!forceCreate && device) { + //this.OnPreparingDeviceSettings((object) this, new PreparingDeviceSettingsEventArgs(bestDevice)); + + if (CanResetDevice(*bestDevice)) { + auto deviceInformation = snew(*bestDevice); + //MassagePresentParameters(bestDevice.PresentationParameters); + //ValidateGraphicsDeviceInformation(bestDevice); + //device.Reset(deviceInformation.PresentationParameters, deviceInformation.Adapter); + //GraphicsDeviceManager.ConfigureTouchInput(deviceInformation.PresentationParameters); + flag2 = false; + } + + if (flag2) + CreateDevice(*bestDevice); + + auto presentationParameters = device->PresentParameters(); + + screenDeviceName = device->Adapter()->DeviceName(); + + isReallyFullScreen = presentationParameters.IsFullscreen; + + if (presentationParameters.BackBufferWidth != 0) + clientWidth = presentationParameters.BackBufferWidth; + + if (presentationParameters.BackBufferHeight != 0) + clientHeight = presentationParameters.BackBufferHeight; + + isDeviceDirty = false; + } } void GraphicsDeviceManager::AddDevices(bool anySuitableDevice, std::vector>& foundDevices) { @@ -309,6 +343,10 @@ namespace xna { std::sort(foundDevices.begin(), foundDevices.end(), comparer); } + bool GraphicsDeviceManager::CanResetDevice(GraphicsDeviceInformation& newDeviceInfo) { + return device->Profile() == newDeviceInfo.Profile; + } + bool IsWindowOnAdapter(intptr_t windowHandle, GraphicsAdapter const& adapter) { return GameWindow::ScreenFromAdapter(adapter) == GameWindow::ScreenFromHandle(windowHandle); } diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index 3f68f8d..fa24992 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -154,6 +154,8 @@ namespace xna { return FindBestPlatformDevice(anySuitableDevice); } + virtual bool CanResetDevice(GraphicsDeviceInformation& newDeviceInfo); + private: void ChangeDevice(bool forceCreate); void AddDevices(bool anySuitableDevice, std::vector>& foundDevices); @@ -166,6 +168,8 @@ namespace xna { void RankDevicesPlatform(std::vector>& foundDevices); + void CreateDevice(GraphicsDeviceInformation& newInfo){} + private: sptr game = nullptr; bool isDeviceDirty{ false }; @@ -185,6 +189,7 @@ namespace xna { Int resizedBackBufferHeight{ 0 }; bool allowMultiSampling{ false }; bool inDeviceTransition{ false }; + bool isReallyFullScreen{ false }; std::vector> foundDevices; }; diff --git a/inc/xna/graphics/device.hpp b/inc/xna/graphics/device.hpp index 1d78173..052f387 100644 --- a/inc/xna/graphics/device.hpp +++ b/inc/xna/graphics/device.hpp @@ -2,6 +2,7 @@ #define XNA_GRAPHICS_DEVICE_HPP #include "../default.hpp" +#include "presentparams.hpp" namespace xna { //Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. @@ -32,7 +33,15 @@ namespace xna { Int MultiSampleMask() const; //Gets or sets a bitmask controlling modification of the samples in a multisample render target. The default value is -1 (0xffffffff). void MultiSampleMask(Int value); + + constexpr GraphicsProfile Profile() const { + return GraphicsProfile::HiDef; + } + constexpr PresentationParameters PresentParameters() const { + return PresentationParameters(); + } + void Clear(Color const& color); void Clear(ClearOptions options, Color const& color, float depth, Int stencil); void Clear(ClearOptions options, Vector4 const& color, float depth, Int stencil); From aa25e035972f01971b63f936badbc144d42b9ea6 Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 21:36:22 -0300 Subject: [PATCH 24/30] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20GraphicsD?= =?UTF-8?q?eviceManager::ChangeDevice(bool=20forceCreate)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/device.cpp | 8 +++ framework/platform-dx/gdevicemanager.cpp | 71 +++++++++++++++++++++++- inc/xna/game/gdevicemanager.hpp | 4 ++ inc/xna/graphics/device.hpp | 2 + 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 879c03b..69c8b0d 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -274,4 +274,12 @@ namespace xna { void GraphicsDevice::MultiSampleMask(Int value) { impl->_multiSampleMask = value; } + + void GraphicsDevice::Reset(sptr const& presentationParameters, sptr const& graphicsAdapter){ + impl = unew(); + impl->_adapter = graphicsAdapter; + impl->_presentationParameters = presentationParameters; + + Initialize(); + } } \ No newline at end of file diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index b5338f0..6875f9a 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -130,9 +130,9 @@ namespace xna { if (CanResetDevice(*bestDevice)) { auto deviceInformation = snew(*bestDevice); - //MassagePresentParameters(bestDevice.PresentationParameters); - //ValidateGraphicsDeviceInformation(bestDevice); - //device.Reset(deviceInformation.PresentationParameters, deviceInformation.Adapter); + MassagePresentParameters(*bestDevice->PresentParameters); + ValidateGraphicsDeviceInformation(*bestDevice); + device->Reset(deviceInformation->PresentParameters, deviceInformation->Adapter); //GraphicsDeviceManager.ConfigureTouchInput(deviceInformation.PresentationParameters); flag2 = false; } @@ -154,6 +154,11 @@ namespace xna { isDeviceDirty = false; } + + //if (flag1) game->Window()->EndScreenDeviceChange(screenDeviceName, clientWidth, clientHeight); + + currentWindowOrientation = game->Window()->CurrentOrientation(); + inDeviceTransition = false; } void GraphicsDeviceManager::AddDevices(bool anySuitableDevice, std::vector>& foundDevices) { @@ -347,6 +352,66 @@ namespace xna { return device->Profile() == newDeviceInfo.Profile; } + void GraphicsDeviceManager::MassagePresentParameters(PresentationParameters& pp) { + const auto flag1 = pp.BackBufferWidth == 0; + const auto flag2 = pp.BackBufferHeight == 0; + + if (pp.IsFullscreen) + return; + + auto hWnd = pp.DeviceWindowHandle; + + if (hWnd == 0) { + if (!game) + Exception::Throw(Exception::INVALID_OPERATION); + + hWnd = game->Window()->Handle(); + } + + /*NativeMethods.RECT rect; + NativeMethods.GetClientRect(hWnd, out rect); + if (flag1 && rect.Right == 0) + pp.BackBufferWidth = 1; + if (!flag2 || rect.Bottom != 0) + return; + pp.BackBufferHeight = 1;*/ + } + + void GraphicsDeviceManager::ValidateGraphicsDeviceInformation(GraphicsDeviceInformation& devInfo) { + const auto& adapter = devInfo.Adapter; + auto& presentationParameters = devInfo.PresentParameters; + + if (!presentationParameters->IsFullscreen) + return; + + if (presentationParameters->BackBufferWidth == 0 || presentationParameters->BackBufferHeight == 0) + Exception::Throw(Exception::INVALID_OPERATION); + + bool flag = true; + + const auto& currentDisplayMode = adapter->CurrentDisplayMode(); + + if (currentDisplayMode->Format() != presentationParameters->BackBufferFormat && currentDisplayMode->Width() != presentationParameters->BackBufferWidth + && currentDisplayMode->Height() != presentationParameters->BackBufferHeight) + { + flag = false; + + const auto& supportedDisplayModes = adapter->SupportedDisplayModes(); + const size_t count = supportedDisplayModes->Count(); + + for (size_t i = 0; i < count; ++i) { + const auto& displayMode = supportedDisplayModes->DisplayModes[i]; + + if (displayMode->Width() == presentationParameters->BackBufferWidth && displayMode->Height() == presentationParameters->BackBufferHeight) { + flag = true; + break; + } + } + } + if (!flag) + Exception::Throw(Exception::INVALID_OPERATION); + } + bool IsWindowOnAdapter(intptr_t windowHandle, GraphicsAdapter const& adapter) { return GameWindow::ScreenFromAdapter(adapter) == GameWindow::ScreenFromHandle(windowHandle); } diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index fa24992..119d14e 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -169,6 +169,9 @@ namespace xna { void RankDevicesPlatform(std::vector>& foundDevices); void CreateDevice(GraphicsDeviceInformation& newInfo){} + + void MassagePresentParameters(PresentationParameters& pp); + void ValidateGraphicsDeviceInformation(GraphicsDeviceInformation& devInfo); private: sptr game = nullptr; @@ -190,6 +193,7 @@ namespace xna { bool allowMultiSampling{ false }; bool inDeviceTransition{ false }; bool isReallyFullScreen{ false }; + DisplayOrientation currentWindowOrientation{ DisplayOrientation::Default }; std::vector> foundDevices; }; diff --git a/inc/xna/graphics/device.hpp b/inc/xna/graphics/device.hpp index 052f387..ee5dbb8 100644 --- a/inc/xna/graphics/device.hpp +++ b/inc/xna/graphics/device.hpp @@ -48,6 +48,8 @@ namespace xna { bool Initialize(); bool Present(); + void Reset(sptr const& presentationParameters, sptr const& graphicsAdapter); + xna::Viewport Viewport() const; void Viewport(xna::Viewport const& viewport); void UseVSync(bool use); From 60193f3a75d96ec230532bcb474839a655f0a00f Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 30 Jul 2024 21:56:17 -0300 Subject: [PATCH 25/30] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20GraphicsD?= =?UTF-8?q?evice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/device.cpp | 27 ++++++++++++++++-------- framework/platform-dx/gdevicemanager.cpp | 20 ++++++++++++++++++ inc/xna/game/gdevicemanager.hpp | 2 +- inc/xna/graphics/device.hpp | 1 + inc/xna/xna-dx.hpp | 2 +- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 69c8b0d..bf34329 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -100,10 +100,15 @@ namespace xna { impl = unew(); impl->_adapter = info.Adapter; - impl->_gameWindow = info.Window; impl->_presentationParameters = info.PresentParameters; } + GraphicsDevice::GraphicsDevice(sptr const& adapter, GraphicsProfile const& graphicsProfile, sptr const& presentationParameters) { + impl = unew(); + impl->_adapter = adapter; + impl->_presentationParameters = presentationParameters; + } + bool GraphicsDevice::Initialize() { auto _this = shared_from_this(); @@ -119,23 +124,27 @@ namespace xna { if FAILED(hr) Exception::Throw(Exception::FAILED_TO_CREATE); - const auto bounds = impl->_gameWindow->ClientBounds(); + //const auto bounds = impl->_gameWindow->ClientBounds(); impl->_viewport = xna::Viewport(0.0F, 0.0F, - static_cast(bounds.Width), - static_cast(bounds.Height), + impl->_presentationParameters->BackBufferWidth, + impl->_presentationParameters->BackBufferHeight, 0.0F, 1.F); - COLORREF color = impl->_gameWindow->impl->Color(); - impl->_backgroundColor[0] = GetRValue(color) / 255.0f; - impl->_backgroundColor[1] = GetGValue(color) / 255.0f; - impl->_backgroundColor[2] = GetBValue(color) / 255.0f; + //COLORREF color = impl->_gameWindow->impl->Color(); + const auto backColor = Colors::CornflowerBlue; + const auto backColorV3 = backColor.ToVector3(); + + impl->_backgroundColor[0] = backColorV3.X; + impl->_backgroundColor[1] = backColorV3.Y; + impl->_backgroundColor[2] = backColorV3.Z; impl->_backgroundColor[3] = 1.0f; impl->_swapChain = snew(_this); impl->_swapChain->Initialize(); - hr = impl->_factory->MakeWindowAssociation(impl->_gameWindow->impl->WindowHandle(), DXGI_MWA_NO_ALT_ENTER); + auto hwnd = reinterpret_cast(impl->_presentationParameters->DeviceWindowHandle); + hr = impl->_factory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER); if (FAILED(hr)) Exception::Throw(Exception::FAILED_TO_MAKE_WINDOW_ASSOCIATION); diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index 6875f9a..d11e9d4 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -161,6 +161,26 @@ namespace xna { inDeviceTransition = false; } + void GraphicsDeviceManager::CreateDevice(GraphicsDeviceInformation& newInfo) { + if (device) { + device = nullptr; + } + + //this.OnPreparingDeviceSettings((object)this, new PreparingDeviceSettingsEventArgs(newInfo)); + MassagePresentParameters(*newInfo.PresentParameters); + ValidateGraphicsDeviceInformation(newInfo); + + device = snew(newInfo.Adapter, newInfo.Profile, newInfo.PresentParameters); + + //device.DeviceResetting += new EventHandler(this.HandleDeviceResetting); + //device.DeviceReset += new EventHandler(this.HandleDeviceReset); + //device.DeviceLost += new EventHandler(this.HandleDeviceLost); + //device.Disposing += new EventHandler(this.HandleDisposing); + + //GraphicsDeviceManager.ConfigureTouchInput(newInfo.PresentationParameters); + //this.OnDeviceCreated((object)this, EventArgs.Empty);*/ + } + void GraphicsDeviceManager::AddDevices(bool anySuitableDevice, std::vector>& foundDevices) { const auto handle = game->Window()->Handle(); diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index 119d14e..1c678f0 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -168,7 +168,7 @@ namespace xna { void RankDevicesPlatform(std::vector>& foundDevices); - void CreateDevice(GraphicsDeviceInformation& newInfo){} + void CreateDevice(GraphicsDeviceInformation& newInfo); void MassagePresentParameters(PresentationParameters& pp); void ValidateGraphicsDeviceInformation(GraphicsDeviceInformation& devInfo); diff --git a/inc/xna/graphics/device.hpp b/inc/xna/graphics/device.hpp index ee5dbb8..6ce3e31 100644 --- a/inc/xna/graphics/device.hpp +++ b/inc/xna/graphics/device.hpp @@ -10,6 +10,7 @@ namespace xna { public: GraphicsDevice(); GraphicsDevice(GraphicsDeviceInformation const& info); + GraphicsDevice(sptr const& adapter, GraphicsProfile const& graphicsProfile, sptr const& presentationParameters); //Gets the graphics adapter. sptr Adapter() const; diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index 42486fa..a3079c4 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -845,7 +845,7 @@ namespace xna { sptr _swapChain = nullptr; sptr _adapter = nullptr; sptr _renderTarget2D = nullptr; - sptr _gameWindow = nullptr; + intptr_t windowHandle{ 0 }; xna::Viewport _viewport{}; sptr _presentationParameters; From 6663aeb7566dbfe3cf50548a05a0ae219bde31f9 Mon Sep 17 00:00:00 2001 From: Danilo Date: Wed, 31 Jul 2024 16:30:47 -0300 Subject: [PATCH 26/30] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20=20em=20ApplyC?= =?UTF-8?q?hanges=20em=20GraphicsDeviceManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/adapter.cpp | 18 +++++--- framework/platform-dx/device.cpp | 2 +- framework/platform-dx/game.cpp | 7 ++- framework/platform-dx/gdevicemanager.cpp | 54 ++++++++++++------------ framework/platform-dx/window.cpp | 14 +++--- inc/xna/game/window.hpp | 3 +- inc/xna/xna-dx.hpp | 2 +- samples/01_blank/xna.cpp | 3 +- 8 files changed, 59 insertions(+), 44 deletions(-) diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index 102783e..e522a20 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -39,9 +39,11 @@ namespace xna { adp->supportedDisplayModes = getSupportedDisplayModes(pAdapter); - setCurrentDisplayMode(*adp->supportedDisplayModes, SurfaceFormat::Color, - GraphicsDeviceManager::DefaultBackBufferWidth, - GraphicsDeviceManager::DefaultBackBufferHeight, adp->currentDisplayMode); + if (adp->supportedDisplayModes && adp->supportedDisplayModes->Count() > 0) { + setCurrentDisplayMode(*adp->supportedDisplayModes, SurfaceFormat::Color, + GraphicsDeviceManager::DefaultBackBufferWidth, + GraphicsDeviceManager::DefaultBackBufferHeight, adp->currentDisplayMode); + } return adp; } @@ -75,12 +77,14 @@ namespace xna { setOutputVars(pAdapter, adp->deviceName, adp->monitorHandle); - setCurrentDisplayMode(*adp->supportedDisplayModes, SurfaceFormat::Color, - GraphicsDeviceManager::DefaultBackBufferWidth, - GraphicsDeviceManager::DefaultBackBufferHeight, adp->currentDisplayMode); - adp->supportedDisplayModes = getSupportedDisplayModes(pAdapter); + if (adp->supportedDisplayModes && adp->supportedDisplayModes->Count() > 0) { + setCurrentDisplayMode(*adp->supportedDisplayModes, SurfaceFormat::Color, + GraphicsDeviceManager::DefaultBackBufferWidth, + GraphicsDeviceManager::DefaultBackBufferHeight, adp->currentDisplayMode); + } + adapters.push_back(std::move(adp)); } } diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index bf34329..5cbc57a 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -106,7 +106,7 @@ namespace xna { GraphicsDevice::GraphicsDevice(sptr const& adapter, GraphicsProfile const& graphicsProfile, sptr const& presentationParameters) { impl = unew(); impl->_adapter = adapter; - impl->_presentationParameters = presentationParameters; + impl->_presentationParameters = presentationParameters; } bool GraphicsDevice::Initialize() { diff --git a/framework/platform-dx/game.cpp b/framework/platform-dx/game.cpp index 6892f0b..51389c5 100644 --- a/framework/platform-dx/game.cpp +++ b/framework/platform-dx/game.cpp @@ -64,7 +64,12 @@ namespace xna { int Game::Run() { try { - Initialize(); + if (!_gameWindow->impl->Create()) { + Exception::Throw(Exception::FAILED_TO_CREATE); + return false; + } + + Initialize(); if (graphicsDevice == nullptr) { MessageBox(nullptr, "O dispositivo gráfico não foi inicializado corretamente", "XN65", MB_OK); diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index d11e9d4..bc72d9f 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -14,10 +14,8 @@ namespace xna { parameters->BackBufferHeight = backBufferHeight; parameters->BackBufferFormat = SurfaceFormat::Color; parameters->IsFullscreen = false; - _information.PresentParameters = parameters; - - if (game) - _information.Window = game->Window(); + _information.PresentParameters = parameters; + _information.Window = game->Window(); } bool GraphicsDeviceManager::Initialize() { @@ -92,14 +90,13 @@ namespace xna { return true; } - void GraphicsDeviceManager::CreateDevice() { if (isDeviceDirty) { _information.PresentParameters->BackBufferWidth = backBufferWidth; _information.PresentParameters->BackBufferHeight = backBufferHeight; } - auto result = initWindow(_information, *game, backBufferWidth, backBufferHeight); + //auto result = initWindow(_information, *game, backBufferWidth, backBufferHeight); //if (!result) return false; @@ -135,29 +132,30 @@ namespace xna { device->Reset(deviceInformation->PresentParameters, deviceInformation->Adapter); //GraphicsDeviceManager.ConfigureTouchInput(deviceInformation.PresentationParameters); flag2 = false; - } - - if (flag2) - CreateDevice(*bestDevice); - - auto presentationParameters = device->PresentParameters(); - - screenDeviceName = device->Adapter()->DeviceName(); - - isReallyFullScreen = presentationParameters.IsFullscreen; - - if (presentationParameters.BackBufferWidth != 0) - clientWidth = presentationParameters.BackBufferWidth; - - if (presentationParameters.BackBufferHeight != 0) - clientHeight = presentationParameters.BackBufferHeight; - - isDeviceDirty = false; + } } + if (flag2) + CreateDevice(*bestDevice); + + auto presentationParameters = device->PresentParameters(); + + screenDeviceName = device->Adapter()->DeviceName(); + + isReallyFullScreen = presentationParameters.IsFullscreen; + + if (presentationParameters.BackBufferWidth != 0) + clientWidth = presentationParameters.BackBufferWidth; + + if (presentationParameters.BackBufferHeight != 0) + clientHeight = presentationParameters.BackBufferHeight; + + isDeviceDirty = false; + //if (flag1) game->Window()->EndScreenDeviceChange(screenDeviceName, clientWidth, clientHeight); currentWindowOrientation = game->Window()->CurrentOrientation(); + game->graphicsDevice = this->device; inDeviceTransition = false; } @@ -171,6 +169,7 @@ namespace xna { ValidateGraphicsDeviceInformation(newInfo); device = snew(newInfo.Adapter, newInfo.Profile, newInfo.PresentParameters); + device->Initialize(); //device.DeviceResetting += new EventHandler(this.HandleDeviceResetting); //device.DeviceReset += new EventHandler(this.HandleDeviceReset); @@ -187,7 +186,7 @@ namespace xna { std::vector> adapters; GraphicsAdapter::Adapters(adapters); - for (size_t i = 0; adapters.size(); ++i) { + for (size_t i = 0; i < adapters.size(); ++i) { auto& adapter = adapters[i]; if (!anySuitableDevice) { @@ -433,6 +432,9 @@ namespace xna { } bool IsWindowOnAdapter(intptr_t windowHandle, GraphicsAdapter const& adapter) { - return GameWindow::ScreenFromAdapter(adapter) == GameWindow::ScreenFromHandle(windowHandle); + const auto fromAdapter = GameWindow::ScreenFromAdapter(adapter); + const auto fromHandle = GameWindow::ScreenFromHandle(windowHandle); + + return (fromAdapter && fromHandle) && (*fromAdapter == *fromHandle); } } \ No newline at end of file diff --git a/framework/platform-dx/window.cpp b/framework/platform-dx/window.cpp index f9ce345..f73b11e 100644 --- a/framework/platform-dx/window.cpp +++ b/framework/platform-dx/window.cpp @@ -85,7 +85,8 @@ namespace xna { winRect.bottom - winRect.top, TRUE); - return _windowHandle ? true : false; + if (!_windowHandle) + return false; } @@ -93,7 +94,9 @@ namespace xna { // GameWindow // - gameWindow->handle = reinterpret_cast(_windowHandle); + const auto handle = reinterpret_cast(_windowHandle); + + gameWindow->handle = handle; gameWindow->title = _windowTitle; gameWindow->clientBounds = { _windowPosX, _windowPosY, _windowWidth, _windowHeight }; gameWindow->currentOrientation = DisplayOrientation::Default; @@ -200,15 +203,14 @@ namespace xna { if (screen->DeviceName() == adapter.DeviceName()) return std::move(screen); - } - - Exception::Throw("Invalid screen adapter."); + } return nullptr; } uptr GameWindow::ScreenFromHandle(intptr_t windowHandle) { - auto hMonitor = reinterpret_cast(windowHandle); + const auto handle = reinterpret_cast(windowHandle); + auto hMonitor = MonitorFromWindow(handle, MONITOR_DEFAULTTOPRIMARY); if (!hMonitor) return nullptr; diff --git a/inc/xna/game/window.hpp b/inc/xna/game/window.hpp index cbd6b13..f9f7329 100644 --- a/inc/xna/game/window.hpp +++ b/inc/xna/game/window.hpp @@ -41,7 +41,7 @@ namespace xna { bool IsWindowMinimized() const; inline static constexpr Int DefaultClientWidth = 800; - inline static constexpr Int DefaultClientHeight = 600; + inline static constexpr Int DefaultClientHeight = 480; private: String title; @@ -53,6 +53,7 @@ namespace xna { public: struct PlatformImplementation; + friend struct PlatformImplementation; uptr impl = nullptr; }; } diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index a3079c4..da05180 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -778,7 +778,7 @@ namespace xna { private: friend class GameWindow; - GameWindow*& gameWindow; + GameWindow* gameWindow = nullptr; HINSTANCE _hInstance{ nullptr }; HWND _windowHandle{ nullptr }; diff --git a/samples/01_blank/xna.cpp b/samples/01_blank/xna.cpp index c57eaa3..ae50f89 100644 --- a/samples/01_blank/xna.cpp +++ b/samples/01_blank/xna.cpp @@ -17,7 +17,8 @@ namespace xna { void Initialize() override { auto game = reinterpret_cast(this); graphics = snew(game->shared_from_this()); - graphics->Initialize(); + //graphics->Initialize(); + graphics->ApplyChanges(); std::any device = graphicsDevice; services->AddService(*typeof(), device); From d70cac0d773eb11517fbee64bfce691c1d03af52 Mon Sep 17 00:00:00 2001 From: Danilo Date: Wed, 31 Jul 2024 16:37:44 -0300 Subject: [PATCH 27/30] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20CreateDev?= =?UTF-8?q?ice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/gdevicemanager.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index bc72d9f..a34f63b 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -155,7 +155,7 @@ namespace xna { //if (flag1) game->Window()->EndScreenDeviceChange(screenDeviceName, clientWidth, clientHeight); currentWindowOrientation = game->Window()->CurrentOrientation(); - game->graphicsDevice = this->device; + inDeviceTransition = false; } @@ -168,9 +168,20 @@ namespace xna { MassagePresentParameters(*newInfo.PresentParameters); ValidateGraphicsDeviceInformation(newInfo); + const auto windowBounds = game->Window()->ClientBounds(); + + if (windowBounds.Width != newInfo.PresentParameters->BackBufferWidth || windowBounds.Height != newInfo.PresentParameters->BackBufferHeight) { + game->Window()->impl->Size( + newInfo.PresentParameters->BackBufferWidth, + newInfo.PresentParameters->BackBufferHeight); + game->Window()->impl->Update(); + } + device = snew(newInfo.Adapter, newInfo.Profile, newInfo.PresentParameters); device->Initialize(); + game->graphicsDevice = this->device; + //device.DeviceResetting += new EventHandler(this.HandleDeviceResetting); //device.DeviceReset += new EventHandler(this.HandleDeviceReset); //device.DeviceLost += new EventHandler(this.HandleDeviceLost); From dd43a0c1ed06425ba6885f8f4ba203fb490fed3a Mon Sep 17 00:00:00 2001 From: Danilo Date: Wed, 31 Jul 2024 17:07:06 -0300 Subject: [PATCH 28/30] Implementa ScreenDeviceName --- framework/platform-dx/gdevicemanager.cpp | 63 +----------------------- framework/platform-dx/window.cpp | 4 +- inc/xna/game/gdevicemanager.hpp | 6 +-- 3 files changed, 5 insertions(+), 68 deletions(-) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index a34f63b..464cbbd 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -16,16 +16,7 @@ namespace xna { parameters->IsFullscreen = false; _information.PresentParameters = parameters; _information.Window = game->Window(); - } - - bool GraphicsDeviceManager::Initialize() { - if (!game) - return false; - - CreateDevice(); - - return true; - } + } void GraphicsDeviceManager::ApplyChanges() { if (device && !isDeviceDirty) @@ -53,58 +44,6 @@ namespace xna { return true; } - - bool initWindow(GraphicsDeviceInformation& info, Game& game, int backWidth, int backHeight) - { - auto window = info.Window; - - if (!window) { - window = game.Window(); - info.Window = window; - } - - window->impl->Size(backWidth, backHeight); - - if (!window->impl->Create()) { - MessageBox(nullptr, "Falha na criação da janela", "XN65", MB_OK); - return false; - } - - info.PresentParameters->DeviceWindowHandle = reinterpret_cast(window->impl->WindowHandle()); - - return true; - } - - bool initDevice(GraphicsDeviceInformation& info, Game& game, sptr& device) - { - device = snew(info); - - if (!device->Initialize()) { - MessageBox(info.Window->impl->WindowHandle(), "Falha na inicialização do dispositivo gráfico", "XN65", MB_OK); - device = nullptr; - return false; - } - - game.graphicsDevice = device; - - return true; - } - - void GraphicsDeviceManager::CreateDevice() { - if (isDeviceDirty) { - _information.PresentParameters->BackBufferWidth = backBufferWidth; - _information.PresentParameters->BackBufferHeight = backBufferHeight; - } - - //auto result = initWindow(_information, *game, backBufferWidth, backBufferHeight); - - //if (!result) return false; - - initDevice(_information, *game, device); - } - - void GraphicsDeviceManager::ChangeDevice() { - } void GraphicsDeviceManager::ChangeDevice(bool forceCreate) { if (!game) diff --git a/framework/platform-dx/window.cpp b/framework/platform-dx/window.cpp index f73b11e..3f54760 100644 --- a/framework/platform-dx/window.cpp +++ b/framework/platform-dx/window.cpp @@ -248,7 +248,7 @@ namespace xna { } String GameWindow::ScreenDeviceName() const { - //TODO - return std::string(); + const auto screen = ScreenFromHandle(handle); + return screen->DeviceName(); } } \ No newline at end of file diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index 1c678f0..a9d192d 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -141,11 +141,9 @@ namespace xna { bool Initialize(); bool ToggleFullScreen(); + inline void CreateDevice() { ChangeDevice(true); } - protected: - void CreateDevice(); - void ChangeDevice(); - + protected: inline virtual void RankDevices(std::vector>& foundDevices) { RankDevicesPlatform(foundDevices); } From 1fe092449fca4de533c4fee5f3fdeb95c3b9c9eb Mon Sep 17 00:00:00 2001 From: Danilo Date: Wed, 31 Jul 2024 17:52:59 -0300 Subject: [PATCH 29/30] =?UTF-8?q?Corre=C3=A7=C3=B5es=20em=20GraphicsDevice?= =?UTF-8?q?Manager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/gdevicemanager.cpp | 8 ++--- inc/xna/game/gdevicemanager.hpp | 44 ++++++++---------------- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index 464cbbd..b124720 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -6,16 +6,16 @@ namespace xna { GraphicsDeviceManager::GraphicsDeviceManager(sptr const& game) : game(game) { sptr adp = GraphicsAdapter::DefaultAdapter(); - _information.Adapter = adp; - _information.Profile = xna::GraphicsProfile::HiDef; + information.Adapter = adp; + information.Profile = xna::GraphicsProfile::HiDef; auto parameters = snew(); parameters->BackBufferWidth = backBufferWidth; parameters->BackBufferHeight = backBufferHeight; parameters->BackBufferFormat = SurfaceFormat::Color; parameters->IsFullscreen = false; - _information.PresentParameters = parameters; - _information.Window = game->Window(); + information.PresentParameters = parameters; + information.Window = game->Window(); } void GraphicsDeviceManager::ApplyChanges() { diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index a9d192d..0c09a22 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -9,16 +9,16 @@ namespace xna { struct IGraphicsDeviceService { virtual sptr GetGraphicsDevice() = 0; - EventHandler DeviceDisposing; - EventHandler DeviceReset; - EventHandler DeviceResetting; - EventHandler DeviceCreated; + //EventHandler DeviceDisposing; + //EventHandler DeviceReset; + //EventHandler DeviceResetting; + //EventHandler DeviceCreated; }; class IGraphicsDeviceManager { virtual void CreateDevice() = 0; - virtual bool BeginDraw() = 0; - virtual void EndDraw() = 0; + //virtual bool BeginDraw() = 0; + //virtual void EndDraw() = 0; }; class GraphicsDeviceManager : public IGraphicsDeviceService, public IGraphicsDeviceManager { @@ -104,10 +104,12 @@ namespace xna { isDeviceDirty = true; } + //Gets or sets a value that indicates whether to enable a multisampled back buffer. constexpr bool PreferMultiSampling() const { return allowMultiSampling; } + //Gets or sets a value that indicates whether to enable a multisampled back buffer. constexpr void PreferMultiSampling(bool value) { allowMultiSampling = value; isDeviceDirty = true; @@ -138,36 +140,22 @@ namespace xna { public: //Applies any changes to device-related properties, changing the graphics device as necessary. void ApplyChanges(); - bool Initialize(); + //Toggles between full screen and windowed mode. bool ToggleFullScreen(); - - inline void CreateDevice() { ChangeDevice(true); } protected: - inline virtual void RankDevices(std::vector>& foundDevices) { - RankDevicesPlatform(foundDevices); - } - - inline virtual sptr FindBestDevice(bool anySuitableDevice) { - return FindBestPlatformDevice(anySuitableDevice); - } - + inline virtual void RankDevices(std::vector>& foundDevices) { RankDevicesPlatform(foundDevices); } + inline virtual sptr FindBestDevice(bool anySuitableDevice) { return FindBestPlatformDevice(anySuitableDevice); } virtual bool CanResetDevice(GraphicsDeviceInformation& newDeviceInfo); private: + inline void CreateDevice() { ChangeDevice(true); } void ChangeDevice(bool forceCreate); void AddDevices(bool anySuitableDevice, std::vector>& foundDevices); - void AddDevices(GraphicsAdapter const& adapter, DisplayMode const& mode, sptr& baseDeviceInfo, std::vector>& foundDevices) const; - - bool BeginDraw() override { return false; } - void EndDraw() override{ } - + void AddDevices(GraphicsAdapter const& adapter, DisplayMode const& mode, sptr& baseDeviceInfo, std::vector>& foundDevices) const; sptr FindBestPlatformDevice(bool anySuitableDevice); - void RankDevicesPlatform(std::vector>& foundDevices); - - void CreateDevice(GraphicsDeviceInformation& newInfo); - + void CreateDevice(GraphicsDeviceInformation& newInfo); void MassagePresentParameters(PresentationParameters& pp); void ValidateGraphicsDeviceInformation(GraphicsDeviceInformation& devInfo); @@ -175,8 +163,7 @@ namespace xna { sptr game = nullptr; bool isDeviceDirty{ false }; sptr device = nullptr; - GraphicsDeviceInformation _information{}; - + GraphicsDeviceInformation information{}; bool isFullScreen{ false }; Int backBufferWidth{ DefaultBackBufferWidth }; Int backBufferHeight{ DefaultBackBufferHeight }; @@ -192,7 +179,6 @@ namespace xna { bool inDeviceTransition{ false }; bool isReallyFullScreen{ false }; DisplayOrientation currentWindowOrientation{ DisplayOrientation::Default }; - std::vector> foundDevices; }; } From a6231a1fbd5228d33145dff3cba993ff559abf31 Mon Sep 17 00:00:00 2001 From: Danilo Date: Wed, 31 Jul 2024 17:56:51 -0300 Subject: [PATCH 30/30] =?UTF-8?q?Corre=C3=A7=C3=B5es=20em=20PlatformStater?= =?UTF-8?q?Kit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/02_PlatfformerStarterKit/circle.hpp | 5 +++-- samples/02_PlatfformerStarterKit/game.cpp | 2 +- samples/CMakeLists.txt | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/samples/02_PlatfformerStarterKit/circle.hpp b/samples/02_PlatfformerStarterKit/circle.hpp index 8fbc889..a04dd8d 100644 --- a/samples/02_PlatfformerStarterKit/circle.hpp +++ b/samples/02_PlatfformerStarterKit/circle.hpp @@ -14,8 +14,9 @@ namespace PlatformerStarterKit { Center(position), Radius(radius){} constexpr bool Intersects(xna::Rectangle const& rectangle) const { - const auto v = xna::Vector2(xna::MathHelper::Clamp(Center.X, rectangle.Left(), rectangle.Right()), - xna::MathHelper::Clamp(Center.Y, rectangle.Top(), rectangle.Bottom())); + const auto v = + xna::Vector2(xna::MathHelper::Clamp(Center.X, static_cast(rectangle.Left()), static_cast(rectangle.Right())), + xna::MathHelper::Clamp(Center.Y, static_cast(rectangle.Top()), static_cast(rectangle.Bottom()))); const auto direction = Center - v; auto distanceSquared = direction.LengthSquared(); diff --git a/samples/02_PlatfformerStarterKit/game.cpp b/samples/02_PlatfformerStarterKit/game.cpp index 958688f..cd3d5fd 100644 --- a/samples/02_PlatfformerStarterKit/game.cpp +++ b/samples/02_PlatfformerStarterKit/game.cpp @@ -22,7 +22,7 @@ namespace PlatformerStarterKit { void Initialize() override { auto game = reinterpret_cast(this); graphics = snew(game->shared_from_this()); - graphics->Initialize(); + graphics->ApplyChanges(); std::any device = graphicsDevice; services->AddService(*typeof(), device); diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 24ef843..45257e9 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -4,4 +4,4 @@ # Add source to this project's executable. add_subdirectory ("01_blank") -#add_subdirectory ("02_PlatfformerStarterKit") +add_subdirectory ("02_PlatfformerStarterKit")