tty-solitaire/lib/cursor.c

92 lines
2.2 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);
2011-05-09 05:04:38 +00:00
} else {
(*cursor)->window = NULL;
}
}
void initialize_cursor(struct cursor *cursor) {
2011-05-09 05:04:38 +00:00
cursor->window = newwin(0, 0, cursor->y, cursor->x);
2011-05-09 03:38:31 +00:00
cursor->x = CURSOR_BEGIN_X;
cursor->y = CURSOR_BEGIN_Y;
}
2011-05-09 05:04:38 +00:00
void free_cursor(struct cursor *cursor) {
if (cursor) {
delwin(cursor->window);
}
free(cursor);
}
void move_cursor(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
erase_cursor(cursor);
cursor->x = cursor->x - 8;
2011-05-09 03:38:31 +00:00
if (cursor->y > CURSOR_BEGIN_Y) {
2011-02-06 06:14:17 +00:00
move_cursor(cursor, UP);
move_cursor(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
erase_cursor(cursor);
switch (cursor->x - 3) {
2011-05-09 03:38:31 +00:00
case MANEUVRE_0_BEGIN_X:
cursor->y = cursor->y + 7 + 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 + 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 + 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 + 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 + 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 + 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 + 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) {
erase_cursor(cursor);
cursor->x = cursor->x + 8;
2011-05-09 03:38:31 +00:00
if (cursor->y > CURSOR_BEGIN_Y) {
2011-02-06 06:14:17 +00:00
move_cursor(cursor, UP);
move_cursor(cursor, DOWN);
}
2011-02-06 06:14:17 +00:00
}
break;
case UP:
if (cursor->y > 1) {
erase_cursor(cursor);
2011-05-09 03:38:31 +00:00
cursor->y = CURSOR_BEGIN_Y;
2011-02-06 06:14:17 +00:00
}
break;
}
}