From 509c6ceeca4f8ab4b14606df0140d3974a970be5 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Sun, 11 May 2025 03:29:09 +0200 Subject: [PATCH] feat: update Ball and PhysicalObject classes to include window size/resize --- src/Ball.cpp | 12 ++++++------ src/Ball.hpp | 4 ++-- src/Constants.hpp | 2 +- src/Game.cpp | 26 ++++++++++++++++++++++++-- src/Game.hpp | 1 + src/PhysicalObject.hpp | 2 +- 6 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/Ball.cpp b/src/Ball.cpp index 1be7adb..6f97af1 100644 --- a/src/Ball.cpp +++ b/src/Ball.cpp @@ -16,7 +16,7 @@ Ball::Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel, m_shape.setFillColor(color); } -void Ball::update(float dt) { +void Ball::update(float dt, const sf::Vector2f& windowSize) { if (m_atRest) return; m_velocity.y += Constants::GRAVITY * dt; m_shape.move(m_velocity * dt); @@ -25,7 +25,7 @@ void Ball::update(float dt) { m_pixelVelocity = (currentPosition - m_lastPosition) / dt; m_lastPosition = currentPosition; - handleWallCollision(); + handleWallCollision(windowSize); } void Ball::draw(sf::RenderWindow& window) { window.draw(m_shape); } @@ -41,7 +41,7 @@ sf::Vector2f Ball::getVelocity() const { return m_velocity; } bool Ball::isAtRest() const { return m_atRest; } -void Ball::handleWallCollision() { +void Ball::handleWallCollision(const sf::Vector2f& windowSize) { sf::Vector2f pos = m_shape.getPosition(); auto handleAxis = [&](int axis, float min, float max, float& velocity, @@ -60,12 +60,12 @@ void Ball::handleWallCollision() { (axis == 0 ? pos.x : pos.y) = value; }; - handleAxis(0, 0.f, Constants::WIDTH, m_velocity.x, m_radius, + handleAxis(0, 0.f, windowSize.x, m_velocity.x, m_radius, Constants::RESTITUTION); - handleAxis(1, 0.f, Constants::HEIGHT, m_velocity.y, m_radius, + handleAxis(1, 0.f, windowSize.y, m_velocity.y, m_radius, Constants::RESTITUTION); - if (pos.y + m_radius >= Constants::HEIGHT - 1.0f) { + if (pos.y + m_radius >= windowSize.y - 1.0f) { m_velocity.x *= Constants::FRICTION; if (std::abs(m_velocity.x) < 5.f) m_velocity.x = 0.f; if (std::abs(m_pixelVelocity.x) < Constants::REST_PIXEL_VELOCITY && diff --git a/src/Ball.hpp b/src/Ball.hpp index fb28d17..3219d08 100644 --- a/src/Ball.hpp +++ b/src/Ball.hpp @@ -12,13 +12,13 @@ class Ball : public PhysicalObject { sf::Vector2f m_lastPosition; sf::Vector2f m_pixelVelocity; - void handleWallCollision(); + void handleWallCollision(const sf::Vector2f& windowSize); public: Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel, const sf::Color& color); - void update(float dt) override; + void update(float dt, const sf::Vector2f& windowSize) override; void draw(sf::RenderWindow& window) override; void applyImpulse(const sf::Vector2f& impulse) override; void resolveCollision(Ball& other); diff --git a/src/Constants.hpp b/src/Constants.hpp index bf658fc..f639dab 100644 --- a/src/Constants.hpp +++ b/src/Constants.hpp @@ -9,5 +9,5 @@ constexpr float FRICTION = 0.9f; constexpr float BALL_RADIUS = 20.f; constexpr float IMPULSE = 2500.f; constexpr float REST_PIXEL_VELOCITY = 2.0f; -constexpr unsigned BALL_QUANTITY = 10; +constexpr unsigned BALL_QUANTITY = 500; } // namespace Constants diff --git a/src/Game.cpp b/src/Game.cpp index d51c180..d75f891 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -21,6 +21,7 @@ Game::Game() m_window.create(sf::VideoMode({Constants::WIDTH, Constants::HEIGHT}), "SFML Playground"); m_window.setVerticalSyncEnabled(true); + m_windowSize = sf::Vector2f(Constants::WIDTH, Constants::HEIGHT); m_objects.clear(); auto balls = BallFactory::generateBalls(); @@ -108,7 +109,11 @@ void Game::update() { if (dt > 0.1f) dt = 0.1f; for (auto &object : m_objects) { - object->update(dt); + if (auto *ball = dynamic_cast(object.get())) { + ball->update(dt, m_windowSize); + } else { + object->update(dt, m_windowSize); + } } auto grid = buildSpatialGrid(); @@ -143,7 +148,24 @@ void Game::render() { void Game::run() { while (m_window.isOpen()) { - m_inputManager.processEvents(m_window); + while (const std::optional event = m_window.pollEvent()) { + if (event->is()) { + m_window.close(); + return; + } + if (const auto *resized = event->getIf()) { + sf::Vector2f position(0.f, 0.f); + sf::Vector2f size(static_cast(resized->size.x), + static_cast(resized->size.y)); + sf::FloatRect visibleArea(position, size); + m_window.setView(sf::View(visibleArea)); + m_windowSize = size; + } + if (auto kP = event->getIf()) + processKeyPressed(*kP); + if (auto mP = event->getIf()) + processMousePressed(*mP); + } update(); render(); } diff --git a/src/Game.hpp b/src/Game.hpp index 1ca7068..991cc48 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -22,6 +22,7 @@ class Game { private: float m_timeScale = 1.0f; sf::RenderWindow m_window; + sf::Vector2f m_windowSize; std::vector> m_objects; sf::Clock m_clock; InputManager m_inputManager; diff --git a/src/PhysicalObject.hpp b/src/PhysicalObject.hpp index 2f6f4fd..bfef246 100644 --- a/src/PhysicalObject.hpp +++ b/src/PhysicalObject.hpp @@ -5,7 +5,7 @@ class PhysicalObject { public: virtual ~PhysicalObject() = default; - virtual void update(float dt) = 0; + virtual void update(float dt, const sf::Vector2f& windowSize) = 0; virtual void draw(sf::RenderWindow& window) = 0; virtual void applyImpulse(const sf::Vector2f& impulse) = 0; };