commit 5a3d0555f5c4c687ebce1bc3c509f366dbd218a4 Author: Floriansylvain Date: Sat Dec 27 21:20:01 2025 +0100 feat: initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a11e750 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.cache +/.vscode +/build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..422a42c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.28) +project(CMakeSFMLProject LANGUAGES CXX) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +set(ASSETS_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/assets") +set(ASSETS_DEST_DIR "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/assets") + +include(FetchContent) +FetchContent_Declare(SFML + GIT_REPOSITORY https://github.com/SFML/SFML.git + GIT_TAG 3.0.2 + GIT_SHALLOW ON + EXCLUDE_FROM_ALL + SYSTEM) +FetchContent_MakeAvailable(SFML) + +add_executable(main src/main.cpp) +target_compile_features(main PRIVATE cxx_std_17) +target_link_libraries(main PRIVATE SFML::Graphics) + +add_custom_command(TARGET main POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + "${ASSETS_SRC_DIR}" + "${ASSETS_DEST_DIR}" +) diff --git a/assets/Consolas.ttf b/assets/Consolas.ttf new file mode 100644 index 0000000..2162134 Binary files /dev/null and b/assets/Consolas.ttf differ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..15c4c3c --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +constexpr int DEBUG_REFRESH_RATE_IN_MS = 250; +constexpr bool VSYNC = true; +constexpr int WIDTH = 1920; +constexpr int HEIGHT = 1080; + +void updateShapePosition(sf::RectangleShape &shape, sf::RenderWindow &window) { + if (!window.hasFocus()) + return; + + auto shapeSize = shape.getSize(); + auto mousePos = sf::Mouse::getPosition(window); + + shape.setPosition( + sf::Vector2f(mousePos.x - shapeSize.x / 2, window.getSize().y - 50)); +} + +std::string getDebugText(double elapsedMilliseconds) { + if (elapsedMilliseconds <= 0) + return ""; + + return "V-SYNC: " + std::string(VSYNC ? "On" : "Off") + + "\nFrametime: " + std::to_string(elapsedMilliseconds) + + "\nFPS: " + std::to_string((int)(1000 / elapsedMilliseconds)); +} + +int main() { + sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "Whatever SFML"); + window.setVerticalSyncEnabled(VSYNC); + + sf::RectangleShape shape(sf::Vector2f(200, 25)); + shape.setFillColor(sf::Color(100, 250, 50)); + shape.setPosition(sf::Vector2f(0, HEIGHT - 50)); + + sf::Clock clock; + + sf::Font font; + if (!font.openFromFile("assets/Consolas.ttf")) + return 0; + + sf::Text text(font); + text.setString(getDebugText(0)); + + sf::View view; + view.setSize(sf::Vector2f(WIDTH, HEIGHT)); + view.setCenter(sf::Vector2f((float)WIDTH / 2, (float)HEIGHT / 2)); + + double lastTextUpdateElapsedMs = 0; + + while (window.isOpen()) { + while (const std::optional event = window.pollEvent()) { + if (event->is()) { + window.close(); + return 0; + } else if (event->is()) { + auto size = window.getSize(); + view.setSize(sf::Vector2f(size.x, size.y)); + view.setCenter(sf::Vector2f((float)size.x / 2, (float)size.y / 2)); + } + } + + window.clear(); + updateShapePosition(shape, window); + window.setView(view); + window.draw(shape); + window.draw(text); + window.display(); + + double elapsedMilliseconds = + (double)clock.restart().asMicroseconds() / 1000; + + lastTextUpdateElapsedMs += elapsedMilliseconds; + + if (lastTextUpdateElapsedMs > DEBUG_REFRESH_RATE_IN_MS) { + text.setString(getDebugText(elapsedMilliseconds)); + lastTextUpdateElapsedMs = 0; + } + } + + return 0; +}