Skip to content

Commit

Permalink
Simplify management of window object
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Jul 8, 2024
1 parent 39b47d5 commit 47374e8
Showing 1 changed file with 75 additions and 77 deletions.
152 changes: 75 additions & 77 deletions src/Mandelbrot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,91 +88,89 @@ int main()
auto window
= sf::RenderWindow(sf::VideoMode({ length, length }), "Mandelbrot", sf::Style::Default ^ sf::Style::Resize);
window.setFramerateLimit(60);
while (window.isOpen()) {
while (const std::optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>()) {
window.close();
} else if (const auto* key_pressed = event->getIf<sf::Event::KeyPressed>()) {
switch (key_pressed->scancode) {
case sf::Keyboard::Scan::Escape:
window.close();
break;
case sf::Keyboard::Scan::Up:
origin = { origin.real(), origin.imag() + extent / 25 };
break;
case sf::Keyboard::Scan::Down:
origin = { origin.real(), origin.imag() - extent / 25 };
break;
case sf::Keyboard::Scan::Left:
origin = { origin.real() - extent / 25, origin.imag() };
break;
case sf::Keyboard::Scan::Right:
origin = { origin.real() + extent / 25, origin.imag() };
break;
case sf::Keyboard::Scan::W:
extent /= 1.5;
zoom_sound.setPitch(zoom_sound.getPitch() * 1.02f);

while (const std::optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>()) {
return 0;
} else if (const auto* key_pressed = event->getIf<sf::Event::KeyPressed>()) {
switch (key_pressed->scancode) {
case sf::Keyboard::Scan::Escape:
return 0;
case sf::Keyboard::Scan::Up:
origin = { origin.real(), origin.imag() + extent / 25 };
break;
case sf::Keyboard::Scan::Down:
origin = { origin.real(), origin.imag() - extent / 25 };
break;
case sf::Keyboard::Scan::Left:
origin = { origin.real() - extent / 25, origin.imag() };
break;
case sf::Keyboard::Scan::Right:
origin = { origin.real() + extent / 25, origin.imag() };
break;
case sf::Keyboard::Scan::W:
extent /= 1.5;
zoom_sound.setPitch(zoom_sound.getPitch() * 1.02f);
zoom_sound.play();
break;
case sf::Keyboard::Scan::S:
if (extent * 1.5 <= max_extent) {
extent = extent * 1.5;
zoom_sound.setPitch(zoom_sound.getPitch() / 1.02f);
zoom_sound.play();
break;
case sf::Keyboard::Scan::S:
if (extent * 1.5 <= max_extent) {
extent = extent * 1.5;
zoom_sound.setPitch(zoom_sound.getPitch() / 1.02f);
zoom_sound.play();
}
break;
case sf::Keyboard::Scan::R:
origin = initial_origin;
extent = initial_extent;
iteration_limit = initial_iteration_limit;
zoom_sound.setPitch(1);
break;
case sf::Keyboard::Scan::RBracket:
iteration_limit += 25;
break;
case sf::Keyboard::Scan::LBracket:
iteration_limit = std::max(iteration_limit - 25, 25);
break;
default:
break;
}
recalculate = true;
} else if (const auto* mouse_button_pressed = event->getIf<sf::Event::MouseButtonPressed>()) {
origin += extent
* Complex(double(mouse_button_pressed->position.x) / double(window.getSize().x) - 0.5,
-double(mouse_button_pressed->position.y) / double(window.getSize().y) + 0.5);
recalculate = true;
} else if (const auto* mouse_wheel_scrolled = event->getIf<sf::Event::MouseWheelScrolled>()) {
if (mouse_wheel_scrolled->delta > 0)
extent /= 1.2;
else if (mouse_wheel_scrolled->delta < 0)
extent = std::min(extent * 1.2, max_extent);
recalculate = true;
break;
case sf::Keyboard::Scan::R:
origin = initial_origin;
extent = initial_extent;
iteration_limit = initial_iteration_limit;
zoom_sound.setPitch(1);
break;
case sf::Keyboard::Scan::RBracket:
iteration_limit += 25;
break;
case sf::Keyboard::Scan::LBracket:
iteration_limit = std::max(iteration_limit - 25, 25);
break;
default:
break;
}
recalculate = true;
} else if (const auto* mouse_button_pressed = event->getIf<sf::Event::MouseButtonPressed>()) {
origin += extent
* Complex(double(mouse_button_pressed->position.x) / double(window.getSize().x) - 0.5,
-double(mouse_button_pressed->position.y) / double(window.getSize().y) + 0.5);
recalculate = true;
} else if (const auto* mouse_wheel_scrolled = event->getIf<sf::Event::MouseWheelScrolled>()) {
if (mouse_wheel_scrolled->delta > 0)
extent /= 1.2;
else if (mouse_wheel_scrolled->delta < 0)
extent = std::min(extent * 1.2, max_extent);
recalculate = true;
}
}

window.clear();
window.clear();

if (recalculate) {
recalculate = false;
if (recalculate) {
recalculate = false;

const auto rows_per_thread = std::size_t(length / threads.size());
for (std::size_t i = 0; i < threads.size(); ++i)
threads[i] = std::thread(render_rows, i * rows_per_thread, (i + 1) * rows_per_thread);
for (auto& thread : threads)
thread.join();
const auto rows_per_thread = std::size_t(length / threads.size());
for (std::size_t i = 0; i < threads.size(); ++i)
threads[i] = std::thread(render_rows, i * rows_per_thread, (i + 1) * rows_per_thread);
for (auto& thread : threads)
thread.join();

texture = sf::Texture::loadFromImage(image).value();
}
texture = sf::Texture::loadFromImage(image).value();
}

window.draw(sf::Sprite(*texture));
window.draw(text);
window.display();
window.draw(sf::Sprite(*texture));
window.draw(text);
window.display();

auto text_builder = std::ostringstream();
text_builder << std::setw(2) << int(1 / clock.restart().asSeconds()) << " fps\n";
text_builder << iteration_limit << " iters\n";
text_builder << std::setprecision(1) << std::scientific << initial_extent / extent << '\n';
text.setString(text_builder.str());
}
auto text_builder = std::ostringstream();
text_builder << std::setw(2) << int(1 / clock.restart().asSeconds()) << " fps\n";
text_builder << iteration_limit << " iters\n";
text_builder << std::setprecision(1) << std::scientific << initial_extent / extent << '\n';
text.setString(text_builder.str());
}

0 comments on commit 47374e8

Please sign in to comment.