Skip to content

Commit

Permalink
Improved fps calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
denyskryvytskyi committed Apr 3, 2024
1 parent 289d04f commit acd56ff
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
64 changes: 39 additions & 25 deletions Engine/src/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "Input.h"
#include "Profiler.h"
#include "SettingsConfig.h"
#include "Timer.h"
#include "Window.h"

#include "Renderer/CameraController.h"
Expand Down Expand Up @@ -53,8 +52,10 @@ Application::Application()
gSceneManager.Init();

#if EDITOR_MODE
m_imGuiOverlay.Init();
m_editor.OnInit();
if (gEngineSettings.enableEditor) {
m_imGuiOverlay.Init();
m_editor.OnInit();
}
#endif

events::Subscribe<events::WindowCloseEvent>(m_windowCloseCallback);
Expand Down Expand Up @@ -82,7 +83,9 @@ Application::~Application()
OnDestroy();

#if EDITOR_MODE
m_imGuiOverlay.Shutdown();
if (gEngineSettings.enableEditor) {
m_imGuiOverlay.Shutdown();
}
#endif

gAudioManager.Shutdown();
Expand Down Expand Up @@ -111,20 +114,7 @@ void Application::Run()
const float elapsedTime = elapsedTimeMs * 0.001f;
timer.Restart();

{
if (timerFpsCounter.ElapsedMs() > 100.0f) {
s_telemetry.frameTime = elapsedTimeMs;

s_telemetry.fps = 1.0f / elapsedTime;

timerFpsCounter.Restart();
}

if (gEngineSettings.enableFpsCounter) {
GetScene().GetComponent<TextComponent>(m_fpsCounterEntityId).text
= fmt::format("{:0.3f} ms ({} fps)", s_telemetry.frameTime, static_cast<int>(std::floor(s_telemetry.fps)));
}
}
UpdateFps(timerFpsCounter, elapsedTime, elapsedTimeMs);

// ============== Process input step ==============
{
Expand Down Expand Up @@ -167,14 +157,12 @@ void Application::Run()
}

#if EDITOR_MODE
{
if (gEngineSettings.enableEditor) {
PROFILE_SCOPE("ImGui Render in: ");
if (gEngineSettings.enableEditor) {
m_imGuiOverlay.Begin();
m_editor.OnImGuiRender();
OnImguiRender();
m_imGuiOverlay.End();
}
m_imGuiOverlay.Begin();
m_editor.OnImGuiRender();
OnImguiRender();
m_imGuiOverlay.End();
}
#endif

Expand Down Expand Up @@ -215,4 +203,30 @@ void Application::OnWindowResize(const events::WindowResizeEvent& e)
OnWindowResizeApp();
}

void Application::UpdateFps(Timer& fpsCounter, const float elapsed, const float elapsedMs)
{
static float accumTime = 0.0f;
static float accumTimeMs = 0.0f;
static float accumFrameCount = 0.0f;

if (fpsCounter.ElapsedMs() > 1000.0f) {
s_telemetry.frameTime = accumTimeMs / accumFrameCount;
s_telemetry.fps = accumFrameCount / accumTime;

fpsCounter.Restart();
accumTime = 0.0f;
accumTimeMs = 0.0f;
accumFrameCount = 0.0f;

if (gEngineSettings.enableFpsCounter) {
GetScene().GetComponent<TextComponent>(m_fpsCounterEntityId).text
= fmt::format("{:0.3f} ms ({} fps)", s_telemetry.frameTime, static_cast<int>(std::floor(s_telemetry.fps)));
}
} else {
accumTime += elapsed;
accumTimeMs += elapsedMs;
accumFrameCount += 1.0f;
}
}

} // namespace elv
4 changes: 4 additions & 0 deletions Engine/src/Core/Application.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "Timer.h"

#include "Events/ApplicationEvent.h"
#include "Events/EventHandler.h"
#include "Scene/Entity.h"
Expand Down Expand Up @@ -54,6 +56,8 @@ class Application {
void OnWindowClose(const events::WindowCloseEvent& e);
void OnWindowResize(const events::WindowResizeEvent& e);

void UpdateFps(Timer& fpsCounter, const float elapsed, const float elapsedMs);

protected:
ecs::Entity m_orthoCameraEntity { ecs::INVALID_ENTITY_ID };
bool m_running { false };
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Elven Engine is primarily a 2D/3D game engine that is being developed from scrat
- [x] Load/Save engine settings from/to JSON files
+ [x] Sound engine
- [x] Support formats: mp3, wav, ogg, flac etc.
- [x] Add/Play/Pause/Stop functionality
- [x] Add/Play/Pause/Stop/Loop functionality
+ [ ] In-engine editor (WIP; based on ImGui)
- [x] Editor Camera (holding RMB): WASD movement, QE up/down, ZX rotation
- [x] Scene hierarchy panel
Expand All @@ -61,7 +61,7 @@ Elven Engine is primarily a 2D/3D game engine that is being developed from scrat
- [x] "Add component" button
- [x] Component settings context menu
- [x] Settings panel: fullscreen, VSync, MSAA
- [ ] Telemetry: performance panel
- [x] Telemetry: performance panel
- [ ] Graphics stats
+ [x] Just cool stuff
- [x] Orthographic camera controller (OrthographicCameraController), that can be used if needed
Expand Down

0 comments on commit acd56ff

Please sign in to comment.