screen display center

This commit is contained in:
_N3m0 2023-08-14 14:42:42 +02:00
parent 2c3d79c581
commit d7c235c365
2 changed files with 42 additions and 9 deletions

View File

@ -3,6 +3,9 @@
#include <iostream> #include <iostream>
#include "jdc.hpp" #include "jdc.hpp"
#include <sys/ioctl.h>
#include <termios.h>
//#include <unistd.h>
template <typename T> template <typename T>
struct point{ struct point{
@ -21,6 +24,8 @@ class Screen{
char** screen; char** screen;
Screen(int row, int col){ Screen(int row, int col){
puts("\033[?1049h\033[H"); // alt screen
puts("\033[?25l"); // inv cursor
this->col = col; this->col = col;
this->row = row; this->row = row;
@ -35,28 +40,38 @@ class Screen{
delete this->screen[i]; delete this->screen[i];
} }
delete this->screen; delete this->screen;
puts("\033[?1049l"); // ~ alt screen
puts("\033[?25h"); // ~ inv cursor
}; };
void background(){ void background(){
for (int i=0; i<row; i++){ for (int i=0; i<row; i++){
for (int y=0; y<col; y++){ for (int y=0; y<col; y++){
if (this->screen[i][y]) continue;
if (y%2 && i%2) this->screen[i][y] = 'X'; if (y%2 && i%2) this->screen[i][y] = 'X';
else if (!(i%2) && !(y%2)) this->screen[i][y] = 'X'; else if (!(i%2) && !(y%2)) this->screen[i][y] = 'X';
else this->screen[i][y] = 'O'; else this->screen[i][y] = 'O';
//this->screen[i][y] = 'X';
} }
} }
}; };
void display(){ void display(){
std::cout << "\033[1;48;5;22m" << std::endl; 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);
for (int i=0; i<row; i++){ for (int i=0; i<row; i++){
for (int y=0; y<col; y++){ for (int y=0; y<col; y++){
std::cout << this->screen[i][y]; if (this->screen[i][y]) std::cout << this->screen[i][y];
else std::cout << ' ';
} }
std::cout << std::endl; printf("\033[%d;%dH", coord.y+i+1, coord.x);
} }
std::cout << "\033[0m" << std::endl; puts("\033[0m"); // reset
} }
void addCard(point<int> p, carte c){ void addCard(point<int> p, carte c){
@ -81,11 +96,20 @@ class Screen{
int ligne(){ int ligne(){
return this->row; return this->row;
} };
int colone(){ int colone(){
return this->col; return this->col;
} };
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;
};
private: private:
int row; int row;
int col; int col;

View File

@ -1,4 +1,5 @@
#include <iostream> #include <iostream>
#include <unistd.h>
#include "screen.hpp" #include "screen.hpp"
#include "jdc.hpp" #include "jdc.hpp"
@ -6,18 +7,26 @@ int main(){
int row = 5; int row = 5;
int col = 11; int col = 11;
Screen s(row, col); Screen s(row, col);
s.background();
s.display(); s.display();
sleep(1);
JDC jdc; JDC jdc;
jdc.init(); jdc.init();
jdc.shuffle(); jdc.shuffle();
point<int> p = {0, 0}; point<int> p = {0, 0};
s.addCard(p, jdc.top()); s.addCard(p, jdc.top());
s.display(); s.display();
sleep(1);
s.background();
s.display();
sleep(1);
p = {col-4, row-3}; p = {col-4, row-3};
s.addCard(p, CARTERETOURNE); s.addCard(p, CARTERETOURNE);
s.display(); s.display();
sleep(1);
return 0;
} }