-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChip8.cpp
More file actions
89 lines (70 loc) · 1.69 KB
/
Copy pathChip8.cpp
File metadata and controls
89 lines (70 loc) · 1.69 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
#include "Chip8.h"
#include "Cpu.h"
using namespace chip8;
int main(int argc, char *argv[])
{
//argv[argc++] = "test_opcodes.ch8";
bool tracing{ false };
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " [-t] <program>" << std::endl;
return 1;
}
if (std::string("-t") == argv[1]) {
tracing = true;
++argv;
--argc;
}
std::string fileName = argv[1];
sf::VideoMode mode{ Screen::kWidth * Screen::kScale, Screen::kHeight * Screen::kScale };
std::shared_ptr<sf::RenderWindow> window = std::make_shared<sf::RenderWindow>(mode, "Chip8sfml!");
window->setFramerateLimit(30);
std::shared_ptr<Screen> screen =
std::make_shared<Screen>(window, Screen::kWidth, Screen::kHeight, float(Screen::kScale));
std::shared_ptr<Keypad> keypad = std::make_shared<Keypad>();
Cpu cpu(screen, keypad);
try {
cpu.loadProgram(fileName);
}
catch (const std::runtime_error& e) {
std::cerr << "Failed to load \"" << fileName << "\": " << e.what() << std::endl;
return 1;
}
std::srand(12345);
sf::Clock clock;
float tickTimer{ 0 };
cpu.setTracing(tracing);
cpu.start();
while (window->isOpen())
{
sf::Time deltaTime = clock.restart();
sf::Event event;
// keyboard
while (window->pollEvent(event))
{
switch (event.type) {
case sf::Event::Closed:
window->close();
break;
case sf::Event::KeyPressed:
keypad->keyDown(event.key);
break;
case sf::Event::KeyReleased:
keypad->keyUp(event.key);
break;
default:
break;
}
}
// timer
tickTimer += deltaTime.asSeconds();
if (tickTimer > Cpu::kTimerStep) {
tickTimer = 0.0f;
cpu.timerTick();
}
// execute
if (cpu.isRunning())
if (cpu.step())
screen->update();
}
return 0;
}