Skip to content

Commit

Permalink
[APP] Extracted GameApplication::init from run. Made window member.
Browse files Browse the repository at this point in the history
Added empty (virtual) destructor definition in .cpp to allow class-forwarding RenderWindow accessed via unique_ptr
  • Loading branch information
hsandt committed Jul 15, 2020
1 parent 1e9adda commit 4ffa011
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/GameApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ GameApplication::GameApplication()
{
}

void GameApplication::run()
GameApplication::~GameApplication()
{
}

void GameApplication::init()
{
// no aliasing
sf::ContextSettings settings;
settings.antialiasingLevel = 0;

// windowed 720p. no resize
sf::RenderWindow window(sf::VideoMode(1280, 720), "Game", sf::Style::Close, settings);
window = std::make_unique<sf::RenderWindow>(sf::VideoMode(1280, 720), "Game", sf::Style::Close, settings);

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

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);
Expand All @@ -33,7 +40,7 @@ void GameApplication::run()
sf::Clock clock;
sf::Time time;

while (window.isOpen())
while (window->isOpen())
{
// Time check
sf::Time elapsedTime = clock.restart();
Expand All @@ -45,26 +52,26 @@ void GameApplication::run()

// Event handling
sf::Event event;
while (window.pollEvent(event))
while (window->pollEvent(event))
{
if (event.type == sf::Event::Closed ||
(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape))
{
window.close();
window->close();
}
}

// move camera
view.move(0.f, std::sin(time.asSeconds()) * 50.f * elapsedTime.asSeconds());

// clear sky
window.clear(sf::Color::Cyan);
window->clear(sf::Color::Cyan);

// show grass with moving camera
window.setView(view);
window.draw(grass);
window->setView(view);
window->draw(grass);

// flip
window.display();
window->display();
}
}
18 changes: 18 additions & 0 deletions src/GameApplication.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
#pragma once

#include <memory>

namespace sf
{
class RenderWindow;
}

/// Game Application. Handles game loop.
class GameApplication
{
public:
GameApplication();
virtual ~GameApplication();

GameApplication(const GameApplication&) = delete;
GameApplication& operator=(const GameApplication&) = delete;

public:
/// Initialize game application.
void init();

/// Run game loop. Returns when the loop is over, i.e. the window is closed.
void run();

private:
/* State */

/// Render window
std::unique_ptr<sf::RenderWindow> window;
};
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
int main(int argc, char const *argv[])
{
GameApplication gameApp;
gameApp.init();
gameApp.run();

return 0;
Expand Down

0 comments on commit 4ffa011

Please sign in to comment.