feat: added window, input and elements
This commit is contained in:
+5
-3
@@ -6,12 +6,14 @@ include_directories(include)
|
||||
|
||||
set(SOURCES
|
||||
src/main.cpp
|
||||
src/input.cpp
|
||||
src/window.cpp
|
||||
)
|
||||
|
||||
add_executable(tic_tac_toe ${SOURCES})
|
||||
|
||||
# find_package(Curses REQUIRED)
|
||||
# include_directories(${CURSES_INCLUDE_DIR})
|
||||
# target_link_libraries(tic_tac_toe ${CURSES_LIBRARIES})
|
||||
find_package(Curses REQUIRED)
|
||||
include_directories(${CURSES_INCLUDE_DIR})
|
||||
target_link_libraries(tic_tac_toe ${CURSES_LIBRARIES})
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef ELEMENTS_HPP
|
||||
#define ELEMENTS_HPP
|
||||
|
||||
// Add code here
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef GLOBALS_HPP
|
||||
#define GLOBALS_HPP
|
||||
|
||||
constexpr int ROW = 10;
|
||||
constexpr int COL = 10;
|
||||
|
||||
#endif
|
||||
@@ -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
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef WINDOW_HPP
|
||||
#define WINDOW_HPP
|
||||
|
||||
#include <array>
|
||||
#include "globals.hpp"
|
||||
// #include "elements.hpp"
|
||||
|
||||
struct Window {
|
||||
std::array<std::array<char, COL>, ROW> m_board = {};
|
||||
void update_display();
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "input.hpp"
|
||||
#include <ncurses.h>
|
||||
|
||||
void Input::get_input(bool &running) {
|
||||
while(running) {
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
|
||||
m_getch = getch();
|
||||
|
||||
endwin();
|
||||
if(m_getch == 'q') {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
-1
@@ -1,6 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include "window.hpp"
|
||||
#include "input.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;
|
||||
}
|
||||
|
||||
input_thread.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "window.hpp"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
void Window::update_display() {
|
||||
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
|
||||
// Modify board
|
||||
|
||||
// Refresh and draw screen
|
||||
std::cout << "\033[H" << std::flush;
|
||||
for(std::array<char, COL> &row : m_board) {
|
||||
for(char &col : row) {
|
||||
std::cout << col << " ";
|
||||
}
|
||||
std::cout << "\r\n";
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // 1000 / fps; 200ms = 5fps
|
||||
}
|
||||
Reference in New Issue
Block a user