-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmulatorWindow.cpp
80 lines (71 loc) · 2.17 KB
/
EmulatorWindow.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
#include "EmulatorWindow.h"
// SDL Constructor
EmulatorWindow::EmulatorWindow() {
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("CHIP-8 Emulator", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 320, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, 64, 32);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
}
// SDL Destructor
EmulatorWindow::~EmulatorWindow() {
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
// Handle input
bool EmulatorWindow::handleKeys(unsigned char key[]) {
SDL_Event event;
SDL_KeyCode keyMap[16];
// first row
keyMap[0xA] = SDLK_z;
keyMap[0] = SDLK_x;
keyMap[0xB] = SDLK_c;
keyMap[0xF] = SDLK_v;
// second row
keyMap[7] = SDLK_a;
keyMap[8] = SDLK_s;
keyMap[9] = SDLK_d;
keyMap[0xE] = SDLK_f;
// third row
keyMap[4] = SDLK_q;
keyMap[5] = SDLK_w;
keyMap[6] = SDLK_e;
keyMap[0xD] = SDLK_r;
// fourth row
keyMap[1] = SDLK_1;
keyMap[2] = SDLK_2;
keyMap[3] = SDLK_3;
keyMap[0xC] = SDLK_4;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
return true;
break;
case SDL_KEYDOWN:
for (int i = 0; i < 16; i++) {
if (keyMap[i] == event.key.keysym.sym) {
key[i] = 1;
}
}
break;
case SDL_KEYUP:
for (int i = 0; i < 16; i++) {
if (keyMap[i] == event.key.keysym.sym) {
key[i] = 0;
}
}
break;
}
}
return false;
}
// Update display based on display array
void EmulatorWindow::updateDisplay(uint32_t display[], int pitch) {
SDL_UpdateTexture(texture, nullptr, display, pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
}