From 6001970ecacc858a2a5e022ea5de193c1d52a94d Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Mon, 14 Apr 2025 13:34:32 +0200 Subject: [PATCH] add async clock --- src/gameske_funkce.cpp | 58 +++++++++++++++++++++++++++++++++++++++--- src/gameske_funkce.h | 4 ++- src/menu.cpp | 2 ++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/gameske_funkce.cpp b/src/gameske_funkce.cpp index 9e6f13a..57ffa79 100644 --- a/src/gameske_funkce.cpp +++ b/src/gameske_funkce.cpp @@ -3,11 +3,15 @@ #include #include #include +#include #include #include #include #include +#include +#include #include +#include size_t spawn_menu(uint16_t begin_y, uint16_t begin_x, const char** choices, size_t n_choices) { @@ -109,9 +113,6 @@ std::string spawncmd() { keypad(cmd_win, TRUE); box(cmd_win, 0, 0); mvwprintw(cmd_win, 1, 1, "Command: "); - - // Disable automatic echo, we'll handle it manually - noecho(); curs_set(1); wrefresh(cmd_win); @@ -148,7 +149,6 @@ std::string spawncmd() { cmd[pos] = '\0'; // Ensure null termination // Restore echo state as needed - echo(); curs_set(0); wclear(cmd_win); @@ -156,3 +156,53 @@ std::string spawncmd() { delwin(cmd_win); return std::string(cmd); } + +void async_clock(WINDOW* win) { + while (true) { + auto now = std::chrono::system_clock::now(); + std::string time_str; + try { + // Format just the time (HH:MM:SS) + time_str = std::format("{:%T}", now); + // Remove fractional seconds + size_t dot_pos = time_str.find('.'); + if (dot_pos != std::string::npos) { + time_str.erase(dot_pos); + } + } catch (const std::exception& e) { + std::clog << "Format error: " << e.what() << std::endl; + time_str = "Error"; + } + + // Clear window content but preserve border + werase(win); + wborder(win, 0, 0, 0, 0, ACS_TTEE, 0, ACS_BTEE, 0); + + // Print the time in the window + mvwprintw(win, 1, 1, "%s", time_str.c_str()); + wrefresh(win); + std::this_thread::sleep_for(std::chrono::seconds(1)); + } +} + +void show_clock_text(WINDOW* text_win) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + wborder(text_win, 0, ' ', 0, 0, 0, ACS_HLINE, 0, ACS_HLINE); + mvwprintw(text_win, 1, 1, "správný čas na podání trestního oznámení je"); + wrefresh(text_win); +} + +void async_clock_init() { + //memory leak + WINDOW* win = newwin(3, 10, LINES - 3, COLS - 10); + box(win, 0, 0); + wrefresh(win); + std::thread clock_thread(async_clock, win); + clock_thread.detach(); + + //memory leak + WINDOW* text_win = newwin(3, 52, LINES - 3, COLS - 10 - 44); + + std::thread text_thread(show_clock_text, text_win); + text_thread.detach(); +} diff --git a/src/gameske_funkce.h b/src/gameske_funkce.h index 453a27e..70f1df8 100644 --- a/src/gameske_funkce.h +++ b/src/gameske_funkce.h @@ -9,4 +9,6 @@ size_t spawn_menu(uint16_t begin_y, uint16_t begin_x, const char** choices, void print_in_middle(WINDOW* win, int starty, int startx, int width, char* string, chtype color); -std::string spawncmd(); \ No newline at end of file +std::string spawncmd(); + +void async_clock_init(); \ No newline at end of file diff --git a/src/menu.cpp b/src/menu.cpp index 9d85f00..d470de2 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -24,10 +24,12 @@ void menu() { start_color(); cbreak(); noecho(); + curs_set(0); // Makes cursor invisible keypad(stdscr, TRUE); for (uint8_t i = 0; i < 8; i++) { init_pair(i, i, COLOR_BLACK); } + async_clock_init(); /* Create items */ n_choices = ARRAY_SIZE(choices);