-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsdlgui.c
More file actions
285 lines (231 loc) · 6.42 KB
/
sdlgui.c
File metadata and controls
285 lines (231 loc) · 6.42 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "terminal.h"
#include "eia.h"
#ifdef COL_132
#define SDLGUI_WIDTH 1452
#else
#define SDLGUI_WIDTH 880
#endif
#define SDLGUI_HEIGHT 240
#define CHAR_WIDTH 11
#define CHAR_HEIGHT 10
extern uint8_t _binary_char_rom_start[];
static SDL_Window *sdlgui_window = NULL;
static SDL_Renderer *sdlgui_renderer = NULL;
static SDL_Texture *sdlgui_texture = NULL;
static SDL_PixelFormat *sdlgui_pixel_format = NULL;
static Uint32 *sdlgui_pixels = NULL;
static int sdlgui_pixel_pitch = 0;
static Uint32 sdlgui_ticks = 0;
static void sdlgui_exit_handler(void)
{
if (sdlgui_pixel_format != NULL) {
SDL_FreeFormat(sdlgui_pixel_format);
}
if (sdlgui_texture != NULL) {
SDL_UnlockTexture(sdlgui_texture);
SDL_DestroyTexture(sdlgui_texture);
}
if (sdlgui_renderer != NULL) {
SDL_DestroyRenderer(sdlgui_renderer);
}
if (sdlgui_window != NULL) {
SDL_DestroyWindow(sdlgui_window);
}
SDL_Quit();
}
int sdlgui_init(void)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Unable to initalize SDL: %s\n", SDL_GetError());
return -1;
}
atexit(sdlgui_exit_handler);
if ((sdlgui_window = SDL_CreateWindow("Terminominal",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SDLGUI_WIDTH, SDLGUI_HEIGHT, 0)) == NULL) {
fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
return -1;
}
if ((sdlgui_renderer = SDL_CreateRenderer(sdlgui_window, -1, 0)) == NULL) {
fprintf(stderr, "Unable to create renderer: %s\n", SDL_GetError());
return -1;
}
if ((sdlgui_texture = SDL_CreateTexture(sdlgui_renderer,
SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
SDLGUI_WIDTH, SDLGUI_HEIGHT)) == NULL) {
fprintf(stderr, "Unable to create texture: %s\n", SDL_GetError());
return -1;
}
if (SDL_LockTexture(sdlgui_texture, NULL,
(void **)&sdlgui_pixels, &sdlgui_pixel_pitch) != 0) {
fprintf(stderr, "Unable to lock texture: %s\n", SDL_GetError());
return -1;
}
if ((sdlgui_pixel_format = SDL_AllocFormat(SDL_PIXELFORMAT_ARGB8888))
== NULL) {
fprintf(stderr, "Unable to create pixel format: %s\n", SDL_GetError());
return -1;
}
return 0;
}
static inline void sdlgui_set_pixel(uint16_t y, uint16_t x, uint8_t shade)
{
sdlgui_pixels[(y * SDLGUI_WIDTH) + x] =
SDL_MapRGB(sdlgui_pixel_format, shade, shade, shade);
}
static inline uint8_t sdlgui_shade(bool on, terminal_char_t c)
{
if (on ^ ((c.attribute >> TERMINAL_ATTRIBUTE_REVERSE) & 0x1)) {
if (((c.attribute >> TERMINAL_ATTRIBUTE_BLINK) & 0x1) &&
((sdlgui_ticks % 1000) > 500)) {
return 0x0;
} else {
if ((c.attribute >> TERMINAL_ATTRIBUTE_BOLD) & 0x1) {
return 0x7F;
} else {
return 0xFF;
}
}
} else {
if (((c.attribute >> TERMINAL_ATTRIBUTE_REVERSE) & 0x1) &&
(((c.attribute >> TERMINAL_ATTRIBUTE_BLINK) & 0x1) &&
((sdlgui_ticks % 1000) > 500))) {
if ((c.attribute >> TERMINAL_ATTRIBUTE_BOLD) & 0x1) {
return 0x7F;
} else {
return 0xFF;
}
} else {
return 0x0;
}
}
}
static inline void sdlgui_char(uint8_t row, uint8_t col, terminal_char_t c)
{
int y, x;
uint8_t char_data;
int offset;
bool on;
for (y = 0; y < CHAR_HEIGHT; y++) {
offset = (c.byte * CHAR_HEIGHT * 2) + (y * 2);
char_data = _binary_char_rom_start[offset];
for (x = 0; x < 8; x++) {
if (((c.attribute >> TERMINAL_ATTRIBUTE_UNDERLINE) & 0x1)
&& y == (CHAR_HEIGHT - 1)) {
on = true;
} else {
on = (char_data >> x) & 0x1;
}
sdlgui_set_pixel((row * CHAR_HEIGHT) + y,
(col * CHAR_WIDTH) + (7 - x),
sdlgui_shade(on, c));
}
char_data = _binary_char_rom_start[offset + 1];
for (x = 0; x < 3; x++) {
if (((c.attribute >> TERMINAL_ATTRIBUTE_UNDERLINE) & 0x1)
&& y == (CHAR_HEIGHT - 1)) {
on = true;
} else {
on = (char_data >> x) & 0x1;
}
sdlgui_set_pixel((row * CHAR_HEIGHT) + y,
(col * CHAR_WIDTH) + (2 - x) + 8,
sdlgui_shade(on, c));
}
}
}
void sdlgui_update(void)
{
int row, col;
SDL_Event event;
SDL_Keymod keymod;
while (SDL_PollEvent(&event) == 1) {
switch (event.type) {
case SDL_QUIT:
exit(0);
break;
case SDL_TEXTINPUT:
eia_send(event.text.text[0] & 0xFF);
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_RETURN:
eia_send('\n');
break;
case SDLK_ESCAPE:
eia_send(0x1B);
break;
case SDLK_BACKSPACE:
eia_send(0x08);
break;
case SDLK_TAB:
eia_send(0x09);
break;
case SDLK_UP:
eia_send(0x1B);
if (terminal_cursor_key_code() != 0) {
eia_send(terminal_cursor_key_code());
}
eia_send('A');
break;
case SDLK_LEFT:
eia_send(0x1B);
if (terminal_cursor_key_code() != 0) {
eia_send(terminal_cursor_key_code());
}
eia_send('D');
break;
case SDLK_DOWN:
eia_send(0x1B);
if (terminal_cursor_key_code() != 0) {
eia_send(terminal_cursor_key_code());
}
eia_send('B');
break;
case SDLK_RIGHT:
eia_send(0x1B);
if (terminal_cursor_key_code() != 0) {
eia_send(terminal_cursor_key_code());
}
eia_send('C');
break;
case SDLK_c:
keymod = SDL_GetModState();
if (keymod & KMOD_LCTRL || keymod & KMOD_RCTRL) {
eia_send(0x03); /* Ctrl+C */
}
break;
}
break;
}
}
if (sdlgui_renderer != NULL) {
SDL_UnlockTexture(sdlgui_texture);
SDL_RenderCopy(sdlgui_renderer, sdlgui_texture, NULL, NULL);
if (SDL_LockTexture(sdlgui_texture, NULL,
(void **)&sdlgui_pixels, &sdlgui_pixel_pitch) != 0) {
fprintf(stderr, "Unable to lock texture: %s\n", SDL_GetError());
exit(0);
}
}
/* Force 60 Hz (NTSC) */
while ((SDL_GetTicks() - sdlgui_ticks) < 16) {
SDL_Delay(1);
}
for (row = 0; row < (SDLGUI_HEIGHT / CHAR_HEIGHT); row++) {
for (col = 0; col < (SDLGUI_WIDTH / CHAR_WIDTH); col++) {
if (terminal_char_changed(row, col)) {
sdlgui_char(row, col, terminal_char_get(row, col));
}
}
}
if (sdlgui_renderer != NULL) {
SDL_RenderPresent(sdlgui_renderer);
}
sdlgui_ticks = SDL_GetTicks();
}