Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
92 lines (73 loc) · 2.49 KB
/
Copy pathmain.cpp
File metadata and controls
92 lines (73 loc) · 2.49 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
#include <iostream>
#include <filesystem>
#include <vector>
#include <fstream>
#include <thread>
#include "include/chip8.h"
#include "include/graphicsChip8.h"
#include "include/gameReader.h"
#include "include/eventSystem/eventsystem.h"
#include "include/keyboardChip8.h"
#include "nfd.h"
using namespace std::chrono_literals;
#if defined(_WIN32) && defined(_RELEASE)
#include <Windows.h>
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
#else
int main(int argc, char* argv[])
#endif
{
sf::RenderWindow window(sf::VideoMode(640, 320), "Chip-8", sf::Style::Titlebar | sf::Style::Close);
GraphicsChip8 graphicsChip(window, 32, 64, 10);
Chip8 chip;
GameReader readerGame;
EventSystem eventSystem(window);
bool isStop = true;
int sizeKeyboardArray;
bool* keyBoardArray = chip.getKeyArray(sizeKeyboardArray);
auto& bufferDisplay = chip.getDisplay();
KeyboardChip8 keyboard(keyBoardArray, sizeKeyboardArray);
eventSystem.addEvent(sf::Event::EventType::Closed, [&window](sf::Event&) { window.close(); });
eventSystem.addEvent(sf::Event::EventType::KeyPressed, [&keyboard](sf::Event& event) {keyboard.keyPressed(event); });
eventSystem.addEvent(sf::Event::EventType::KeyReleased, [&keyboard](sf::Event& event) {keyboard.keyReleased(event); });
eventSystem.addEvent(sf::Event::EventType::KeyPressed, [&isStop](sf::Event& event) {if (event.key.code == sf::Keyboard::Escape) isStop = true; });
readerGame.readGame("C:\\Users\\User\\Desktop\\invaders.c8");
chip.loadProgram(readerGame.getGame());
auto endTime = std::chrono::steady_clock::now();
double timeFrameMS = 5;
while (window.isOpen())
{
if (isStop == true)
{
chip.clearDisplay();
chip.clearMemory();
nfdchar_t* outPath = NULL;
nfdresult_t result = NFD_OpenDialog("c8", NULL, &outPath);
if (result == NFD_OKAY)
{
if (readerGame.readGame(outPath))
{
chip.loadProgram(readerGame.getGame());
isStop = false;
}
}
}
else
{
eventSystem.proccessEvent();
chip.emulateCycle();
if (chip.getDrawFlag())
{
auto startTime = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli> duractionTime = startTime - endTime;
double milliseconds = duractionTime.count();
if (timeFrameMS > milliseconds)
std::this_thread::sleep_for(std::chrono::milliseconds(int(timeFrameMS - milliseconds)));
endTime = startTime;
graphicsChip.drawForCycle(bufferDisplay);
chip.resetDrawFlag();
}
}
}
return 0;
}