2023-08-13 13:50:05 +02:00
|
|
|
#ifndef SCREEN_HPP
|
|
|
|
#define SCREEN_HPP
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include "jdc.hpp"
|
2023-08-14 14:42:42 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
//#include <unistd.h>
|
2023-08-13 13:50:05 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct point{
|
|
|
|
T y;
|
2023-08-14 13:12:08 +02:00
|
|
|
T x;
|
2023-08-13 13:50:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sprite{
|
2023-08-14 13:12:08 +02:00
|
|
|
char** data;
|
2023-08-13 13:50:05 +02:00
|
|
|
int row;
|
|
|
|
int col;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Screen{
|
|
|
|
public:
|
2023-08-14 13:12:08 +02:00
|
|
|
char** screen;
|
2023-08-13 13:50:05 +02:00
|
|
|
|
2023-08-14 13:12:08 +02:00
|
|
|
Screen(int row, int col){
|
2023-08-14 14:42:42 +02:00
|
|
|
puts("\033[?1049h\033[H"); // alt screen
|
|
|
|
puts("\033[?25l"); // inv cursor
|
2023-08-13 13:50:05 +02:00
|
|
|
this->col = col;
|
|
|
|
this->row = row;
|
|
|
|
|
2023-08-14 13:12:08 +02:00
|
|
|
this->screen = new char*[row];
|
2023-08-13 13:50:05 +02:00
|
|
|
for (int i=0; i<row; i++){
|
2023-08-14 13:12:08 +02:00
|
|
|
this->screen[i] = new char[col];
|
2023-08-13 13:50:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
~Screen(){
|
|
|
|
for (int i=0; i<row; i++){
|
|
|
|
delete this->screen[i];
|
|
|
|
}
|
|
|
|
delete this->screen;
|
2023-08-14 14:42:42 +02:00
|
|
|
puts("\033[?1049l"); // ~ alt screen
|
|
|
|
puts("\033[?25h"); // ~ inv cursor
|
2023-08-13 13:50:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void background(){
|
|
|
|
for (int i=0; i<row; i++){
|
|
|
|
for (int y=0; y<col; y++){
|
2023-08-14 14:42:42 +02:00
|
|
|
if (this->screen[i][y]) continue;
|
2023-08-14 13:12:08 +02:00
|
|
|
if (y%2 && i%2) this->screen[i][y] = 'X';
|
|
|
|
else if (!(i%2) && !(y%2)) this->screen[i][y] = 'X';
|
|
|
|
else this->screen[i][y] = 'O';
|
2023-08-13 13:50:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void display(){
|
2023-08-14 14:42:42 +02:00
|
|
|
puts("\033[2J"); // clear screen
|
|
|
|
puts("\033[1;48;5;22m"); // bold + background green
|
|
|
|
|
|
|
|
point<int> coord = this->size();
|
|
|
|
coord.x = coord.x/2 - col/2;
|
|
|
|
coord.y = coord.y/2 - row/2;
|
|
|
|
printf("\033[%d;%dH", coord.y, coord.x);
|
|
|
|
|
2023-08-13 13:50:05 +02:00
|
|
|
for (int i=0; i<row; i++){
|
2023-08-14 13:12:08 +02:00
|
|
|
for (int y=0; y<col; y++){
|
2023-08-14 14:42:42 +02:00
|
|
|
if (this->screen[i][y]) std::cout << this->screen[i][y];
|
|
|
|
else std::cout << ' ';
|
2023-08-14 13:12:08 +02:00
|
|
|
}
|
2023-08-14 14:42:42 +02:00
|
|
|
printf("\033[%d;%dH", coord.y+i+1, coord.x);
|
2023-08-13 13:50:05 +02:00
|
|
|
}
|
2023-08-14 14:42:42 +02:00
|
|
|
puts("\033[0m"); // reset
|
2023-08-13 13:50:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void addCard(point<int> p, carte c){
|
2023-08-14 13:12:08 +02:00
|
|
|
this->screen[p.x][p.y] = '+';
|
|
|
|
this->screen[p.x][p.y+3] = '+';
|
|
|
|
this->screen[p.x+2][p.y+3] = '+';
|
|
|
|
this->screen[p.x+2][p.y] = '+';
|
|
|
|
|
|
|
|
this->screen[p.x][p.y+1] = '-';
|
|
|
|
this->screen[p.x][p.y+2] = '-';
|
|
|
|
this->screen[p.x+2][p.y+1] = '-';
|
|
|
|
this->screen[p.x+2][p.y+2] = '-';
|
|
|
|
|
|
|
|
this->screen[p.x+1][p.y] = '|';
|
|
|
|
this->screen[p.x+1][p.y+3] = '|';
|
|
|
|
|
|
|
|
this->screen[p.x+1][p.y+1] = JDC::valeur(c);
|
|
|
|
this->screen[p.x+1][p.y+2] = JDC::couleur(c);
|
2023-08-13 13:50:05 +02:00
|
|
|
};
|
|
|
|
|
2023-08-14 18:07:29 +02:00
|
|
|
void addSprite(point<int> p, sprite s){
|
|
|
|
for (int i=0; i<s.row; i++){
|
|
|
|
for (int y=0; y<s.col; y++){
|
|
|
|
if (!s.data[i][y]) continue;
|
|
|
|
this->screen[p.x+i][p.y+y] = s.data[i][y];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-08-14 13:12:08 +02:00
|
|
|
|
2023-08-13 13:50:05 +02:00
|
|
|
int ligne(){
|
|
|
|
return this->row;
|
2023-08-14 14:42:42 +02:00
|
|
|
};
|
2023-08-13 13:50:05 +02:00
|
|
|
|
|
|
|
int colone(){
|
|
|
|
return this->col;
|
2023-08-14 14:42:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static point<int> size(){
|
|
|
|
point<int> p;
|
|
|
|
struct winsize size;
|
|
|
|
ioctl(1, TIOCGWINSZ, &size);
|
|
|
|
p.x = size.ws_col;
|
|
|
|
p.y = size.ws_row;
|
|
|
|
return p;
|
|
|
|
};
|
2023-08-13 13:50:05 +02:00
|
|
|
private:
|
|
|
|
int row;
|
|
|
|
int col;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|