-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDL2Widget.cpp
301 lines (241 loc) · 5.95 KB
/
SDL2Widget.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "SDL2Widget.h"
#include <QPointer>
#include <chrono>
SDL_AudioSpec wavSpec;
Uint32 wavLength;
Uint8* wavBuffer;
SDL_AudioDeviceID deviceId;
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
SDL_Event e;
const int SCREEN_FPS = 240;
Chip8 c8;
SDL2Widget *sw;
Debugger *debug;
bool loaded = false;
bool anim = false;
SDL2Widget::SDL2Widget(QWidget* parent) : QWidget(parent) {
sw = this;
// Turn off double buffering for this widget. Double buffering
// interferes with the ability for SDL to be properly displayed
// on the QWidget.
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NativeWindow, true);
setAttribute(Qt::WA_OpaquePaintEvent);
setMouseTracking(true);
/* setting setUpdatesEnabled to false is MOST important here!
* if you set it to true or comment it out entirely then when you
* resize the SDL2Widget by dragging it from corners you will
* notice the widget will flicker with white frames which are
* produced by the widget updates.
*/
setUpdatesEnabled(false);
if (SDL_Init(SDL_INIT_VIDEO) != 0)
qDebug() << "Failed to initialize SDL video: " << SDL_GetError();
sdlWindow = SDL_CreateWindowFrom((void*)this->winId());
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (SDL_Init(SDL_INIT_AUDIO) != 0)
qDebug() << "Failed to initialize SDL audio: " << SDL_GetError();
if (!SDL_LoadWAV("E3.wav", &wavSpec, &wavBuffer, &wavLength))
qDebug() << "error loading audio";
deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(mainLoop()));
// timer->start(1000 / SCREEN_FPS);
}
SDL2Widget::~SDL2Widget() {
SDL_CloseAudioDevice(deviceId);
SDL_FreeWAV(wavBuffer);
SDL_DestroyRenderer(sdlRenderer);
SDL_DestroyWindow(sdlWindow);
delete timer;
sdlRenderer = nullptr;
sdlWindow = nullptr;
timer = nullptr;
}
void drawPixel(unsigned char p, short x, short y) {
SDL_Rect fillRect = { x, y, 8, 8 };
SDL_SetRenderDrawColor(sdlRenderer, p ? 0xFF : 0x00, \
p ? 0xFF : 0x00, \
p ? 0xFF : 0x00, 0xFF);
SDL_RenderFillRect(sdlRenderer, &fillRect);
}
void drawGraphics() {
for (int y = 0; y < 32; ++y) {
for (int x = 0; x < 64; ++x) {
drawPixel(c8.gfx[x + (64 * y)], x * 8, y * 8);
}
}
SDL_RenderPresent(sdlRenderer);
c8.draw = false;
}
void keydown(int k) {
switch (k) {
case SDLK_1:
c8.key[0x01] = 1;
break;
case SDLK_2:
c8.key[0x02] = 1;
break;
case SDLK_3:
c8.key[0x03] = 1;
break;
case SDLK_4:
c8.key[0x0C] = 1;
break;
case SDLK_q - 0x20:
c8.key[0x04] = 1;
break;
case SDLK_w - 0x20:
c8.key[0x05] = 1;
break;
case SDLK_e - 0x20:
c8.key[0x06] = 1;
break;
case SDLK_r - 0x20:
c8.key[0x0D] = 1;
break;
case SDLK_a - 0x20:
c8.key[0x07] = 1;
break;
case SDLK_s - 0x20:
c8.key[0x08] = 1;
break;
case SDLK_d - 0x20:
c8.key[0x09] = 1;
break;
case SDLK_f - 0x20:
c8.key[0x0E] = 1;
break;
case SDLK_z - 0x20:
c8.key[0x0A] = 1;
break;
case SDLK_x - 0x20:
c8.key[0x00] = 1;
break;
case SDLK_c - 0x20:
c8.key[0x0B] = 1;
break;
case SDLK_v - 0x20:
c8.key[0x0F] = 1;
break;
}
}
void keyup(int k) {
switch (k) {
case SDLK_1:
c8.key[0x01] = 0;
break;
case SDLK_2:
c8.key[0x02] = 0;
break;
case SDLK_3:
c8.key[0x03] = 0;
break;
case SDLK_4:
c8.key[0x0C] = 0;
break;
case SDLK_q - 0x20:
c8.key[0x04] = 0;
break;
case SDLK_w - 0x20:
c8.key[0x05] = 0;
break;
case SDLK_e - 0x20:
c8.key[0x06] = 0;
break;
case SDLK_r - 0x20:
c8.key[0x0D] = 0;
break;
case SDLK_a - 0x20:
c8.key[0x07] = 0;
break;
case SDLK_s - 0x20:
c8.key[0x08] = 0;
break;
case SDLK_d - 0x20:
c8.key[0x09] = 0;
break;
case SDLK_f - 0x20:
c8.key[0x0E] = 0;
break;
case SDLK_z - 0x20:
c8.key[0x0A] = 0;
break;
case SDLK_x - 0x20:
c8.key[0x00] = 0;
break;
case SDLK_c - 0x20:
c8.key[0x0B] = 0;
break;
case SDLK_v - 0x20:
c8.key[0x0F] = 0;
break;
}
}
void playBeep(SDL_AudioDeviceID deviceId) {
if (!SDL_QueueAudio(deviceId, wavBuffer, wavLength)) {
SDL_PauseAudioDevice(deviceId, 0);
SDL_Delay(200);
}
}
Chip8* SDL2Widget::getC8Context() {
return &c8;
}
SDL2Widget* SDL2Widget::getSDLContext() {
return sw;
}
void SDL2Widget::mainLoop() {
if (loaded) {
debug = Debugger::getDebugContext();
c8.emulateCycle();
if (debug) {
if (anim) {
debug->updateWidgets();
debug->updateCurrentLine();
}
}
if (c8.draw)
drawGraphics();
if (c8.beep) {
playBeep(deviceId);
c8.beep = false;
}
}
}
void SDL2Widget::recKey(int k, bool state) {
if (state)
keydown(k);
else
keyup(k);
}
void SDL2Widget::loadRom(std::vector<unsigned char> rom) {
c8.init(rom);
loaded = true;
run();
}
void SDL2Widget::run() {
if (loaded) {
anim = false;
this->activateWindow();
timer->start(1000 / SCREEN_FPS);
}
}
void SDL2Widget::breakPoint() {
if (loaded) {
timer->stop();
}
}
void SDL2Widget::singleStep() {
if (loaded) {
anim = true;
mainLoop();
}
}
void SDL2Widget::animate() {
if (loaded) {
anim = true;
this->activateWindow();
timer->start(1000 / SCREEN_FPS);
}
}