Use doubles for time everywhere in Timer/SceneTree

This commit is contained in:
Aaron Franke 2021-05-21 01:23:35 -04:00
parent 0c68ccecda
commit 4ecb6fba80
No known key found for this signature in database
GPG key ID: 40A1750B977E56BF
13 changed files with 68 additions and 68 deletions

View file

@ -66,7 +66,7 @@ void MainLoop::initialize() {
} }
} }
bool MainLoop::physics_process(float p_time) { bool MainLoop::physics_process(double p_time) {
if (get_script_instance()) { if (get_script_instance()) {
return get_script_instance()->call("_physics_process", p_time); return get_script_instance()->call("_physics_process", p_time);
} }
@ -74,7 +74,7 @@ bool MainLoop::physics_process(float p_time) {
return false; return false;
} }
bool MainLoop::process(float p_time) { bool MainLoop::process(double p_time) {
if (get_script_instance()) { if (get_script_instance()) {
return get_script_instance()->call("_process", p_time); return get_script_instance()->call("_process", p_time);
} }

View file

@ -60,8 +60,8 @@ public:
}; };
virtual void initialize(); virtual void initialize();
virtual bool physics_process(float p_time); virtual bool physics_process(double p_time);
virtual bool process(float p_time); virtual bool process(double p_time);
virtual void finalize(); virtual void finalize();
void set_initialize_script(const Ref<Script> &p_initialize_script); void set_initialize_script(const Ref<Script> &p_initialize_script);

View file

@ -30,7 +30,7 @@
#include "main_timer_sync.h" #include "main_timer_sync.h"
void MainFrameTime::clamp_process_step(float min_process_step, float max_process_step) { void MainFrameTime::clamp_process_step(double min_process_step, double max_process_step) {
if (process_step < min_process_step) { if (process_step < min_process_step) {
process_step = min_process_step; process_step = min_process_step;
} else if (process_step > max_process_step) { } else if (process_step > max_process_step) {
@ -43,25 +43,25 @@ void MainFrameTime::clamp_process_step(float min_process_step, float max_process
// returns the fraction of p_physics_step required for the timer to overshoot // returns the fraction of p_physics_step required for the timer to overshoot
// before advance_core considers changing the physics_steps return from // before advance_core considers changing the physics_steps return from
// the typical values as defined by typical_physics_steps // the typical values as defined by typical_physics_steps
float MainTimerSync::get_physics_jitter_fix() { double MainTimerSync::get_physics_jitter_fix() {
return Engine::get_singleton()->get_physics_jitter_fix(); return Engine::get_singleton()->get_physics_jitter_fix();
} }
// gets our best bet for the average number of physics steps per render frame // gets our best bet for the average number of physics steps per render frame
// return value: number of frames back this data is consistent // return value: number of frames back this data is consistent
int MainTimerSync::get_average_physics_steps(float &p_min, float &p_max) { int MainTimerSync::get_average_physics_steps(double &p_min, double &p_max) {
p_min = typical_physics_steps[0]; p_min = typical_physics_steps[0];
p_max = p_min + 1; p_max = p_min + 1;
for (int i = 1; i < CONTROL_STEPS; ++i) { for (int i = 1; i < CONTROL_STEPS; ++i) {
const float typical_lower = typical_physics_steps[i]; const double typical_lower = typical_physics_steps[i];
const float current_min = typical_lower / (i + 1); const double current_min = typical_lower / (i + 1);
if (current_min > p_max) { if (current_min > p_max) {
return i; // bail out of further restrictions would void the interval return i; // bail out of further restrictions would void the interval
} else if (current_min > p_min) { } else if (current_min > p_min) {
p_min = current_min; p_min = current_min;
} }
const float current_max = (typical_lower + 1) / (i + 1); const double current_max = (typical_lower + 1) / (i + 1);
if (current_max < p_min) { if (current_max < p_min) {
return i; return i;
} else if (current_max < p_max) { } else if (current_max < p_max) {
@ -73,7 +73,7 @@ int MainTimerSync::get_average_physics_steps(float &p_min, float &p_max) {
} }
// advance physics clock by p_process_step, return appropriate number of steps to simulate // advance physics clock by p_process_step, return appropriate number of steps to simulate
MainFrameTime MainTimerSync::advance_core(float p_physics_step, int p_physics_fps, float p_process_step) { MainFrameTime MainTimerSync::advance_core(double p_physics_step, int p_physics_fps, double p_process_step) {
MainFrameTime ret; MainFrameTime ret;
ret.process_step = p_process_step; ret.process_step = p_process_step;
@ -146,7 +146,7 @@ MainFrameTime MainTimerSync::advance_core(float p_physics_step, int p_physics_fp
} }
// calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero // calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero
MainFrameTime MainTimerSync::advance_checked(float p_physics_step, int p_physics_fps, float p_process_step) { MainFrameTime MainTimerSync::advance_checked(double p_physics_step, int p_physics_fps, double p_process_step) {
if (fixed_fps != -1) { if (fixed_fps != -1) {
p_process_step = 1.0 / fixed_fps; p_process_step = 1.0 / fixed_fps;
} }
@ -163,7 +163,7 @@ MainFrameTime MainTimerSync::advance_checked(float p_physics_step, int p_physics
// first, least important clamping: keep ret.process_step consistent with typical_physics_steps. // first, least important clamping: keep ret.process_step consistent with typical_physics_steps.
// this smoothes out the process steps and culls small but quick variations. // this smoothes out the process steps and culls small but quick variations.
{ {
float min_average_physics_steps, max_average_physics_steps; double min_average_physics_steps, max_average_physics_steps;
int consistent_steps = get_average_physics_steps(min_average_physics_steps, max_average_physics_steps); int consistent_steps = get_average_physics_steps(min_average_physics_steps, max_average_physics_steps);
if (consistent_steps > 3) { if (consistent_steps > 3) {
ret.clamp_process_step(min_average_physics_steps * p_physics_step, max_average_physics_steps * p_physics_step); ret.clamp_process_step(min_average_physics_steps * p_physics_step, max_average_physics_steps * p_physics_step);
@ -171,7 +171,7 @@ MainFrameTime MainTimerSync::advance_checked(float p_physics_step, int p_physics
} }
// second clamping: keep abs(time_deficit) < jitter_fix * frame_slise // second clamping: keep abs(time_deficit) < jitter_fix * frame_slise
float max_clock_deviation = get_physics_jitter_fix() * p_physics_step; double max_clock_deviation = get_physics_jitter_fix() * p_physics_step;
ret.clamp_process_step(p_process_step - max_clock_deviation, p_process_step + max_clock_deviation); ret.clamp_process_step(p_process_step - max_clock_deviation, p_process_step + max_clock_deviation);
// last clamping: make sure time_accum is between 0 and p_physics_step for consistency between physics and process // last clamping: make sure time_accum is between 0 and p_physics_step for consistency between physics and process
@ -191,7 +191,7 @@ MainFrameTime MainTimerSync::advance_checked(float p_physics_step, int p_physics
} }
// determine wall clock step since last iteration // determine wall clock step since last iteration
float MainTimerSync::get_cpu_process_step() { double MainTimerSync::get_cpu_process_step() {
uint64_t cpu_ticks_elapsed = current_cpu_ticks_usec - last_cpu_ticks_usec; uint64_t cpu_ticks_elapsed = current_cpu_ticks_usec - last_cpu_ticks_usec;
last_cpu_ticks_usec = current_cpu_ticks_usec; last_cpu_ticks_usec = current_cpu_ticks_usec;
@ -220,8 +220,8 @@ void MainTimerSync::set_fixed_fps(int p_fixed_fps) {
} }
// advance one physics frame, return timesteps to take // advance one physics frame, return timesteps to take
MainFrameTime MainTimerSync::advance(float p_physics_step, int p_physics_fps) { MainFrameTime MainTimerSync::advance(double p_physics_step, int p_physics_fps) {
float cpu_process_step = get_cpu_process_step(); double cpu_process_step = get_cpu_process_step();
return advance_checked(p_physics_step, p_physics_fps, cpu_process_step); return advance_checked(p_physics_step, p_physics_fps, cpu_process_step);
} }

View file

@ -34,11 +34,11 @@
#include "core/config/engine.h" #include "core/config/engine.h"
struct MainFrameTime { struct MainFrameTime {
float process_step; // delta time to advance during process() double process_step; // delta time to advance during process()
int physics_steps; // number of times to iterate the physics engine int physics_steps; // number of times to iterate the physics engine
float interpolation_fraction; // fraction through the current physics tick double interpolation_fraction; // fraction through the current physics tick
void clamp_process_step(float min_process_step, float max_process_step); void clamp_process_step(double min_process_step, double max_process_step);
}; };
class MainTimerSync { class MainTimerSync {
@ -47,10 +47,10 @@ class MainTimerSync {
uint64_t current_cpu_ticks_usec = 0; uint64_t current_cpu_ticks_usec = 0;
// logical game time since last physics timestep // logical game time since last physics timestep
float time_accum = 0; double time_accum = 0;
// current difference between wall clock time and reported sum of process_steps // current difference between wall clock time and reported sum of process_steps
float time_deficit = 0; double time_deficit = 0;
// number of frames back for keeping accumulated physics steps roughly constant. // number of frames back for keeping accumulated physics steps roughly constant.
// value of 12 chosen because that is what is required to make 144 Hz monitors // value of 12 chosen because that is what is required to make 144 Hz monitors
@ -70,20 +70,20 @@ protected:
// returns the fraction of p_physics_step required for the timer to overshoot // returns the fraction of p_physics_step required for the timer to overshoot
// before advance_core considers changing the physics_steps return from // before advance_core considers changing the physics_steps return from
// the typical values as defined by typical_physics_steps // the typical values as defined by typical_physics_steps
float get_physics_jitter_fix(); double get_physics_jitter_fix();
// gets our best bet for the average number of physics steps per render frame // gets our best bet for the average number of physics steps per render frame
// return value: number of frames back this data is consistent // return value: number of frames back this data is consistent
int get_average_physics_steps(float &p_min, float &p_max); int get_average_physics_steps(double &p_min, double &p_max);
// advance physics clock by p_process_step, return appropriate number of steps to simulate // advance physics clock by p_process_step, return appropriate number of steps to simulate
MainFrameTime advance_core(float p_physics_step, int p_physics_fps, float p_process_step); MainFrameTime advance_core(double p_physics_step, int p_physics_fps, double p_process_step);
// calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero // calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero
MainFrameTime advance_checked(float p_physics_step, int p_physics_fps, float p_process_step); MainFrameTime advance_checked(double p_physics_step, int p_physics_fps, double p_process_step);
// determine wall clock step since last iteration // determine wall clock step since last iteration
float get_cpu_process_step(); double get_cpu_process_step();
public: public:
MainTimerSync(); MainTimerSync();
@ -96,7 +96,7 @@ public:
void set_fixed_fps(int p_fixed_fps); void set_fixed_fps(int p_fixed_fps);
// advance one frame, return timesteps to take // advance one frame, return timesteps to take
MainFrameTime advance(float p_physics_step, int p_physics_fps); MainFrameTime advance(double p_physics_step, int p_physics_fps);
}; };
#endif // MAIN_TIMER_SYNC_H #endif // MAIN_TIMER_SYNC_H

View file

@ -77,7 +77,7 @@ void Performance::_bind_methods() {
BIND_ENUM_CONSTANT(MONITOR_MAX); BIND_ENUM_CONSTANT(MONITOR_MAX);
} }
float Performance::_get_node_count() const { int Performance::_get_node_count() const {
MainLoop *ml = OS::get_singleton()->get_main_loop(); MainLoop *ml = OS::get_singleton()->get_main_loop();
SceneTree *sml = Object::cast_to<SceneTree>(ml); SceneTree *sml = Object::cast_to<SceneTree>(ml);
if (!sml) { if (!sml) {
@ -118,7 +118,7 @@ String Performance::get_monitor_name(Monitor p_monitor) const {
return names[p_monitor]; return names[p_monitor];
} }
float Performance::get_monitor(Monitor p_monitor) const { double Performance::get_monitor(Monitor p_monitor) const {
switch (p_monitor) { switch (p_monitor) {
case TIME_FPS: case TIME_FPS:
return Engine::get_singleton()->get_frames_per_second(); return Engine::get_singleton()->get_frames_per_second();
@ -207,11 +207,11 @@ Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const
return types[p_monitor]; return types[p_monitor];
} }
void Performance::set_process_time(float p_pt) { void Performance::set_process_time(double p_pt) {
_process_time = p_pt; _process_time = p_pt;
} }
void Performance::set_physics_process_time(float p_pt) { void Performance::set_physics_process_time(double p_pt) {
_physics_process_time = p_pt; _physics_process_time = p_pt;
} }

View file

@ -43,10 +43,10 @@ class Performance : public Object {
static Performance *singleton; static Performance *singleton;
static void _bind_methods(); static void _bind_methods();
float _get_node_count() const; int _get_node_count() const;
float _process_time; double _process_time;
float _physics_process_time; double _physics_process_time;
class MonitorCall { class MonitorCall {
Callable _callable; Callable _callable;
@ -96,13 +96,13 @@ public:
MONITOR_TYPE_TIME MONITOR_TYPE_TIME
}; };
float get_monitor(Monitor p_monitor) const; double get_monitor(Monitor p_monitor) const;
String get_monitor_name(Monitor p_monitor) const; String get_monitor_name(Monitor p_monitor) const;
MonitorType get_monitor_type(Monitor p_monitor) const; MonitorType get_monitor_type(Monitor p_monitor) const;
void set_process_time(float p_pt); void set_process_time(double p_pt);
void set_physics_process_time(float p_pt); void set_physics_process_time(double p_pt);
void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args); void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args);
void remove_custom_monitor(const StringName &p_id); void remove_custom_monitor(const StringName &p_id);

View file

@ -66,11 +66,11 @@ void SceneTreeTimer::_bind_methods() {
ADD_SIGNAL(MethodInfo("timeout")); ADD_SIGNAL(MethodInfo("timeout"));
} }
void SceneTreeTimer::set_time_left(float p_time) { void SceneTreeTimer::set_time_left(double p_time) {
time_left = p_time; time_left = p_time;
} }
float SceneTreeTimer::get_time_left() const { double SceneTreeTimer::get_time_left() const {
return time_left; return time_left;
} }
@ -403,7 +403,7 @@ void SceneTree::initialize() {
MainLoop::initialize(); MainLoop::initialize();
} }
bool SceneTree::physics_process(float p_time) { bool SceneTree::physics_process(double p_time) {
root_lock++; root_lock++;
current_frame++; current_frame++;
@ -432,7 +432,7 @@ bool SceneTree::physics_process(float p_time) {
return _quit; return _quit;
} }
bool SceneTree::process(float p_time) { bool SceneTree::process(double p_time) {
root_lock++; root_lock++;
MainLoop::process(p_time); MainLoop::process(p_time);
@ -474,7 +474,7 @@ bool SceneTree::process(float p_time) {
continue; continue;
} }
float time_left = E->get()->get_time_left(); double time_left = E->get()->get_time_left();
if (E->get()->is_ignore_time_scale()) { if (E->get()->is_ignore_time_scale()) {
time_left -= Engine::get_singleton()->get_process_step(); time_left -= Engine::get_singleton()->get_process_step();
} else { } else {
@ -1124,7 +1124,7 @@ void SceneTree::add_current_scene(Node *p_current) {
root->add_child(p_current); root->add_child(p_current);
} }
Ref<SceneTreeTimer> SceneTree::create_timer(float p_delay_sec, bool p_process_always) { Ref<SceneTreeTimer> SceneTree::create_timer(double p_delay_sec, bool p_process_always) {
Ref<SceneTreeTimer> stt; Ref<SceneTreeTimer> stt;
stt.instantiate(); stt.instantiate();
stt->set_process_always(p_process_always); stt->set_process_always(p_process_always);

View file

@ -52,7 +52,7 @@ class Tween;
class SceneTreeTimer : public RefCounted { class SceneTreeTimer : public RefCounted {
GDCLASS(SceneTreeTimer, RefCounted); GDCLASS(SceneTreeTimer, RefCounted);
float time_left = 0.0; double time_left = 0.0;
bool process_always = true; bool process_always = true;
bool ignore_time_scale = false; bool ignore_time_scale = false;
@ -60,8 +60,8 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
void set_time_left(float p_time); void set_time_left(double p_time);
float get_time_left() const; double get_time_left() const;
void set_process_always(bool p_process_always); void set_process_always(bool p_process_always);
bool is_process_always(); bool is_process_always();
@ -91,8 +91,8 @@ private:
Window *root = nullptr; Window *root = nullptr;
uint64_t tree_version = 1; uint64_t tree_version = 1;
float physics_process_time = 1.0; double physics_process_time = 1.0;
float process_time = 1.0; double process_time = 1.0;
bool accept_quit = true; bool accept_quit = true;
bool quit_on_go_back = true; bool quit_on_go_back = true;
@ -237,8 +237,8 @@ public:
virtual void initialize() override; virtual void initialize() override;
virtual bool physics_process(float p_time) override; virtual bool physics_process(double p_time) override;
virtual bool process(float p_time) override; virtual bool process(double p_time) override;
virtual void finalize() override; virtual void finalize() override;
@ -247,8 +247,8 @@ public:
void quit(int p_exit_code = EXIT_SUCCESS); void quit(int p_exit_code = EXIT_SUCCESS);
_FORCE_INLINE_ float get_physics_process_time() const { return physics_process_time; } _FORCE_INLINE_ double get_physics_process_time() const { return physics_process_time; }
_FORCE_INLINE_ float get_process_time() const { return process_time; } _FORCE_INLINE_ double get_process_time() const { return process_time; }
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
bool is_node_being_edited(const Node *p_node) const; bool is_node_being_edited(const Node *p_node) const;
@ -317,7 +317,7 @@ public:
Error change_scene_to(const Ref<PackedScene> &p_scene); Error change_scene_to(const Ref<PackedScene> &p_scene);
Error reload_current_scene(); Error reload_current_scene();
Ref<SceneTreeTimer> create_timer(float p_delay_sec, bool p_process_always = true); Ref<SceneTreeTimer> create_timer(double p_delay_sec, bool p_process_always = true);
Ref<Tween> create_tween(); Ref<Tween> create_tween();
Array get_processed_tweens(); Array get_processed_tweens();

View file

@ -81,12 +81,12 @@ void Timer::_notification(int p_what) {
} }
} }
void Timer::set_wait_time(float p_time) { void Timer::set_wait_time(double p_time) {
ERR_FAIL_COND_MSG(p_time <= 0, "Time should be greater than zero."); ERR_FAIL_COND_MSG(p_time <= 0, "Time should be greater than zero.");
wait_time = p_time; wait_time = p_time;
} }
float Timer::get_wait_time() const { double Timer::get_wait_time() const {
return wait_time; return wait_time;
} }
@ -106,7 +106,7 @@ bool Timer::has_autostart() const {
return autostart; return autostart;
} }
void Timer::start(float p_time) { void Timer::start(double p_time) {
ERR_FAIL_COND_MSG(!is_inside_tree(), "Timer was not added to the SceneTree. Either add it or set autostart to true."); ERR_FAIL_COND_MSG(!is_inside_tree(), "Timer was not added to the SceneTree. Either add it or set autostart to true.");
if (p_time > 0) { if (p_time > 0) {
@ -139,7 +139,7 @@ bool Timer::is_stopped() const {
return get_time_left() <= 0; return get_time_left() <= 0;
} }
float Timer::get_time_left() const { double Timer::get_time_left() const {
return time_left > 0 ? time_left : 0; return time_left > 0 ? time_left : 0;
} }

View file

@ -36,7 +36,7 @@
class Timer : public Node { class Timer : public Node {
GDCLASS(Timer, Node); GDCLASS(Timer, Node);
float wait_time = 1.0; double wait_time = 1.0;
bool one_shot = false; bool one_shot = false;
bool autostart = false; bool autostart = false;
bool processing = false; bool processing = false;
@ -54,8 +54,8 @@ public:
TIMER_PROCESS_IDLE, TIMER_PROCESS_IDLE,
}; };
void set_wait_time(float p_time); void set_wait_time(double p_time);
float get_wait_time() const; double get_wait_time() const;
void set_one_shot(bool p_one_shot); void set_one_shot(bool p_one_shot);
bool is_one_shot() const; bool is_one_shot() const;
@ -63,7 +63,7 @@ public:
void set_autostart(bool p_start); void set_autostart(bool p_start);
bool has_autostart() const; bool has_autostart() const;
void start(float p_time = -1); void start(double p_time = -1);
void stop(); void stop();
void set_paused(bool p_paused); void set_paused(bool p_paused);
@ -71,7 +71,7 @@ public:
bool is_stopped() const; bool is_stopped() const;
float get_time_left() const; double get_time_left() const;
void set_timer_process_callback(TimerProcessCallback p_callback); void set_timer_process_callback(TimerProcessCallback p_callback);
TimerProcessCallback get_timer_process_callback() const; TimerProcessCallback get_timer_process_callback() const;

View file

@ -386,7 +386,7 @@ public:
//_add_plane(Vector2(-1,0).normalized(),-600); //_add_plane(Vector2(-1,0).normalized(),-600);
} }
virtual bool process(float p_time) override { virtual bool process(double p_time) override {
return false; return false;
} }
virtual void finalize() override { virtual void finalize() override {

View file

@ -313,7 +313,7 @@ public:
test_fall(); test_fall();
quit = false; quit = false;
} }
virtual bool physics_process(float p_time) override { virtual bool physics_process(double p_time) override {
if (mover.is_valid()) { if (mover.is_valid()) {
static real_t joy_speed = 10; static real_t joy_speed = 10;
PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
@ -399,7 +399,7 @@ public:
create_static_plane(Plane(Vector3(0, 1, 0), -1)); create_static_plane(Plane(Vector3(0, 1, 0), -1));
} }
virtual bool process(float p_time) override { virtual bool process(double p_time) override {
return false; return false;
} }

View file

@ -199,7 +199,7 @@ public:
ofs = 0; ofs = 0;
quit = false; quit = false;
} }
virtual bool iteration(float p_time) { virtual bool iteration(double p_time) {
RenderingServer *vs = RenderingServer::get_singleton(); RenderingServer *vs = RenderingServer::get_singleton();
//Transform3D t; //Transform3D t;
//t.rotate(Vector3(0, 1, 0), ofs); //t.rotate(Vector3(0, 1, 0), ofs);
@ -223,7 +223,7 @@ public:
return quit; return quit;
} }
virtual bool idle(float p_time) { virtual bool idle(double p_time) {
return quit; return quit;
} }