-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
78 lines (62 loc) · 1.33 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
/*This source code copyrighted by Lazy Foo' Productions (2004-2019)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <stdio.h>
#include <iostream>
#include "chip8.h"
#include "Graphics.h"
#include "Input.h"
#include <fstream>
#include <vector>
#include <thread>
#include <chrono>
Chip8 cpu;
Graphics graphics;
Input input;
void load();
int main( int argc, char* args[] )
{
cpu = Chip8();
graphics = Graphics();
input = Input();
// Initialize the Chip8 system and load the game into the memory
cpu.Initialize();
load();
graphics.Initialize();
input.Initialize();
// Emulation loop
while(true)
{
// Emulate one cycle
cpu.EmulateCycle();
// If the draw flag is set, update the screen
if (cpu.GetDrawFlag())
{
graphics.Draw(cpu.gfx);
cpu.SetDrawFlag(false);
}
if (cpu.GetPlaySound())
{
// add sound
}
if (input.quit)
{
break;
}
// Store key press state (Press and Release)
input.SetKeys(cpu.key);
std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
return 0;
}
void load()
{
std::vector<char> buffer;
std::ifstream f("./PONG", std::ios::in | std::ios::binary | std::ios::ate);
int fsize = f.tellg();
f.seekg(std::ios::beg);
buffer.resize(fsize);
f.read(buffer.data(), fsize);
f.close();
cpu.LoadProgram(buffer);
}