From b956854826281857a9dd963dc22c692c3bb8f20d Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Tue, 22 Mar 2022 17:03:26 +0100 Subject: [PATCH] ^l to refresh screen --- CMakeLists.txt | 4 +- console.cpp | 90 +++++++++++++++++++++++++++++++++++++++++---- console.h | 17 ++++++++- console_ncurses.cpp | 2 +- console_ncurses.h | 4 +- console_posix.cpp | 2 +- console_posix.h | 4 +- 7 files changed, 106 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c2a75a9..d1a2cc0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,8 +27,8 @@ add_executable( include(CheckIPOSupported) check_ipo_supported(RESULT supported) -#set(CMAKE_BUILD_TYPE RelWithDebInfo) -set(CMAKE_BUILD_TYPE Debug) +set(CMAKE_BUILD_TYPE RelWithDebInfo) +#set(CMAKE_BUILD_TYPE Debug) set(CMAKE_THREAD_PREFER_PTHREAD TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE) diff --git a/console.cpp b/console.cpp index 6fc73a2..65d06ba 100644 --- a/console.cpp +++ b/console.cpp @@ -1,9 +1,17 @@ +#include +#include +#include +#include + #include "console.h" +#include "utils.h" console::console(std::atomic_bool *const terminate) : terminate(terminate) { + memset(screen_buffer, ' ', sizeof screen_buffer); + th = new std::thread(std::ref(*this)); } @@ -16,32 +24,100 @@ console::~console() bool console::poll_char() { - return buffer.empty() == false; + return input_buffer.empty() == false; } uint8_t console::get_char() { - if (buffer.empty()) + if (input_buffer.empty()) return 0x00; - char c = buffer.at(0); + char c = input_buffer.at(0); - buffer.erase(buffer.begin() + 0); + input_buffer.erase(input_buffer.begin() + 0); return c; } +void console::debug(const std::string fmt, ...) +{ + char *buffer = nullptr; + + va_list ap; + va_start(ap, fmt); + + int len = vasprintf(&buffer, fmt.c_str(), ap); + + va_end(ap); + + for(int i=0; i +constexpr const int t_width { 80 }; +constexpr const int t_height { 25 }; + class console { private: @@ -12,11 +15,19 @@ private: std::thread *th { nullptr }; - std::vector buffer; + std::vector input_buffer; + + char screen_buffer[t_height][t_width]; + uint8_t tx { 0 }; + uint8_t ty { 0 }; protected: virtual int wait_for_char(const int timeout) = 0; + virtual void put_char_ll(const char c) = 0; + + void put_string_ll(const std::string & what); + public: console(std::atomic_bool *const terminate); virtual ~console(); @@ -25,7 +36,9 @@ public: uint8_t get_char(); - virtual void put_char(const char c) = 0; + void put_char(const char c); + + void debug(const std::string fmt, ...); virtual void resize_terminal() = 0; diff --git a/console_ncurses.cpp b/console_ncurses.cpp index bd3c0fd..2dfa6a2 100644 --- a/console_ncurses.cpp +++ b/console_ncurses.cpp @@ -35,7 +35,7 @@ int console_ncurses::wait_for_char(const int timeout) return -1; } -void console_ncurses::put_char(const char c) +void console_ncurses::put_char_ll(const char c) { if (c >= 32 || (c != 12 && c != 27 && c != 13)) { wprintw(w_main->win, "%c", c); diff --git a/console_ncurses.h b/console_ncurses.h index a255bc5..266e433 100644 --- a/console_ncurses.h +++ b/console_ncurses.h @@ -13,11 +13,11 @@ private: protected: int wait_for_char(const int timeout) override; + void put_char_ll(const char c) override; + public: console_ncurses(std::atomic_bool *const terminate); virtual ~console_ncurses(); - void put_char(const char c) override; - void resize_terminal() override; }; diff --git a/console_posix.cpp b/console_posix.cpp index 581883b..dedcf63 100644 --- a/console_posix.cpp +++ b/console_posix.cpp @@ -35,7 +35,7 @@ int console_posix::wait_for_char(const int timeout) return -1; } -void console_posix::put_char(const char c) +void console_posix::put_char_ll(const char c) { printf("%c", c); diff --git a/console_posix.h b/console_posix.h index 7722576..454ea38 100644 --- a/console_posix.h +++ b/console_posix.h @@ -11,11 +11,11 @@ private: protected: int wait_for_char(const int timeout) override; + void put_char_ll(const char c) override; + public: console_posix(std::atomic_bool *const terminate); virtual ~console_posix(); - void put_char(const char c) override; - void resize_terminal() override; };