From 090b6d62acbe03c139f4dcb96d86402c03d25fc3 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Mon, 20 May 2013 16:39:40 -0400 Subject: Start implementing game loop. --- diff --git a/src/game.c b/src/game.c index c6edaf5..9b66d1a 100644 --- a/src/game.c +++ b/src/game.c @@ -5,6 +5,9 @@ #include "player.h" static void draw_paddles(struct game *g); +static void wait(struct game *g); +static void input(struct game *g); +static void draw(struct game *g); struct game * new_game(void) @@ -32,7 +35,53 @@ free_game(struct game *g) } void -draw_game(struct game *g) +run_game(struct game *g) +{ + g->running = 1; + + while (g->running) { + wait(g); + input(g); + draw(g); + } +} + +static void +wait(struct game *g) +{ + g->running = 1; +} + +static void +input(struct game *g) +{ + int c; + + timeout(0); + for (;;) { + c = getch(); + if (c == 'q') { + g->running = 0; + } else if (c == ERR) { + break; + } else if (c == KEY_LEFT) { + g->players[0]->paddle_h.pos -= + g->players[0]->paddle_h.speed; + } else if (c == KEY_RIGHT) { + g->players[0]->paddle_h.pos += + g->players[0]->paddle_h.speed; + } else if (c == KEY_UP) { + g->players[0]->paddle_v.pos -= + g->players[0]->paddle_v.speed; + } else if (c == KEY_DOWN) { + g->players[0]->paddle_v.pos += + g->players[0]->paddle_v.speed; + } + } +} + +static void +draw(struct game *g) { draw_paddles(g); } diff --git a/src/game.h b/src/game.h index f404ff0..9a0f096 100644 --- a/src/game.h +++ b/src/game.h @@ -5,10 +5,11 @@ struct game { struct player *players[2]; + char running; }; struct game *new_game(void); void free_game(struct game *g); -void draw_game(struct game *g); +void run_game(struct game *g); #endif diff --git a/src/main.c b/src/main.c index 10cdf35..408f47b 100644 --- a/src/main.c +++ b/src/main.c @@ -13,8 +13,7 @@ main(void) init_curses(); g = new_game(); - draw_game(g); - getch(); + run_game(g); free_game(g); fini_curses(); @@ -28,6 +27,7 @@ init_curses(void) initscr(); noecho(); raw(); + keypad(stdscr, TRUE); curs_set(0); } -- cgit v0.9.1