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 #endif
#define USEC_TO_SEC(m_usec) ((m_usec) / 1000000.0) #define USEC_TO_SEC(m_usec) ((m_usec) / 1000000.0)
#define SEC_TO_USEC(m_usec) ((m_usec) * 1000000.0)
enum ClockDirection { enum ClockDirection {
CLOCKWISE, CLOCKWISE,

View File

@ -1,7 +1,9 @@
#include "render_core/application.h" #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" #include "render_core/window.h"
void Application::input_event(const Ref<InputEvent> &event) { void Application::input_event(const Ref<InputEvent> &event) {
@ -19,16 +21,10 @@ void Application::render() {
} }
void Application::main_loop() { 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 //handle input
/* Input::get_singleton()->iteration(frame_delta);
SDL_Event current_event;
while (SDL_PollEvent(&current_event)) {
event(current_event);
}
*/
//update_world //update_world
update(frame_delta); update(frame_delta);
@ -36,23 +32,23 @@ void Application::main_loop() {
//render //render
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); real_t tfps = 1.0 / static_cast<float>(target_fps);
double remaining = tfps - t; real_t remaining = tfps - elapsed_seconds;
++_idle_frames;
if (remaining > 0) { if (remaining > 0) {
//uint32_t fms = static_cast<uint32_t>(remaining * 1000.0);
frame_delta = tfps; frame_delta = tfps;
//SDL_Delay(fms); STime::sleep_us((double)SEC_TO_USEC(remaining));
} else { } else {
frame_delta = t; frame_delta = elapsed_seconds;
} }
} }
@ -66,6 +62,10 @@ Application::Application() {
frame_delta = 0; frame_delta = 0;
if (!Input::get_singleton()) {
memnew(Input());
}
/* /*
SDL_SetMainReady(); SDL_SetMainReady();
@ -119,5 +119,4 @@ Application *Application::get_singleton() {
return _instance; return _instance;
} }
Application * Application::_instance = NULL; Application *Application::_instance = NULL;

View File

@ -27,14 +27,6 @@ public:
return _idle_frames; 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(); void main_loop();
Application(); Application();
@ -46,14 +38,14 @@ public:
static Application *get_singleton(); static Application *get_singleton();
double frame_delta; real_t frame_delta;
protected: protected:
static Application *_instance; static Application *_instance;
uint64_t _idle_frames; uint64_t _idle_frames;
uint64_t _physics_frames;
bool _is_in_physics_frame; float _time_scale;
}; };
#endif // APPLICATION_H #endif // APPLICATION_H

View File

@ -5,13 +5,13 @@
#include "input.h" #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/default_controller_mappings.h"
#include "render_core/input/input_map.h" #include "render_core/input/input_map.h"
#include "render_core/texture.h" #include "render_core/texture.h"
#include "render_core/application.h"
#include "render_core/window.h" #include "render_core/window.h"
#include "core/stime.h"
#include "core/logger.h"
Input *Input::get_singleton() { Input *Input::get_singleton() {
return singleton; return singleton;
@ -70,11 +70,7 @@ 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. // Backward compatibility for legacy behavior, only return true if currently pressed.
bool pressed_requirement = legacy_just_pressed_behavior ? E->get().pressed : true; 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(); 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 { bool Input::is_action_just_released(const StringName &p_action, bool p_exact) const {
@ -91,11 +87,7 @@ 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. // Backward compatibility for legacy behavior, only return true if currently released.
bool released_requirement = legacy_just_pressed_behavior ? !E->get().pressed : true; 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(); 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 { float Input::get_action_strength(const StringName &p_action, bool p_exact) const {
@ -668,7 +660,6 @@ void Input::action_press(const StringName &p_action, float p_strength) {
// Create or retrieve existing action. // Create or retrieve existing action.
Action &action = action_state[p_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_idle_frame = Application::get_singleton()->get_idle_frames();
action.pressed = true; action.pressed = true;
action.exact = true; action.exact = true;
@ -680,7 +671,6 @@ void Input::action_release(const StringName &p_action) {
// Create or retrieve existing action. // Create or retrieve existing action.
Action &action = action_state[p_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.released_idle_frame = Application::get_singleton()->get_idle_frames();
action.pressed = false; action.pressed = false;
action.exact = true; action.exact = true;
@ -956,7 +946,10 @@ void Input::set_main_loop(Application *p_main_loop) {
main_loop = 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() { Input::Input() {
@ -981,7 +974,7 @@ Input::Input() {
} }
// If defined, parse SDL_GAMECONTROLLERCONFIG for possible new mappings/overrides. // If defined, parse SDL_GAMECONTROLLERCONFIG for possible new mappings/overrides.
String env_mapping = "";//OS::get_singleton()->get_environment("SDL_GAMECONTROLLERCONFIG"); String env_mapping = ""; //OS::get_singleton()->get_environment("SDL_GAMECONTROLLERCONFIG");
if (env_mapping != "") { if (env_mapping != "") {
Vector<String> entries = env_mapping.split("\n"); Vector<String> entries = env_mapping.split("\n");
for (int i = 0; i < entries.size(); i++) { for (int i = 0; i < entries.size(); i++) {
@ -992,7 +985,7 @@ Input::Input() {
} }
} }
String env_ignore_devices = "";//OS::get_singleton()->get_environment("SDL_GAMECONTROLLER_IGNORE_DEVICES"); String env_ignore_devices = ""; //OS::get_singleton()->get_environment("SDL_GAMECONTROLLER_IGNORE_DEVICES");
if (!env_ignore_devices.empty()) { if (!env_ignore_devices.empty()) {
Vector<String> entries = env_ignore_devices.split(","); Vector<String> entries = env_ignore_devices.split(",");
for (int i = 0; i < entries.size(); i++) { for (int i = 0; i < entries.size(); i++) {
@ -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_echo() && is_action_pressed(E->key(), false) != p_event->is_action_pressed(E->key())) {
if (p_event->is_action_pressed(E->key())) { if (p_event->is_action_pressed(E->key())) {
action.pressed = true; action.pressed = true;
action.pressed_physics_frame = Application::get_singleton()->get_physics_frames();
action.pressed_idle_frame = Application::get_singleton()->get_idle_frames(); action.pressed_idle_frame = Application::get_singleton()->get_idle_frames();
} else { } else {
action.pressed = false; action.pressed = false;
action.released_physics_frame = Application::get_singleton()->get_physics_frames();
action.released_idle_frame = Application::get_singleton()->get_idle_frames(); action.released_idle_frame = Application::get_singleton()->get_idle_frames();
} }

View File

@ -9,11 +9,11 @@
#include "core/vector2i.h" #include "core/vector2i.h"
#include "object/object.h" #include "object/object.h"
//#include "core/os/main_loop.h" //#include "core/os/main_loop.h"
#include "core/rb_set.h"
#include "core/rb_map.h" #include "core/rb_map.h"
#include "core/rb_set.h"
#include "core/thread_safe.h" #include "core/thread_safe.h"
#include "object/reference.h"
#include "object/psignal.h" #include "object/psignal.h"
#include "object/reference.h"
#include "render_core/input/input_event.h" #include "render_core/input/input_event.h"
class Application; 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 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 set_main_loop(Application *p_main_loop);
void iteration(float p_step); void iteration(real_t p_step);
Input(); Input();
@ -283,9 +283,7 @@ protected:
}; };
struct Action { struct Action {
uint64_t pressed_physics_frame;
uint64_t pressed_idle_frame; uint64_t pressed_idle_frame;
uint64_t released_physics_frame;
uint64_t released_idle_frame; uint64_t released_idle_frame;
bool pressed; bool pressed;
bool exact; bool exact;
@ -293,9 +291,7 @@ protected:
float raw_strength; float raw_strength;
Action() { Action() {
pressed_physics_frame = UINT64_MAX;
pressed_idle_frame = UINT64_MAX; pressed_idle_frame = UINT64_MAX;
released_physics_frame = UINT64_MAX;
released_idle_frame = UINT64_MAX; released_idle_frame = UINT64_MAX;
pressed = false; pressed = false;
exact = true; exact = true;