From a049134af7b0f82746eab9ec032f61a62eb67ecd Mon Sep 17 00:00:00 2001 From: Joursoir Date: Fri, 12 Mar 2021 18:57:05 +0000 Subject: add arguments and buttons handling --- main.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index e013252..56d639c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -7,26 +8,67 @@ #include "models.hpp" #include "veh_models.hpp" +#define APP_NAME "aroad" +const int key_escape = 27; + int main(int argc, char *argv[]) { + if(argc > 1) { + if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { + printf("Usage: %s [-h, --help]\n" + "Copyright (C) 2021 Aleksandr D. Goncharov (Joursoir)\n" + "Control:\n" + "\t'Q', 'q', 'ESC' - exit app\n" + "\tKey right - speed up vehicles\n" + "\tKey left - speed down vehicles\n", APP_NAME); + return 0; + } + printf("%s: invalid argument -- '%s'\n", APP_NAME, argv[1]); + return 1; + } int total_rows, total_cols; initscr(); noecho(); curs_set(0); srand(time(0)); + nodelay(stdscr, true); + keypad(stdscr, true); getmaxyx(stdscr, total_rows, total_cols); struct timespec my_timer; my_timer.tv_sec = 0; - my_timer.tv_nsec = 50000000; // 100000000 + my_timer.tv_nsec = 100000000; GameWorld *world = new GameWorld(total_rows, total_cols); Vehicle *car = 0; struct ll_vehicle *ptr_first = new struct ll_vehicle(0, 0); struct ll_vehicle *ptr; int wait_move = 0; + int pressed_button = 0; + bool flag_ride = true; for(;;) { + /* buttons handling */ + while((pressed_button = getch()) != ERR) { + if(pressed_button == key_escape || + pressed_button == 'Q' || + pressed_button == 'q') { + flag_ride = false; + break; + } + else if(pressed_button == KEY_RIGHT) { + if(my_timer.tv_nsec > 10000000) + my_timer.tv_nsec -= 10000000; + } + else if(pressed_button == KEY_LEFT) { + if(my_timer.tv_nsec < 900000000) + my_timer.tv_nsec += 10000000; + } + else + break; + } + + /* vehicle road */ ptr = ptr_first; while(ptr) { @@ -34,6 +76,8 @@ int main(int argc, char *argv[]) if(!car) { if(wait_move > 0) break; + if(!flag_ride) + break; int type = rand() % veh_types_max; ptr->data = new Vehicle(type, total_rows-1, -veh_store[type].length); @@ -42,23 +86,28 @@ int main(int argc, char *argv[]) ptr->next = new struct ll_vehicle(0, 0); } - + + ptr = ptr->next; if(!car->MoveRight(world)) { - ptr = ptr->next; delete car; delete ptr_first; ptr_first = ptr; - continue; + if(ptr_first->data == 0) { + delete ptr_first; + ptr_first = 0; + ptr = 0; + } } - - ptr = ptr->next; } + if(ptr_first == 0) + break; wait_move--; world->Update(); nanosleep(&my_timer, NULL); } + delete world; endwin(); return 0; } -- cgit v1.2.3-18-g5258