Skip to content

Commit

Permalink
[APP] Extracted update and render, made view, grass, m_Time members o…
Browse files Browse the repository at this point in the history
…f GameApplication
  • Loading branch information
hsandt committed Jul 15, 2020
1 parent 69cb78e commit ad2bdcd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
54 changes: 31 additions & 23 deletions src/GameApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,31 @@ void GameApplication::init()

// 60 FPS, no vsync
window->setFramerateLimit(60);

// camera view
view = std::make_unique<sf::View>();
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::RectangleShape>(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
Expand All @@ -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();
}
18 changes: 17 additions & 1 deletion src/GameApplication.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#pragma once

#include <memory>
#include <SFML/System/Time.hpp>

namespace sf
{
class RenderWindow;
class View;
class RectangleShape;
}

/// Game Application. Handles game loop.
Expand All @@ -25,8 +28,21 @@ class GameApplication
void run();

private:
/* State */
void update(sf::Time elapsedTime);
void render();

private:
/* Components */

/// Render window
std::unique_ptr<sf::RenderWindow> window;

/// View used to draw grass
std::unique_ptr<sf::View> view;

/// Grass to draw
std::unique_ptr<sf::RectangleShape> grass;

/* State */
sf::Time m_Time;
};

0 comments on commit ad2bdcd

Please sign in to comment.