-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from stevenewald/refactor
Refactor into observer pattern
- Loading branch information
Showing
19 changed files
with
465 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.