From 565107b449094350cce42532dca6568f2becbd0f Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 27 Aug 2020 14:48:35 +0200 Subject: [PATCH] Support multiple windows --- ComplexPlotting/CMakeLists.txt | 2 +- ComplexPlotting/PlotWindow.cpp | 20 +++++++++----- ComplexPlotting/PlotWindow.hpp | 9 ++++--- ComplexPlotting/PlotWindowManager.hpp | 38 ++++++++++++++++++++++----- ComplexPlotting/main.cpp | 16 ++++++----- SDLFramework | 2 +- 6 files changed, 63 insertions(+), 24 deletions(-) diff --git a/ComplexPlotting/CMakeLists.txt b/ComplexPlotting/CMakeLists.txt index cf07efe..0381c44 100644 --- a/ComplexPlotting/CMakeLists.txt +++ b/ComplexPlotting/CMakeLists.txt @@ -14,7 +14,7 @@ endif() # Add source to this project's executable. add_executable (ComplexPlotting "main.cpp" - "PlotWindow.hpp" "PlotWindow.cpp" "PlotWindowFactory.hpp" "PlotWindowManager.hpp") + "PlotWindow.hpp" "PlotWindow.cpp" "PlotWindowManager.hpp") target_include_directories(ComplexPlotting PRIVATE ${SDL2_INCLUDES} diff --git a/ComplexPlotting/PlotWindow.cpp b/ComplexPlotting/PlotWindow.cpp index 381042d..fe688e6 100644 --- a/ComplexPlotting/PlotWindow.cpp +++ b/ComplexPlotting/PlotWindow.cpp @@ -1,25 +1,33 @@ #include "PlotWindow.hpp" -PlotWindow::PlotWindow(std::string title) : +#include + +PlotWindow::PlotWindow(Uint32 id) : IWindow::IWindow( UnitVector2u * 400, UnitVector2i * SDL_WINDOWPOS_UNDEFINED, - title, - NULL) + "Plot " + std::to_string(id), + NULL), + id(id) { } -void PlotWindow::OnEvent(const SDL_Event& e) +bool PlotWindow::OnEvent(const SDL_Event& e) { if (e.window.windowID != m_uWindowID) - return; + return true; - switch (e.window.type) + switch (e.window.event) { case SDL_WINDOWEVENT_CLOSE: m_isWindowOpen = false; + return false; + + default: break; } + + return true; } bool PlotWindow::OnUpdate(double frametime) diff --git a/ComplexPlotting/PlotWindow.hpp b/ComplexPlotting/PlotWindow.hpp index ca1872c..2d89248 100644 --- a/ComplexPlotting/PlotWindow.hpp +++ b/ComplexPlotting/PlotWindow.hpp @@ -10,11 +10,12 @@ class PlotWindow : public IWindow { public: - PlotWindow(std::string title); + PlotWindow(Uint32 id); - void OnEvent(const SDL_Event& e) override; - bool OnUpdate(double frametime) override; - void OnRender(SDL_Renderer* renderer) override; + bool OnEvent(const SDL_Event& e) override; + bool OnUpdate(double frametime = 0) override; + void OnRender(SDL_Renderer* renderer = nullptr) override; private: + Uint32 id; }; \ No newline at end of file diff --git a/ComplexPlotting/PlotWindowManager.hpp b/ComplexPlotting/PlotWindowManager.hpp index 44f4326..abb02a4 100644 --- a/ComplexPlotting/PlotWindowManager.hpp +++ b/ComplexPlotting/PlotWindowManager.hpp @@ -1,6 +1,6 @@ #include "PlotWindow.hpp" -#include +#include class PlotWindowManager { @@ -10,17 +10,43 @@ public: static void MakeNew() { - PlotWindows.emplace_back("Plot " + std::to_string(PlotWindowCount)); + PlotWindow* plt = new PlotWindow(PlotWindowCount); + PlotWindows.insert({ PlotWindowCount, plt }); PlotWindowCount++; + plt->Open(); } - static void HandleEvents() + static void HandleEvents(const SDL_Event& e) { - for(auto plot : PlotWIndows) + for (std::map::iterator it = PlotWindows.begin(); it != PlotWindows.end(); ) + { + if (!it->second->OnEvent(e)) + { + it->second->Stop(); + delete it->second; + it->second = NULL; + it = PlotWindows.erase(it); + } + else + { + it++; + } + } + } + static void Update() + { + for (std::map::iterator it = PlotWindows.begin(); it != PlotWindows.end(); it++) + it->second->OnUpdate(); + } + + static void Render() + { + for (std::map::iterator it = PlotWindows.begin(); it != PlotWindows.end(); it++) + it->second->OnRender(); } private: - static inline Uint32 PlotWindowCount; - static std::vector PlotWindows; + static inline Uint32 PlotWindowCount = 1; + static inline std::map PlotWindows; }; \ No newline at end of file diff --git a/ComplexPlotting/main.cpp b/ComplexPlotting/main.cpp index aab2ab1..d0d0fb0 100644 --- a/ComplexPlotting/main.cpp +++ b/ComplexPlotting/main.cpp @@ -1,8 +1,9 @@ #include #include +#include -#include -#include "PlotWindow.hpp" +#include "SDL.h" +#include "PlotWindowManager.hpp" #undef main @@ -13,7 +14,10 @@ int main(int argc, char** argv) SDL_Init(SDL_INIT_VIDEO); // Create and open some windows - PlotWindow window("Plot"); + for (int i = 0; i < 3; i++) + { + PlotWindowManager::MakeNew(); + } // Wait for window processes to end SDL_Event e; @@ -25,12 +29,12 @@ int main(int argc, char** argv) if (e.type == SDL_QUIT) quit = true; - window.OnEvent(e); + PlotWindowManager::HandleEvents(e); } - window.OnUpdate(0); + PlotWindowManager::Update(); - window.OnRender(nullptr); + PlotWindowManager::Render(); } return 0; diff --git a/SDLFramework b/SDLFramework index ca91b0a..9c57e5c 160000 --- a/SDLFramework +++ b/SDLFramework @@ -1 +1 @@ -Subproject commit ca91b0aff404e1fae4ed61cfd3408688bebb0ec0 +Subproject commit 9c57e5c1d0df339aefb1e235ab968ae4dadcb980