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);