Support multiple windows

This commit is contained in:
Robert 2020-08-27 14:48:35 +02:00
parent 47c7d729df
commit 565107b449
6 changed files with 63 additions and 24 deletions

View file

@ -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}

View file

@ -1,25 +1,33 @@
#include "PlotWindow.hpp"
PlotWindow::PlotWindow(std::string title) :
#include <iostream>
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)

View file

@ -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;
};

View file

@ -1,6 +1,6 @@
#include "PlotWindow.hpp"
#include <vector>
#include <map>
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<Uint32, PlotWindow*>::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<Uint32, PlotWindow*>::iterator it = PlotWindows.begin(); it != PlotWindows.end(); it++)
it->second->OnUpdate();
}
static void Render()
{
for (std::map<Uint32, PlotWindow*>::iterator it = PlotWindows.begin(); it != PlotWindows.end(); it++)
it->second->OnRender();
}
private:
static inline Uint32 PlotWindowCount;
static std::vector<PlotWindow> PlotWindows;
static inline Uint32 PlotWindowCount = 1;
static inline std::map<Uint32, PlotWindow*> PlotWindows;
};

View file

@ -1,8 +1,9 @@
#include <stdio.h>
#include <vector>
#include <iostream>
#include <SDL.h>
#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;

@ -1 +1 @@
Subproject commit ca91b0aff404e1fae4ed61cfd3408688bebb0ec0
Subproject commit 9c57e5c1d0df339aefb1e235ab968ae4dadcb980