diff options
-rw-r--r-- | floormat/main.hpp | 3 | ||||
-rw-r--r-- | main/draw.cpp | 16 | ||||
-rw-r--r-- | main/main-impl.cpp | 4 | ||||
-rw-r--r-- | main/main-impl.hpp | 19 | ||||
-rw-r--r-- | main/setup.cpp | 14 |
5 files changed, 40 insertions, 16 deletions
diff --git a/floormat/main.hpp b/floormat/main.hpp index f28ffebc..2ed8518f 100644 --- a/floormat/main.hpp +++ b/floormat/main.hpp @@ -55,7 +55,7 @@ struct floormat_main virtual struct lightmap_shader& lightmap_shader() noexcept = 0; virtual const tile_shader& shader() const noexcept = 0; virtual void bind() noexcept = 0; - float smoothed_frame_time() const noexcept; + virtual float smoothed_frame_time() const noexcept = 0; virtual fm_settings& settings() noexcept = 0; virtual const fm_settings& settings() const noexcept = 0; @@ -92,7 +92,6 @@ struct floormat_main [[maybe_unused]] static void debug_break(); protected: - float _smoothed_frame_time = 0; Vector2 _dpi_scale{ 1, 1 }, _virtual_scale{ 1, 1 }; Vector2i _framebuffer_size; bool _do_render_vobjs : 1 = true; diff --git a/main/draw.cpp b/main/draw.cpp index 9409d66c..d2fde66e 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -13,6 +13,13 @@ namespace floormat { +namespace { + +size_t bad_frame_counter = 0; + +} // namespace + + void main_impl::do_update() { constexpr auto eps = 1e-5f; @@ -22,13 +29,14 @@ void main_impl::do_update() #if 1 constexpr float RC = 60; constexpr auto alpha = 1 / (1 + RC); - _smoothed_frame_time = _smoothed_frame_time * (1 - alpha) + alpha * secs; + auto& value = _frame_timings.smoothed_frame_time; + + value = value*(1 - alpha) + alpha * secs; #else - _frame_time = secs; + value = secs; #endif - static size_t ctr = 0; if (secs > 35e-3f /* && !dt_expected.do_sleep */) - fm_debug("%zu frame took %.2f milliseconds", ctr++, (double)secs*1e3); + fm_debug("%zu frame took %.2f milliseconds", bad_frame_counter++, (double)secs*1e3); } else swapBuffers(); diff --git a/main/main-impl.cpp b/main/main-impl.cpp index 9f1e1e97..fab0d249 100644 --- a/main/main-impl.cpp +++ b/main/main-impl.cpp @@ -57,11 +57,9 @@ const Platform::Sdl2Application& main_impl::application() const noexcept { retur struct texture_unit_cache& main_impl::texture_unit_cache() { return _tuc; } path_search& main_impl::search() { return *_search; } astar& main_impl::astar() { return *_astar; } - Vector2i floormat_main::window_size() const noexcept { return _framebuffer_size; } +float main_impl::smoothed_frame_time() const noexcept { return _frame_timings.smoothed_frame_time; } void floormat_main::set_render_vobjs(bool value) { _do_render_vobjs = value; } bool floormat_main::is_rendering_vobjs() const { return _do_render_vobjs; } -float floormat_main::smoothed_frame_time() const noexcept { return _smoothed_frame_time; } - } // namespace floormat diff --git a/main/main-impl.hpp b/main/main-impl.hpp index e9d996bf..12169d6a 100644 --- a/main/main-impl.hpp +++ b/main/main-impl.hpp @@ -59,6 +59,7 @@ struct main_impl final : Platform::Sdl2Application, floormat_main class world& reset_world(class world&& w) noexcept override; SDL_Window* window() noexcept override; void update_window_state(); + float smoothed_frame_time() const noexcept override; fm_settings& settings() noexcept override; const fm_settings& settings() const noexcept override; @@ -102,17 +103,29 @@ struct main_impl final : Platform::Sdl2Application, floormat_main class astar& astar() override; private: + struct frame_timings_s + { + static constexpr unsigned min_refresh_rate = 20; + + unsigned refresh_rate = min_refresh_rate; + float smoothed_frame_time = 0; + bool vsync : 1 = false; + bool minimized : 1 = false; + bool focused : 1 = true; + }; + Time timeline; - struct texture_unit_cache _tuc; fm_settings s; [[maybe_unused]] char _dummy = (register_debug_callback(), '\0'); - floormat_app& app; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) + floormat_app& app; + struct texture_unit_cache _tuc; tile_shader _shader; struct lightmap_shader _lightmap_shader{_tuc}; Array<clickable> _clickable_scenery; class world _world{}; uint32_t _mouse_cursor = (uint32_t)-1; + frame_timings_s _frame_timings; ground_mesh _ground_mesh; wall_mesh _wall_mesh; anim_mesh _anim_mesh; @@ -124,8 +137,6 @@ private: void recalc_viewport(Vector2i fb_size, Vector2i win_size) noexcept; void draw_world() noexcept; - //bool draw_lights(chunk& c, const std::array<chunk*, 8>& neighbors) noexcept; - //bool draw_lights_for_chunk(chunk& c, Vector2b neighbor_offset) noexcept; draw_bounds get_draw_bounds() const noexcept override; diff --git a/main/setup.cpp b/main/setup.cpp index 70b1563b..82fbb9b0 100644 --- a/main/setup.cpp +++ b/main/setup.cpp @@ -103,7 +103,7 @@ auto main_impl::make_gl_conf(const fm_settings&) -> GLConfiguration .setStencilBufferSize(0); } -static int get_window_refresh_rate(SDL_Window* window) +unsigned get_window_refresh_rate(SDL_Window* window, unsigned min, unsigned max) { fm_assert(window != nullptr); if (int index = SDL_GetWindowDisplayIndex(window); index < 0) @@ -111,12 +111,20 @@ static int get_window_refresh_rate(SDL_Window* window) else if (SDL_DisplayMode dpymode{}; SDL_GetCurrentDisplayMode(index, &dpymode) < 0) fm_warn_once("SDL_GetCurrentDisplayMode: %s", SDL_GetError()); else - return Math::clamp(dpymode.refresh_rate, 30, 400); - return 30; + { + fm_assert(dpymode.refresh_rate > 0 && dpymode.refresh_rate < max); + return (unsigned)dpymode.refresh_rate; + } + return min; } void main_impl::update_window_state() // todo window minimized, out of focus, fake vsync etc { + auto refresh_rate = get_window_refresh_rate(window(), _frame_timings.min_refresh_rate, 10000); + fm_assert(refresh_rate > 0 && refresh_rate < 1000); + _frame_timings = { + .refresh_rate = refresh_rate, + }; } auto main_impl::meshes() noexcept -> struct meshes |