-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
49 lines (41 loc) · 1.49 KB
/
main.cpp
File metadata and controls
49 lines (41 loc) · 1.49 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
#include <SDL.h>
#include <iostream>
#include "Chip8.h"
#include "Window.h"
std::pair<std::string, int> parseArgs(int argc, const char * argv[]);
int main(int argc, const char * argv[]) {
// Initialize Chip8 emulator
std::pair<std::string, int> parsedArgs = parseArgs(argc, argv);
Chip8 c8 = Chip8();
std::cout<<parsedArgs.first<<" "<<std::to_string(parsedArgs.second)<<std::endl;
// Load ROM
c8.loadProgram(parsedArgs.first);
// Initialize platform
Window w = Window();
Keypad k = Keypad();
// Initialize event handler for keyboard
EventHandler e = EventHandler();
// Main loop
while(true){
e.handleEvent(k); // Poll keyboard event
c8.cycle(k); // Fetch, decode, execute
c8.handleTime(parsedArgs.second); // Process delay timer
w.render(c8.getDisplay()); // Render to SDL
}
}
std::pair<std::string, int> parseArgs(int argc, const char * argv[]){
if (argc!=3){
std::cout<<"[ERROR] Command error. Usage: "<<argv[0]<<" <ROM-filename> <frame-rate"
"(400-800)>"<<std::endl;
exit(EXIT_FAILURE);
}
int inputFrameRate = std::stoi(argv[2]);
if (inputFrameRate<0){
std::cerr<<"[ERROR] Command error. Please input a positive frame rate";
exit(EXIT_FAILURE);
}
if (inputFrameRate<400 || inputFrameRate>800){
std::cerr<<"[WARNING] Suggested frame rate between 400-800 Hz";
}
return {argv[1], inputFrameRate};
}