Give cursor its own WINDOW.

This commit is contained in:
Murilo Pereira 2011-05-09 02:04:38 -03:00
parent e70a87feac
commit 6a08aad3d3
5 changed files with 24 additions and 11 deletions

View File

@ -13,14 +13,24 @@ void allocate_cursor(struct cursor **cursor) {
if (!(*cursor = malloc(sizeof(**cursor)))) { if (!(*cursor = malloc(sizeof(**cursor)))) {
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno); exit(errno);
} else {
(*cursor)->window = NULL;
} }
} }
void initialize_cursor(struct cursor *cursor) { void initialize_cursor(struct cursor *cursor) {
cursor->window = newwin(0, 0, cursor->y, cursor->x);
cursor->x = CURSOR_BEGIN_X; cursor->x = CURSOR_BEGIN_X;
cursor->y = CURSOR_BEGIN_Y; 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) { void move_cursor(struct cursor *cursor, enum movement movement) {
switch (movement) { switch (movement) {
case LEFT: case LEFT:
@ -31,7 +41,6 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
move_cursor(cursor, UP); move_cursor(cursor, UP);
move_cursor(cursor, DOWN); move_cursor(cursor, DOWN);
} }
draw_cursor(cursor);
} }
break; break;
case DOWN: case DOWN:
@ -60,7 +69,6 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
cursor->y = cursor->y + 7 + length(deck->maneuvre_6); cursor->y = cursor->y + 7 + length(deck->maneuvre_6);
break; break;
} }
draw_cursor(cursor);
} }
break; break;
case RIGHT: case RIGHT:
@ -71,20 +79,13 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
move_cursor(cursor, UP); move_cursor(cursor, UP);
move_cursor(cursor, DOWN); move_cursor(cursor, DOWN);
} }
draw_cursor(cursor);
} }
break; break;
case UP: case UP:
if (cursor->y > 1) { if (cursor->y > 1) {
erase_cursor(cursor); erase_cursor(cursor);
cursor->y = CURSOR_BEGIN_Y; cursor->y = CURSOR_BEGIN_Y;
draw_cursor(cursor);
} }
break; break;
} }
/* this is needed because of the screen glitch that moving the cursor
* on the maneuvre's stacks causes */
refresh();
draw_deck(deck);
} }

View File

@ -25,6 +25,7 @@
#define CURSOR_MANEUVRE_6_X 52 #define CURSOR_MANEUVRE_6_X 52
struct cursor { struct cursor {
WINDOW *window;
int x; int x;
int y; int y;
}; };

View File

@ -156,9 +156,10 @@ void draw_deck(struct deck *deck) {
} }
void draw_cursor(struct cursor *cursor) { 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) { void erase_cursor(struct cursor *cursor) {
mvdelch(cursor->y, cursor->x); mvwdelch(cursor->window, cursor->y, cursor->x);
} }

View File

@ -291,4 +291,5 @@ void initialize_game() {
void end_game() { void end_game() {
free_deck(deck); free_deck(deck);
free_cursor(cursor);
} }

View File

@ -72,18 +72,22 @@ static void handle_card_movement(struct cursor *cursor) {
case 'h': case 'h':
case KEY_LEFT: case KEY_LEFT:
move_cursor(cursor, LEFT); move_cursor(cursor, LEFT);
draw_cursor(cursor);
break; break;
case 'j': case 'j':
case KEY_DOWN: case KEY_DOWN:
move_cursor(cursor, DOWN); move_cursor(cursor, DOWN);
draw_cursor(cursor);
break; break;
case 'k': case 'k':
case KEY_UP: case KEY_UP:
move_cursor(cursor, UP); move_cursor(cursor, UP);
draw_cursor(cursor);
break; break;
case 'l': case 'l':
case KEY_RIGHT: case KEY_RIGHT:
move_cursor(cursor, RIGHT); move_cursor(cursor, RIGHT);
draw_cursor(cursor);
break; break;
case KEY_SPACEBAR: case KEY_SPACEBAR:
destination = cursor_stack(cursor); destination = cursor_stack(cursor);
@ -96,6 +100,7 @@ static void handle_card_movement(struct cursor *cursor) {
case 'q': case 'q':
case 'Q': case 'Q':
end_curses(); end_curses();
end_game();
exit(0); exit(0);
} }
} }
@ -106,18 +111,22 @@ void handle_keyboard_event(int key) {
case 'h': case 'h':
case KEY_LEFT: case KEY_LEFT:
move_cursor(cursor, LEFT); move_cursor(cursor, LEFT);
draw_cursor(cursor);
break; break;
case 'j': case 'j':
case KEY_DOWN: case KEY_DOWN:
move_cursor(cursor, DOWN); move_cursor(cursor, DOWN);
draw_cursor(cursor);
break; break;
case 'k': case 'k':
case KEY_UP: case KEY_UP:
move_cursor(cursor, UP); move_cursor(cursor, UP);
draw_cursor(cursor);
break; break;
case 'l': case 'l':
case KEY_RIGHT: case KEY_RIGHT:
move_cursor(cursor, RIGHT); move_cursor(cursor, RIGHT);
draw_cursor(cursor);
break; break;
case KEY_SPACEBAR: case KEY_SPACEBAR:
if (cursor_on_stock(cursor)) { if (cursor_on_stock(cursor)) {