Add an average framerate for the previous 120 frames

This commit is contained in:
2024-01-25 05:30:28 -05:00
parent b9f64bc49c
commit 30660b1f45
3 changed files with 61 additions and 8 deletions

40
src/engine/Average.hpp Normal file
View File

@@ -0,0 +1,40 @@
//
// Created by kj16609 on 1/25/24.
//
#pragma once
#include <concepts>
#include <numeric>
template < typename T, std::uint64_t max_count = 100 >
requires std::is_integral_v< T > || std::is_floating_point_v< T >
class Average
{
std::array< T, max_count > data {};
public:
std::array< T, max_count >& getData() { return data; }
consteval std::uint64_t count() const { return max_count; }
void push( const T t )
{
std::array< T, max_count > shift_array {};
for ( std::uint64_t i = 1; i < max_count; ++i )
{
shift_array[ i - 1 ] = data[ i ];
}
shift_array[ max_count - 1 ] = t;
data = shift_array;
}
T average() const
{
const T accum { std::accumulate( data.begin(), data.end(), T() ) };
return accum / static_cast< T >( data.size() );
}
};

View File

@@ -26,8 +26,8 @@ target_include_directories(FGLEngine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)
set_target_properties(FGLEngine PROPERTIES COMPILE_FLAGS ${FGL_FLAGS})
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_UPPER_BUILD_TYPE)
#if (CMAKE_UPPER_BUILD_TYPE STREQUAL "DEBUG")
# target_compile_definitions(FGLEngine PRIVATE ENABLE_IMGUI=1)
#else ()
if (CMAKE_UPPER_BUILD_TYPE STREQUAL "DEBUG")
target_compile_definitions(FGLEngine PRIVATE ENABLE_IMGUI=1)
else ()
target_compile_definitions(FGLEngine PRIVATE ENABLE_IMGUI=0)
#endif ()
endif ()

View File

@@ -15,6 +15,7 @@
#include <thread>
#include "KeyboardMovementController.hpp"
#include "engine/Average.hpp"
#include "engine/buffers/UniqueFrameSuballocation.hpp"
#include "engine/descriptors/Descriptor.hpp"
#include "engine/descriptors/DescriptorPool.hpp"
@@ -46,6 +47,8 @@ namespace fgl::engine
loadGameObjects();
}
static Average< float, 120 > rolling_ms_average;
void EngineContext::run()
{
using namespace fgl::literals::size_literals;
@@ -99,15 +102,28 @@ namespace fgl::engine
auto current_time { std::chrono::high_resolution_clock::now() };
auto previous_frame_start { std::chrono::high_resolution_clock::now() };
while ( !m_window.shouldClose() )
{
ZoneScopedN( "Poll" );
glfwPollEvents();
const auto new_time { std::chrono::high_resolution_clock::now() };
{
//Calculate time change from previous frame and add to accumulator
const auto time_diff { new_time - previous_frame_start };
rolling_ms_average.push(
static_cast< float >( std::chrono::duration_cast< std::chrono::microseconds >( time_diff ).count() )
/ 1000.0f );
previous_frame_start = new_time;
}
auto delta_time {
std::chrono::duration< float, std::chrono::seconds::period >( new_time - current_time ).count()
};
current_time = new_time;
delta_time = glm::min( delta_time, MAX_DELTA_TIME );
@@ -163,10 +179,7 @@ namespace fgl::engine
ImGui::Text( "Frame Time" );
ImGui::SameLine();
ImGui::Text( "%.3f ms", 1000.0f / ImGui::GetIO().Framerate );
ImGui::Text( "Frame: " );
ImGui::SameLine();
ImGui::Text( "%i", frame_info.frame_idx );
ImGui::Text( "Average rolling frametime: %.3f ms", rolling_ms_average.average() );
if ( ImGui::CollapsingHeader( "Camera" ) )
{