-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APP] Refactor: extract main into GameApplication
- Loading branch information
Showing
3 changed files
with
87 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |