summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pjm@nac.net>2013-05-20 16:39:40 (EDT)
committer P. J. McDermott <pjm@nac.net>2013-05-20 16:41:22 (EDT)
commit090b6d62acbe03c139f4dcb96d86402c03d25fc3 (patch)
tree14e731fbb8c094fbbc76468f3a2b7b014f6841ed
parent1f4b8a236c39ae2fc0bc702943baf4c3f6953607 (diff)
downloadcursespong-090b6d62acbe03c139f4dcb96d86402c03d25fc3.zip
cursespong-090b6d62acbe03c139f4dcb96d86402c03d25fc3.tar.gz
cursespong-090b6d62acbe03c139f4dcb96d86402c03d25fc3.tar.bz2
Start implementing game loop.
-rw-r--r--src/game.c51
-rw-r--r--src/game.h3
-rw-r--r--src/main.c4
3 files changed, 54 insertions, 4 deletions
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);
}