From 6cb55e0e1a3cce8d19fe02ee6460662228350523 Mon Sep 17 00:00:00 2001 From: DarthMooMancer Date: Mon, 1 Sep 2025 00:46:11 -0400 Subject: [PATCH] feat: window and input handling --- CMakeLists.txt | 4 ++-- include/globals.hpp | 7 +++++++ include/input.hpp | 10 ++++++++++ include/window.hpp | 12 ++++++++++++ src/input.cpp | 17 +++++++++++++++++ src/main.cpp | 16 +++++++++++++++- src/window.cpp | 19 +++++++++++++++++++ 7 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 include/globals.hpp create mode 100644 include/input.hpp create mode 100644 include/window.hpp create mode 100644 src/input.cpp create mode 100644 src/window.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 15d2946..946a378 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,8 @@ include_directories(include) set(SOURCES src/main.cpp - # src/input.cpp - # src/window.cpp + src/input.cpp + src/window.cpp # src/elements.cpp ) diff --git a/include/globals.hpp b/include/globals.hpp new file mode 100644 index 0000000..c203563 --- /dev/null +++ b/include/globals.hpp @@ -0,0 +1,7 @@ +#ifndef GLOBALS_HPP +#define GLOBALS_HPP + +constexpr int ROW = 10; +constexpr int COL = 10; + +#endif diff --git a/include/input.hpp b/include/input.hpp new file mode 100644 index 0000000..70892f4 --- /dev/null +++ b/include/input.hpp @@ -0,0 +1,10 @@ +#ifndef INPUT_HPP +#define INPUT_HPP +// #include "elements.hpp" + +struct Input { + char m_getch = ' '; + void get_input(bool &running); +}; + +#endif // INPUT_HPP diff --git a/include/window.hpp b/include/window.hpp new file mode 100644 index 0000000..c5a22c8 --- /dev/null +++ b/include/window.hpp @@ -0,0 +1,12 @@ +#ifndef WINDOW_HPP +#define WINDOW_HPP + +#include +#include "globals.hpp" +// #include "elements.hpp" + +struct Window { + std::array, ROW> m_board = {}; + void update_display(); +}; +#endif diff --git a/src/input.cpp b/src/input.cpp new file mode 100644 index 0000000..63361b1 --- /dev/null +++ b/src/input.cpp @@ -0,0 +1,17 @@ +#include "input.hpp" +#include + +void Input::get_input(bool &running) { + while(running) { + initscr(); + cbreak(); + noecho(); + + m_getch = getch(); + + endwin(); + if(m_getch == 'q') { + running = false; + } + } +} diff --git a/src/main.cpp b/src/main.cpp index 1055224..f0027d6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,20 @@ #include +#include +#include "input.hpp" +#include "window.hpp" int main() { - std::cout << "Hello World" << std::endl; + Input input; + Window window; + + bool running = true; + std::thread input_thread(&Input::get_input, &input, std::ref(running)); + + while (running) { + std::cout << "Hello World" << std::endl; + window.update_display(); + + } + input_thread.join(); return 0; } diff --git a/src/window.cpp b/src/window.cpp new file mode 100644 index 0000000..b0325d9 --- /dev/null +++ b/src/window.cpp @@ -0,0 +1,19 @@ +#include "window.hpp" +#include +#include +#include + +void Window::update_display() { + for(std::array &row : m_board) { row.fill('.'); } + // Modify board + + // Refresh and draw screen + std::cout << "\033[H" << std::flush; + for(std::array &row : m_board) { + for(char &col : row) { + std::cout << col << " "; + } + std::cout << "\r\n"; + } + std::this_thread::sleep_for(std::chrono::milliseconds(130)); // 1000 / fps; 200ms = 5fps +}