Skip to content

Commit efa4d7f

Browse files
committed
Slightly improve code quality of basic display
1 parent 7f7f0c5 commit efa4d7f

File tree

4 files changed

+53
-46
lines changed

4 files changed

+53
-46
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ include(cmake/variables.cmake)
1818
add_library(
1919
fractal-generator_lib OBJECT
2020
source/lib.cpp
21+
source/graphics/basic_display.cpp
2122
)
2223

2324
target_include_directories(

source/graphics/basic_display.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "basic_display.hpp"
2+
3+
#include <SFML/Graphics.hpp>
4+
5+
namespace fractal {
6+
void BasicDisplay::set_pixel(std::size_t x_pos, std::size_t y_pos, uint8_t value)
7+
{
8+
pixels_.at(x_pos).at(y_pos) = value;
9+
}
10+
11+
void BasicDisplay::display_window()
12+
{
13+
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML Window");
14+
15+
window.setFramerateLimit(FRAME_RATE);
16+
17+
sf::Image image;
18+
image.create(WINDOW_WIDTH, WINDOW_HEIGHT);
19+
for (std::size_t x_pos = 0; x_pos < WINDOW_WIDTH; ++x_pos) {
20+
for (std::size_t y_pos = 0; y_pos < WINDOW_HEIGHT; ++y_pos) {
21+
std::uint8_t pixel_value = pixels_.at(x_pos).at(y_pos);
22+
image.setPixel(
23+
static_cast<unsigned int>(x_pos), static_cast<unsigned int>(y_pos),
24+
sf::Color(pixel_value, pixel_value, pixel_value)
25+
);
26+
}
27+
}
28+
29+
sf::Texture texture;
30+
texture.loadFromImage(image);
31+
32+
sf::Sprite sprite(texture);
33+
34+
while (window.isOpen()) {
35+
sf::Event event;
36+
while (window.pollEvent(event)) {
37+
if (event.type == sf::Event::Closed) {
38+
window.close();
39+
}
40+
}
41+
42+
window.clear(sf::Color::Black);
43+
44+
window.draw(sprite);
45+
46+
window.display();
47+
}
48+
}
49+
} // namespace fractal

source/graphics/basic_display.hpp

+2-45
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
#include "config.hpp"
44

5-
#include <SFML/Graphics.hpp>
6-
7-
#include <cstddef>
85
#include <cstdint>
96

107
#include <array>
@@ -14,47 +11,7 @@ class BasicDisplay {
1411
std::array<std::array<uint8_t, WINDOW_HEIGHT>, WINDOW_WIDTH> pixels_{};
1512

1613
public:
17-
void set_pixel(std::size_t x_pos, std::size_t y_pos, uint8_t value)
18-
{
19-
pixels_.at(x_pos).at(y_pos) = value;
20-
}
21-
22-
void display_window()
23-
{
24-
sf::RenderWindow window(
25-
sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML Window"
26-
);
27-
28-
window.setFramerateLimit(FRAME_RATE);
29-
30-
sf::Image image;
31-
image.create(WINDOW_WIDTH, WINDOW_HEIGHT);
32-
for (std::size_t x = 0; x < WINDOW_WIDTH; ++x) {
33-
for (std::size_t y = 0; y < WINDOW_HEIGHT; ++y) {
34-
uint8_t pixel_value = pixels_.at(x).at(y);
35-
image.setPixel(x, y, sf::Color(pixel_value, pixel_value, pixel_value));
36-
}
37-
}
38-
39-
sf::Texture texture;
40-
texture.loadFromImage(image);
41-
42-
sf::Sprite sprite(texture);
43-
44-
while (window.isOpen()) {
45-
sf::Event event;
46-
while (window.pollEvent(event)) {
47-
if (event.type == sf::Event::Closed) {
48-
window.close();
49-
}
50-
}
51-
52-
window.clear(sf::Color::Black);
53-
54-
window.draw(sprite);
55-
56-
window.display();
57-
}
58-
}
14+
void set_pixel(std::size_t x_pos, std::size_t y_pos, std::uint8_t value);
15+
void display_window();
5916
};
6017
} // namespace fractal

source/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
int main()
44
{
55
fractal::BasicDisplay display;
6-
for (size_t i = 0; i < 100; i++) {
6+
for (std::size_t i = 0; i < 100; i++) {
77
display.set_pixel(100, 100 + i, 255);
88
}
99
display.display_window();

0 commit comments

Comments
 (0)