Added complex function callback
This commit is contained in:
parent
3e597fc2ab
commit
7fd559f3eb
|
@ -2,6 +2,41 @@
|
|||
|
||||
#include <iostream>
|
||||
|
||||
#define PI 3.1415926535f
|
||||
|
||||
SDL_Color HSVtoRGB(float H, float S, float V)
|
||||
{
|
||||
float s = S / 100;
|
||||
float v = V / 100;
|
||||
float C = s * v;
|
||||
float X = C * (1 - abs(fmod(H / 60.0, 2) - 1));
|
||||
float m = v - C;
|
||||
float r, g, b;
|
||||
if (H >= 0 && H < 60) {
|
||||
r = C, g = X, b = 0;
|
||||
}
|
||||
else if (H >= 60 && H < 120) {
|
||||
r = X, g = C, b = 0;
|
||||
}
|
||||
else if (H >= 120 && H < 180) {
|
||||
r = 0, g = C, b = X;
|
||||
}
|
||||
else if (H >= 180 && H < 240) {
|
||||
r = 0, g = X, b = C;
|
||||
}
|
||||
else if (H >= 240 && H < 300) {
|
||||
r = X, g = 0, b = C;
|
||||
}
|
||||
else {
|
||||
r = C, g = 0, b = X;
|
||||
}
|
||||
Uint8 R = (r + m) * 255;
|
||||
Uint8 G = (g + m) * 255;
|
||||
Uint8 B = (b + m) * 255;
|
||||
|
||||
return SDL_Color{ R, G, B, 255 };
|
||||
}
|
||||
|
||||
PlotWindow::PlotWindow(Uint32 id, std::string title) :
|
||||
IWindow::IWindow(
|
||||
UnitVector2u * 400,
|
||||
|
@ -12,6 +47,11 @@ PlotWindow::PlotWindow(Uint32 id, std::string title) :
|
|||
{
|
||||
}
|
||||
|
||||
void PlotWindow::SetCallback(CmplxFunc callback)
|
||||
{
|
||||
this->callback = callback;
|
||||
}
|
||||
|
||||
bool PlotWindow::OnEvent(const SDL_Event& e)
|
||||
{
|
||||
if (e.window.windowID != m_uWindowID)
|
||||
|
@ -37,7 +77,14 @@ bool PlotWindow::OnUpdate(double frametime)
|
|||
|
||||
void PlotWindow::OnRender(SDL_Renderer* renderer)
|
||||
{
|
||||
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
|
||||
float a = std::arg(callback(std::complex<float>{0.f, 0.f}));
|
||||
a += PI;
|
||||
a /= PI;
|
||||
a *= 360;
|
||||
|
||||
SDL_Color c = HSVtoRGB(a, 100, 100);
|
||||
|
||||
SDL_SetRenderDrawColor(m_pRenderer, c.r, c.g, c.b, c.a);
|
||||
SDL_RenderClear(m_pRenderer);
|
||||
|
||||
SDL_RenderPresent(m_pRenderer);
|
||||
|
|
|
@ -1,21 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include <complex>
|
||||
#include <functional>
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
#include "Window.hpp"
|
||||
|
||||
using namespace sf;
|
||||
|
||||
typedef std::function<std::complex<float>(std::complex<float>)> CmplxFunc;
|
||||
|
||||
class PlotWindow :
|
||||
public IWindow
|
||||
{
|
||||
public:
|
||||
PlotWindow(Uint32 id, std::string title);
|
||||
|
||||
void SetCallback(CmplxFunc callback);
|
||||
|
||||
bool OnEvent(const SDL_Event& e) override;
|
||||
bool OnUpdate(double frametime = 0) override;
|
||||
void OnRender(SDL_Renderer* renderer = nullptr) override;
|
||||
|
||||
private:
|
||||
Uint32 id;
|
||||
CmplxFunc callback;
|
||||
};
|
|
@ -10,12 +10,14 @@ public:
|
|||
PlotWindowManager() = delete;
|
||||
PlotWindowManager& operator=(const PlotWindowManager& other) = delete;
|
||||
|
||||
static void MakeNew(std::string title)
|
||||
static PlotWindow* MakeNew(std::string title)
|
||||
{
|
||||
PlotWindow* plt = new PlotWindow(PlotWindowCount, title);
|
||||
PlotWindows.insert({ PlotWindowCount, plt });
|
||||
PlotWindowCount++;
|
||||
plt->Open();
|
||||
|
||||
return plt;
|
||||
}
|
||||
|
||||
static void HandleEvents(const SDL_Event& e)
|
||||
|
@ -41,7 +43,18 @@ public:
|
|||
// Check for input from the console
|
||||
if (InputFuture.wait_for(std::chrono::milliseconds(0)) != std::future_status::timeout)
|
||||
{
|
||||
MakeNew(InputFuture.get());
|
||||
std::string title = InputFuture.get();
|
||||
if (title != "")
|
||||
{
|
||||
PlotWindow* newWindow = MakeNew(title);
|
||||
newWindow->SetCallback(std::bind([](std::complex<float> c)
|
||||
{
|
||||
return std::complex<float>{ 1.f, 1.f };
|
||||
},
|
||||
std::placeholders::_1));
|
||||
|
||||
PrintThreadSafe("-- Success\n");
|
||||
}
|
||||
|
||||
InputPromise = std::promise<std::string>();
|
||||
InputFuture = InputPromise.get_future();
|
||||
|
@ -65,6 +78,19 @@ public:
|
|||
InputThread.detach();
|
||||
}
|
||||
|
||||
static void PrintThreadSafe(std::string s, bool block = true)
|
||||
{
|
||||
if (block)
|
||||
StdOutMutex.lock();
|
||||
else
|
||||
if (!StdOutMutex.try_lock())
|
||||
return;
|
||||
|
||||
std::cout << s;
|
||||
|
||||
StdOutMutex.unlock();
|
||||
}
|
||||
|
||||
private:
|
||||
static inline Uint32 PlotWindowCount = 1;
|
||||
static inline std::map<Uint32, PlotWindow*> PlotWindows;
|
||||
|
@ -72,12 +98,16 @@ private:
|
|||
static inline std::promise<std::string> InputPromise;
|
||||
static inline std::future<std::string> InputFuture = InputPromise.get_future();
|
||||
|
||||
static inline std::mutex StdOutMutex;
|
||||
|
||||
private:
|
||||
static void InputThreadFunc()
|
||||
{
|
||||
std::string in;
|
||||
std::cout << "> ";
|
||||
|
||||
PrintThreadSafe("> ");
|
||||
std::getline(std::cin, in);
|
||||
|
||||
InputPromise.set_value(in);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue