From cba0615b24ac5ce955c09c3ebb8b75c04ff65cbf Mon Sep 17 00:00:00 2001 From: Murilo Pereira Date: Mon, 6 Jun 2011 22:19:08 -0300 Subject: [PATCH] Moved functions to the cursor module. --- src/cursor.c | 40 +++++++++++++++++++++++++++++++++ src/cursor.h | 3 +++ src/keyboard.c | 60 +++++++++----------------------------------------- 3 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/cursor.c b/src/cursor.c index ab1df19..79609a3 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -112,3 +112,43 @@ enum movement cursor_direction(int key) { assert(false && "invalid cursor direction"); } } + +struct stack **cursor_stack(struct cursor *cursor) { + if (cursor->y == CURSOR_BEGIN_Y) { + switch (cursor->x) { + case CURSOR_STOCK_X: return(&(deck->stock)); + case CURSOR_WASTE_PILE_X: return(&(deck->waste_pile)); + case CURSOR_FOUNDATION_0_X: return(&(deck->foundation[0])); + case CURSOR_FOUNDATION_1_X: return(&(deck->foundation[1])); + case CURSOR_FOUNDATION_2_X: return(&(deck->foundation[2])); + case CURSOR_FOUNDATION_3_X: return(&(deck->foundation[3])); + case CURSOR_INVALID_SPOT_X: return(NULL); + default: + endwin(); + game_end(); + assert(false && "invalid stack"); + } + } else { + switch (cursor->x) { + case CURSOR_MANEUVRE_0_X: return(&(deck->maneuvre[0])); + case CURSOR_MANEUVRE_1_X: return(&(deck->maneuvre[1])); + case CURSOR_MANEUVRE_2_X: return(&(deck->maneuvre[2])); + case CURSOR_MANEUVRE_3_X: return(&(deck->maneuvre[3])); + case CURSOR_MANEUVRE_4_X: return(&(deck->maneuvre[4])); + case CURSOR_MANEUVRE_5_X: return(&(deck->maneuvre[5])); + case CURSOR_MANEUVRE_6_X: return(&(deck->maneuvre[6])); + default: + endwin(); + game_end(); + assert(false && "invalid stack"); + } + } +} + +bool cursor_on_stock(struct cursor *cursor) { + return(cursor_stack(cursor) && *cursor_stack(cursor) == deck->stock); +} + +bool cursor_on_invalid_spot(struct cursor *cursor) { + return(!cursor_stack(cursor)); +} diff --git a/src/cursor.h b/src/cursor.h index 6b7c706..2782a90 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -42,5 +42,8 @@ void cursor_mark(struct cursor *); void cursor_unmark(struct cursor *); void cursor_move(struct cursor *, enum movement); enum movement cursor_direction(int); +struct stack **cursor_stack(struct cursor *); +bool cursor_on_stock(struct cursor *); +bool cursor_on_invalid_spot(struct cursor *); #endif diff --git a/src/keyboard.c b/src/keyboard.c index cb3b47d..a74b514 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -8,56 +8,6 @@ #include "cursor.h" #include "draw.h" -static struct stack **cursor_stack(struct cursor *cursor) { - if (cursor->y == CURSOR_BEGIN_Y) { - switch (cursor->x) { - case CURSOR_STOCK_X: return(&(deck->stock)); - case CURSOR_WASTE_PILE_X: return(&(deck->waste_pile)); - case CURSOR_FOUNDATION_0_X: return(&(deck->foundation[0])); - case CURSOR_FOUNDATION_1_X: return(&(deck->foundation[1])); - case CURSOR_FOUNDATION_2_X: return(&(deck->foundation[2])); - case CURSOR_FOUNDATION_3_X: return(&(deck->foundation[3])); - case CURSOR_INVALID_SPOT_X: return(NULL); - default: - endwin(); - game_end(); - assert(false && "invalid stack"); - } - } else { - switch (cursor->x) { - case CURSOR_MANEUVRE_0_X: return(&(deck->maneuvre[0])); - case CURSOR_MANEUVRE_1_X: return(&(deck->maneuvre[1])); - case CURSOR_MANEUVRE_2_X: return(&(deck->maneuvre[2])); - case CURSOR_MANEUVRE_3_X: return(&(deck->maneuvre[3])); - case CURSOR_MANEUVRE_4_X: return(&(deck->maneuvre[4])); - case CURSOR_MANEUVRE_5_X: return(&(deck->maneuvre[5])); - case CURSOR_MANEUVRE_6_X: return(&(deck->maneuvre[6])); - default: - endwin(); - game_end(); - assert(false && "invalid stack"); - } - } -} - -static bool cursor_on_stock(struct cursor *cursor) { - return(cursor_stack(cursor) && *cursor_stack(cursor) == deck->stock); -} - -static bool cursor_on_invalid_spot(struct cursor *cursor) { - return(!cursor_stack(cursor)); -} - -static void handle_stock_event() { - if (!stack_empty(deck->stock)) { - move_card(&(deck->stock), &(deck->waste_pile)); - card_expose(deck->waste_pile->card); - erase_stack(deck->waste_pile); - draw_stack(deck->stock); - draw_stack(deck->waste_pile); - } -} - /* FIXME: this function does not work on stacks with no marked cards. * In that case it returns the stack's length. */ static int marked_cards_count(struct stack *stack) { @@ -83,6 +33,16 @@ static void unmark_cards(struct stack *stack) { } } +static void handle_stock_event() { + if (!stack_empty(deck->stock)) { + move_card(&(deck->stock), &(deck->waste_pile)); + card_expose(deck->waste_pile->card); + erase_stack(deck->waste_pile); + draw_stack(deck->stock); + draw_stack(deck->waste_pile); + } +} + static void handle_card_movement(struct cursor *cursor) { struct stack **origin = cursor_stack(cursor); struct stack **destination;