Added input to create windows

This commit is contained in:
Robert 2020-08-27 15:36:39 +02:00
parent 565107b449
commit 3e597fc2ab
4 changed files with 46 additions and 11 deletions

View file

@ -2,11 +2,11 @@
#include <iostream>
PlotWindow::PlotWindow(Uint32 id) :
PlotWindow::PlotWindow(Uint32 id, std::string title) :
IWindow::IWindow(
UnitVector2u * 400,
UnitVector2i * SDL_WINDOWPOS_UNDEFINED,
"Plot " + std::to_string(id),
"Plot " + std::to_string(id) + " [" + title + "]",
NULL),
id(id)
{
@ -37,5 +37,10 @@ bool PlotWindow::OnUpdate(double frametime)
void PlotWindow::OnRender(SDL_Renderer* renderer)
{
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
SDL_RenderClear(m_pRenderer);
SDL_RenderPresent(m_pRenderer);
return;
}

View file

@ -10,7 +10,7 @@ class PlotWindow :
public IWindow
{
public:
PlotWindow(Uint32 id);
PlotWindow(Uint32 id, std::string title);
bool OnEvent(const SDL_Event& e) override;
bool OnUpdate(double frametime = 0) override;

View file

@ -1,6 +1,8 @@
#include "PlotWindow.hpp"
#include <map>
#include <thread>
#include <future>
class PlotWindowManager
{
@ -8,9 +10,9 @@ public:
PlotWindowManager() = delete;
PlotWindowManager& operator=(const PlotWindowManager& other) = delete;
static void MakeNew()
static void MakeNew(std::string title)
{
PlotWindow* plt = new PlotWindow(PlotWindowCount);
PlotWindow* plt = new PlotWindow(PlotWindowCount, title);
PlotWindows.insert({ PlotWindowCount, plt });
PlotWindowCount++;
plt->Open();
@ -36,6 +38,18 @@ public:
static void Update()
{
// Check for input from the console
if (InputFuture.wait_for(std::chrono::milliseconds(0)) != std::future_status::timeout)
{
MakeNew(InputFuture.get());
InputPromise = std::promise<std::string>();
InputFuture = InputPromise.get_future();
InputThread.join();
InputThread = std::thread(PlotWindowManager::InputThreadFunc);
}
for (std::map<Uint32, PlotWindow*>::iterator it = PlotWindows.begin(); it != PlotWindows.end(); it++)
it->second->OnUpdate();
}
@ -46,7 +60,27 @@ public:
it->second->OnRender();
}
static void Quit()
{
InputThread.detach();
}
private:
static inline Uint32 PlotWindowCount = 1;
static inline std::map<Uint32, PlotWindow*> PlotWindows;
static inline std::promise<std::string> InputPromise;
static inline std::future<std::string> InputFuture = InputPromise.get_future();
private:
static void InputThreadFunc()
{
std::string in;
std::cout << "> ";
std::getline(std::cin, in);
InputPromise.set_value(in);
}
private:
static inline std::thread InputThread = std::thread(PlotWindowManager::InputThreadFunc);
};

View file

@ -13,12 +13,6 @@ int main(int argc, char** argv)
SDL_Init(SDL_INIT_VIDEO);
// Create and open some windows
for (int i = 0; i < 3; i++)
{
PlotWindowManager::MakeNew();
}
// Wait for window processes to end
SDL_Event e;
bool quit = false;
@ -37,5 +31,7 @@ int main(int argc, char** argv)
PlotWindowManager::Render();
}
PlotWindowManager::Quit();
return 0;
}