-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreen.cpp
More file actions
54 lines (45 loc) · 1.1 KB
/
Screen.cpp
File metadata and controls
54 lines (45 loc) · 1.1 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
#include "Screen.h"
namespace chip8
{
Screen::Screen(std::shared_ptr<sf::RenderWindow> renderWindow,
unsigned int screenWidth, unsigned int screenHeight, float spriteScale)
: width(screenWidth)
, height(screenHeight)
, scale(spriteScale)
, window(renderWindow)
{
init();
}
void Screen::init()
{
pixels.resize(std::size_t(width) * height);
screen.resize(std::size_t(width) * height);
texture.create(width, height);
sprite.setTexture(texture);
sprite.setScale({ scale,scale });
}
void Screen::clear()
{
std::fill(screen.begin(), screen.end(), 0);
}
Byte Screen::setPixel(int x, int y, int color)
{
Word addr = (y * width + x) % (width * height);
screen[addr] ^= color;
return screen[addr];
}
int Screen::getPixel(int x, int y) const
{
Word addr = (y * width + x) % (width * height);
return screen[addr];
}
void Screen::update()
{
for (std::size_t i = 0; i < pixels.size(); ++i)
pixels[i] = (screen[i] == 1) ? kForeColor : kBackColor;
texture.update(reinterpret_cast<sf::Uint8*>(pixels.data()));
window->clear();
window->draw(sprite);
window->display();
}
}