diff --git a/src/GameApplication.cpp b/src/GameApplication.cpp new file mode 100644 index 0000000..cecfdd9 --- /dev/null +++ b/src/GameApplication.cpp @@ -0,0 +1,70 @@ +#include "GameApplication.h" + +#include +#include + +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(); + } +} diff --git a/src/GameApplication.h b/src/GameApplication.h new file mode 100644 index 0000000..0130354 --- /dev/null +++ b/src/GameApplication.h @@ -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(); +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0f79474..69d31ee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,67 +1,9 @@ -#include -#include -#include +#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; } \ No newline at end of file