From ad2bdcdd313d175d8c326cafafd2722188a29382 Mon Sep 17 00:00:00 2001 From: huulong Date: Wed, 15 Jul 2020 14:56:09 +0100 Subject: [PATCH] [APP] Extracted update and render, made view, grass, m_Time members of GameApplication --- src/GameApplication.cpp | 54 +++++++++++++++++++++++------------------ src/GameApplication.h | 18 +++++++++++++- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/src/GameApplication.cpp b/src/GameApplication.cpp index ae00003..5b50d55 100644 --- a/src/GameApplication.cpp +++ b/src/GameApplication.cpp @@ -22,32 +22,31 @@ void GameApplication::init() // 60 FPS, no vsync window->setFramerateLimit(60); + + // camera view + view = std::make_unique(); + view->setCenter({1280.f * 0.5f, 720.f * 0.5f}); + view->setSize({1280.f, 720.f}); } void GameApplication::run() { // grass rectangle (goes beyond the bottom of the screen on start to allow camera motion on Y) - sf::RectangleShape grass({1280.f, 400.f}); - grass.setFillColor(sf::Color::Green); - grass.setPosition(0.f, 420.f); - - // camera view - sf::View view; - view.setCenter({1280.f * 0.5f, 720.f * 0.5f}); - view.setSize({1280.f, 720.f}); + grass = std::make_unique(sf::Vector2f{1280.f, 400.f}); + grass->setFillColor(sf::Color::Green); + grass->setPosition(0.f, 420.f); // time management sf::Clock clock; - sf::Time time; while (window->isOpen()) { // Time check sf::Time elapsedTime = clock.restart(); - time += elapsedTime; - if (time.asSeconds() > 1000 * 1000) + m_Time += elapsedTime; + if (m_Time.asSeconds() > 1000 * 1000) { - time = sf::Time::Zero; + m_Time = sf::Time::Zero; } // Event handling @@ -61,17 +60,26 @@ void GameApplication::run() } } - // move camera - view.move(0.f, std::sin(time.asSeconds()) * 50.f * elapsedTime.asSeconds()); + update(elapsedTime); + render(); + } +} + +void GameApplication::update(sf::Time elapsedTime) +{ + // move camera + view->move(0.f, std::sin(m_Time.asSeconds()) * 50.f * elapsedTime.asSeconds()); +} - // clear sky - window->clear(sf::Color::Cyan); +void GameApplication::render() +{ + // clear sky + window->clear(sf::Color::Cyan); - // show grass with moving camera - window->setView(view); - window->draw(grass); + // show grass with moving camera + window->setView(*view); + window->draw(*grass); - // flip - window->display(); - } -} + // flip + window->display(); +} \ No newline at end of file diff --git a/src/GameApplication.h b/src/GameApplication.h index f9cbebb..5200c55 100644 --- a/src/GameApplication.h +++ b/src/GameApplication.h @@ -1,10 +1,13 @@ #pragma once #include +#include namespace sf { class RenderWindow; + class View; + class RectangleShape; } /// Game Application. Handles game loop. @@ -25,8 +28,21 @@ class GameApplication void run(); private: - /* State */ + void update(sf::Time elapsedTime); + void render(); + +private: + /* Components */ /// Render window std::unique_ptr window; + + /// View used to draw grass + std::unique_ptr view; + + /// Grass to draw + std::unique_ptr grass; + + /* State */ + sf::Time m_Time; }; \ No newline at end of file