Skip to content

Commit

Permalink
Merge pull request #7 from stevenewald/refactor
Browse files Browse the repository at this point in the history
Refactor into observer pattern
  • Loading branch information
stevenewald authored Nov 25, 2024
2 parents 2bb27f3 + 2fb74f5 commit 1791ea2
Show file tree
Hide file tree
Showing 19 changed files with 465 additions and 264 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ include(cmake/variables.cmake)

add_library(
fractal-generator_lib OBJECT
source/graphics/basic_display.cpp
source/graphics/color_conversions.cpp
source/graphics/display/display.cpp
source/graphics/selection_window/selection_window.cpp
source/mandelbrot/mandelbrot_window.cpp
source/graphics/color_conversions/color_conversions.cpp
source/graphics/aspect_ratio/aspect_ratio.cpp
)

target_include_directories(
Expand Down
17 changes: 17 additions & 0 deletions source/config.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
#pragma once

#include "coordinates.hpp"
#include "units.hpp"

#include <cstddef>

namespace fractal {

constexpr std::size_t WINDOW_WIDTH = 800UZ;
constexpr std::size_t WINDOW_HEIGHT = 600UZ;
constexpr std::size_t FRAME_RATE = 60UZ;

constexpr display_domain DISPLAY_DOMAIN{
{0, 0 },
{WINDOW_WIDTH - 1, WINDOW_HEIGHT - 1}
};

constexpr complex_domain START_COMPLEX_DOMAIN{
{complex_underlying{-2}, complex_underlying{-1.5}},
{complex_underlying{1}, complex_underlying{1.5} }
};

const complex_underlying MANDELBROT_DIVERGENCE_NORM = 4;
const iteration_count MANDELBROT_MAX_ITERATIONS = 256;
} // namespace fractal
36 changes: 36 additions & 0 deletions source/graphics/aspect_ratio/aspect_ratio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "aspect_ratio.hpp"

namespace fractal {

display_coordinate calculate_rectangle_end_point(
display_coordinate start, display_coordinate current, float target_aspect_ratio
)
{
auto width = static_cast<float>(std::abs(current.first - start.first));
auto height = static_cast<float>(std::abs(current.second - start.second));

// Adjust the dimensions to maintain the target aspect ratio
if (width / height > target_aspect_ratio) {
// Too wide, adjust width
width = height * target_aspect_ratio;
}
else {
// Too tall, adjust height
height = width / target_aspect_ratio;
}

auto x = static_cast<float>(std::min(current.first, start.first));
auto y = static_cast<float>(std::min(current.second, start.second));

// Adjust the top-left corner based on new dimensions
if (current.first < start.first) {
x = static_cast<float>(start.first) - width;
}
if (current.second < start.second) {
y = static_cast<float>(start.second) - height;
}

// Return the top-left and bottom-right corners as a pair of sf::Vector2f
return {x + width, y + height};
}
} // namespace fractal
10 changes: 10 additions & 0 deletions source/graphics/aspect_ratio/aspect_ratio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "coordinates.hpp"

namespace fractal {
display_coordinate calculate_rectangle_end_point(
display_coordinate start, display_coordinate current,
float target_aspect_ratio = 800.0f / 600.0f
);
} // namespace fractal
127 changes: 0 additions & 127 deletions source/graphics/basic_display.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions source/graphics/basic_display.hpp

This file was deleted.

12 changes: 0 additions & 12 deletions source/graphics/color_conversions.hpp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "color_conversions.hpp"

#include <fmt/format.h>

#include <cmath>

#include <stdexcept>

namespace fractal {
std::tuple<uint16_t, uint16_t, uint16_t>
hsv_to_rgb(float hue, float saturation, float value)
color hsv_to_rgb(float hue, float saturation, float value)
{
float chroma = value * saturation;
float hue_prime = hue / 60.0f;
Expand Down Expand Up @@ -50,16 +53,20 @@ hsv_to_rgb(float hue, float saturation, float value)
float green = green_temp + match_value;
float blue = blue_temp + match_value;

auto red_int = static_cast<uint16_t>(red * 255);
auto green_int = static_cast<uint16_t>(green * 255);
auto blue_int = static_cast<uint16_t>(blue * 255);
auto red_int = static_cast<uint8_t>(red * 255);
auto green_int = static_cast<uint8_t>(green * 255);
auto blue_int = static_cast<uint8_t>(blue * 255);

return {red_int, green_int, blue_int};
}

std::tuple<uint16_t, uint16_t, uint16_t> number_to_rgb(uint16_t number)
color ratio_to_rgb(float ratio)
{
float hue = (static_cast<float>(number) / 65535.0f) * 360.0f;
if (ratio < 0 || ratio > 1) [[unlikely]] {
throw std::out_of_range(fmt::format("Ratio out of range: {}", ratio));
}

float hue = ratio * 360.0f;
float saturation = 1.0f;
float value = 1.0f;

Expand Down
9 changes: 9 additions & 0 deletions source/graphics/color_conversions/color_conversions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "units.hpp"

namespace fractal {
color hsv_to_rgb(float hue, float saturation, float value);

color ratio_to_rgb(float ratio);
} // namespace fractal
69 changes: 69 additions & 0 deletions source/graphics/display/display.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "display.hpp"

#include <fmt/format.h>

#include <cmath>

namespace fractal {
PixelDisplay::PixelDisplay()
{
window_.clear(sf::Color::Black);
window_.setFramerateLimit(FRAME_RATE);
window_.display();
}

void PixelDisplay::handle_event_(const sf::Event& event)
{
switch (event.type) {
case sf::Event::MouseMoved:
for (const auto& observer : observers_) {
observer->on_mouse_moved(event.mouseMove);
}
return;
case sf::Event::MouseButtonPressed:
std::for_each(
observers_.begin(), observers_.end(),
[&](const auto& observer) {
observer->on_mouse_button_pressed(event.mouseButton);
}
);
return;
case sf::Event::MouseButtonReleased:
std::for_each(
observers_.begin(), observers_.end(),
[&](const auto& observer) {
observer->on_mouse_button_released(event.mouseButton);
}
);
return;
default:
return;
}
}

void PixelDisplay::add_observer(std::unique_ptr<DisplayEventObserver> observer)
{
observers_.push_back(std::move(observer));
}

void PixelDisplay::poll_window_events()
{
sf::Event event{};
while (window_.pollEvent(event)) {
handle_event_(event);
}
}

void PixelDisplay::display_window()
{
auto draw_from_observer = [this](const auto& observer) {
if (auto opt = observer->get_drawable(); opt) {
window_.draw(*(opt.value()));
}
};

window_.clear(sf::Color::Black);
std::for_each(observers_.begin(), observers_.end(), draw_from_observer);
window_.display();
}
} // namespace fractal
Loading

0 comments on commit 1791ea2

Please sign in to comment.