swarmc/include/screen.hpp

96 lines
2.1 KiB
C++
Raw Permalink Normal View History

2024-11-21 21:22:14 +00:00
#pragma once
#include "vec.hpp"
2024-11-21 21:22:14 +00:00
#include <cstddef>
#include <cstdlib>
#include <cstdio>
#include <functional>
2024-11-21 21:22:14 +00:00
#include <unistd.h>
#include <termios.h>
#include <vector>
2024-11-21 21:22:14 +00:00
struct Screen {
/**
* Unit pixel information struct
*/
struct Symbol {
char sym;
vec<3> color = {0,0,0};
bool visible = true;
Symbol &operator=(char c) { sym = c; return *this; };
};
/**
* Function taking screen coords and space coords and returning a Symbol for
* that position
*/
using char_shader_t = std::function<Symbol(int, int, float, float)>;
2024-11-21 21:22:14 +00:00
std::size_t w, h;
float dx = 0, dy = 0;
float sx = 1, sy = 1.8;
2024-11-21 21:22:14 +00:00
void draw();
void clear();
Symbol &at(std::size_t n);
Symbol &at(int x, int y);
Symbol &at(float x, float y);
2024-11-21 21:22:14 +00:00
void resize(std::size_t x, std::size_t y);
void move_to(float x, float y);
std::pair<float, float> screen_to_xy(int w, int h);
std::pair<int, int> xy_to_screen(float x, float y);
Screen(std::size_t x, std::size_t y) : buf(nullptr)
{ resize(x, y); move_to(0, 0); }
2024-11-21 21:22:14 +00:00
~Screen() { delete[] buf; }
std::vector<const vec<2>*> points;
std::vector<std::pair<const vec<2>*,const vec<2>*>> vecs;
char_shader_t shader;
bool draw_vecs = true;
2024-11-21 21:22:14 +00:00
private:
Symbol *buf;
Symbol _dummy;
2024-11-21 21:22:14 +00:00
static inline void gotoxy(int x, int y)
{
std::printf("\033[%d;%dH", y, x);
}
};
using colorizer_t = std::function<vec<3>(float)>;
struct TanhColorizer {
float scale = 1.0;
float translate = 0.0;
vec<3> operator()(float y);
private:
static vec<3> hsl_to_rgb(float h, float s, float l);
static float hue_to_rgb(float p, float q, float t);
};
2024-11-21 21:22:14 +00:00
static termios old, current;
static inline void enter_noncanonical_mode(void)
{
tcgetattr(STDIN_FILENO, &old);
current = old;
current.c_lflag &= ~ICANON; /* disable buffered i/o */
current.c_lflag &= ~ECHO; /* set no echo mode */
current.c_cc[VMIN] = 0;/* no wait */
current.c_cc[VTIME] = 0;/* no wait */
tcsetattr(STDIN_FILENO, TCSANOW, &current);
}
static inline void enter_canonical_mode(void){ tcsetattr(0, TCSANOW, &old); }