From 6a08aad3d38f75ae360c26ce7640d02efd2cf725 Mon Sep 17 00:00:00 2001 From: Murilo Pereira Date: Mon, 9 May 2011 02:04:38 -0300 Subject: [PATCH] Give cursor its own WINDOW. --- lib/cursor.c | 19 ++++++++++--------- lib/cursor.h | 1 + lib/display.c | 5 +++-- lib/game.c | 1 + lib/keyboard.c | 9 +++++++++ 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/cursor.c b/lib/cursor.c index 081f88c..d04bbef 100644 --- a/lib/cursor.c +++ b/lib/cursor.c @@ -13,14 +13,24 @@ void allocate_cursor(struct cursor **cursor) { if (!(*cursor = malloc(sizeof(**cursor)))) { fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); exit(errno); + } else { + (*cursor)->window = NULL; } } void initialize_cursor(struct cursor *cursor) { + cursor->window = newwin(0, 0, cursor->y, cursor->x); cursor->x = CURSOR_BEGIN_X; cursor->y = CURSOR_BEGIN_Y; } +void free_cursor(struct cursor *cursor) { + if (cursor) { + delwin(cursor->window); + } + free(cursor); +} + void move_cursor(struct cursor *cursor, enum movement movement) { switch (movement) { case LEFT: @@ -31,7 +41,6 @@ void move_cursor(struct cursor *cursor, enum movement movement) { move_cursor(cursor, UP); move_cursor(cursor, DOWN); } - draw_cursor(cursor); } break; case DOWN: @@ -60,7 +69,6 @@ void move_cursor(struct cursor *cursor, enum movement movement) { cursor->y = cursor->y + 7 + length(deck->maneuvre_6); break; } - draw_cursor(cursor); } break; case RIGHT: @@ -71,20 +79,13 @@ void move_cursor(struct cursor *cursor, enum movement movement) { move_cursor(cursor, UP); move_cursor(cursor, DOWN); } - draw_cursor(cursor); } break; case UP: if (cursor->y > 1) { erase_cursor(cursor); cursor->y = CURSOR_BEGIN_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_deck(deck); } diff --git a/lib/cursor.h b/lib/cursor.h index 58208b5..0cf5fed 100644 --- a/lib/cursor.h +++ b/lib/cursor.h @@ -25,6 +25,7 @@ #define CURSOR_MANEUVRE_6_X 52 struct cursor { + WINDOW *window; int x; int y; }; diff --git a/lib/display.c b/lib/display.c index 2077eb8..7a044d1 100644 --- a/lib/display.c +++ b/lib/display.c @@ -156,9 +156,10 @@ void draw_deck(struct deck *deck) { } void draw_cursor(struct cursor *cursor) { - mvaddch(cursor->y, cursor->x, '*'); + mvwaddch(cursor->window, cursor->y, cursor->x, '*'); + wrefresh(cursor->window); } void erase_cursor(struct cursor *cursor) { - mvdelch(cursor->y, cursor->x); + mvwdelch(cursor->window, cursor->y, cursor->x); } diff --git a/lib/game.c b/lib/game.c index a387478..fafe1ac 100644 --- a/lib/game.c +++ b/lib/game.c @@ -291,4 +291,5 @@ void initialize_game() { void end_game() { free_deck(deck); + free_cursor(cursor); } diff --git a/lib/keyboard.c b/lib/keyboard.c index f8ae58e..5cfc3f0 100644 --- a/lib/keyboard.c +++ b/lib/keyboard.c @@ -72,18 +72,22 @@ static void handle_card_movement(struct cursor *cursor) { case 'h': case KEY_LEFT: move_cursor(cursor, LEFT); + draw_cursor(cursor); break; case 'j': case KEY_DOWN: move_cursor(cursor, DOWN); + draw_cursor(cursor); break; case 'k': case KEY_UP: move_cursor(cursor, UP); + draw_cursor(cursor); break; case 'l': case KEY_RIGHT: move_cursor(cursor, RIGHT); + draw_cursor(cursor); break; case KEY_SPACEBAR: destination = cursor_stack(cursor); @@ -96,6 +100,7 @@ static void handle_card_movement(struct cursor *cursor) { case 'q': case 'Q': end_curses(); + end_game(); exit(0); } } @@ -106,18 +111,22 @@ void handle_keyboard_event(int key) { case 'h': case KEY_LEFT: move_cursor(cursor, LEFT); + draw_cursor(cursor); break; case 'j': case KEY_DOWN: move_cursor(cursor, DOWN); + draw_cursor(cursor); break; case 'k': case KEY_UP: move_cursor(cursor, UP); + draw_cursor(cursor); break; case 'l': case KEY_RIGHT: move_cursor(cursor, RIGHT); + draw_cursor(cursor); break; case KEY_SPACEBAR: if (cursor_on_stock(cursor)) {