-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.c
200 lines (180 loc) · 4.66 KB
/
render.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
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
#include "chip.h"
#include "render.h"
void render_init(Render *render)
{
SDL_Init(SDL_INIT_VIDEO);
render->window = SDL_CreateWindow(
"CHIP-8 Emulator",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
320,
SDL_WINDOW_SHOWN);
render->renderer = SDL_CreateRenderer(render->window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(render->renderer, 0, 0, 0, 255);
SDL_RenderClear(render->renderer);
SDL_RenderPresent(render->renderer);
render->texture = SDL_CreateTexture(
render->renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
64,
32);
}
void render_quit(Render *render)
{
SDL_DestroyTexture(render->texture);
SDL_DestroyRenderer(render->renderer);
SDL_DestroyWindow(render->window);
SDL_Quit();
}
void render_update(Render *render, Chip8 *chip)
{
SDL_UpdateTexture(render->texture, NULL, chip->video, 64 * sizeof(uint32_t));
SDL_RenderClear(render->renderer);
SDL_RenderCopy(render->renderer, render->texture, NULL, NULL);
SDL_RenderPresent(render->renderer);
clear();
mvprintw(0, 0, "Registers:");
for (int i = 0; i < 16; i++)
{
mvprintw(i + 1, 0, "V%X: 0x%04X\t", i, chip->registers[i]);
}
mvprintw(18, 0, "Index:");
mvprintw(19, 0, "I: 0x%04X\t", chip->index);
mvprintw(21, 0, "Program Counter:");
mvprintw(22, 0, "PC: 0x%04X\t", chip->pc);
mvprintw(24, 0, "Keypad:");
for (int i = 0; i < 16; i++)
{
mvprintw(i + 25, 0, "%d\t", chip->keypad[i]);
}
refresh();
}
bool render_input(uint8_t *keys)
{
bool quit = false;
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_x:
keys[0] = 1;
break;
case SDLK_1:
keys[1] = 1;
break;
case SDLK_2:
keys[2] = 1;
break;
case SDLK_3:
keys[3] = 1;
break;
case SDLK_q:
keys[4] = 1;
break;
case SDLK_w:
keys[5] = 1;
break;
case SDLK_e:
keys[6] = 1;
break;
case SDLK_a:
keys[7] = 1;
break;
case SDLK_s:
keys[8] = 1;
break;
case SDLK_d:
keys[9] = 1;
break;
case SDLK_z:
keys[0xA] = 1;
break;
case SDLK_c:
keys[0xB] = 1;
break;
case SDLK_4:
keys[0xC] = 1;
break;
case SDLK_r:
keys[0xD] = 1;
break;
case SDLK_f:
keys[0xE] = 1;
break;
case SDLK_v:
keys[0xF] = 1;
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_x:
keys[0] = 0;
break;
case SDLK_1:
keys[1] = 0;
break;
case SDLK_2:
keys[2] = 0;
break;
case SDLK_3:
keys[3] = 0;
break;
case SDLK_q:
keys[4] = 0;
break;
case SDLK_w:
keys[5] = 0;
break;
case SDLK_e:
keys[6] = 0;
break;
case SDLK_a:
keys[7] = 0;
break;
case SDLK_s:
keys[8] = 0;
break;
case SDLK_d:
keys[9] = 0;
break;
case SDLK_z:
keys[0xA] = 0;
break;
case SDLK_c:
keys[0xB] = 0;
break;
case SDLK_4:
keys[0xC] = 0;
break;
case SDLK_r:
keys[0xD] = 0;
break;
case SDLK_f:
keys[0xE] = 0;
break;
case SDLK_v:
keys[0xF] = 0;
break;
}
break;
}
}
return quit;
}