Input and Application cleanups.

This commit is contained in:
Relintai 2024-01-03 13:39:53 +01:00
parent 116a7d0eaf
commit a8f046ffb0
5 changed files with 88 additions and 109 deletions

View File

@ -34,6 +34,7 @@
#endif
#define USEC_TO_SEC(m_usec) ((m_usec) / 1000000.0)
#define SEC_TO_USEC(m_usec) ((m_usec) * 1000000.0)
enum ClockDirection {
CLOCKWISE,

View File

@ -1,7 +1,9 @@
#include "render_core/application.h"
#include <chrono>
#include "core/math_defs.h"
#include "core/stime.h"
#include "render_core/input.h"
#include "render_core/window.h"
void Application::input_event(const Ref<InputEvent> &event) {
@ -19,16 +21,10 @@ void Application::render() {
}
void Application::main_loop() {
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
uint64_t start = STime::time_us();
//handle input
/*
SDL_Event current_event;
while (SDL_PollEvent(&current_event)) {
event(current_event);
}
*/
Input::get_singleton()->iteration(frame_delta);
//update_world
update(frame_delta);
@ -36,23 +32,23 @@ void Application::main_loop() {
//render
render();
std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
uint64_t end = STime::time_us();
std::chrono::duration<double> elapsed_seconds = end - start;
uint64_t elapsed_us = end - start;
double t = elapsed_seconds.count();
real_t elapsed_seconds = USEC_TO_SEC(elapsed_us);
double tfps = 1.0 / static_cast<float>(target_fps);
double remaining = tfps - t;
real_t tfps = 1.0 / static_cast<float>(target_fps);
real_t remaining = tfps - elapsed_seconds;
++_idle_frames;
if (remaining > 0) {
//uint32_t fms = static_cast<uint32_t>(remaining * 1000.0);
frame_delta = tfps;
//SDL_Delay(fms);
STime::sleep_us((double)SEC_TO_USEC(remaining));
} else {
frame_delta = t;
frame_delta = elapsed_seconds;
}
}
@ -66,6 +62,10 @@ Application::Application() {
frame_delta = 0;
if (!Input::get_singleton()) {
memnew(Input());
}
/*
SDL_SetMainReady();
@ -120,4 +120,3 @@ Application *Application::get_singleton() {
}
Application *Application::_instance = NULL;

View File

@ -27,14 +27,6 @@ public:
return _idle_frames;
}
uint64_t get_physics_frames() const {
return _physics_frames;
}
bool is_in_physics_frame() const {
return _is_in_physics_frame;
}
void main_loop();
Application();
@ -46,14 +38,14 @@ public:
static Application *get_singleton();
double frame_delta;
real_t frame_delta;
protected:
static Application *_instance;
uint64_t _idle_frames;
uint64_t _physics_frames;
bool _is_in_physics_frame;
float _time_scale;
};
#endif // APPLICATION_H

View File

@ -5,13 +5,13 @@
#include "input.h"
#include "core/logger.h"
#include "core/stime.h"
#include "render_core/application.h"
#include "render_core/input/default_controller_mappings.h"
#include "render_core/input/input_map.h"
#include "render_core/texture.h"
#include "render_core/application.h"
#include "render_core/window.h"
#include "core/stime.h"
#include "core/logger.h"
Input *Input::get_singleton() {
return singleton;
@ -70,12 +70,8 @@ bool Input::is_action_just_pressed(const StringName &p_action, bool p_exact) con
// Backward compatibility for legacy behavior, only return true if currently pressed.
bool pressed_requirement = legacy_just_pressed_behavior ? E->get().pressed : true;
if (Application::get_singleton()->is_in_physics_frame()) {
return pressed_requirement && E->get().pressed_physics_frame == Application::get_singleton()->get_physics_frames();
} else {
return pressed_requirement && E->get().pressed_idle_frame == Application::get_singleton()->get_idle_frames();
}
}
bool Input::is_action_just_released(const StringName &p_action, bool p_exact) const {
ERR_FAIL_COND_V_MSG(!InputMap::get_singleton()->has_action(p_action), false, InputMap::get_singleton()->suggest_actions(p_action));
@ -91,12 +87,8 @@ bool Input::is_action_just_released(const StringName &p_action, bool p_exact) co
// Backward compatibility for legacy behavior, only return true if currently released.
bool released_requirement = legacy_just_pressed_behavior ? !E->get().pressed : true;
if (Application::get_singleton()->is_in_physics_frame()) {
return released_requirement && E->get().released_physics_frame == Application::get_singleton()->get_physics_frames();
} else {
return released_requirement && E->get().released_idle_frame == Application::get_singleton()->get_idle_frames();
}
}
float Input::get_action_strength(const StringName &p_action, bool p_exact) const {
ERR_FAIL_COND_V_MSG(!InputMap::get_singleton()->has_action(p_action), 0.0, InputMap::get_singleton()->suggest_actions(p_action));
@ -668,7 +660,6 @@ void Input::action_press(const StringName &p_action, float p_strength) {
// Create or retrieve existing action.
Action &action = action_state[p_action];
action.pressed_physics_frame = Application::get_singleton()->get_physics_frames();
action.pressed_idle_frame = Application::get_singleton()->get_idle_frames();
action.pressed = true;
action.exact = true;
@ -680,7 +671,6 @@ void Input::action_release(const StringName &p_action) {
// Create or retrieve existing action.
Action &action = action_state[p_action];
action.released_physics_frame = Application::get_singleton()->get_physics_frames();
action.released_idle_frame = Application::get_singleton()->get_idle_frames();
action.pressed = false;
action.exact = true;
@ -956,7 +946,10 @@ void Input::set_main_loop(Application *p_main_loop) {
main_loop = p_main_loop;
}
void Input::iteration(float p_step) {
void Input::iteration(real_t p_step) {
if (is_using_input_buffering()) {
flush_buffered_events();
}
}
Input::Input() {
@ -1400,11 +1393,9 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
if (!p_event->is_echo() && is_action_pressed(E->key(), false) != p_event->is_action_pressed(E->key())) {
if (p_event->is_action_pressed(E->key())) {
action.pressed = true;
action.pressed_physics_frame = Application::get_singleton()->get_physics_frames();
action.pressed_idle_frame = Application::get_singleton()->get_idle_frames();
} else {
action.pressed = false;
action.released_physics_frame = Application::get_singleton()->get_physics_frames();
action.released_idle_frame = Application::get_singleton()->get_idle_frames();
}

View File

@ -9,11 +9,11 @@
#include "core/vector2i.h"
#include "object/object.h"
//#include "core/os/main_loop.h"
#include "core/rb_set.h"
#include "core/rb_map.h"
#include "core/rb_set.h"
#include "core/thread_safe.h"
#include "object/reference.h"
#include "object/psignal.h"
#include "object/reference.h"
#include "render_core/input/input_event.h"
class Application;
@ -178,7 +178,7 @@ public:
void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options, const String &quote_style) const;
void set_main_loop(Application *p_main_loop);
void iteration(float p_step);
void iteration(real_t p_step);
Input();
@ -283,9 +283,7 @@ protected:
};
struct Action {
uint64_t pressed_physics_frame;
uint64_t pressed_idle_frame;
uint64_t released_physics_frame;
uint64_t released_idle_frame;
bool pressed;
bool exact;
@ -293,9 +291,7 @@ protected:
float raw_strength;
Action() {
pressed_physics_frame = UINT64_MAX;
pressed_idle_frame = UINT64_MAX;
released_physics_frame = UINT64_MAX;
released_idle_frame = UINT64_MAX;
pressed = false;
exact = true;