use std::chrono::steady_clock timer
This commit is contained in:
parent
e04c8cb649
commit
4016d64859
|
|
@ -466,7 +466,7 @@ void Cam_KeyControl( camera_t& camera, float dtime ){
|
||||||
else{ /* accelerate */
|
else{ /* accelerate */
|
||||||
camera.m_keymove_speed_current = std::min( camera.m_keymove_speed_current
|
camera.m_keymove_speed_current = std::min( camera.m_keymove_speed_current
|
||||||
+ g_camwindow_globals_private.m_nMoveSpeed * dtime
|
+ g_camwindow_globals_private.m_nMoveSpeed * dtime
|
||||||
/ g_camwindow_globals_private.m_time_toMaxSpeed * static_cast<float>( msec_per_sec ),
|
/ g_camwindow_globals_private.m_time_toMaxSpeed * 1000,
|
||||||
static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) );
|
static_cast<float>( g_camwindow_globals_private.m_nMoveSpeed ) );
|
||||||
}
|
}
|
||||||
const float dpos = dtime * camera.m_keymove_speed_current;
|
const float dpos = dtime * camera.m_keymove_speed_current;
|
||||||
|
|
@ -500,7 +500,7 @@ void Camera_keyMove( camera_t& camera ){
|
||||||
camera.m_mouseMove.flush();
|
camera.m_mouseMove.flush();
|
||||||
|
|
||||||
//globalOutputStream() << "keymove... ";
|
//globalOutputStream() << "keymove... ";
|
||||||
float time_seconds = camera.m_keycontrol_timer.elapsed_msec() / static_cast<float>( msec_per_sec );
|
float time_seconds = camera.m_keycontrol_timer.elapsed_sec();
|
||||||
if( time_seconds == 0 ) /* some reasonable move at the very start */
|
if( time_seconds == 0 ) /* some reasonable move at the very start */
|
||||||
time_seconds = 0.008f;
|
time_seconds = 0.008f;
|
||||||
camera.m_keycontrol_timer.start();
|
camera.m_keycontrol_timer.start();
|
||||||
|
|
|
||||||
|
|
@ -1117,11 +1117,9 @@ class ScopeTimer
|
||||||
public:
|
public:
|
||||||
ScopeTimer( const char* message )
|
ScopeTimer( const char* message )
|
||||||
: m_message( message ){
|
: m_message( message ){
|
||||||
m_timer.start();
|
|
||||||
}
|
}
|
||||||
~ScopeTimer(){
|
~ScopeTimer(){
|
||||||
double elapsed_time = m_timer.elapsed_msec() / 1000.f;
|
globalOutputStream() << m_message << " timer: " << FloatFormat( m_timer.elapsed_sec(), 5, 2 ) << " second(s) elapsed\n";
|
||||||
globalOutputStream() << m_message << " timer: " << FloatFormat( elapsed_time, 5, 2 ) << " second(s) elapsed\n";
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,81 +20,15 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
|
||||||
#if defined( WIN32 )
|
void Timer::start(){
|
||||||
|
m_start = std::chrono::steady_clock::now().time_since_epoch().count();
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
MillisecondTime MillisecondTime::current(){
|
|
||||||
static class Cached
|
|
||||||
{
|
|
||||||
LONGLONG m_frequency;
|
|
||||||
LONGLONG m_base;
|
|
||||||
public:
|
|
||||||
Cached(){
|
|
||||||
QueryPerformanceFrequency( (LARGE_INTEGER *) &m_frequency );
|
|
||||||
QueryPerformanceCounter( (LARGE_INTEGER *) &m_base );
|
|
||||||
}
|
|
||||||
LONGLONG frequency(){
|
|
||||||
return m_frequency;
|
|
||||||
}
|
|
||||||
LONGLONG base(){
|
|
||||||
return m_base;
|
|
||||||
}
|
|
||||||
} cached;
|
|
||||||
|
|
||||||
if ( cached.frequency() > 0 ) {
|
|
||||||
LONGLONG count;
|
|
||||||
QueryPerformanceCounter( (LARGE_INTEGER *) &count );
|
|
||||||
return time_from_ticks( count - cached.base(), cached.frequency() );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
#if 1
|
|
||||||
return MillisecondTime();
|
|
||||||
#else
|
|
||||||
return time_from_ticks( timeGetTime(), 1000 );
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Timer::elapsed_msec() const {
|
||||||
|
return ( std::chrono::steady_clock::now().time_since_epoch().count() - m_start )
|
||||||
|
* std::chrono::steady_clock::period::num
|
||||||
#elif defined( POSIX )
|
/ ( std::chrono::steady_clock::period::den / 1000 );
|
||||||
|
|
||||||
#include <ctime>
|
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
MillisecondTime MillisecondTime::current(){
|
|
||||||
static class Cached
|
|
||||||
{
|
|
||||||
time_t m_base;
|
|
||||||
public:
|
|
||||||
Cached(){
|
|
||||||
time( &m_base );
|
|
||||||
}
|
|
||||||
time_t base(){
|
|
||||||
return m_base;
|
|
||||||
}
|
|
||||||
} cached;
|
|
||||||
|
|
||||||
timeval time;
|
|
||||||
gettimeofday( &time, 0 );
|
|
||||||
return MillisecondTime( ( time.tv_sec - cached.base() ) * 1000 + time.tv_usec / 1000 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#include <ctime>
|
|
||||||
|
|
||||||
MillisecondTime MillisecondTime::current(){
|
|
||||||
return time_from_ticks<std::clock_t>( std::clock(), CLOCKS_PER_SEC );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
|
||||||
|
|
@ -21,70 +21,19 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#if 1
|
#include <cstdint>
|
||||||
|
|
||||||
const int msec_per_sec = 1000;
|
|
||||||
|
|
||||||
class MillisecondTime
|
|
||||||
{
|
|
||||||
unsigned int m_milliseconds;
|
|
||||||
public:
|
|
||||||
MillisecondTime( unsigned int milliseconds )
|
|
||||||
: m_milliseconds( milliseconds ){
|
|
||||||
}
|
|
||||||
MillisecondTime(){
|
|
||||||
}
|
|
||||||
static MillisecondTime current();
|
|
||||||
|
|
||||||
unsigned int milliseconds_since( const MillisecondTime& other ) const {
|
|
||||||
return m_milliseconds - other.m_milliseconds;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename tick_type>
|
|
||||||
inline MillisecondTime time_from_ticks( tick_type tick_count, tick_type ticks_per_sec ){
|
|
||||||
return MillisecondTime( static_cast<unsigned int>( tick_count / static_cast<double>( ticks_per_sec / msec_per_sec ) ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
const unsigned int usec_per_sec = 1000000;
|
|
||||||
|
|
||||||
class MillisecondTime
|
|
||||||
{
|
|
||||||
unsigned int m_sec;
|
|
||||||
unsigned int m_usec;
|
|
||||||
public:
|
|
||||||
MillisecondTime( unsigned int sec, unsigned int usec )
|
|
||||||
: m_sec( sec ), m_usec( usec ){
|
|
||||||
}
|
|
||||||
MillisecondTime(){
|
|
||||||
}
|
|
||||||
staticMillisecondTime current();
|
|
||||||
|
|
||||||
unsigned int milliseconds_since( const MillisecondTime& other ) const {
|
|
||||||
return static_cast<unsigned int>( ( m_sec * static_cast<double>( usec_per_sec ) + m_usec )
|
|
||||||
- ( other.m_sec * static_cast<double>( usec_per_sec ) + other.m_usec ) ) / 1000;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename tick_type>
|
|
||||||
inline MillisecondTime time_from_ticks( tick_type tick_count, tick_type ticks_per_sec ){
|
|
||||||
return MillisecondTime( static_cast<unsigned int>( tick_count / ticks_per_sec ),
|
|
||||||
static_cast<unsigned int>( ( tick_count % ticks_per_sec ) * ( usec_per_sec / static_cast<double>( ticks_per_sec ) ) ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class Timer
|
class Timer
|
||||||
{
|
{
|
||||||
MillisecondTime m_start;
|
std::intmax_t m_start;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void start(){
|
Timer(){
|
||||||
m_start = MillisecondTime::current();
|
start();
|
||||||
}
|
}
|
||||||
unsigned int elapsed_msec(){
|
void start();
|
||||||
return MillisecondTime::current().milliseconds_since( m_start );
|
int elapsed_msec() const;
|
||||||
|
double elapsed_sec() const {
|
||||||
|
return elapsed_msec() / 1000.0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -44,10 +44,9 @@ class DebugScopeTimer
|
||||||
public:
|
public:
|
||||||
DebugScopeTimer( const char* operation )
|
DebugScopeTimer( const char* operation )
|
||||||
: m_operation( operation ){
|
: m_operation( operation ){
|
||||||
m_timer.start();
|
|
||||||
}
|
}
|
||||||
~DebugScopeTimer(){
|
~DebugScopeTimer(){
|
||||||
unsigned int elapsed = m_timer.elapsed_msec();
|
const int elapsed = m_timer.elapsed_msec();
|
||||||
if ( elapsed > 0 ) {
|
if ( elapsed > 0 ) {
|
||||||
globalOutputStream() << m_operation << ": " << elapsed << " msec\n";
|
globalOutputStream() << m_operation << ": " << elapsed << " msec\n";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -398,7 +398,7 @@ void WXY_Print(){
|
||||||
Timer g_chasemouse_timer;
|
Timer g_chasemouse_timer;
|
||||||
|
|
||||||
void XYWnd::ChaseMouse(){
|
void XYWnd::ChaseMouse(){
|
||||||
float multiplier = g_chasemouse_timer.elapsed_msec() / 10.0f;
|
const float multiplier = g_chasemouse_timer.elapsed_msec() / 10.0f;
|
||||||
Scroll( float_to_integer( multiplier * m_chasemouse_delta_x ), float_to_integer( multiplier * -m_chasemouse_delta_y ) );
|
Scroll( float_to_integer( multiplier * m_chasemouse_delta_x ), float_to_integer( multiplier * -m_chasemouse_delta_y ) );
|
||||||
|
|
||||||
//globalOutputStream() << "chasemouse: multiplier=" << multiplier << " x=" << m_chasemouse_delta_x << " y=" << m_chasemouse_delta_y << '\n';
|
//globalOutputStream() << "chasemouse: multiplier=" << multiplier << " x=" << m_chasemouse_delta_x << " y=" << m_chasemouse_delta_y << '\n';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user