-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.cpp
91 lines (69 loc) · 2.32 KB
/
graphics.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
#include <iostream>
#include <algorithm>
#include "graphics.h"
namespace Chip8 {
const int kGraphicsWidth = 64;
const int kGraphicsHeight = 32;
const int kGraphicsScale = 10;
Graphics::Graphics() {
_window = SDL_CreateWindow(
"Chip8",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
kGraphicsWidth * kGraphicsScale,
kGraphicsHeight * kGraphicsScale,
SDL_WINDOW_SHOWN
);
if (_window == nullptr)
std::cout << "error: " << SDL_GetError() << std::endl;
_renderer = SDL_CreateRenderer(
_window,
-1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
);
if (_window == nullptr)
std::cout << "error: " << SDL_GetError() << std::endl;
_dirty_buffer = false;
clear();
}
Graphics::~Graphics() {
SDL_DestroyRenderer(_renderer);
SDL_DestroyWindow(_window);
}
void Graphics::set(int x, int y, bool value) {
//std::cout << "[graphics] settingx: " << x << " settingy: " << y << std::endl;
_gfx[(kGraphicsWidth * y) + x - 1] = value;
_dirty_buffer = true;
}
bool Graphics::get(int x, int y) const {
return _gfx[(kGraphicsWidth * y) + x - 1];
}
bool Graphics::dirty() const {
return _dirty_buffer;
}
void Graphics::refresh() {
if (SDL_RenderClear(_renderer) != 0) {
std::cout << "[graphics] clear error: " << SDL_GetError() << std::endl;
}
for (int pixel = 0; pixel < kGraphicsWidth * kGraphicsHeight; pixel++) {
SDL_Rect scaled_pixel = {
(pixel % kGraphicsWidth) * kGraphicsScale,
(pixel / kGraphicsWidth) * kGraphicsScale,
kGraphicsScale,
kGraphicsScale
};
if (_gfx[pixel]) {
SDL_SetRenderDrawColor(_renderer, 255, 255, 255, 255);
} else {
SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 255);
}
SDL_RenderFillRect(_renderer, &scaled_pixel);
}
_dirty_buffer = false;
SDL_RenderPresent(_renderer);
}
void Graphics::clear() {
std::fill_n(_gfx, sizeof(_gfx), 0);
_dirty_buffer = true;
}
} // namespace Chip8