adding fps display

This commit is contained in:
2025-06-01 12:03:09 +02:00
parent 202b269302
commit e1c4698a56
8 changed files with 74 additions and 2 deletions

22
Application/FpsCounter.cpp git.filemode.normal_file
View File

@@ -0,0 +1,22 @@
#include <FpsCounter.h>
#include <chrono>
FpsCounter::FpsCounter()
{
m_past = std::chrono::high_resolution_clock::now();
m_now = m_past;
}
void FpsCounter::Update()
{
using namespace std::chrono_literals;
m_now = std::chrono::high_resolution_clock::now();
if(std::chrono::duration_cast<std::chrono::seconds>(m_now - m_past) >= 1s)
{
m_past = m_now;
m_current_fps = m_counter;
m_counter = 0;
}
m_counter++;
}

24
Application/FpsCounter.h git.filemode.normal_file
View File

@@ -0,0 +1,24 @@
#ifndef FPS_COUNTER_H
#define FPS_COUNTER_H
#include <cstdint>
#include <chrono>
class FpsCounter
{
public:
FpsCounter();
void Update();
[[nodiscard]] inline std::uint32_t GetFPSCount() const noexcept { return m_current_fps; }
~FpsCounter() = default;
private:
std::chrono::time_point<std::chrono::high_resolution_clock> m_now;
std::chrono::time_point<std::chrono::high_resolution_clock> m_past;
std::uint32_t m_counter = 0;
std::uint32_t m_current_fps = 0;
};
#endif

View File

@@ -6,14 +6,14 @@
#include <World.h>
#include <Utils.h>
World::World(Scop::Scene& scene) : m_noisecollection(42), m_scene(scene), m_previous_chunk_position(-1000, 10000)
World::World(Scop::Scene& scene) : m_noisecollection(42), m_fps_counter(), m_scene(scene), m_previous_chunk_position(-1000, 10000)
{
Scop::Vec2ui32 map_size;
Scop::MaterialTextures material_params;
material_params.albedo = std::make_shared<Scop::Texture>(Scop::LoadBMPFile(GetResourcesPath() / "atlas.bmp", map_size), map_size.x, map_size.y);
p_block_material = std::make_shared<Scop::Material>(material_params);
scene.LoadFont(GetResourcesPath() / "OpenSans_Regular.ttf", 32.0f);
scene.LoadFont(GetResourcesPath() / "OpenSans_Bold.ttf", 32.0f);
Scop::Text& text = scene.CreateText("FPS:");
text.SetPosition(Scop::Vec2ui{ 30, 30 });
@@ -25,6 +25,16 @@ World::World(Scop::Scene& scene) : m_noisecollection(42), m_scene(scene), m_prev
static bool generation_debounce = false;
static bool wireframe_debounce = false;
m_fps_counter.Update();
if(m_fps_counter.GetFPSCount() != m_last_fps_count)
{
m_last_fps_count = m_fps_counter.GetFPSCount();
if(p_fps_text)
m_scene.RemoveText(*p_fps_text);
p_fps_text = &m_scene.CreateText(std::to_string(m_last_fps_count));
p_fps_text->SetPosition(Scop::Vec2ui{ 80, 30 });
}
Scop::FirstPerson3D* camera = reinterpret_cast<Scop::FirstPerson3D*>(m_scene.GetCamera().get());
std::int32_t x_chunk = static_cast<std::int32_t>(camera->GetPosition().x) / static_cast<std::int32_t>(CHUNK_SIZE.x);
std::int32_t z_chunk = static_cast<std::int32_t>(camera->GetPosition().z) / static_cast<std::int32_t>(CHUNK_SIZE.z);

View File

@@ -8,6 +8,7 @@
#include <Chunk.h>
#include <Utils.h>
#include <FpsCounter.h>
#include <NoiseCollection.h>
constexpr std::uint8_t RENDER_DISTANCE = 15;
@@ -40,6 +41,7 @@ class World
private:
NoiseCollection m_noisecollection;
FpsCounter m_fps_counter;
std::unordered_map<Scop::Vec2i, Chunk> m_chunks;
ThreadSafeQueue<std::reference_wrapper<Chunk>> m_chunks_to_upload;
std::shared_ptr<Scop::Material> p_block_material;
@@ -47,6 +49,8 @@ class World
Scop::Vec2i m_previous_chunk_position;
Scop::Vec2i m_current_chunk_position;
std::atomic<GenerationState> m_generation_status = GenerationState::Ready;
Scop::NonOwningPtr<Scop::Text> p_fps_text;
std::uint32_t m_last_fps_count = 0;
};
#endif

BIN
Resources/OpenSans_Bold.ttf git.filemode.normal_file

Binary file not shown.

Binary file not shown.

View File

@@ -76,6 +76,7 @@ namespace Scop
void RemoveActor(Actor& actor) noexcept;
void RemoveNarrator(Narrator& narrator) noexcept;
void RemoveSprite(Sprite& sprite) noexcept;
void RemoveText(Text& text) noexcept;
[[nodiscard]] inline Scene& AddChildScene(std::string_view name, SceneDescriptor desc) { return m_scene_children.emplace_back(name, std::move(desc), this); }
inline void AddSkybox(std::shared_ptr<CubeTexture> cubemap) { p_skybox = cubemap; }

View File

@@ -120,6 +120,17 @@ namespace Scop
m_sprites.erase(it);
}
void Scene::RemoveText(Text& text) noexcept
{
auto it = m_texts.find(text.GetUUID());
if(it == m_texts.end())
{
Error("Text not found");
return;
}
m_texts.erase(it);
}
void Scene::SwitchToChild(std::string_view name) const noexcept
{
auto it = std::find_if(m_scene_children.begin(), m_scene_children.end(), [name](const Scene& scene){ return name == scene.GetName(); });