Skip to content

Commit 2fb74f5

Browse files
committed
Finish refactor/cleanup
1 parent 75930cd commit 2fb74f5

17 files changed

+283
-209
lines changed

CMakeLists.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ include(cmake/variables.cmake)
1717

1818
add_library(
1919
fractal-generator_lib OBJECT
20-
source/graphics/basic_display.cpp
21-
source/graphics/color_conversions.cpp
20+
source/graphics/display/display.cpp
21+
source/graphics/selection_window/selection_window.cpp
22+
source/mandelbrot/mandelbrot_window.cpp
23+
source/graphics/color_conversions/color_conversions.cpp
24+
source/graphics/aspect_ratio/aspect_ratio.cpp
2225
)
2326

2427
target_include_directories(

source/config.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "coordinates.hpp"
4+
#include "units.hpp"
45

56
#include <cstddef>
67

@@ -19,4 +20,7 @@ constexpr complex_domain START_COMPLEX_DOMAIN{
1920
{complex_underlying{-2}, complex_underlying{-1.5}},
2021
{complex_underlying{1}, complex_underlying{1.5} }
2122
};
23+
24+
const complex_underlying MANDELBROT_DIVERGENCE_NORM = 4;
25+
const iteration_count MANDELBROT_MAX_ITERATIONS = 256;
2226
} // namespace fractal

source/graphics/ends.hpp source/graphics/aspect_ratio/aspect_ratio.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#pragma once
2-
3-
#include "coordinates.hpp"
1+
#include "aspect_ratio.hpp"
42

