tty-solitaire/lib/cursor.c

91 lines
2.3 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <errno.h>
#include <ncurses.h>
#include "display.h"
#include "game.h"
#include "cursor.h"
2011-05-08 23:20:22 +00:00
#include "common.h"
void allocate_cursor(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);
}
}
void initialize_cursor(struct cursor *cursor) {
cursor->x = CURSOR_STARTING_X;
cursor->y = CURSOR_STARTING_Y;
}
void move_cursor(struct cursor *cursor, enum movement movement) {
switch (movement) {
2011-02-06 06:14:17 +00:00
case LEFT:
if (cursor->x > CURSOR_STARTING_X) {
erase_cursor(cursor);
cursor->x = cursor->x - 8;
if (cursor->y > CURSOR_STARTING_Y) {
move_cursor(cursor, UP);
move_cursor(cursor, DOWN);
}
2011-02-06 06:14:17 +00:00
draw_cursor(cursor);
}
break;
case DOWN:
if (cursor->y == CURSOR_STARTING_Y) {
erase_cursor(cursor);
switch (cursor->x - 3) {
case MANEUVRE_0_STARTING_X:
cursor->y = cursor->y + 7 + length(deck->maneuvre_0);
break;
case MANEUVRE_1_STARTING_X:
cursor->y = cursor->y + 7 + length(deck->maneuvre_1);
break;
case MANEUVRE_2_STARTING_X:
cursor->y = cursor->y + 7 + length(deck->maneuvre_2);
break;
case MANEUVRE_3_STARTING_X:
cursor->y = cursor->y + 7 + length(deck->maneuvre_3);
break;
case MANEUVRE_4_STARTING_X:
cursor->y = cursor->y + 7 + length(deck->maneuvre_4);
break;
case MANEUVRE_5_STARTING_X:
cursor->y = cursor->y + 7 + length(deck->maneuvre_5);
break;
case MANEUVRE_6_STARTING_X:
cursor->y = cursor->y + 7 + length(deck->maneuvre_6);
break;
}
2011-02-06 06:14:17 +00:00
draw_cursor(cursor);
}
break;
case RIGHT:
if (cursor->x < 49) {
erase_cursor(cursor);
cursor->x = cursor->x + 8;
if (cursor->y > CURSOR_STARTING_Y) {
move_cursor(cursor, UP);
move_cursor(cursor, DOWN);
}
2011-02-06 06:14:17 +00:00
draw_cursor(cursor);
}
break;
case UP:
if (cursor->y > 1) {
erase_cursor(cursor);
cursor->y = CURSOR_STARTING_Y;
draw_cursor(cursor);
}
break;
}
/* this is needed because of the screen glitch that moving the cursor
* on the maneuvre's stacks causes */
refresh();
2011-02-06 02:22:16 +00:00
draw_deck(deck);
}