Created
July 27, 2019 21:43
-
-
Save janderudder/ac9d04c34813e1f147781aae177794dc to your computer and use it in GitHub Desktop.
SFML main.cpp - blank project snippet
This file contains hidden or 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
#if __has_include(<experimental/filesystem>) | |
#include <experimental/filesystem> | |
namespace filesystem = std::experimental::filesystem; | |
#elif __has_include(<filesystem>) | |
#include <filesystem> | |
namespace filesystem = std::filesystem; | |
#else | |
#error "Couldn't find a standard 'filesystem' header." | |
#endif | |
#include <SFML/Graphics.hpp> | |
#include <iostream> | |
int main([[maybe_unused]]const int argc, const char** argv) | |
{ | |
// Window | |
sf::RenderWindow window { | |
sf::VideoMode{800, 600}, | |
"SFML App", | |
sf::Style::Default | |
}; | |
sf::View win_view = window.getView(); | |
// Config | |
window.setVerticalSyncEnabled(true); | |
bool show_fps = true; | |
struct Settings { | |
struct Window { | |
const sf::Vector2f move_speed_vec { 720, 720 }; | |
} window; | |
} cfg; | |
// Resources | |
const auto resource_path = | |
filesystem::canonical(argv[0]) | |
.remove_filename() | |
.parent_path(); | |
// Objects | |
sf::RectangleShape rect; | |
rect.setSize({220, 200}); | |
rect.setFillColor(sf::Color::Red); | |
rect.setOutlineColor(sf::Color::White); | |
rect.setOutlineThickness(2.f); | |
rect.setPosition(230, 32); | |
// Time | |
sf::Clock clock; | |
sf::Time app_time; | |
auto frame_count = 0ULL; | |
// App loop | |
while (window.isOpen()) | |
{ | |
sf::Event event; | |
while (window.pollEvent(event)) | |
{ | |
if (event.type == sf::Event::KeyPressed) switch (event.key.code) | |
{ | |
case sf::Keyboard::Enter: | |
case sf::Keyboard::Escape: | |
case sf::Keyboard::Space: | |
window.close(); | |
default: | |
break; | |
} | |
else if (event.type == sf::Event::Resized) | |
{ // preserve aspect ratio | |
const auto [ width, height ] = event.size; | |
win_view.setSize(width, height); | |
win_view.setCenter(width/2.f, height/2.f); | |
} | |
else if (event.type == sf::Event::Closed) | |
window.close(); | |
} | |
// Frame time | |
const auto frame_time = clock.restart(); | |
const auto frame_seconds = frame_time.asSeconds(); | |
app_time += frame_time; | |
frame_count++; | |
// Real-time events | |
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { | |
win_view.move(-frame_seconds * cfg.window.move_speed_vec.x, 0); | |
} | |
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { | |
win_view.move(frame_seconds * cfg.window.move_speed_vec.x, 0); | |
} | |
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { | |
win_view.move(0, -frame_seconds * cfg.window.move_speed_vec.x); | |
} | |
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { | |
win_view.move(0, frame_seconds * cfg.window.move_speed_vec.x); | |
} | |
// Rendering | |
window.clear(); | |
window.setView(win_view); | |
window.draw(rect); | |
window.display(); | |
} | |
// App end | |
if (show_fps) { | |
const auto fps = frame_count / app_time.asSeconds(); | |
std::cout << "~" << fps << " fps\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment