From 4d51c8173d024b738d8c2100922afe6c08987f86 Mon Sep 17 00:00:00 2001 From: Hannes Mann Date: Tue, 14 Jun 2016 04:29:23 +0200 Subject: [PATCH] Complete timing functions --- src/engine.cc | 19 ++++++++++++++++++- src/engine.h | 19 ++++++++++++++++++- src/main.cc | 5 +---- src/platform/backend_specifics.h | 4 ++++ src/platform/standalone_backend_specifics.cc | 11 +++++++++++ src/platform/standalone_backend_specifics.h | 2 ++ 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/engine.cc b/src/engine.cc index 092755c..1152a90 100644 --- a/src/engine.cc +++ b/src/engine.cc @@ -61,6 +61,7 @@ namespace openrayman { double current_timer_value = m_backend_specifics.get_time(); m_current_delta_time = current_timer_value - m_last_timer_value; + m_accumulated_time += m_current_delta_time; m_total_time += m_current_delta_time; m_last_timer_value = current_timer_value; @@ -73,9 +74,25 @@ namespace openrayman if(m_current_input.command(input_command::toggle_fullscreen) && !m_last_input.command(input_command::toggle_fullscreen)) m_config.fullscreen = !m_config.fullscreen; + std::cout << "Update: " << m_current_delta_time * 1000 << "ms, " << m_total_frames << std::endl; + m_total_frames++; + while(m_accumulated_time >= 1 / m_static_info.updates_per_second) + { + std::cout << "Fixed update: " << m_total_fixed_updates << std::endl; + m_total_fixed_updates++; + m_accumulated_time -= 1 / m_static_info.updates_per_second; + } + + m_window.present(); + + if(m_config.max_fps > 0) + { + while(m_backend_specifics.get_time() - m_last_timer_value < 1 / m_config.max_fps) + m_backend_specifics.yield_cpu(); + } + m_window.set_vsync(m_config.vsync); m_window.set_fullscreen(m_config.fullscreen); - m_window.present(); m_exit_requested = m_exit_requested || m_window.wants_close(); } diff --git a/src/engine.h b/src/engine.h index 3741bee..9b7cde2 100644 --- a/src/engine.h +++ b/src/engine.h @@ -7,6 +7,7 @@ #include #include #include +#include #ifdef LIBRETRO_CORE #error Building as a libretro core is not yet supported! @@ -35,6 +36,9 @@ public: m_last_timer_value(0), m_current_delta_time(0), m_total_time(0), + m_accumulated_time(0), + m_total_frames(0), + m_total_fixed_updates(0), m_static_info(m_backend_specifics), m_config(m_static_info, m_backend_specifics) { }; @@ -95,6 +99,18 @@ public: return m_current_delta_time; } + // Returns the total amount of frames that have passed since the start of the game. + inline std::uint64_t get_total_frames() const + { + return m_total_frames; + } + + // Returns the total amount of fixed updates that have passed since the start of the game. + inline std::uint64_t get_total_fixed_updates() const + { + return m_total_fixed_updates; + } + // Returns a reference to the active static engine info. inline const info& get_static_info() const { @@ -112,7 +128,8 @@ private: backend_specifics& m_backend_specifics; input_state m_current_input; input_state m_last_input; - double m_last_timer_value, m_current_delta_time, m_total_time; + double m_last_timer_value, m_current_delta_time, m_total_time, m_accumulated_time; + std::uint64_t m_total_frames, m_total_fixed_updates; info m_static_info; config m_config; bool m_exit_requested; diff --git a/src/main.cc b/src/main.cc index 55f3716..b860019 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,9 +2,6 @@ #include #include #include -#ifndef _WIN32 -#include -#endif int main(int argc, char** argv) { @@ -30,7 +27,7 @@ int main(int argc, char** argv) std::cout << "Options:" << std::endl; std::cout << " --version Display version information" << std::endl; std::cout << " --game Specifies what game to run" << std::endl; - std::cout << " This will default to the game set in \"info.json\" if no game is specified." << std::endl; + std::cout << " This will default to the game set in \"config.json\" if no game is specified." << std::endl; return EXIT_SUCCESS; } if(str == "--version") diff --git a/src/platform/backend_specifics.h b/src/platform/backend_specifics.h index ec88b6d..7d15935 100644 --- a/src/platform/backend_specifics.h +++ b/src/platform/backend_specifics.h @@ -2,6 +2,7 @@ #define BACKEND_SPECIFICS_H #include +#include namespace openrayman { @@ -19,6 +20,9 @@ public: // Returns a high-resolution timer value in seconds. virtual double get_time() const = 0; + + // Yields CPU using the most efficient method possible. + virtual void yield_cpu() const = 0; }; } diff --git a/src/platform/standalone_backend_specifics.cc b/src/platform/standalone_backend_specifics.cc index 0dfc4d4..ba9e722 100644 --- a/src/platform/standalone_backend_specifics.cc +++ b/src/platform/standalone_backend_specifics.cc @@ -103,4 +103,15 @@ namespace openrayman // we use GLFW when we are run standalone. return glfwGetTime(); } + + void standalone_backend_specifics::yield_cpu() const + { +#ifdef _WIN32 + // might consume 100% cpu? who cares lmao + Sleep(0); +#else + // usleep has the same effect as sched_yield, but does not consume 100% CPU. + usleep(0); +#endif + } } diff --git a/src/platform/standalone_backend_specifics.h b/src/platform/standalone_backend_specifics.h index 79248ab..b3cdde5 100644 --- a/src/platform/standalone_backend_specifics.h +++ b/src/platform/standalone_backend_specifics.h @@ -24,6 +24,8 @@ public: const std::string& get_storage_path() const override; double get_time() const override; + + void yield_cpu() const override; private: void initialize_data_path(); void initialize_storage_path();