diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f138040 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.10) +project(snake) +include_directories(include) +set(SOURCES + src/main.cpp + src/snake.cpp +) +add_executable(snake ${SOURCES}) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_C_STANDARD 23) +set(CMAKE_C_STANDARD_REQUIRED ON) + +# find_package(SDL3 CONFIG REQUIRED) # An example of how to import libraries from vcpkg +# target_link_libraries(Game PRIVATE SDL3::SDL3) diff --git a/include/snake.hpp b/include/snake.hpp new file mode 100644 index 0000000..fecb7a6 --- /dev/null +++ b/include/snake.hpp @@ -0,0 +1,7 @@ +#ifndef SNAKE_HPP +#define SNAKE_HPP + +void create_snake(); +void draw_snake(); + +#endif diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..51ced0a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include "snake.hpp" + +const int ROW = 10; +const int COL = 10; + +char getch(); +void create_board(); +void draw_board(); + +struct { + char board[ROW][COL]; +} window; + +char (*g_pboard)[ROW][COL] = &window.board; + +char getch() { + termios oldt, newt; + char ch; + tcgetattr(STDIN_FILENO, &oldt); // get current terminal attributes + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); // disable canonical mode and echo + tcsetattr(STDIN_FILENO, TCSANOW, &newt); // apply new settings + read(STDIN_FILENO, &ch, 1); // read one char + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // restore original settings + return ch; +} + +void create_board() { + for(int i = 0; i < ROW; i++) { + for(int a = 0; a < COL; a++) { + (*g_pboard)[i][a] = '.'; + } + } +} + +void draw_board() { + for(int i = 0; i < ROW; i++) { + for(int a = 0; a < COL; a++) { + std::cout << (*g_pboard)[i][a] << " "; + } + std::cout << "\n"; + } +} + +int main() { + create_board(); + draw_board(); + draw_snake(); + // int count = 0; + // std::cout << "Press any key other than space or esc: " << std::endl; + // while(count < 10) { + // char ch = getch(); + // std::cout << "\nYou pressed: " << ch << '\n'; + // ++count; + // } + return 0; +} diff --git a/src/snake.cpp b/src/snake.cpp new file mode 100644 index 0000000..b10add5 --- /dev/null +++ b/src/snake.cpp @@ -0,0 +1,17 @@ +#include "snake.hpp" +#include + +void create_snake() { + std::cout << "Create Snake Test" << std::endl; +} +void draw_snake() { + std::cout << "Draw Snake Test" << std::endl; +} + +/* snake head for instance is (3, 0) + * the direct body to the head when going down would be (snake_head_x, snake_head_y - 1) + * the next and so on would be (previous_body_segment_x - 1, previous_body_segment_y - 1) + * so save the head and body positions as previous and then do the math until the segment hits a point at direction changes. + * so if I change direction at (0, 0) from north to west then at (0, 0) there is a postion change saved and once a body hits that + * switch direction +*/