Name-spaced functions for cursor.

This commit is contained in:
Murilo Pereira 2011-06-06 02:21:50 -03:00
parent 955f2fbc90
commit 6b709f047a
4 changed files with 30 additions and 30 deletions

View File

@ -8,7 +8,7 @@
#include "game.h"
#include "common.h"
void allocate_cursor(struct cursor **cursor) {
void cursor_malloc(struct cursor **cursor) {
if (!(*cursor = malloc(sizeof(**cursor)))) {
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno);
@ -16,34 +16,34 @@ void allocate_cursor(struct cursor **cursor) {
(*cursor)->window = newwin(1, 1, CURSOR_BEGIN_Y, CURSOR_BEGIN_X);
}
void initialize_cursor(struct cursor *cursor) {
void cursor_init(struct cursor *cursor) {
mvwin(cursor->window, CURSOR_BEGIN_Y, CURSOR_BEGIN_X);
cursor->y = CURSOR_BEGIN_Y;
cursor->x = CURSOR_BEGIN_X;
cursor->marked = false;
}
void free_cursor(struct cursor *cursor) {
void cursor_free(struct cursor *cursor) {
delwin(cursor->window);
free(cursor);
}
void mark_cursor(struct cursor *cursor) {
void cursor_mark(struct cursor *cursor) {
cursor->marked = true;
}
void unmark_cursor(struct cursor *cursor) {
void cursor_unmark(struct cursor *cursor) {
cursor->marked = false;
}
void move_cursor(struct cursor *cursor, enum movement movement) {
void cursor_move(struct cursor *cursor, enum movement movement) {
switch (movement) {
case LEFT:
if (cursor->x > CURSOR_BEGIN_X) {
cursor->x = cursor->x - 8;
if (cursor->y > CURSOR_BEGIN_Y) {
move_cursor(cursor, UP);
move_cursor(cursor, DOWN);
cursor_move(cursor, UP);
cursor_move(cursor, DOWN);
}
}
break;
@ -78,8 +78,8 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
if (cursor->x < 49) {
cursor->x = cursor->x + 8;
if (cursor->y > CURSOR_BEGIN_Y) {
move_cursor(cursor, UP);
move_cursor(cursor, DOWN);
cursor_move(cursor, UP);
cursor_move(cursor, DOWN);
}
}
break;

View File

@ -35,11 +35,11 @@ enum movement { LEFT, DOWN, UP, RIGHT };
extern struct deck *deck;
void allocate_cursor(struct cursor **);
void initialize_cursor(struct cursor *);
void free_cursor(struct cursor *);
void mark_cursor(struct cursor *);
void unmark_cursor(struct cursor *);
void move_cursor(struct cursor *, enum movement);
void cursor_malloc(struct cursor **);
void cursor_init(struct cursor *);
void cursor_free(struct cursor *);
void cursor_mark(struct cursor *);
void cursor_unmark(struct cursor *);
void cursor_move(struct cursor *, enum movement);
#endif

View File

@ -178,8 +178,8 @@ static void deal_cards(struct deck *deck) {
}
void game_init() {
allocate_cursor(&cursor);
initialize_cursor(cursor);
cursor_malloc(&cursor);
cursor_init(cursor);
deck_malloc(&deck);
deck_init(deck);
@ -202,7 +202,7 @@ void game_init() {
}
void game_end() {
free_cursor(cursor);
cursor_free(cursor);
deck_free(deck);
}

View File

@ -99,7 +99,7 @@ static void handle_card_movement(struct cursor *cursor) {
cursor->y++;
}
erase_cursor(cursor);
mark_cursor(cursor);
cursor_mark(cursor);
draw_cursor(cursor);
while (1) {
@ -107,25 +107,25 @@ static void handle_card_movement(struct cursor *cursor) {
case 'h':
case KEY_LEFT:
erase_cursor(cursor);
move_cursor(cursor, LEFT);
cursor_move(cursor, LEFT);
draw_cursor(cursor);
break;
case 'j':
case KEY_DOWN:
erase_cursor(cursor);
move_cursor(cursor, DOWN);
cursor_move(cursor, DOWN);
draw_cursor(cursor);
break;
case 'k':
case KEY_UP:
erase_cursor(cursor);
move_cursor(cursor, UP);
cursor_move(cursor, UP);
draw_cursor(cursor);
break;
case 'l':
case KEY_RIGHT:
erase_cursor(cursor);
move_cursor(cursor, RIGHT);
cursor_move(cursor, RIGHT);
draw_cursor(cursor);
break;
case 'm':
@ -197,7 +197,7 @@ static void handle_card_movement(struct cursor *cursor) {
cursor->y--;
}
}
unmark_cursor(cursor);
cursor_unmark(cursor);
draw_cursor(cursor);
return;
case KEY_ESCAPE:
@ -211,7 +211,7 @@ static void handle_card_movement(struct cursor *cursor) {
draw_stack(*origin);
}
if (cursor->marked) {
unmark_cursor(cursor);
cursor_unmark(cursor);
draw_cursor(cursor);
}
return;
@ -229,25 +229,25 @@ void handle_keyboard_event(int key) {
case 'h':
case KEY_LEFT:
erase_cursor(cursor);
move_cursor(cursor, LEFT);
cursor_move(cursor, LEFT);
draw_cursor(cursor);
break;
case 'j':
case KEY_DOWN:
erase_cursor(cursor);
move_cursor(cursor, DOWN);
cursor_move(cursor, DOWN);
draw_cursor(cursor);
break;
case 'k':
case KEY_UP:
erase_cursor(cursor);
move_cursor(cursor, UP);
cursor_move(cursor, UP);
draw_cursor(cursor);
break;
case 'l':
case KEY_RIGHT:
erase_cursor(cursor);
move_cursor(cursor, RIGHT);
cursor_move(cursor, RIGHT);
draw_cursor(cursor);
break;
case KEY_SPACEBAR: