-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
143 lines (126 loc) · 4.78 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cstring>
#include "chip_8.hh"
Chip8 chip8;
void beep ()
{
sf::SoundBuffer buffer;
buffer.loadFromFile("beep.wav");
sf::Sound sound;
sound.setBuffer(buffer);
sound.play();
}
int main (int argc, char* argv[])
{
chip8.initialize ();
chip8.beep_fn = beep;
chip8.load_program ("invaders.c8");
sf::RenderWindow* window = new sf::RenderWindow { sf::VideoMode (64*16, 32*16), "CHIP-8" };
sf::Event event;
window->clear ();
window->display ();
window->setFramerateLimit (1200); // 60Hz Clock
sf::VertexArray varray = sf::VertexArray (sf::PrimitiveType::Quads, 64 * 32 * 4);
std::size_t index = 0;
while (window->isOpen ())
{
chip8.emulate_cycle ();
if (chip8.draw_flag)
{
//window->clear ();
//varray.clear ();
index = 0;
for (int x = 0; x < 64; ++x)
for (int y = 0; y < 32; ++y)
{
sf::Color color = chip8.gfx[y * 64 + x] == 0 ? sf::Color::Black : sf::Color::White;
varray[index + 0] = sf::Vertex ({ x * 16.0f, y * 16.0f }, color);
varray[index + 1] = sf::Vertex ({ x * 16.0f + 16, y * 16.0f }, color);
varray[index + 2] = sf::Vertex ({ x * 16.0f + 16, y * 16.0f + 16 }, color);
varray[index + 3] = sf::Vertex ({ x * 16.0f, y * 16.0f + 16 }, color);
index += 4;
}
window->draw (varray);
window->display ();
}
for (int i = 0; i < sizeof (chip8.key); ++i)
{
if (chip8.key[i] != 0)
printf (" 0x%X: 0x%X\n", i, chip8.key[i]);
}
//printf ("\n\n\nI: 0x%X\n", chip8.I);
//printf ("pc: 0x%X\n", chip8.pc);
//printf ("delay_timer: 0x%X\n", chip8.delay_timer);
//printf ("sound_timer: 0x%X\n", chip8.sound_timer);
//printf ("sp: 0x%X\n", chip8.sp);
//printf ("draw_flag: 0x%X\n", chip8.draw_flag);
// clear all keys
memset (chip8.key, 0x0, sizeof (chip8.key));
while (window->pollEvent (event))
{
switch (event.type)
{
case sf::Event::Closed:
window->close ();
case sf::Event::KeyPressed:
switch (event.key.code)
{
case sf::Keyboard::Num1:
chip8.key[0x1] = 0x1;
chip8.beep_fn ();
break;
case sf::Keyboard::Num2:
chip8.key[0x2] = 0x1;
break;
case sf::Keyboard::Num3:
chip8.key[0x3] = 0x1;
break;
case sf::Keyboard::Num4:
chip8.key[0xC] = 0x1;
break;
case sf::Keyboard::Q:
chip8.key[0x4] = 0x1;
break;
case sf::Keyboard::W:
chip8.key[0x5] = 0x1;
break;
case sf::Keyboard::E:
chip8.key[0x6] = 0x1;
break;
case sf::Keyboard::R:
chip8.key[0xD] = 0x1;
break;
case sf::Keyboard::A:
chip8.key[0x7] = 0x1;
break;
case sf::Keyboard::S:
chip8.key[0x8] = 0x1;
break;
case sf::Keyboard::D:
chip8.key[0x9] = 0x1;
break;
case sf::Keyboard::F:
chip8.key[0xE] = 0x1;
break;
case sf::Keyboard::Y: // QWERTZ
case sf::Keyboard::Z: // QWERTY
chip8.key[0xA] = 0x1;
break;
case sf::Keyboard::X:
chip8.key[0x0] = 0x1;
break;
case sf::Keyboard::C:
chip8.key[0xB] = 0x1;
break;
case sf::Keyboard::V:
chip8.key[0xF] = 0x1;
break;
}
break;
}
}
}
return 0;
}