-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
177 lines (159 loc) · 4.1 KB
/
Copy pathmain.cpp
File metadata and controls
177 lines (159 loc) · 4.1 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
#include "chip8.h" // Your cpu core implementation
#include <map>
const int WINDOW_GFX_SCALE = 8;
const int FPS = 60;
const int CLOCK_SPEED_IN_HZ = 600;
const char *defaultColours[] = { "0", "0", "0"};
SDL_Window *initWindow();
std::map<SDL_Keycode, unsigned char> getKeyboard();
void drawGraphics(SDL_Renderer *renderer, unsigned char gfx[], unsigned int r, unsigned int g, unsigned int b);
int main(int argc, char *argv[])
{
SDL_Window *window = NULL;
SDL_Event event;
SDL_Keycode eventKey;
SDL_Renderer *renderer;
Chip8CPU cpu;
std::map<SDL_Keycode, unsigned char> keyMap = getKeyboard();
unsigned int red;
unsigned int green;
unsigned int blue;
try {
red = std::stoi(argv[2]);
green = std::stoi(argv[3]);
blue = std::stoi(argv[4]);
} catch (...) {
red = 255;
green = 255;
blue = 255;
}
window = SDL_CreateWindow(
"Chip-8 Interpreter", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WINDOW_GFX_SCALE * 64,
WINDOW_GFX_SCALE * 32,
SDL_WINDOW_SHOWN);
// Setup renderer
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
cpu.initializeMemory();
cpu.loadGame(argv[1]);
//Interpreter event loop
while (true)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
exit(0);
} else if (event.type == SDL_KEYDOWN) { //Handle keyboard key press
eventKey = event.key.keysym.sym;
if (keyMap.count(eventKey) == 1) {
cpu.inputKeys[keyMap[eventKey]] = true;
}
} else if (event.type == SDL_KEYUP) { //Handle keyboard key release
eventKey = event.key.keysym.sym;
if (keyMap.count(eventKey) == 1) {
cpu.inputKeys[keyMap[eventKey]] = false;
}
}
}
//Begin clock cycle
for (unsigned int i = 0; i < CLOCK_SPEED_IN_HZ / FPS; i++) {
cpu.emulateCycle();
}
//Draw graphics if required
if (cpu.drawFlag)
{
drawGraphics(renderer, cpu.gfx, red, green, blue);
}
//Delay to reach 60 FPS
SDL_Delay(1000 / FPS);
}
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
void drawGraphics(SDL_Renderer *renderer, unsigned char gfx[], unsigned int r, unsigned int g, unsigned int b)
{
SDL_Rect rect;
int index = 0;
rect.w = WINDOW_GFX_SCALE;
rect.h = WINDOW_GFX_SCALE;
for (unsigned int i = 0; i < 32; i++)
{
for (unsigned int c = 0; c < 64; c++)
{
index = i * 64 + c;
rect.x = c * WINDOW_GFX_SCALE;
rect.y = i * WINDOW_GFX_SCALE;
gfx[index] ? SDL_SetRenderDrawColor(renderer, r, g, b, 100) : SDL_SetRenderDrawColor(renderer, 0, 0, 0, 100);
SDL_RenderFillRect(renderer, &rect);
}
}
SDL_RenderPresent(renderer);
}
std::map<SDL_Keycode, unsigned char> getKeyboard() {
std::map<SDL_Keycode, unsigned char> keyboard;
SDL_Keycode hexPad [] = {
SDLK_1,
SDLK_2,
SDLK_3,
SDLK_4,
SDLK_q,
SDLK_w,
SDLK_e,
SDLK_r,
SDLK_a,
SDLK_s,
SDLK_d,
SDLK_f,
SDLK_z,
SDLK_x,
SDLK_c,
SDLK_v
};
unsigned char hexPadMapped [] = {
0x1,
0x2,
0x3,
0xC,
0x4,
0x5,
0x6,
0xD,
0x7,
0x8,
0x9,
0xE,
0xA,
0x0,
0xB,
0xF
};
for (unsigned int i = 0; i < sizeof(hexPad); i++) {
keyboard.insert(std::make_pair(hexPad[i], hexPadMapped[i]));
}
return keyboard;
}
SDL_Window *initWindow()
{
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"CHIP-8 Interpreter", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
512, // width, in pixels
256, // height, in pixels
SDL_WINDOW_OPENGL // flag
);
// Check that the window was successfully created
if (window == NULL)
{
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return NULL;
}
return window;
}