-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncurses_driver.c
90 lines (78 loc) · 1.82 KB
/
ncurses_driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "game.h"
#include "driver.h"
#include <stdlib.h>
#include <ncurses.h>
#include <unistd.h>
static int init(const Game* game);
static void start(void(*callback)(void*));
static int get_move(void);
static void draw_bg(void);
static void draw_entity(int ent_id);
static void update(void);
Driver ncurses_driver = {
.game = NULL,
.init = init,
.start = start,
.get_move = get_move,
.draw_bg = draw_bg,
.draw_entity = draw_entity,
.update = update
};
#define GAME (ncurses_driver.game)
static char tiles[NCell] = {'-', '#', '_','_',' ',' ',',','o','T'};
static const char* sprites[NSprite] = {"@@@@@@@@@@@@@@@@@@@","EEEEEEEEEEEEEEEEEEE","EEEEEEEEEEEEEEEEEEEE","EEEEEEEEEEEEEEEEEEEE"};
enum { FPS = 15 };
static int init(const Game* game) {
GAME = game;
initscr();
curs_set(0);
noecho();
halfdelay(1);
return 0;
}
static void start(void(*callback)(void*)) {
for(;;) {
callback(&ncurses_driver);
}
}
static int get_move(void) {
int car = getch();
switch(car) {
case 'z':
return Up;
case 'q':
return Left;
case 's':
return Down;
case 'd':
return Right;
case 'a':
return HoleLeft;
case 'e':
return HoleRight;
case 'p':
endwin();
exit(0);
default:
break;
}
return Nothing;
}
static void draw_bg(void) {
int y, x;
for(y = 0; y < GAME->h; ++y) {
for(x = 0; x < GAME->w; ++x) {
int typecell = GAME->background[y * GAME->w + x];
mvprintw(y, x, "%c", tiles[typecell]);
}
}
}
static void draw_entity(int ent_id) {
static int anim[4];
anim[ent_id]=0;
mvprintw(GAME->entity[ent_id].y, GAME->entity[ent_id].x, "%c", sprites[ent_id][4 * GAME->entity[ent_id].dir + anim[ent_id]]);
anim[ent_id] = (anim[ent_id]*NSprite);
}
static void update(void) {
refresh();
}