From 7241ed73216dde964cc612071673dfcd16996dbf Mon Sep 17 00:00:00 2001 From: trotFunky Date: Mon, 5 Feb 2024 13:55:27 +0000 Subject: [PATCH] Debug: Support frame tracing via Tracy Use CMake FetchContent to pull tracy sources and build it if frame tracing is enabled. As frame tracing requires annotations in the code, create a wrapper that replaces them with our own defines that are empty when tracing is not enabled. --- CMakeLists.txt | 16 ++++++++++++++++ FrameTracing.h | 24 ++++++++++++++++++++++++ World.cpp | 5 +++++ main.cpp | 5 +++++ 4 files changed, 50 insertions(+) create mode 100644 FrameTracing.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0845878..83dc7d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,22 @@ else () ImGui-SFML::ImGui-SFML) endif() +if(TRACE_FRAMES) + message("Building with Tracy") + add_compile_definitions(TRACE_FRAMES) + + include(FetchContent) + FetchContent_Declare( + Tracy + GIT_REPOSITORY https://github.com/wolfpld/tracy.git + GIT_TAG d46ffb4e9f132fc95bdd7d04207d8d669a9d4100 + ) + FetchContent_MakeAvailable(Tracy) + + set(LIBS ${LIBS} + TracyClient) +endif() + add_compile_options(-Wall -Wextra) add_executable(raycasting diff --git a/FrameTracing.h b/FrameTracing.h new file mode 100644 index 0000000..64ca952 --- /dev/null +++ b/FrameTracing.h @@ -0,0 +1,24 @@ +// +// Created by trotfunky on 05/02/24. +// + +#ifndef RAYCASTING_FRAMETRACING_H +#define RAYCASTING_FRAMETRACING_H + +/* + * Basic wrapper to allow enabling and disabling frame tracing using + * Tracy. + * https://github.com/wolfpld/tracy + */ +#ifdef TRACE_FRAMES + #include "tracy/Tracy.hpp" + #define FTrace_FrameMark FrameMark + #define FTrace_Scope ZoneScoped + #define FTrace_NamedScope(name) ZoneScopedN(name) +#else + #define FTrace_FrameMark + #define FTrace_Scope + #define FTrace_NamedScope(name) +#endif /* TRACE_FRAMES */ + +#endif //RAYCASTING_FRAMETRACING_H diff --git a/World.cpp b/World.cpp index d23aa1f..1300b7f 100644 --- a/World.cpp +++ b/World.cpp @@ -9,6 +9,7 @@ #include #include #endif +#include "FrameTracing.h" World::World(int w, int h, sf::Color groundColor, sf::Color ceilingColor, std::vector worldMap) : player(0,0,0), w(w), h(h), map(std::move(worldMap)), @@ -117,6 +118,7 @@ RaycastResult World::castRay(float originX, float originY, float orientation) co * built around left-handed axes (x→,y↓), so the rendered world is * mirrored. This also explains some weird signs for rotations. */ + FTrace_Scope; /* Offsets to get back on the grid from the ray's origin. */ float hOffsetX; float hOffsetY; @@ -234,6 +236,7 @@ RaycastResult World::castRay(float originX, float originY, float orientation) co void World::fillColumn(sf::RenderWindow& window, unsigned int column, float scale, sf::Color fillColor) { + FTrace_Scope; float columnHeight = static_cast(window.getSize().y)*scale; sf::RectangleShape& pixelColumn = renderColumns[column]; if (pixelColumn.getSize().y != columnHeight) { @@ -249,6 +252,7 @@ void World::fillColumn(sf::RenderWindow& window, unsigned int column, void World::render(sf::RenderWindow& window) { + FTrace_Scope; float windowX = static_cast(window.getSize().x); float windowY = static_cast(window.getSize().y); /* @@ -291,6 +295,7 @@ void World::render(sf::RenderWindow& window) } void World::step(const float& stepTime) { + FTrace_Scope; player.move(player.currentMoveSpeedX*stepTime, player.currentMoveSpeedY*stepTime); /* Undo last move if the player would end up in a wall. */ diff --git a/main.cpp b/main.cpp index 83ef919..a42b76b 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,7 @@ #include #include #endif +#include "FrameTracing.h" #include "World.h" @@ -37,8 +38,11 @@ int main() sf::Clock frameTime; while (window.isOpen()) { + FTrace_NamedScope("MainLoop"); + while (window.pollEvent(event)) { + FTrace_NamedScope("EventLoop"); if (event.type == sf::Event::Closed) { window.close(); continue; @@ -135,6 +139,7 @@ int main() #endif window.display(); + FTrace_FrameMark; } #ifdef IMGUI