Complete timing functions

This commit is contained in:
Hannes Mann 2016-06-14 04:29:23 +02:00
parent 4f53002fd8
commit 4d51c8173d
6 changed files with 54 additions and 6 deletions

View file

@ -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();
}

View file

@ -7,6 +7,7 @@
#include <GL/gl3w.h>
#include <config/info.h>
#include <config/config.h>
#include <cstdint>
#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;

View file

@ -2,9 +2,6 @@
#include <iostream>
#include <info.h>
#include <engine.h>
#ifndef _WIN32
#include <gtk/gtk.h>
#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")

View file

@ -2,6 +2,7 @@
#define BACKEND_SPECIFICS_H
#include <string>
#include <cstdint>
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;
};
}

View file

@ -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
}
}

View file

@ -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();