-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulator.cpp
executable file
·81 lines (64 loc) · 1.62 KB
/
emulator.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 <iostream>
#include "chip8.h"
#include "raylib.h"
using namespace std;
chip8Machine chip8;
// Key mappings for CHIP-8 keys
const int keymap[16] = {
KEY_X, // 0
KEY_ONE, // 1
KEY_TWO, // 2
KEY_THREE,// 3
KEY_Q, // 4
KEY_W, // 5
KEY_E, // 6
KEY_A, // 7
KEY_S, // 8
KEY_D, // 9
KEY_Z, // A
KEY_C, // B
KEY_FOUR, // C
KEY_R, // D
KEY_F, // E
KEY_V, // F
};
int main(int argc, char* argv[])
{
if (argc < 2) {
cout << "Usage: " << argv[0] << " <path to ROM file>\n";
return 1;
}
if (!chip8.initialize(argv[1])) {
return 1;
}
constexpr int screenWidth = 1024;
constexpr int screenHeight = 512;
constexpr int scale = screenHeight / 32;
InitWindow(screenWidth, screenHeight, "CHIP-8 Emulator");
SetTargetFPS(60);
while (!WindowShouldClose()) {
if (IsKeyDown(KEY_F1)) {
chip8.initialize(argv[1]);
}
chip8.emulationCycle();
PollInputEvents();
for (int i = 0; i < 16; i++) {
chip8.key[i] = IsKeyDown(keymap[i]);
}
if (chip8.draw_flag) {
chip8.draw_flag = 0;
BeginDrawing();
ClearBackground(BLACK);
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 64; x++) {
if (chip8.screen[y * 64 + x] == 1) {
DrawRectangle(x * scale, y * scale, scale, scale, WHITE);
}
}
}
EndDrawing();
}
}
CloseWindow();
return 0;
}