diff options
author | Joursoir <chat@joursoir.net> | 2021-03-08 20:36:26 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2021-03-08 20:36:26 +0000 |
commit | ac3ab1206a7bddec5312bf974479853429426dfa (patch) | |
tree | 26c0aacd3c78b8c2bd2d9db8c21af530d68459e6 | |
download | ascii-road-ac3ab1206a7bddec5312bf974479853429426dfa.tar.gz ascii-road-ac3ab1206a7bddec5312bf974479853429426dfa.tar.bz2 ascii-road-ac3ab1206a7bddec5312bf974479853429426dfa.zip |
init project
-rw-r--r-- | Vehicle.cpp | 25 | ||||
-rw-r--r-- | Vehicle.hpp | 16 | ||||
-rw-r--r-- | main.cpp | 86 | ||||
-rw-r--r-- | veh_models.hpp | 135 |
4 files changed, 262 insertions, 0 deletions
diff --git a/Vehicle.cpp b/Vehicle.cpp new file mode 100644 index 0000000..3e71db4 --- /dev/null +++ b/Vehicle.cpp @@ -0,0 +1,25 @@ +#include <ncurses.h> + +#include "Vehicle.hpp" +#include "veh_models.hpp" + +bool Vehicle::Draw(int bound_x) +{ + bool ret = false; + int i, j, k; + const struct vehicle_info car = veh_info[type]; + + for(i = pos_x, j = car.length-1; i >= 0 && j >= 0; i--, j--) { + if(i >= bound_x) + continue; + ret = true; + + int h = car.height; + for(k = 0; k < h; k++) { + if(car.model[k][j] != SUPPORT_CHAR) + mvaddch(pos_y - (h - k), i, car.model[k][j]); + } + } + + return ret; +} diff --git a/Vehicle.hpp b/Vehicle.hpp new file mode 100644 index 0000000..418a532 --- /dev/null +++ b/Vehicle.hpp @@ -0,0 +1,16 @@ +#ifndef ASCIIROAD_VEHICLE_H +#define ASCIIROAD_VEHICLE_H + +class Vehicle { + int type; + int pos_y, pos_x; +public: + Vehicle(int a_type, int a_y, int a_x) + : type(a_type), pos_y(a_y), pos_x(a_x) { } + ~Vehicle() { } + + void MoveRight() { pos_x++; } + bool Draw(int bound_x); /* return false if not draw anything */ +}; + +#endif /* ASCIIROAD_VEHICLE_H */ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..ca406f4 --- /dev/null +++ b/main.cpp @@ -0,0 +1,86 @@ +#include <ncurses.h> +#include <stdlib.h> +#include <time.h> + +#include "Vehicle.hpp" +#include "veh_models.hpp" + +int total_rows, total_cols; + +void draw_line(int y, char ch) +{ + int i; + move(y, 0); + for(i = 0; i < total_cols; i++) + addch(ch); +} + +/* This version is a dummy */ +void draw_background() +{ + draw_line(total_rows-1, '_'); // road + + mvprintw(total_rows-9, 10, " _-_"); + mvprintw(total_rows-8, 10, " /~~ ~~\\"); + mvprintw(total_rows-7, 10, " /~~ ~~\\"); + mvprintw(total_rows-6, 10, "{ }"); + mvprintw(total_rows-5, 10, " \\ _- -_ /"); + mvprintw(total_rows-4, 10, " ~ \\\\ // ~"); + mvprintw(total_rows-3, 10, " | |"); + mvprintw(total_rows-2, 10, " | |"); + mvprintw(total_rows-1, 10, "______// \\\\"); + + mvprintw(total_rows-9, 40, " _-_"); + mvprintw(total_rows-8, 40, " /~~ ~~\\"); + mvprintw(total_rows-7, 40, " /~~ ~~\\"); + mvprintw(total_rows-6, 40, "{ }"); + mvprintw(total_rows-5, 40, " \\ _- -_ /"); + mvprintw(total_rows-4, 40, " ~ \\\\ // ~"); + mvprintw(total_rows-3, 40, " | |"); + mvprintw(total_rows-2, 40, " | |"); + mvprintw(total_rows-1, 40, "______// \\\\"); + + refresh(); +} + +Vehicle *get_random_vehicle(int y, int x) +{ + Vehicle *car; + car = new Vehicle(rand() % veh_types_max, y, x); + return car; +} + +int main(int argc, char *argv[]) +{ + initscr(); + noecho(); + curs_set(0); + srand(time(0)); + + struct timespec my_timer; + my_timer.tv_sec = 0; + my_timer.tv_nsec = 50000000; // 100000000 + getmaxyx(stdscr, total_rows, total_cols); + + Vehicle *car = 0; + for(;;) + { + clear(); + draw_background(); + + if(!car) + car = get_random_vehicle(total_rows, -1); + + car->MoveRight(); + if(!(car->Draw(total_cols))) { + delete car; + car = 0; + } + + refresh(); + nanosleep(&my_timer, NULL); + } + + endwin(); + return 0; +} diff --git a/veh_models.hpp b/veh_models.hpp new file mode 100644 index 0000000..165715a --- /dev/null +++ b/veh_models.hpp @@ -0,0 +1,135 @@ +#ifndef ASCIIROAD_VEHMODELS_H +#define ASCIIROAD_VEHMODELS_H + +#define SUPPORT_CHAR ' ' + +enum vehicle_types { + veh_default, + veh_ems, + veh_cabriolet, + veh_bus, + veh_zaz, + veh_landrover, + veh_police, + veh_smalltruck, + veh_bigtruck, + veh_bicycle, + veh_manipulator, + veh_camper, + veh_types_max +}; + +struct vehicle_info { + const char * const *model; + int length; + int height; +}; + +/* SUPPORT_CHAR use only for support rectangular array */ +const char * const veh_default_model[] = { + " ______ ", + " /|_||_\\`.__ ", + "( _ _ _\\", + //"(___________\\", + "=`-(_)--(_)-'" +}; + +const char * const veh_ems_model[] = { + "o____________<0> ", + "| |@ \\ ", + "| EMS ||_/_\\__ ", + "|-----------| | ", + "| _ | _ []", + "`-(_)-----------(_)-' " +}; + +const char * const veh_cabriolet_model[] = { + " @ \\ ", + " ______|_/_>____ ", + "/ _\\<> | _ \\ ", + "`-(_)--------(_)-]" +}; + +const char * const veh_bus_model[] = { + " _____________________ ", + "| | | | |@ \\ ", + "|___|_____|_____|_||_/_\\ ", + "| _ _ | |_ \\", + "`--(_)(_)---------(_)---'" +}; + +const char * const veh_zaz_model[] = { + " _____ ", + " ___/__|__\\____ ", + "| _ | _ o)", + "`-(_)------(_)-'" +}; + +const char * const veh_landrover_model[] = { + " ___ ", + " _______|___\\____ ", + "|o _ |- | _ o)", + "`--(_)-------(_)' " +}; + +const char * const veh_police_model[] = { + " ________<o>_ ", + "|___][__][__\\\\____ ", + "|o _ PD |- |_ o)", + "`-(_)--------(_)-' " +}; + +const char * const veh_smalltruck_model[] = { + " __________ ___ ", + "| | |__\\\\_ ", + "| _ _ |-| _ |", + "`-(_)--(_)-' `-(_)-'" +}; + +const char * const veh_bigtruck_model[] = { + " __________ __________ ___ ", + "| | | | |__\\\\_ ", + "| _ _ |-| _ _ |-| _ |", + "`-(_)--(_)-' `-(_)--(_)-' `-(_)-'" +}; + +const char * const veh_bicycle_model[] = { + " @ ", + " / \\> ", + " _I--.\\ ", + "(_))\"(_)" +}; + +const char * const veh_manipulator_model[] = { + " .. ", + " //\\\\ ", + " _ // \\\\", + " _[_]// (_/", + "|_____| ", + "(O_o_O) " +}; + +const char * const veh_camper_model[] = { + " ______________ ", + "| __] ", + "| [] [] [] |_\\__ ", + "| ]", + "`--(_)-------(_)-'" +}; + +const struct vehicle_info veh_info[] = { + {veh_default_model, 13, 4}, + {veh_ems_model, 22, 6}, + {veh_cabriolet_model, 18, 4}, + {veh_bus_model, 25, 5}, + {veh_zaz_model, 16, 4}, + {veh_landrover_model, 18, 4}, + {veh_police_model, 19, 4}, + {veh_smalltruck_model, 20, 4}, + {veh_bigtruck_model, 33, 4}, + {veh_bicycle_model, 8, 4}, + {veh_manipulator_model, 12, 6}, + {veh_camper_model, 18, 5} +}; + +#endif /* ASCIIROAD_VEHMODELS_H */
\ No newline at end of file |