Skip to content

Commit

Permalink
[APP] Refactor: extract main into GameApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
hsandt committed Jul 15, 2020
1 parent 929a0e1 commit 1e9adda
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 61 deletions.
70 changes: 70 additions & 0 deletions src/GameApplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "GameApplication.h"

#include <cmath>
#include <SFML/Graphics.hpp>

GameApplication::GameApplication()
{
}

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

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

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

// 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});

// 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)
{
time = sf::Time::Zero;
}

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

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

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

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

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

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

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

public:
void run();
};
64 changes: 3 additions & 61 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,67 +1,9 @@
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>
#include "GameApplication.h"

int main(int argc, char const *argv[])
{
// no aliasing
sf::ContextSettings settings;
settings.antialiasingLevel = 0;

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

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

// 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});

// 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)
{
time = sf::Time::Zero;
}

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

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

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

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

// flip
window.display();
}
GameApplication gameApp;
gameApp.run();

return 0;
}

0 comments on commit 1e9adda

Please sign in to comment.