-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
226 lines (213 loc) · 6.81 KB
/
Copy pathmain.cpp
File metadata and controls
226 lines (213 loc) · 6.81 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
#include <memory>
#include <iterator>
#include <fstream>
#include <vector>
#include <chrono>
#include <fmt/format.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "chip8.h"
constexpr int ZOOM = 4;
int grp[DISPLAY_HEIGHT * ZOOM][DISPLAY_WIDTH * ZOOM];
using namespace std::chrono;
uint64_t getMillis()
{
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
class Game
{
Display *D;
Window win;
XImage *image;
public:
Game()
{
D = XOpenDisplay(NULL);
auto visual = DefaultVisual(D, 0);
win = XCreateSimpleWindow(D, RootWindow(D, 0), 0, 0, DISPLAY_WIDTH * ZOOM, DISPLAY_HEIGHT * ZOOM, 1, 0, 0);
XSelectInput(D, win, ExposureMask | KeyPressMask | KeyReleaseMask);
XStoreName(D, win, "CHIP-8 Interpreter");
XMapWindow(D, win);
image = XCreateImage(D, visual, 24, ZPixmap, 0, (char *)grp, DISPLAY_WIDTH * ZOOM, DISPLAY_HEIGHT * ZOOM, 32, 0);
}
void redraw()
{
XPutImage(D, win, DefaultGC(D, 0), image, 0, 0, 0, 0, DISPLAY_WIDTH * ZOOM, DISPLAY_HEIGHT * ZOOM);
}
void renderVram(Chip8Display &vram)
{
for (int y = 0; y < DISPLAY_HEIGHT; y++)
{
auto oy = y * ZOOM;
for (int x = 0; x < DISPLAY_WIDTH; x++)
{
auto ox = x * ZOOM;
for (int i = 0; i < ZOOM; i++)
{
for (int j = 0; j < ZOOM; j++)
{
grp[oy + i][ox + j] = vram[y][x] ? 0xFFFFFF : 0x000000;
}
}
}
}
}
bool processEvent(Chip8 &chip8)
{
while (1)
{
static union
{
XEvent event;
XMotionEvent motion;
XKeyEvent key;
XButtonEvent button;
XClientMessageEvent clientMessage;
} ev;
Bool t = XCheckWindowEvent(D, win, ~0L, &ev.event);
if (!t)
return true;
switch (ev.event.type)
{
case Expose:
redraw();
break;
// case ButtonPress:;
// click(ev.button.x, ev.button.y);
case KeyPress:;
{
KeySym key = XLookupKeysym(&ev.key, 0);
if (!key)
break;
switch (key)
{
case XK_1:
chip8.keyboard[1] = true;
break;
case XK_2:
chip8.keyboard[2] = true;
break;
case XK_3:
chip8.keyboard[3] = true;
break;
case XK_4:
chip8.keyboard[0xC] = true;
break;
case XK_q:
chip8.keyboard[4] = true;
break;
case XK_w:
chip8.keyboard[5] = true;
break;
case XK_e:
chip8.keyboard[6] = true;
break;
case XK_r:
chip8.keyboard[0xD] = true;
break;
case XK_a:
chip8.keyboard[7] = true;
break;
case XK_s:
chip8.keyboard[8] = true;
break;
case XK_d:
chip8.keyboard[9] = true;
break;
case XK_f:
chip8.keyboard[0xE] = true;
break;
case XK_z:
chip8.keyboard[0xA] = true;
break;
case XK_x:
chip8.keyboard[0] = true;
break;
case XK_c:
chip8.keyboard[0xB] = true;
break;
case XK_v:
chip8.keyboard[0xF] = true;
break;
}
break;
}
case KeyRelease:;
{
KeySym key = XLookupKeysym(&ev.key, 0);
if (!key)
break;
switch (key)
{
case XK_1:
chip8.keyboard[1] = false;
break;
case XK_2:
chip8.keyboard[2] = false;
break;
case XK_3:
chip8.keyboard[3] = false;
break;
case XK_4:
chip8.keyboard[0xC] = false;
break;
case XK_q:
chip8.keyboard[4] = false;
break;
case XK_w:
chip8.keyboard[5] = false;
break;
case XK_e:
chip8.keyboard[6] = false;
break;
case XK_r:
chip8.keyboard[0xD] = false;
break;
case XK_a:
chip8.keyboard[7] = false;
break;
case XK_s:
chip8.keyboard[8] = false;
break;
case XK_d:
chip8.keyboard[9] = false;
break;
case XK_f:
chip8.keyboard[0xE] = false;
break;
case XK_z:
chip8.keyboard[0xA] = false;
break;
case XK_x:
chip8.keyboard[0] = false;
break;
case XK_c:
chip8.keyboard[0xB] = false;
break;
case XK_v:
chip8.keyboard[0xF] = false;
break;
}
}
}
}
}
};
int main()
{
auto game = Game();
auto chip8 = Chip8();
// load the test file
std::ifstream infile("./tetris.rom", std::ios_base::binary);
std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(infile), {});
chip8.load(buffer);
// we should load the example file i guess
while (game.processEvent(chip8))
{
chip8.cycle();
chip8.timer(getMillis());
game.renderVram(chip8.vram);
game.redraw();
}
return 0;
}