2011-02-06 05:44:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2010-04-20 04:10:42 +00:00
|
|
|
#include <malloc.h>
|
2011-02-06 05:44:45 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ncurses.h>
|
2010-04-20 04:10:42 +00:00
|
|
|
#include "display.h"
|
|
|
|
#include "game.h"
|
|
|
|
#include "cursor.h"
|
2011-05-08 23:20:22 +00:00
|
|
|
#include "common.h"
|
2010-04-20 04:10:42 +00:00
|
|
|
|
|
|
|
void allocate_cursor(struct cursor **cursor) {
|
2011-02-06 05:44:45 +00:00
|
|
|
if (!(*cursor = malloc(sizeof(**cursor)))) {
|
2011-05-08 23:20:22 +00:00
|
|
|
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
2011-02-06 05:44:45 +00:00
|
|
|
exit(errno);
|
2011-05-09 05:04:38 +00:00
|
|
|
} else {
|
|
|
|
(*cursor)->window = NULL;
|
2011-02-06 05:44:45 +00:00
|
|
|
}
|
2010-04-20 04:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-31 05:42:10 +00:00
|
|
|
cursor->marked = false;
|
2010-04-20 04:10:42 +00:00
|
|
|
}
|
|
|
|
|
2011-05-09 05:04:38 +00:00
|
|
|
void free_cursor(struct cursor *cursor) {
|
|
|
|
if (cursor) {
|
|
|
|
delwin(cursor->window);
|
|
|
|
}
|
|
|
|
free(cursor);
|
|
|
|
}
|
|
|
|
|
2011-05-31 05:42:10 +00:00
|
|
|
void mark_cursor(struct cursor *cursor) {
|
|
|
|
cursor->marked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unmark_cursor(struct cursor *cursor) {
|
|
|
|
cursor->marked = false;
|
|
|
|
}
|
|
|
|
|
2010-04-20 04:10:42 +00:00
|
|
|
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);
|
2010-04-20 04:10:42 +00:00
|
|
|
}
|
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:
|
2011-05-29 20:12:52 +00:00
|
|
|
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:
|
2011-05-29 20:12:52 +00:00
|
|
|
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:
|
2011-05-29 20:12:52 +00:00
|
|
|
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:
|
2011-05-29 20:12:52 +00:00
|
|
|
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:
|
2011-05-29 20:12:52 +00:00
|
|
|
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:
|
2011-05-29 20:12:52 +00:00
|
|
|
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:
|
2011-05-29 20:12:52 +00:00
|
|
|
cursor->y = cursor->y + 7 + length(deck->maneuvre[6]);
|
2011-02-06 06:14:17 +00:00
|
|
|
break;
|
2010-04-20 04:10:42 +00:00
|
|
|
}
|
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);
|
2010-04-20 04:10:42 +00:00
|
|
|
}
|
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;
|
2010-04-20 04:10:42 +00:00
|
|
|
}
|
|
|
|
}
|