reepblue Posted November 28, 2022 Share Posted November 28, 2022 This is an example of a window that can change sizes and modes using Ultra Engine. This will work with a stock project. Uploading this here before I add any of my custom systems to it. Feel free to use it as a base. Header: #pragma once #include "UltraEngine.h" struct GameWindowSettings { int x; int y; int displayindex; int w; int h; UltraEngine::WindowStyles style; }; class GameWindow : public UltraEngine::Object { std::shared_ptr<UltraEngine::Window> m_spEngineWindow; std::shared_ptr<UltraEngine::Framebuffer> m_spEngineFramebuffer; UltraEngine::WString m_pszTitle; GameWindowSettings settings; int m_iScale; void Build(const UltraEngine::WString& title, const GameWindowSettings& setting); public: GameWindow(); ~GameWindow(); const int GetWidth(); const int GetHeight(); const UltraEngine::iVec2 GetSize(); void SetShape(const int x, const int y, const int w, const int h); void SetStyle(const UltraEngine::WindowStyles style); void SetHidden(const bool hide); void SwitchDisplay(const int display); void SetNativeFullscreen(const bool mode); std::shared_ptr<UltraEngine::Window> GetEngineWindow(); std::shared_ptr<UltraEngine::Framebuffer> GetFramebuffer(); const bool KeyHit(const UltraEngine::KeyCode keycode); const bool KeyDown(const UltraEngine::KeyCode keycode); const bool MouseHit(const UltraEngine::MouseButton button); const bool MouseDown(const UltraEngine::MouseButton button); const bool Active(); const bool Closed(); void Activate(); /// <summary> /// Renders the world to the window's framebuffer. /// </summary> void Render(std::shared_ptr<UltraEngine::World> pWorld); static std::shared_ptr<GameWindow> Create(const UltraEngine::WString& title, const GameWindowSettings& setting); }; extern shared_ptr<GameWindow> ActiveGameWindow(); /// <summary> /// This function is used to create a game window. /// </summary> /// <param name="title"></param> /// <param name="setting"></param> /// <returns></returns> extern std::shared_ptr<GameWindow> CreateGameWindow(const UltraEngine::WString& title, const GameWindowSettings& setting); Source: #include "UltraEngine.h" #include "GameWindow.h" shared_ptr<UltraEngine::Object> activegamewindow = NULL; shared_ptr<GameWindow> ActiveGameWindow() { return activegamewindow->As<GameWindow>(); } GameWindow::GameWindow() { m_spEngineWindow = NULL; m_spEngineFramebuffer = NULL; settings.x = 0; settings.y = 0; settings.displayindex = 0; settings.w = 800; settings.h = 0; settings.style = UltraEngine::WINDOW_CENTER | UltraEngine::WINDOW_TITLEBAR; } GameWindow::~GameWindow() { m_spEngineFramebuffer = NULL; m_spEngineWindow = NULL; } const int GameWindow::GetWidth() { UltraEngine::Assert(m_spEngineWindow); return m_spEngineWindow->GetSize().x; } const int GameWindow::GetHeight() { UltraEngine::Assert(m_spEngineWindow); return m_spEngineWindow->GetSize().y; } const UltraEngine::iVec2 GameWindow::GetSize() { UltraEngine::Assert(m_spEngineWindow); return m_spEngineWindow->GetSize(); } void GameWindow::SetShape(const int x, const int y, const int w, const int h) { UltraEngine::Assert(m_spEngineWindow); settings.x = x; settings.y = y; settings.w = w; settings.h = h; m_spEngineWindow->SetShape(UltraEngine::iVec2(x, y), UltraEngine::iVec2(w, h)); } void GameWindow::SetStyle(const UltraEngine::WindowStyles style) { UltraEngine::Assert(m_spEngineWindow); // Changing styles needs a window rebuild. if (style != settings.style) { settings.style = style; Build(m_pszTitle, settings); } } void GameWindow::SwitchDisplay(const int display) { auto displays = UltraEngine::GetDisplays(); if (displays[display] != NULL) { settings.displayindex = display; Build(m_pszTitle, settings); } } void GameWindow::SetNativeFullscreen(const bool mode) { UltraEngine::Assert(m_spEngineWindow); m_spEngineWindow->SetHidden(true); auto displays = UltraEngine::GetDisplays(); if (mode && settings.style != UltraEngine::WINDOW_FULLSCREEN) { settings.style = UltraEngine::WINDOW_FULLSCREEN; if (m_spEngineWindow != NULL) { UltraEngine::DebugLog("Rebuilding as a Fullscreen Window..."); m_spEngineFramebuffer = NULL; m_spEngineWindow = NULL; } auto displays = UltraEngine::GetDisplays(); m_spEngineWindow = UltraEngine::CreateWindow(m_pszTitle, 0, 0, displays[settings.displayindex]->GetSize().x, displays[settings.displayindex]->GetSize().y, displays[settings.displayindex], settings.style | UltraEngine::WINDOW_HIDDEN); if (m_spEngineWindow == NULL) { UltraEngine::Notify("Failed to create fullscreen window.", "Error", true); return; } m_spEngineFramebuffer = UltraEngine::CreateFramebuffer(m_spEngineWindow); EmitEvent(UltraEngine::EVENT_WINDOWPAINT, Self(), (int)displays[settings.displayindex]->GetScale(), m_spEngineWindow->GetPosition().x, m_spEngineWindow->GetPosition().y, m_spEngineWindow->GetFramebuffer()->size.x, m_spEngineWindow->GetFramebuffer()->size.y); } else { settings.style = UltraEngine::WINDOW_CENTER | UltraEngine::WINDOW_TITLEBAR; Build(m_pszTitle, settings); } Activate(); } void GameWindow::SetHidden(const bool hide) { UltraEngine::Assert(m_spEngineWindow); m_spEngineWindow->SetHidden(hide); if (!hide) m_spEngineWindow->Activate(); } void GameWindow::Build(const UltraEngine::WString& title, const GameWindowSettings& setting) { if (m_spEngineWindow != NULL) { UltraEngine::DebugLog("Rebuilding Game Window..."); m_spEngineFramebuffer = NULL; m_spEngineWindow = NULL; } else { UltraEngine::DebugLog("Building Game Window..."); } if (m_pszTitle.empty()) m_pszTitle = title; settings = setting; auto displays = UltraEngine::GetDisplays(); m_iScale = static_cast<int>(displays[settings.displayindex]->scale); m_spEngineWindow = UltraEngine::CreateWindow(m_pszTitle, settings.x, settings.y, settings.w * m_iScale, settings.h * m_iScale, displays[settings.displayindex], settings.style | UltraEngine::WINDOW_HIDDEN); if (m_spEngineWindow == NULL) { UltraEngine::Notify("Failed to create window.", "Error", true); return; } m_spEngineFramebuffer = UltraEngine::CreateFramebuffer(m_spEngineWindow); EmitEvent(UltraEngine::EVENT_WINDOWPAINT, Self(), (int)displays[settings.displayindex]->GetScale(), m_spEngineWindow->GetPosition().x, m_spEngineWindow->GetPosition().y, m_spEngineWindow->GetFramebuffer()->size.x, m_spEngineWindow->GetFramebuffer()->size.y); } std::shared_ptr<UltraEngine::Window> GameWindow::GetEngineWindow() { return m_spEngineWindow; } std::shared_ptr<UltraEngine::Framebuffer> GameWindow::GetFramebuffer() { return m_spEngineFramebuffer; } const bool GameWindow::KeyHit(const UltraEngine::KeyCode keycode) { if (m_spEngineWindow == NULL) return false; return m_spEngineWindow->KeyHit(keycode); } const bool GameWindow::KeyDown(const UltraEngine::KeyCode keycode) { if (m_spEngineWindow == NULL) return false; return m_spEngineWindow->KeyDown(keycode); } const bool GameWindow::MouseHit(const UltraEngine::MouseButton button) { if (m_spEngineWindow == NULL) return false; return m_spEngineWindow->MouseHit(button); } const bool GameWindow::MouseDown(const UltraEngine::MouseButton button) { if (m_spEngineWindow == NULL) return false; return m_spEngineWindow->MouseDown(button); } const bool GameWindow::Active() { if (m_spEngineWindow == NULL) return false; return !m_spEngineWindow->GetHidden(); } const bool GameWindow::Closed() { if (m_spEngineWindow == NULL) return false; return m_spEngineWindow->Closed(); } void GameWindow::Activate() { if (activegamewindow != Self()) activegamewindow = Self(); m_spEngineWindow->SetHidden(false); m_spEngineWindow->Activate(); } void GameWindow::Render(std::shared_ptr<UltraEngine::World> pWorld) { if (m_spEngineFramebuffer) { pWorld->Render(m_spEngineFramebuffer); } } std::shared_ptr<GameWindow> GameWindow::Create(const UltraEngine::WString& title, const GameWindowSettings& setting) { std::shared_ptr<GameWindow> window = std::make_shared<GameWindow>(); window->Build(title, setting); if (activegamewindow == NULL) activegamewindow = window; return window; } std::shared_ptr<GameWindow> CreateGameWindow(const UltraEngine::WString& title, const GameWindowSettings& setting) { return GameWindow::Create(title, setting); } Example: #include "UltraEngine.h" using namespace UltraEngine; int main(int argc, const char* argv[]) { //Create a world auto world = CreateWorld(); //Get the displays auto displays = GetDisplays(); // Load settings. // Idealy, you'd want to load this from a file. auto settings = GameWindowSettings { 0, 0, 0, 1280, 720, WINDOW_CENTER | WINDOW_TITLEBAR }; // Create the window. // This will also create a framebuffer. auto window = CreateGameWindow("Game", settings); // Create a camera auto camera = CreateCamera(world); camera->SetClearColor(0.125); camera->SetFOV(70); camera->SetPosition(0, 0, -3); //Create a light auto light = CreateBoxLight(world); light->SetRotation(35, 45, 0); light->SetRange(-10, 10); //Create a box auto box = CreateBox(world); window->Activate(); //Main loop while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false) { // Toggle between modes. if (window->KeyHit(KEY_SPACE)) { static bool fullscreen = false; fullscreen = !fullscreen; window->SetNativeFullscreen(fullscreen); } world->Update(); // Since the framebuffer is destoryed and rebuilt between changing modes, // We need to tell the window to manage our world's updating window->Render(world); } return 0; } 3 Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.