-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (96 loc) · 3.84 KB
/
main.cpp
File metadata and controls
115 lines (96 loc) · 3.84 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
#include <iostream>
#include "chip8.h"
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <chrono>
#include <thread>
#include <vector>
#include <cmath>
int keycodes(sf::Keyboard::Scancode sc) {
switch (sc) {
case sf::Keyboard::Scancode::Num1: return 0x1;
case sf::Keyboard::Scancode::Num2: return 0x2;
case sf::Keyboard::Scancode::Num3: return 0x3;
case sf::Keyboard::Scancode::Num4: return 0xC;
case sf::Keyboard::Scancode::Q: return 0x4;
case sf::Keyboard::Scancode::W: return 0x5;
case sf::Keyboard::Scancode::E: return 0x6;
case sf::Keyboard::Scancode::R: return 0xD;
case sf::Keyboard::Scancode::A: return 0x7;
case sf::Keyboard::Scancode::S: return 0x8;
case sf::Keyboard::Scancode::D: return 0x9;
case sf::Keyboard::Scancode::F: return 0xE;
case sf::Keyboard::Scancode::Z: return 0xA;
case sf::Keyboard::Scancode::X: return 0x0;
case sf::Keyboard::Scancode::C: return 0xB;
case sf::Keyboard::Scancode::V: return 0xF;
default: return -1;
}
}
int main(int argc, char* argv[])
{
std::string filepath {argv[1]};
Chip8 chip8 { Chip8::Chip8(filepath) };
chip8.configuration = std::stoi( argv[2], nullptr, 2 );
const int cycles_per_frame = argv[3] ? std::stoi(argv[3]) : 11;
sf::RenderWindow window(sf::VideoMode({640, 320}), "Chip-8");
sf::Image image({64,32}, sf::Color::Black);
sf::Texture texture(image);
sf::Sprite sprite(texture);
sprite.setScale({10.0f, 10.0f});
const auto frame_duration = std::chrono::microseconds(16667);
sf::SoundBuffer beepBuffer { "beep.wav" }; // Load from a file
sf::Sound beepSound(beepBuffer);
while (window.isOpen())
{
auto frame_start = std::chrono::high_resolution_clock::now();
while (const std::optional event = window.pollEvent())
{
if (event->is<sf::Event::Closed>())
window.close();
else if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>())
{
int idx = keycodes(keyPressed->scancode);
if (idx != -1) chip8.key[idx] = 1;
}
else if (const auto* keyReleased = event->getIf<sf::Event::KeyReleased>())
{
int idx = keycodes(keyReleased->scancode);
if (idx != -1) chip8.key[idx] = 0;
}
}
for (int i = 0; i < cycles_per_frame; ++i)
chip8.cycle();
if (chip8.delayTimer > 0) chip8.delayTimer--;
if (chip8.soundTimer > 0) chip8.soundTimer--;
if (chip8.soundTimer > 0)
{
if (beepSound.getStatus() != sf::SoundSource::Status::Playing)
beepSound.play();
}
else
{
if (beepSound.getStatus() == sf::SoundSource::Status::Playing)
beepSound.stop();
}
if (chip8.draw)
{
window.clear();
chip8.draw = 0;
for(int i{ 0 }; i < 64*32; ++i)
{
uint16_t x = static_cast<uint16_t>(i%64);
uint16_t y = static_cast<uint16_t>(i/64);
image.setPixel({x,y}, chip8.display[i] ? sf::Color::White : sf::Color::Black);
}
texture.update(image);
sprite.setTexture(texture);
window.draw(sprite);
window.display();
}
auto frame_end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(frame_end - frame_start);
if (elapsed < frame_duration)
std::this_thread::sleep_for(frame_duration - elapsed);
}
}