-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChipEmulator.cpp
More file actions
202 lines (155 loc) · 5.15 KB
/
ChipEmulator.cpp
File metadata and controls
202 lines (155 loc) · 5.15 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <nfd.h>
#include "CHIP8.h"
#include <vector>
#include <chrono>
#include <thread>
CHIP8 chip;
const unsigned int TILE_SIZE = 15;
std::vector<std::vector<sf::Color>> colorPalette =
{
{ sf::Color(32, 42, 53), sf::Color(143, 145, 133) },
{ sf::Color(143, 145, 133), sf::Color(32, 42, 53) },
{ sf::Color(182, 124, 36), sf::Color(27, 30, 37) }
};
unsigned short pallete_index = 0;
void handle_user_input();
std::string OpenFileDialog();
int main()
{
if (chip.load_file("Chip8Picture.ch8") == false)
{
std::cout << "Error to load a file" << std::endl;
return -1;
}
sf::RenderWindow window(sf::VideoMode({ TILE_SIZE * WIDTH, TILE_SIZE * HEIGHT}), "CHIP-8 Emulator");
while (window.isOpen())
{
// Logic Events
while (const std::optional event = window.pollEvent())
{
if (event->is<sf::Event::Closed>())
window.close();
}
// User Inputs
handle_user_input();
// Chip Logic
chip.update();
// Displaying
window.clear();
for (float i = 0; i < WIDTH; i++)
{
for (float j = 0; j < HEIGHT; j++)
{
sf::RectangleShape rect({ TILE_SIZE, TILE_SIZE });
if (chip.get_display(i, j) == 1)
rect.setFillColor(colorPalette[pallete_index][0]);
else
rect.setFillColor(colorPalette[pallete_index][1]);
rect.setPosition({i*TILE_SIZE, j*TILE_SIZE});
window.draw(rect);
}
}
window.display();
}
return 0;
}
void handle_user_input()
{
// --- Program settings buttons ---
// Restart program
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::F1))
{
chip.reset_program();
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
// Load program path
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::F2))
{
chip.load_file( OpenFileDialog() );
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
// Change theme
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::F3))
{
pallete_index = (pallete_index + 1) % colorPalette.size();
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
// Main 16 input
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num1))
chip.setKeyStatus(1, 1);
else
chip.setKeyStatus(1, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num2))
chip.setKeyStatus(2, 1);
else
chip.setKeyStatus(2, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num3))
chip.setKeyStatus(3, 1);
else
chip.setKeyStatus(3, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num4))
chip.setKeyStatus(12, 1);
else
chip.setKeyStatus(12, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Q))
chip.setKeyStatus(4, 1);
else
chip.setKeyStatus(4, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::W))
chip.setKeyStatus(5, 1);
else
chip.setKeyStatus(5, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::E))
chip.setKeyStatus(6, 1);
else
chip.setKeyStatus(6, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::R))
chip.setKeyStatus(13, 1);
else
chip.setKeyStatus(13, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::A))
chip.setKeyStatus(7, 1);
else
chip.setKeyStatus(7, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::S))
chip.setKeyStatus(8, 1);
else
chip.setKeyStatus(8, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::D))
chip.setKeyStatus(9, 1);
else
chip.setKeyStatus(9, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::F))
chip.setKeyStatus(14, 1);
else
chip.setKeyStatus(14, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Z))
chip.setKeyStatus(10, 1);
else
chip.setKeyStatus(10, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::X))
chip.setKeyStatus(0, 1);
else
chip.setKeyStatus(0, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::C))
chip.setKeyStatus(11, 1);
else
chip.setKeyStatus(11, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::V))
chip.setKeyStatus(15, 1);
else
chip.setKeyStatus(15, 0);
}
std::string OpenFileDialog() {
nfdchar_t* outPath = nullptr;
nfdfilteritem_t filterItem[2] = { { "Chip8 Roms", "ch8" } };
if (NFD_OpenDialog(&outPath, filterItem, 1, NULL) == NFD_OKAY) {
std::string filePath(outPath);
free(outPath);
return filePath;
}
free(outPath);
return "";
}