53
namespace fractal {
6-
inline display_coordinate calculate_end_points(
7-
display_coordinate start, display_coordinate current,
8-
float target_aspect_ratio = 800.0f / 600.0f
4+
5+
display_coordinate calculate_rectangle_end_point(
6+
display_coordinate start, display_coordinate current, float target_aspect_ratio
97
)
108
{
119
auto width = static_cast<float>(std::abs(current.first - start.first));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "coordinates.hpp"
4+
5+
namespace fractal {
6+
display_coordinate calculate_rectangle_end_point(
7+
display_coordinate start, display_coordinate current,
8+
float target_aspect_ratio = 800.0f / 600.0f
9+
);
10+
} // namespace fractal

source/graphics/color_conversions.cpp source/graphics/color_conversions/color_conversions.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ color hsv_to_rgb(float hue, float saturation, float value)
6262

6363
color ratio_to_rgb(float ratio)
6464
{
65-
if (ratio < 0 || ratio > 1) {
65+
if (ratio < 0 || ratio > 1) [[unlikely]] {
6666
throw std::out_of_range(fmt::format("Ratio out of range: {}", ratio));
6767
}
68-
float hue = static_cast<float>(ratio) * 360.0f;
68+
69+
float hue = ratio * 360.0f;
6970
float saturation = 1.0f;
7071
float value = 1.0f;
7172

source/graphics/basic_display.cpp source/graphics/display/display.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
#include "basic_display.hpp"
1+
#include "display.hpp"
22

33
#include <fmt/format.h>
44

55
#include <cmath>
66

77
namespace fractal {
8+
PixelDisplay::PixelDisplay()
9+
{
10+
window_.clear(sf::Color::Black);
11+
window_.setFramerateLimit(FRAME_RATE);
12+
window_.display();
13+
}
814

9-
void BasicDisplay::handle_event_(const sf::Event& event)
15+
void PixelDisplay::handle_event_(const sf::Event& event)
1016
{
1117
switch (event.type) {
1218
case sf::Event::MouseMoved:
@@ -31,35 +37,33 @@ void BasicDisplay::handle_event_(const sf::Event& event)
3137
);
3238
return;
3339
default:
34-
break;
40+
return;
3541
}
3642
}
3743

38-
void BasicDisplay::add_observer(std::unique_ptr<DisplayEventObserver> observer)
44+
void PixelDisplay::add_observer(std::unique_ptr<DisplayEventObserver> observer)
3945
{
4046
observers_.push_back(std::move(observer));
4147
}
4248

43-
void BasicDisplay::poll_window_events()
49+
void PixelDisplay::poll_window_events()
4450
{
4551
sf::Event event{};
4652
while (window_.pollEvent(event)) {
4753
handle_event_(event);
4854
}
4955
}
5056

51-
void BasicDisplay::display_window()
57+
void PixelDisplay::display_window()
5258
{
53-
window_.clear(sf::Color::Black);
54-
5559
auto draw_from_observer = [this](const auto& observer) {
5660
if (auto opt = observer->get_drawable(); opt) {
5761
window_.draw(*(opt.value()));
5862
}
5963
};
6064

65+
window_.clear(sf::Color::Black);
6166
std::for_each(observers_.begin(), observers_.end(), draw_from_observer);
62-
6367
window_.display();
6468
}
6569
} // namespace fractal

source/graphics/basic_display.hpp source/graphics/display/display.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
#include "graphics/display_event_observer.hpp"
55

66
#include <SFML/Graphics.hpp>
7+
#include <SFML/Graphics/Color.hpp>
78
#include <SFML/Graphics/RectangleShape.hpp>
89
#include <SFML/System/Vector2.hpp>
910

1011
#include <memory>
11-
#include <utility>
1212
#include <vector>
1313

1414
namespace fractal {
15-
class BasicDisplay {
15+
class PixelDisplay {
1616
sf::RenderWindow window_{sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML Window"};
1717
std::vector<std::unique_ptr<DisplayEventObserver>> observers_;
1818

1919
void handle_event_(const sf::Event& event);
2020

2121
public:
22-
explicit BasicDisplay() { window_.setFramerateLimit(FRAME_RATE); }
22+
explicit PixelDisplay();
2323

2424
void add_observer(std::unique_ptr<DisplayEventObserver> observer);
2525

source/graphics/selection.hpp

-61
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "selection_window.hpp"
2+
3+
#include "coordinates.hpp"
4+
#include "graphics/aspect_ratio/aspect_ratio.hpp"
5+
6+
namespace fractal {
7+
8+
void SelectionWindow::on_mouse_moved(const sf::Event::MouseMoveEvent& event)
9+
{
10+
current_mouse_x_ = static_cast<float>(event.x);
11+
current_mouse_y_ = static_cast<float>(event.y);
12+
}
13+
14+
void SelectionWindow::on_mouse_button_pressed(const sf::Event::MouseButtonEvent& event)
15+
{
16+
if (event.button != sf::Mouse::Left) {
17+
return;
18+
}
19+
selection_start_x_ = static_cast<float>(event.x);
20+
selection_start_y_ = static_cast<float>(event.y);
21+
left_mouse_down_ = true;
22+
}
23+
24+
void SelectionWindow::on_mouse_button_released(const sf::Event::MouseButtonEvent& event)
25+
{
26+
if (event.button != sf::Mouse::Left) {
27+
return;
28+
}
29+
left_mouse_down_ = false;
30+
}
31+
32+
std::optional<std::unique_ptr<sf::Drawable>> SelectionWindow::get_drawable()
33+
{
34+
if (!left_mouse_down_)
35+
return std::nullopt;
36+
display_coordinate end_point = calculate_rectangle_end_point(
37+
{selection_start_x_, selection_start_y_}, {current_mouse_x_, current_mouse_y_}
38+
);
39+
40+
auto rectangle = std::make_unique<sf::RectangleShape>(sf::Vector2f{
41+
static_cast<float>(end_point.first) - selection_start_x_,
42+
static_cast<float>(end_point.second) - selection_start_y_
43+
});
44+
rectangle->setPosition(selection_start_x_, selection_start_y_);
45+
rectangle->setFillColor(sf::Color(255, 0, 0, 127));
46+
return rectangle;
47+
}
48+
} // namespace fractal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include "graphics/display_event_observer.hpp"
4+
5+
#include <SFML/Graphics/Drawable.hpp>
6+
#include <SFML/Graphics/RectangleShape.hpp>
7+
#include <SFML/System/Vector2.hpp>
8+
9+
namespace fractal {
10+
class SelectionWindow : public DisplayEventObserver {
11+
float selection_start_x_{};
12+
float selection_start_y_{};
13+
float current_mouse_x_{};
14+
float current_mouse_y_{};
15+
bool left_mouse_down_ = false;
16+
17+
void on_mouse_moved(const sf::Event::MouseMoveEvent& event) override;
18+
19+
void on_mouse_button_pressed(const sf::Event::MouseButtonEvent& event) override;
20+
21+
void on_mouse_button_released(const sf::Event::MouseButtonEvent& event) override;
22+
23+
std::optional<std::unique_ptr<sf::Drawable>> get_drawable() override;
24+
};
25+
} // namespace fractal

source/main.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
#include "graphics/basic_display.hpp"
2-
#include "graphics/selection.hpp"
3-
#include "mandelbrot.hpp"
1+
#include "graphics/display/display.hpp"
2+
#include "graphics/selection_window/selection_window.hpp"
3+
#include "mandelbrot/mandelbrot_window.hpp"
44

55
#include <argparse/argparse.hpp>
66

77
namespace fractal {
88

99
void display_mandelbrot()
1010
{
11-
BasicDisplay display;
11+
PixelDisplay display;
1212

13-
display.add_observer(std::make_unique<Mandelbrot>());
13+
display.add_observer(std::make_unique<MandelbrotWindow>());
1414
display.add_observer(std::make_unique<SelectionWindow>());
1515

1616
while (true) {

0 commit comments

Comments
 (0)