-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
125 lines (108 loc) · 4.15 KB
/
main.cpp
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
#include <iostream>
#include <cstring>
#include <string>
#include "SFML\Graphics.hpp"
#include "SFML\Window\Keyboard.hpp"
#include "SFML\Audio.hpp"
#include "Chip8.hpp"
#include "Init.hpp"
#include "KeyHandler.hpp"
void pushBuffer(Chip8& chip8, sf::RenderWindow& mainWindow, sf::RectangleShape& chip8SpriteRect) //Fills the SFML window buffer with the gfx buffer from the chip8 then draws it to the screen.
{
auto [x_range, y_range] = chip8.get_GfxRange();
sf::Vector2f v_Scale((float(x_res) / x_range), (float(y_res) / y_range));
chip8SpriteRect.setSize(v_Scale);
//x_scale = x_res / x_range;
//y_scale = y_res / y_range;
for (unsigned int y = 0; y < y_range; ++y)
{
for (unsigned int x = 0; x < x_range; ++x)
{
//this if statement is not properly containing the float for the quotient of y_res / y_range. This needs to be debugged.
if (chip8.gfx[x][y] == 1) chip8SpriteRect.setPosition(x * v_Scale.x, y * v_Scale.y); //Multiply the position value by the scale factor so nothing overlaps and the array scales.
mainWindow.draw(chip8SpriteRect);
}
}
}
void emulationLoop(Chip8& chip8, sf::Event& event, sf::SoundBuffer& beepBuffer, sf::Sound& beepSound, sf::RenderWindow& mainWindow, sf::RectangleShape& chip8SpriteRect) //Main emulation loop responsible for running cycles, checking events, playing sounds, etc.
{
mainWindow.setFramerateLimit(60);
while (mainWindow.isOpen())
{
while (mainWindow.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
mainWindow.close();
break;
case sf::Event::KeyPressed:
keyStatePressed(chip8, event, beepBuffer, beepSound, mainWindow, chip8SpriteRect);
break;
case sf::Event::KeyReleased:
keyStateReleased(chip8, event);
break;
default:
break;
}
}
for (int clockCycle = 0; clockCycle <= 9; clockCycle++)
{
if (!chip8.isPaused) chip8.emulateCycle();
}
if (chip8.quitFlag) mainWindow.close();
if (chip8.drawFlag)
{
mainWindow.clear(sf::Color::Black);
pushBuffer(chip8, mainWindow, chip8SpriteRect);
#ifdef _DEBUG
//chip8.debugRender();
#endif // _DEBUG
mainWindow.display();
chip8.drawFlag = false;
}
if (chip8.playSound)
{
beepSound.play();
chip8.playSound = false;
}
}
}
int main(int argc, char* argv[])
{
//OBJECT INITIALIZATION//
Chip8 chip8; //Creates emulator object and initializes class state using constructor
sf::Event event; //Creates event object to contain event types necessary for interactivity.
sf::SoundBuffer beepBuffer; //Creates sound buffer to hold beep.wav file.
sf::Sound beepSound; //Creates sound object to control buffer playback.
sf::RenderWindow mainWindow(sf::VideoMode(x_res, y_res), "Chip 8 Emulator"); //Create and declare Window object for rendering.
sf::RectangleShape chip8SpriteRect; //Create RectangleShape object with a size of scale factor.
//OBJECT INITIALIZATION END//
if (chip8.setOpenParams(argc, argv[1]))
{
if (chip8.loadROM())
{
if (!beepBuffer.loadFromFile("beep.wav"))
{
std::cerr << "Error loading sound file. Please check that the file name is beep.wav and is located next to the executable. Continuing execution... \n";
}
else
{
beepSound.setBuffer(beepBuffer);
}
emulationLoop(chip8, event, beepBuffer, beepSound, mainWindow, chip8SpriteRect);
}
else
{
std::cerr << "Error: Something failed with loading the ROM. Check provided errors and try again. \n";
std::cin.get();
return 1;
}
}
else
{
std::cerr << "Something failed with setting opening program parameters. \n";
return 1;
}
return 0;
}