diff --git a/Application/FpsCounter.cpp b/Application/FpsCounter.cpp new file mode 100644 index 0000000..f532ce9 --- /dev/null +++ b/Application/FpsCounter.cpp @@ -0,0 +1,22 @@ +#include +#include + +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(m_now - m_past) >= 1s) + { + m_past = m_now; + m_current_fps = m_counter; + m_counter = 0; + } + m_counter++; +} diff --git a/Application/FpsCounter.h b/Application/FpsCounter.h new file mode 100644 index 0000000..fd0bc8d --- /dev/null +++ b/Application/FpsCounter.h @@ -0,0 +1,24 @@ +#ifndef FPS_COUNTER_H +#define FPS_COUNTER_H + +#include +#include + +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 m_now; + std::chrono::time_point m_past; + std::uint32_t m_counter = 0; + std::uint32_t m_current_fps = 0; +}; + +#endif diff --git a/Application/World.cpp b/Application/World.cpp index 845fce3..73e9949 100644 --- a/Application/World.cpp +++ b/Application/World.cpp @@ -6,14 +6,14 @@ #include #include -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::LoadBMPFile(GetResourcesPath() / "atlas.bmp", map_size), map_size.x, map_size.y); p_block_material = std::make_shared(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(m_scene.GetCamera().get()); std::int32_t x_chunk = static_cast(camera->GetPosition().x) / static_cast(CHUNK_SIZE.x); std::int32_t z_chunk = static_cast(camera->GetPosition().z) / static_cast(CHUNK_SIZE.z); diff --git a/Application/World.h b/Application/World.h index 6ad91be..e9f21e5 100644 --- a/Application/World.h +++ b/Application/World.h @@ -8,6 +8,7 @@ #include #include +#include #include constexpr std::uint8_t RENDER_DISTANCE = 15; @@ -40,6 +41,7 @@ class World private: NoiseCollection m_noisecollection; + FpsCounter m_fps_counter; std::unordered_map m_chunks; ThreadSafeQueue> m_chunks_to_upload; std::shared_ptr p_block_material; @@ -47,6 +49,8 @@ class World Scop::Vec2i m_previous_chunk_position; Scop::Vec2i m_current_chunk_position; std::atomic m_generation_status = GenerationState::Ready; + Scop::NonOwningPtr p_fps_text; + std::uint32_t m_last_fps_count = 0; }; #endif diff --git a/Resources/OpenSans_Bold.ttf b/Resources/OpenSans_Bold.ttf new file mode 100644 index 0000000..efdd5e8 Binary files /dev/null and b/Resources/OpenSans_Bold.ttf differ diff --git a/Resources/OpenSans_Regular.ttf b/Resources/OpenSans_Regular.ttf deleted file mode 100644 index 29bfd35..0000000 Binary files a/Resources/OpenSans_Regular.ttf and /dev/null differ diff --git a/ScopEngine/Runtime/Includes/Graphics/Scene.h b/ScopEngine/Runtime/Includes/Graphics/Scene.h index 54efcc3..3ca341e 100644 --- a/ScopEngine/Runtime/Includes/Graphics/Scene.h +++ b/ScopEngine/Runtime/Includes/Graphics/Scene.h @@ -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 cubemap) { p_skybox = cubemap; } diff --git a/ScopEngine/Runtime/Sources/Graphics/Scene.cpp b/ScopEngine/Runtime/Sources/Graphics/Scene.cpp index b438263..f85af8f 100644 --- a/ScopEngine/Runtime/Sources/Graphics/Scene.cpp +++ b/ScopEngine/Runtime/Sources/Graphics/Scene.cpp @@ -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(); });