From 8e501fd497be3efe2bb93a8383db1bb360a8f69f Mon Sep 17 00:00:00 2001 From: petra Date: Wed, 23 Sep 2020 10:33:53 +0000 Subject: [PATCH] An initial attempt to implement urw-keys --- .gitignore | 3 ++ main.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 .gitignore create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a21c043 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +ntown +*.swp +a.out diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..06d4073 --- /dev/null +++ b/main.cpp @@ -0,0 +1,82 @@ +#include +#include + +enum dir { + N = 0, NE = 1, E = 2, SE = 3, S = 4, SW = 5, W = 6, NW = 7 +}; + +dir rotate(dir initial_d, int rot_dir) { + return (dir)((initial_d + rot_dir) % 8); +} + +int main() { + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + curs_set(0); + int pos_x = 1; + int pos_y = 1; + dir direction = dir::E; + mvaddch(pos_y, pos_x, 'O'); + int ch; + while ((ch = wgetch(stdscr)) != 'q') { + int rotd = 0; + int movval = 0; + switch (ch) { + case KEY_LEFT: + rotd = -1; + break; + case KEY_RIGHT: + rotd = 1; + break; + case KEY_DOWN: + movval = -1; + break; + case KEY_UP: + movval = 1; + break; + default: + break; + }; + direction = rotate(direction, rotd); + switch (direction) { + case dir::N: + pos_x -= movval; + break; + case dir::S: + pos_x += movval; + break; + case dir::W: + pos_y -= movval; + break; + case dir::E: + pos_y += movval; + break; + case dir::NE: + pos_x += movval; + pos_y -= movval; + break; + case dir::NW: + pos_x -= movval; + pos_y -= movval; + break; + case dir::SE: + pos_x += movval; + pos_y += movval; + break; + case dir::SW: + pos_x -= movval; + pos_y += movval; + break; + default: + break; + }; + pos_x = pos_x % COLS; + pos_y = pos_y % LINES; + refresh(); + mvaddch(pos_y, pos_x, 'O'); + } + endwin(); +}