From 4c9336f944251cca1292e73a6cebf8de77b1217a Mon Sep 17 00:00:00 2001 From: Murilo Soares Pereira Date: Tue, 20 Apr 2010 01:10:42 -0300 Subject: [PATCH] Created the cursor object, and it moves! --- lib/cursor.c | 102 ++++++++++++++++++++++++++++++++++++++++++++ lib/cursor.h | 21 +++++++++ lib/game.c | 2 - lib/game.h | 2 + src/tty-solitaire.c | 31 +++++++++++++- 5 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 lib/cursor.c create mode 100644 lib/cursor.h diff --git a/lib/cursor.c b/lib/cursor.c new file mode 100644 index 0000000..a9c07db --- /dev/null +++ b/lib/cursor.c @@ -0,0 +1,102 @@ +#include +#include +#include "stack.h" +#include "deck.h" +#include "display.h" +#include "game.h" +#include "cursor.h" + +void allocate_cursor(struct cursor **cursor) { + *cursor = malloc(sizeof(**cursor)); + + return; +} + +void initialize_cursor(struct cursor *cursor) { + cursor->x = CURSOR_STARTING_X; + cursor->y = CURSOR_STARTING_Y; + + return; +} + +void draw_cursor(struct cursor *cursor) { + mvaddch(cursor->y, cursor->x, '*'); + + return; +} + +void erase_cursor(struct cursor *cursor) { + mvdelch(cursor->y, cursor->x); + + return; +} + +void move_cursor(struct cursor *cursor, enum movement movement) { + switch (movement) { + case LEFT: + if (cursor->x > CURSOR_STARTING_X) { + erase_cursor(cursor); + cursor->x = cursor->x - 8; + if (cursor->y > CURSOR_STARTING_Y) { + move_cursor(cursor, UP); + move_cursor(cursor, DOWN); + } + draw_cursor(cursor); + } + break; + case DOWN: + if (cursor->y == CURSOR_STARTING_Y) { + erase_cursor(cursor); + switch (cursor->x - 3) { + case MANEUVRE_0_STARTING_X: + cursor->y = cursor->y + 7 + length(deck->maneuvre_0); + break; + case MANEUVRE_1_STARTING_X: + cursor->y = cursor->y + 7 + length(deck->maneuvre_1); + break; + case MANEUVRE_2_STARTING_X: + cursor->y = cursor->y + 7 + length(deck->maneuvre_2); + break; + case MANEUVRE_3_STARTING_X: + cursor->y = cursor->y + 7 + length(deck->maneuvre_3); + break; + case MANEUVRE_4_STARTING_X: + cursor->y = cursor->y + 7 + length(deck->maneuvre_4); + break; + case MANEUVRE_5_STARTING_X: + cursor->y = cursor->y + 7 + length(deck->maneuvre_5); + break; + case MANEUVRE_6_STARTING_X: + cursor->y = cursor->y + 7 + length(deck->maneuvre_6); + break; + } + draw_cursor(cursor); + } + break; + case RIGHT: + if (cursor->x < 49) { + erase_cursor(cursor); + cursor->x = cursor->x + 8; + if (cursor->y > CURSOR_STARTING_Y) { + move_cursor(cursor, UP); + move_cursor(cursor, DOWN); + } + draw_cursor(cursor); + } + break; + case UP: + if (cursor->y > 1) { + erase_cursor(cursor); + cursor->y = CURSOR_STARTING_Y; + draw_cursor(cursor); + } + break; + } + + /* this is needed because of the screen glitch that moving the cursor + * on the maneuvre's stacks causes */ + refresh(); + draw_game(deck); + + return; +} diff --git a/lib/cursor.h b/lib/cursor.h new file mode 100644 index 0000000..80d08b4 --- /dev/null +++ b/lib/cursor.h @@ -0,0 +1,21 @@ +#ifndef CURSOR_H +#define CURSOR_H + +#define CURSOR_STARTING_X 4 +#define CURSOR_STARTING_Y 7 + +struct cursor { + int x; + int y; +}; + +enum movement { LEFT, DOWN, UP, RIGHT }; + +extern struct deck *deck; + +void allocate_cursor(struct cursor **); +void initialize_cursor(struct cursor *); +void draw_cursor(struct cursor *); +void move_cursor(struct cursor *, enum movement); + +#endif diff --git a/lib/game.c b/lib/game.c index f4ad7fe..2693b09 100644 --- a/lib/game.c +++ b/lib/game.c @@ -208,8 +208,6 @@ void prepare_game(struct deck **deck) { } void initialize_game() { - struct deck *deck = NULL; - clear_screen(); prepare_game(&deck); draw_game(deck); diff --git a/lib/game.h b/lib/game.h index 9359d51..c2c26fb 100644 --- a/lib/game.h +++ b/lib/game.h @@ -26,6 +26,8 @@ #define MANEUVRE_5_STARTING_X 41 #define MANEUVRE_6_STARTING_X 49 +struct deck *deck; + void set_stacks_coordinates(struct deck *); void fill_deck(struct deck *); void shuffle_deck(struct deck *); diff --git a/src/tty-solitaire.c b/src/tty-solitaire.c index 12d3ef7..dc92450 100644 --- a/src/tty-solitaire.c +++ b/src/tty-solitaire.c @@ -1,16 +1,18 @@ #include #include "../lib/util.h" #include "../lib/game.h" +#include "../lib/cursor.h" #include "../lib/keyboard.h" int main(int argc, const char *argv[]) { int option; + struct cursor *cursor; initialize_curses(); greet_player(); - while (1) { + while (option != KEY_SPACEBAR) { switch (option = getch()) { case KEY_SPACEBAR: initialize_game(); @@ -22,8 +24,33 @@ int main(int argc, const char *argv[]) { } } + allocate_cursor(&cursor); + initialize_cursor(cursor); + draw_cursor(cursor); + while (1) { - key_event(); + switch (option = getch()) { + case 'h': + case KEY_LEFT: + move_cursor(cursor, LEFT); + break; + case 'j': + case KEY_DOWN: + move_cursor(cursor, DOWN); + break; + case 'k': + case KEY_UP: + move_cursor(cursor, UP); + break; + case 'l': + case KEY_RIGHT: + move_cursor(cursor, RIGHT); + break; + case 'q': + case 'Q': + end_curses(); + exit(0); + } } return(0);