tty-solitaire/src/cursor.c

155 lines
4.0 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <errno.h>
#include <ncurses.h>
#include <assert.h>
2011-06-03 05:48:26 +00:00
#include "cursor.h"
2011-06-03 05:48:26 +00:00
#include "game.h"
2011-05-08 23:20:22 +00:00
#include "common.h"
2011-06-06 05:21:50 +00:00
void cursor_malloc(struct cursor **cursor) {
if (!(*cursor = malloc(sizeof(**cursor)))) {
2011-05-08 23:20:22 +00:00
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno);
}
2011-06-03 05:48:26 +00:00
(*cursor)->window = newwin(1, 1, CURSOR_BEGIN_Y, CURSOR_BEGIN_X);
}
2011-06-06 05:21:50 +00:00
void cursor_init(struct cursor *cursor) {
2011-06-03 05:48:26 +00:00
mvwin(cursor->window, CURSOR_BEGIN_Y, CURSOR_BEGIN_X);
2011-05-09 03:38:31 +00:00
cursor->y = CURSOR_BEGIN_Y;
2011-06-03 05:48:26 +00:00
cursor->x = CURSOR_BEGIN_X;
2011-05-31 05:42:10 +00:00
cursor->marked = false;
}
2011-06-06 05:21:50 +00:00
void cursor_free(struct cursor *cursor) {
2011-06-03 05:48:26 +00:00
delwin(cursor->window);
2011-05-09 05:04:38 +00:00
free(cursor);
}
2011-06-06 05:21:50 +00:00
void cursor_mark(struct cursor *cursor) {
2011-05-31 05:42:10 +00:00
cursor->marked = true;
}
2011-06-06 05:21:50 +00:00
void cursor_unmark(struct cursor *cursor) {
2011-05-31 05:42:10 +00:00
cursor->marked = false;
}
2011-06-06 05:21:50 +00:00
void cursor_move(struct cursor *cursor, enum movement movement) {
switch (movement) {
2011-02-06 06:14:17 +00:00
case LEFT:
2011-05-09 03:38:31 +00:00
if (cursor->x > CURSOR_BEGIN_X) {
2011-02-06 06:14:17 +00:00
cursor->x = cursor->x - 8;
2011-05-09 03:38:31 +00:00
if (cursor->y > CURSOR_BEGIN_Y) {
2011-06-06 05:21:50 +00:00
cursor_move(cursor, UP);
cursor_move(cursor, DOWN);
}
2011-02-06 06:14:17 +00:00
}
break;
case DOWN:
2011-05-09 03:38:31 +00:00
if (cursor->y == CURSOR_BEGIN_Y) {
2011-02-06 06:14:17 +00:00
switch (cursor->x - 3) {
2011-05-09 03:38:31 +00:00
case MANEUVRE_0_BEGIN_X:
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[0]);
2011-02-06 06:14:17 +00:00
break;
2011-05-09 03:38:31 +00:00
case MANEUVRE_1_BEGIN_X:
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[1]);
2011-02-06 06:14:17 +00:00
break;
2011-05-09 03:38:31 +00:00
case MANEUVRE_2_BEGIN_X:
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[2]);
2011-02-06 06:14:17 +00:00
break;
2011-05-09 03:38:31 +00:00
case MANEUVRE_3_BEGIN_X:
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[3]);
2011-02-06 06:14:17 +00:00
break;
2011-05-09 03:38:31 +00:00
case MANEUVRE_4_BEGIN_X:
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[4]);
2011-02-06 06:14:17 +00:00
break;
2011-05-09 03:38:31 +00:00
case MANEUVRE_5_BEGIN_X:
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[5]);
2011-02-06 06:14:17 +00:00
break;
2011-05-09 03:38:31 +00:00
case MANEUVRE_6_BEGIN_X:
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[6]);
2011-02-06 06:14:17 +00:00
break;
}
2011-02-06 06:14:17 +00:00
}
break;
case RIGHT:
if (cursor->x < 49) {
cursor->x = cursor->x + 8;
2011-05-09 03:38:31 +00:00
if (cursor->y > CURSOR_BEGIN_Y) {
2011-06-06 05:21:50 +00:00
cursor_move(cursor, UP);
cursor_move(cursor, DOWN);
}
2011-02-06 06:14:17 +00:00
}
break;
case UP:
2011-06-03 05:48:26 +00:00
if (cursor->y > CURSOR_BEGIN_Y) {
2011-05-09 03:38:31 +00:00
cursor->y = CURSOR_BEGIN_Y;
2011-02-06 06:14:17 +00:00
}
break;
}
}
enum movement cursor_direction(int key) {
switch (key) {
case 'h':
case KEY_LEFT:
return(LEFT);
case 'j':
case KEY_DOWN:
return(DOWN);
case 'k':
case KEY_UP:
return(UP);
case 'l':
case KEY_RIGHT:
return(RIGHT);
default:
endwin();
game_end();
assert(false && "invalid cursor direction");
}
}
2011-06-07 01:19:08 +00:00
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));
}