-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen_ncurses.c
148 lines (125 loc) · 2.94 KB
/
screen_ncurses.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdarg.h>
#define NCURSES_WIDECHAR 1
#include <ncurses.h>
#include "main.h"
#include "vm.h"
#include "screen.h"
#define UPPERHALF L"\u2580"
#define LOWERHALF L"\u2584"
#define FULLBLOCK L"\u2588"
uint8_t usecolor = 0;
void nc_init()
{
#ifndef FLOOD_WITH_OPS
initscr();
#endif
savetty();
curs_set(0);
cbreak();
noecho();
nodelay(stdscr, true);
if(has_colors())
{
start_color();
use_default_colors();
init_pair(1, COLOR_BLACK, COLOR_WHITE);
init_pair(2, COLOR_WHITE, -1);
usecolor = 1;
}
}
void nc_end()
{
resetty();
endwin();
}
// TODO: make drawing to ncurses screen more configurable
void nc_draw(vm_t *vm)
{
if(vm->graphicsmode == LORES)
{
for(int i = 0; i < SCREEN_SIZE; i++)
{
int x, y;
if(!itocoord(i, &x, &y, SCREEN_WIDTH, SCREEN_SIZE))
continue;
move(y, x*2);
if(!usecolor)
{
if(!vm->screen[i]) printw(" ");
else printw("00");
}
else
{
if(vm->screen[i]) attron(COLOR_PAIR(1));
addstr(" ");
attroff(COLOR_PAIR(1));
}
}
}
else
{
for(int y = 0; y < SCREEN_HEIGHT_HIRES; y+=2)
{
for(int x = 0; x < SCREEN_WIDTH_HIRES; x++)
{
int i;
if(!coordtoi(&i, x, y, SCREEN_WIDTH_HIRES, SCREEN_HEIGHT_HIRES))
continue;
move(y/2, x);
uint8_t up = vm->screenhr[i];
uint8_t dw = vm->screenhr[i+SCREEN_WIDTH_HIRES];
attron(COLOR_PAIR(2));
if(up && !dw)
{
addwstr(UPPERHALF);
//printw("0");
}
else if(!up && dw)
{
addwstr(LOWERHALF);
//printw("1");
}
else if (up && dw)
{
addwstr(FULLBLOCK);
//printw("2");
}
else
{
printw(" ");
}
attroff(COLOR_PAIR(2));
}
}
}
}
void nc_drawtext(int y, int x, const char *format, ...)
{
va_list args;
move(y, x);
va_start(args, format);
vw_printw(stdscr, format, args);
va_end(args);
}
int getch_bf = ERR;
timing_t getch_buf_cl = 0;
int getch_buf()
{
const timing_t getch_buf_cl_target = hztotiming(GETCH_HZ);
timing_t cl = now();
// FIXME: why is getch_bf == ERR needed here?
if(getch_bf == ERR || cl - getch_buf_cl >= getch_buf_cl_target)
{
getch_buf_cl = cl;
getch_bf = getch();
}
return getch_bf;
}
int nc_input()
{
int ch = getch_buf();
flushinp();
if(ch == ERR)
return NOINP_KEYCODE;
return ch;
}