Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
53 lines (42 loc) · 1.48 KB
/
Copy pathmain.cpp
File metadata and controls
53 lines (42 loc) · 1.48 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
// time related functions, helps with random number generator
#include <chrono>
// input/output library
#include <iostream>
#include "chip8.hpp"
#include "platform.hpp"
int main(int argc, char **argv)
{
// error msg
if (argc != 4)
{
std::cerr << "Usage: " << argv[0] << " <Scale> <Delay> <ROM>\n";
std::exit(EXIT_FAILURE);
}
int videoScale = std::stoi(argv[1]);
int cycleDelay = std::stoi(argv[2]);
char const *romFilename = argv[3];
// create sdl window
Platform platform("CHIP-8 Emulator", VIDEO_WIDTH * videoScale, VIDEO_HEIGHT * videoScale, VIDEO_WIDTH, VIDEO_HEIGHT);
Chip8 chip8;
chip8.LoadROM(romFilename);
// how many bytes wide one row of pixels is, needed for SDL to navigate pixel buffer
int videoPitch = sizeof(chip8.video[0]) * VIDEO_WIDTH;
// record curr time
auto lastCycleTime = std ::chrono::high_resolution_clock::now();
bool quit = false;
while (!quit)
{
quit = platform.ProcessInput(chip8.keypad);
auto currTime = std::chrono::high_resolution_clock::now();
// calculate time elapsed in seconds
float dt = std::chrono::duration<float, std::chrono::milliseconds::period>(currTime - lastCycleTime).count();
// changing clock cycle time to match ROM file rather than CPU
if (dt > cycleDelay)
{
lastCycleTime = currTime;
chip8.Cycle();
platform.Update(chip8.video, videoPitch);
}
}
return 0;
}