From d7c235c365a919372135971917d5a94e0df23705 Mon Sep 17 00:00:00 2001 From: _N3m0 Date: Mon, 14 Aug 2023 14:42:42 +0200 Subject: [PATCH] screen display center --- include/screen.hpp | 38 +++++++++++++++++++++++++++++++------- test/scroon.cpp | 13 +++++++++++-- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/include/screen.hpp b/include/screen.hpp index 2509b41..d1ce203 100644 --- a/include/screen.hpp +++ b/include/screen.hpp @@ -3,6 +3,9 @@ #include #include "jdc.hpp" +#include +#include +//#include template struct point{ @@ -21,6 +24,8 @@ class Screen{ char** screen; Screen(int row, int col){ + puts("\033[?1049h\033[H"); // alt screen + puts("\033[?25l"); // inv cursor this->col = col; this->row = row; @@ -35,28 +40,38 @@ class Screen{ delete this->screen[i]; } delete this->screen; + puts("\033[?1049l"); // ~ alt screen + puts("\033[?25h"); // ~ inv cursor }; void background(){ for (int i=0; iscreen[i][y]) continue; 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'; - //this->screen[i][y] = 'X'; } } }; 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 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; iscreen[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 p, carte c){ @@ -81,11 +96,20 @@ class Screen{ int ligne(){ return this->row; - } + }; int colone(){ return this->col; - } + }; + + static point size(){ + point p; + struct winsize size; + ioctl(1, TIOCGWINSZ, &size); + p.x = size.ws_col; + p.y = size.ws_row; + return p; + }; private: int row; int col; diff --git a/test/scroon.cpp b/test/scroon.cpp index 6fd1c1a..0b01e63 100644 --- a/test/scroon.cpp +++ b/test/scroon.cpp @@ -1,4 +1,5 @@ #include +#include #include "screen.hpp" #include "jdc.hpp" @@ -6,18 +7,26 @@ int main(){ int row = 5; int col = 11; Screen s(row, col); - s.background(); s.display(); + sleep(1); JDC jdc; jdc.init(); jdc.shuffle(); - + point p = {0, 0}; s.addCard(p, jdc.top()); s.display(); + sleep(1); + s.background(); + s.display(); + sleep(1); + p = {col-4, row-3}; s.addCard(p, CARTERETOURNE); s.display(); + sleep(1); + + return 0; }