From 9c57e5c1d0df339aefb1e235ab968ae4dadcb980 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 27 Aug 2020 14:48:09 +0200 Subject: [PATCH] Removed threading --- src/sdlf/Screen.hpp | 2 +- src/sdlf/Window.cpp | 82 ++++++++++++++++++++------------------------- src/sdlf/Window.hpp | 12 ++++--- 3 files changed, 44 insertions(+), 52 deletions(-) diff --git a/src/sdlf/Screen.hpp b/src/sdlf/Screen.hpp index 4491e1f..71594bd 100644 --- a/src/sdlf/Screen.hpp +++ b/src/sdlf/Screen.hpp @@ -12,7 +12,7 @@ namespace sf virtual void OnFocus(IWindow* parent) { } virtual void OnDefocus() { } bool OnUpdate(double frametime) override { return true; } - bool OnEvent(const SDL_Event& event) override { return true; } + bool OnEvent(const SDL_Event& event) override { } void OnRender(SDL_Renderer* renderer) override {} protected: diff --git a/src/sdlf/Window.cpp b/src/sdlf/Window.cpp index 55e67d1..6da5c20 100644 --- a/src/sdlf/Window.cpp +++ b/src/sdlf/Window.cpp @@ -7,7 +7,6 @@ namespace sf { - void IWindow::Create(Vector2u size, Vector2i position, std::string title, Uint32 windowFlags /*= SDL_WINDOW_RESIZABLE*/, Uint32 renderFlags /*= SDL_RENDERER_SOFTWARE*/) { @@ -35,6 +34,7 @@ namespace sf m_pCurrentException = SDL_GetError(); return; } + m_uWindowID = SDL_GetWindowID(m_pWindow); } // Create SDL_Renderer @@ -59,24 +59,15 @@ namespace sf SDL_DestroyWindow(m_pWindow); } - void IWindow::Launch(bool threaded /*= false*/) + void IWindow::Launch() { - m_atomWindowOpen = true; - if (threaded) - { - m_oMsgLoopThread = std::thread(&IWindow::MessageLoop, this); - } - else { - MessageLoop(); - } + MessageLoop(); } void IWindow::Stop() { - m_atomWindowOpen = false; - - if(m_oMsgLoopThread.joinable()) - m_oMsgLoopThread.join(); + OnClose(); + Destroy(); } void IWindow::AddEventCallback(EventCallback callback, void* userdata) @@ -114,12 +105,40 @@ namespace sf Uint32 windowFlags /*= SDL_WINDOW_RESIZABLE*/, Uint32 renderFlags /*= SDL_RENDERER_SOFTWARE*/) : m_pWindow(nullptr), m_pRenderer(nullptr), m_oEvent(), m_oSize(size), m_oPosition(position), m_strTitle(title), m_uWindowFlags(windowFlags), - m_uRenderFlags(renderFlags), m_pCurrentScreen(nullptr), m_pCurrentException("") + m_uRenderFlags(renderFlags), m_pCurrentScreen(nullptr), m_pCurrentException(""), + m_isWindowOpen(false), m_uWindowID(-1) { } void IWindow::MessageLoop() { + Open(); + + std::chrono::steady_clock::time_point pastTime = std::chrono::steady_clock::now(); + while (m_isWindowOpen) + { + while (SDL_PollEvent(&m_oEvent)) + { + m_oEventFunction(m_oEvent); + } + + double frametime = std::chrono::duration_cast>( + std::chrono::steady_clock::now() - pastTime + ).count(); + pastTime = std::chrono::steady_clock::now(); + if (!m_oUpdateFunction(frametime)) + m_isWindowOpen = false; + + m_oRenderFunction(m_pRenderer); + + SDL_RenderPresent(m_pRenderer); + } + } + + void IWindow::Open() + { + m_isWindowOpen = true; + Create(m_oSize, m_oPosition, m_strTitle, m_uWindowFlags, m_uRenderFlags); if (m_pCurrentException != "") { @@ -128,37 +147,8 @@ namespace sf } // Test if the user instance's creation succeeded - if (!OnCreate()) - m_atomWindowOpen = false; - - std::chrono::steady_clock::time_point pastTime = std::chrono::steady_clock::now(); - while (m_atomWindowOpen) - { - while (SDL_PollEvent(&m_oEvent)) - { - if (m_oEventFunction(m_oEvent)) - { - if (m_oEvent.type == SDL_QUIT) - { - m_atomWindowOpen = false; - } - } - } - - double frametime = std::chrono::duration_cast>( - std::chrono::steady_clock::now() - pastTime - ).count(); - pastTime = std::chrono::steady_clock::now(); - if (!m_oUpdateFunction(frametime)) - m_atomWindowOpen = false; - - m_oRenderFunction(m_pRenderer); - - SDL_RenderPresent(m_pRenderer); - } - - OnClose(); - Destroy(); + if (!OnCreate()) + m_isWindowOpen = false; } } \ No newline at end of file diff --git a/src/sdlf/Window.hpp b/src/sdlf/Window.hpp index 3b20f01..f878501 100644 --- a/src/sdlf/Window.hpp +++ b/src/sdlf/Window.hpp @@ -21,10 +21,11 @@ namespace sf void Create(Vector2u size, Vector2i position, std::string title, Uint32 windowFlags = SDL_WINDOW_RESIZABLE, Uint32 rendererFlags = SDL_RENDERER_SOFTWARE); - void Launch(bool threaded = false); + void Open(); + void Launch(); void Stop(); - bool IsOpen() { return m_atomWindowOpen; } + bool IsOpen() { return m_isWindowOpen; } void AddEventCallback(EventCallback callback, void* userdata); @@ -38,6 +39,8 @@ namespace sf virtual bool OnCreate() { return true; } virtual void OnClose() { } + + protected: bool OnEvent(const SDL_Event& event) override { return true; } bool OnUpdate(double frametime) override { return true; } void OnRender(SDL_Renderer* renderer) override { } @@ -46,7 +49,8 @@ namespace sf SDL_Window* m_pWindow; SDL_Renderer* m_pRenderer; SDL_Event m_oEvent; - std::atomic_bool m_atomWindowOpen; + bool m_isWindowOpen; + Uint32 m_uWindowID; std::string m_pCurrentException; @@ -62,8 +66,6 @@ namespace sf IScreen* m_pCurrentScreen; - std::thread m_oMsgLoopThread; - std::function m_oEventFunction; std::function m_oUpdateFunction;