Skip to content

Commit

Permalink
Examples/Mandelbrot: Update SFML version to 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Dec 26, 2024
1 parent c77c66c commit a28f9a1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 49 deletions.
76 changes: 28 additions & 48 deletions examples/sfml-mandelbrot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

int main()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "SFML + NZSL Shader", sf::Style::Titlebar | sf::Style::Close | sf::Style::Resize);
sf::RenderWindow window(sf::VideoMode(sf::Vector2u(1280, 720)), "SFML + NZSL Shader", sf::Style::Titlebar | sf::Style::Close | sf::Style::Resize);
window.setVerticalSyncEnabled(true);

auto mandelbrotShader = nzsl::ParseFromFile("mandelbrot.nzsl");
Expand All @@ -15,7 +15,7 @@ int main()
auto glslShader = glslWriter.Generate(*mandelbrotShader);

sf::Shader shader;
if (!shader.loadFromMemory(glslShader.code, sf::Shader::Fragment))
if (!shader.loadFromMemory(glslShader.code, sf::Shader::Type::Fragment))
{
std::cerr << "failed to load fragment shader" << std::endl;
return 0;
Expand Down Expand Up @@ -49,60 +49,40 @@ int main()
sf::Clock updateClock;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
while (std::optional<sf::Event> event = window.pollEvent())
{
switch (event.type)
if (event->is<sf::Event::Closed>())
{
case sf::Event::Closed:
{
window.close();
break;
}
else if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>())
{
if (keyPressed->code == sf::Keyboard::Key::Escape)
window.close();
break;
}

case sf::Event::KeyPressed:
{
if (event.key.code == sf::Keyboard::Escape)
window.close();
break;
}

case sf::Event::MouseMoved:
{
sf::Vector2i newPos(event.mouseMove.x, event.mouseMove.y);
sf::Vector2i delta = newPos - mousePos;
mousePos = newPos;

if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
center += scale * sf::Vector2f(sf::Vector2i(delta.x, -delta.y));
shader.setUniform("center", center);
break;
}

break;
}
}
else if (const auto* mouseMoved = event->getIf<sf::Event::MouseMoved>())
{
sf::Vector2i delta = mouseMoved->position - mousePos;
mousePos = mouseMoved->position;

case sf::Event::MouseWheelScrolled:
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
{
targetScale = std::clamp(targetScale - targetScale * 0.1f * event.mouseWheelScroll.delta, 0.000001f, 3.f);
break;
center += scale * sf::Vector2f(sf::Vector2i(delta.x, -delta.y));
shader.setUniform("center", center);
}
}
else if (const auto* mouseWheelScrolled = event->getIf<sf::Event::MouseWheelScrolled>())
targetScale = std::clamp(targetScale - targetScale * 0.1f * mouseWheelScrolled->delta, 0.000001f, 3.f);
else if (const auto* resized = event->getIf<sf::Event::Resized>())
{
sf::Vector2f newSize(float(resized->size.x), float(resized->size.y));

case sf::Event::Resized:
{
sf::Vector2f newSize(float(event.size.width), float(event.size.height));

shader.setUniform("screen_size", newSize);
fullscreenShape.setSize(newSize);

sf::FloatRect visibleArea(0.f, 0.f, newSize.x, newSize.y);
window.setView(sf::View(visibleArea));
break;
}
shader.setUniform("screen_size", newSize);
fullscreenShape.setSize(newSize);

default:
break;
sf::FloatRect visibleArea(sf::Vector2f(0.f, 0.f), sf::Vector2f(newSize.x, newSize.y));
window.setView(sf::View(visibleArea));
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/sfml-mandelbrot/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ if is_plat("android", "iphoneos", "wasm") then
return
end

add_requires("sfml", { configs = { audio = false, network = false }})
add_requires("sfml 3.0", { configs = { audio = false, network = false }})

target("sfml-mandelbrot")
set_group("Examples")
Expand Down

0 comments on commit a28f9a1

Please sign in to comment.