swarmc/include/screen.hpp
2024-11-21 22:22:14 +01:00

52 lines
1.1 KiB
C++

#pragma once
#include <cstddef>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <termios.h>
struct Screen {
char *buf;
std::size_t w, h;
float dx = 0, dy = 0;
float sx = 1, sy = 1;
void draw();
void clear();
char &at(std::size_t n);
char &at(std::size_t x, std::size_t y);
void resize(std::size_t n);
void resize(std::size_t x, std::size_t y);
Screen(std::size_t n) : buf(nullptr) { resize(n); }
Screen(std::size_t x, std::size_t y) : buf(nullptr) { resize(x, y); }
~Screen() { delete[] buf; }
private:
char _dummy;
static inline void gotoxy(int x, int y)
{
std::printf("\033[%d;%dH", y, x);
}
};
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); }