diff --git a/include/engine.hpp b/include/engine.hpp index 32746ff..8a55027 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -3,6 +3,7 @@ #include #include #include +#include constexpr int ROW = 15; constexpr int COL = 21; @@ -21,13 +22,13 @@ struct vec2 { struct Window { std::array, ROW> view; void draw_display(); - void update_display(vec2 &head, vec2 &fruit, std::deque &buf); + void update_display(const vec2 fruit, std::deque &buf); }; class Engine { public: Engine(); - ~Engine(); + ~Engine() { endwin(); } void run(); private: diff --git a/src/engine.cpp b/src/engine.cpp index 97f7212..00687a5 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -14,13 +13,10 @@ void Window::draw_display() { mvaddch(i, j * 2, view[i][j]); } } - refresh(); } -void Window::update_display(vec2 &head, vec2 &fruit, std::deque &buf) { - view[head.y][head.x] = '*'; +void Window::update_display(const vec2 fruit, std::deque &buf) { view[fruit.y][fruit.x] = 'o'; - for(const auto& segment : buf) { if(segment.x < COL && segment.y < ROW) { view[segment.y][segment.x] = '~'; @@ -38,16 +34,15 @@ Engine::Engine() { for(auto& row : win.view) row.fill('.'); } -Engine::~Engine() { endwin(); } - void Engine::run() { while (exit_summary() && process_input()) { add_tail_and_respawn_fruit(); determine_index_zero_pos(); move_tail_indicies(); - win.update_display(head, fruit, tail); + win.update_display(fruit, tail); win.draw_display(); + refresh(); std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); } nodelay(stdscr, false); @@ -86,6 +81,7 @@ void Engine::determine_index_zero_pos() { else if(dir == Direction::UP) { head.y = (head.y + ROW - 1) % ROW; } else if(dir == Direction::DOWN) { head.y = (head.y + 1) % ROW; } win.view[prev_head.y][prev_head.x] = '.'; + win.view[head.y][head.x] = '*'; } void Engine::move_tail_indicies() {