From 89a2e7ae300a4cabbb308b5ec2b83ee539fdd07c Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Mon, 11 Feb 2019 09:38:50 +0200 Subject: [PATCH] fixes, optional beeps, accept arrow keys for control --- src/game.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/game.h | 1 + src/main.c | 57 ++++++++++++++++++++++++++++++++++-- src/pieces.h | 2 +- 4 files changed, 146 insertions(+), 5 deletions(-) diff --git a/src/game.c b/src/game.c index 0adc772..1ab330b 100644 --- a/src/game.c +++ b/src/game.c @@ -137,11 +137,14 @@ long update(long msec) fprintf(stderr, "stick at row %d col %d\n", pos[0], pos[1]); stick(cur_piece, next_pos); cur_piece = -1; + return 0; } } else { cur_piece = rand() % NUM_PIECES; + prev_rot = cur_rot = 0; fprintf(stderr, "spawn: %d\n", cur_piece); - pos[0] = next_pos[0] = piece_spawnpos[cur_piece][0]; + pos[0] = piece_spawnpos[cur_piece][0]; + next_pos[0] = pos[0] + 1; pos[1] = next_pos[1] = PF_COLS / 2 + piece_spawnpos[cur_piece][1]; } @@ -158,10 +161,88 @@ long update(long msec) return tick_interval - dt; } + +#define C0 0x9b +#define SS3 0x8f + +static void runesc(int csi, char *buf) +{ + if(csi != C0) return; + + if(buf[1] == 0) { + switch(buf[0]) { + case 'A': + game_input('w'); /* up */ + break; + case 'B': + game_input('s'); /* down */ + break; + case 'C': + game_input('d'); /* right */ + break; + case 'D': + game_input('a'); /* left */ + break; + default: + break; + } + } +} + void game_input(int c) { + static int esc, csi; + static int esctop; + static char escbuf[64]; + + if(esc) { + esc = 0; + if(c == 27) { + quit = 1; + return; + } + + switch(c) { + case '[': + csi = C0; + return; + case 'O': + csi = SS3; + return; + default: + break; + } + } + + if(csi) { + if(c < 0x20 || c >= 0x80) { + csi = 0; + esctop = 0; + } + + escbuf[esctop++] = c; + + if(c >= 0x40) { + int prevcsi = csi; + escbuf[esctop] = 0; + csi = 0; + esctop = 0; + runesc(prevcsi, escbuf); + } + return; + } + switch(c) { case 27: + esc = 1; + break; + + case C0: + esc = 1; + csi = C0; + break; + + case 'q': quit = 1; break; @@ -196,6 +277,7 @@ void game_input(int c) break; default: + fprintf(stderr, "unhandled input: %x\n", c); break; } } @@ -228,6 +310,11 @@ static void stick(int piece, const int *pos) scr[y * SCR_COLS + x] = piece + FIRST_PIECE_TILE; } + + if(use_bell) { + putchar('\a'); + fflush(stdout); + } } static void draw_piece(int piece, const int *pos, int rot, int mode) @@ -241,6 +328,8 @@ static void draw_piece(int piece, const int *pos, int rot, int mode) int y = PF_YOFFS + pos[0] + BLKY(*p); p++; + if(y < 0) continue; + ansi_setcursor(y, x * 2); wrtile(tile); } diff --git a/src/game.h b/src/game.h index 14a6620..7687d6f 100644 --- a/src/game.h +++ b/src/game.h @@ -3,6 +3,7 @@ int quit; long tick_interval; +int use_bell; int init_game(void); void cleanup_game(void); diff --git a/src/main.c b/src/main.c index dc240fd..461f27c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -11,6 +12,8 @@ int init(void); void cleanup(void); +int parse_args(int argc, char **argv); +void print_usage(const char *argv0); long get_msec(void); static const char *termfile = "/dev/tty"; @@ -23,8 +26,8 @@ int main(int argc, char **argv) long msec, next; struct timeval tv; - if(argc > 1) { - termfile = argv[1]; + if(parse_args(argc, argv) == -1) { + return 1; } if(init() == -1) { @@ -94,7 +97,7 @@ int init(void) dup(fd); umask(002); - open("ansitris.log", O_WRONLY | O_CREAT, 0664); + open("ansitris.log", O_WRONLY | O_CREAT | O_TRUNC, 0664); if(init_game() == -1) { @@ -110,6 +113,54 @@ void cleanup(void) tcsetattr(0, TCSAFLUSH, &saved_term); } +int parse_args(int argc, char **argv) +{ + int i; + + for(i=1; i: terminal device (default: /dev/tty)\n"); + printf(" -b: use bell for sound ques (default: off)\n"); + printf(" -h: print usage information and exit\n"); +} + long get_msec(void) { struct timeval tv; diff --git a/src/pieces.h b/src/pieces.h index e3a5c54..90be0a0 100644 --- a/src/pieces.h +++ b/src/pieces.h @@ -60,7 +60,7 @@ static unsigned char pieces[NUM_PIECES][4][4] = { }; static int piece_spawnpos[NUM_PIECES][2] = { - {-1, -2}, {-1, -3}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2} + {-2, -2}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2} }; -- 1.7.10.4