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 "game.h"
#include "common.h" #include "common.h"
void allocate_cursor(struct cursor **cursor) { void cursor_malloc(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);
@ -16,34 +16,34 @@ void allocate_cursor(struct cursor **cursor) {
(*cursor)->window = newwin(1, 1, CURSOR_BEGIN_Y, CURSOR_BEGIN_X); (*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); mvwin(cursor->window, CURSOR_BEGIN_Y, CURSOR_BEGIN_X);
cursor->y = CURSOR_BEGIN_Y; cursor->y = CURSOR_BEGIN_Y;
cursor->x = CURSOR_BEGIN_X; cursor->x = CURSOR_BEGIN_X;
cursor->marked = false; cursor->marked = false;
} }
void free_cursor(struct cursor *cursor) { void cursor_free(struct cursor *cursor) {
delwin(cursor->window); delwin(cursor->window);
free(cursor); free(cursor);
} }
void mark_cursor(struct cursor *cursor) { void cursor_mark(struct cursor *cursor) {
cursor->marked = true; cursor->marked = true;
} }
void unmark_cursor(struct cursor *cursor) { void cursor_unmark(struct cursor *cursor) {
cursor->marked = false; cursor->marked = false;
} }
void move_cursor(struct cursor *cursor, enum movement movement) { void cursor_move(struct cursor *cursor, enum movement movement) {
switch (movement) { switch (movement) {
case LEFT: case LEFT:
if (cursor->x > CURSOR_BEGIN_X) { if (cursor->x > CURSOR_BEGIN_X) {
cursor->x = cursor->x - 8; cursor->x = cursor->x - 8;
if (cursor->y > CURSOR_BEGIN_Y) { if (cursor->y > CURSOR_BEGIN_Y) {
move_cursor(cursor, UP); cursor_move(cursor, UP);
move_cursor(cursor, DOWN); cursor_move(cursor, DOWN);
} }
} }
break; break;
@ -78,8 +78,8 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
if (cursor->x < 49) { if (cursor->x < 49) {
cursor->x = cursor->x + 8; cursor->x = cursor->x + 8;
if (cursor->y > CURSOR_BEGIN_Y) { if (cursor->y > CURSOR_BEGIN_Y) {
move_cursor(cursor, UP); cursor_move(cursor, UP);
move_cursor(cursor, DOWN); cursor_move(cursor, DOWN);
} }
} }
break; break;

View File

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

View File

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

View File

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