mirror of
https://github.com/Relintai/sfw.git
synced 2024-11-08 07:52:09 +01:00
Input and Application cleanups.
This commit is contained in:
parent
116a7d0eaf
commit
a8f046ffb0
@ -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,
|
||||
|
@ -1,123 +1,122 @@
|
||||
#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) {
|
||||
scene->input_event(event);
|
||||
scene->input_event(event);
|
||||
}
|
||||
|
||||
void Application::update(float delta) {
|
||||
scene->update(delta);
|
||||
scene->update(delta);
|
||||
}
|
||||
|
||||
void Application::render() {
|
||||
scene->render();
|
||||
scene->render();
|
||||
|
||||
//SDL_GL_SwapWindow(window);
|
||||
//SDL_GL_SwapWindow(window);
|
||||
}
|
||||
|
||||
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;
|
||||
//handle input
|
||||
Input::get_singleton()->iteration(frame_delta);
|
||||
|
||||
while (SDL_PollEvent(¤t_event)) {
|
||||
event(current_event);
|
||||
}
|
||||
*/
|
||||
//update_world
|
||||
update(frame_delta);
|
||||
|
||||
//update_world
|
||||
update(frame_delta);
|
||||
//render
|
||||
render();
|
||||
|
||||
//render
|
||||
render();
|
||||
uint64_t end = STime::time_us();
|
||||
|
||||
std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
|
||||
uint64_t elapsed_us = end - start;
|
||||
|
||||
std::chrono::duration<double> elapsed_seconds = end - start;
|
||||
real_t elapsed_seconds = USEC_TO_SEC(elapsed_us);
|
||||
|
||||
double t = elapsed_seconds.count();
|
||||
real_t tfps = 1.0 / static_cast<float>(target_fps);
|
||||
real_t remaining = tfps - elapsed_seconds;
|
||||
|
||||
double tfps = 1.0 / static_cast<float>(target_fps);
|
||||
double remaining = tfps - t;
|
||||
++_idle_frames;
|
||||
|
||||
if (remaining > 0) {
|
||||
//uint32_t fms = static_cast<uint32_t>(remaining * 1000.0);
|
||||
if (remaining > 0) {
|
||||
frame_delta = tfps;
|
||||
|
||||
frame_delta = tfps;
|
||||
|
||||
//SDL_Delay(fms);
|
||||
} else {
|
||||
frame_delta = t;
|
||||
}
|
||||
STime::sleep_us((double)SEC_TO_USEC(remaining));
|
||||
} else {
|
||||
frame_delta = elapsed_seconds;
|
||||
}
|
||||
}
|
||||
|
||||
Application::Application() {
|
||||
_instance = this;
|
||||
_instance = this;
|
||||
|
||||
running = true;
|
||||
target_fps = 60;
|
||||
running = true;
|
||||
target_fps = 60;
|
||||
|
||||
scene = NULL;
|
||||
scene = NULL;
|
||||
|
||||
frame_delta = 0;
|
||||
frame_delta = 0;
|
||||
|
||||
/*
|
||||
SDL_SetMainReady();
|
||||
|
||||
int error = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
|
||||
|
||||
if (error) {
|
||||
SDL_Log("SDL_Init fail: %s\n", SDL_GetError());
|
||||
|
||||
running = false;
|
||||
return;
|
||||
if (!Input::get_singleton()) {
|
||||
memnew(Input());
|
||||
}
|
||||
|
||||
/*
|
||||
SDL_SetMainReady();
|
||||
|
||||
int error = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
|
||||
|
||||
if (error) {
|
||||
SDL_Log("SDL_Init fail: %s\n", SDL_GetError());
|
||||
|
||||
running = false;
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
#else
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
||||
#endif
|
||||
|
||||
window = SDL_CreateWindow("SDL + OpenGL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
||||
window = SDL_CreateWindow("SDL + OpenGL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
||||
|
||||
if (!window) {
|
||||
SDL_Log("SDL_CreateWindow Failed! %s\n", SDL_GetError());
|
||||
if (!window) {
|
||||
SDL_Log("SDL_CreateWindow Failed! %s\n", SDL_GetError());
|
||||
|
||||
running = false;
|
||||
running = false;
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
context = SDL_GL_CreateContext(window);
|
||||
context = SDL_GL_CreateContext(window);
|
||||
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);
|
||||
#endif // defined
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);
|
||||
#endif // defined
|
||||
|
||||
printf("%s\n", glGetString(GL_VERSION));
|
||||
*/
|
||||
printf("%s\n", glGetString(GL_VERSION));
|
||||
*/
|
||||
}
|
||||
Application::~Application() {
|
||||
//SDL_DestroyWindow(window);
|
||||
//SDL_DestroyWindow(window);
|
||||
|
||||
// window = NULL;
|
||||
// window = NULL;
|
||||
|
||||
//SDL_Quit();
|
||||
//SDL_Quit();
|
||||
}
|
||||
|
||||
Application *Application::get_singleton() {
|
||||
return _instance;
|
||||
return _instance;
|
||||
}
|
||||
|
||||
Application * Application::_instance = NULL;
|
||||
|
||||
Application *Application::_instance = NULL;
|
||||
|
@ -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
|
||||
|
@ -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,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.
|
||||
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 {
|
||||
@ -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.
|
||||
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 {
|
||||
@ -614,7 +606,7 @@ Point2i Input::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, con
|
||||
const Point2i pos_local = p_motion->get_global_position() - p_rect.position;
|
||||
const Point2i pos_warped(Math::fposmod(pos_local.x, p_rect.size.x), Math::fposmod(pos_local.y, p_rect.size.y));
|
||||
//if (pos_warped != pos_local) {
|
||||
//OS::get_singleton()->warp_mouse_position(pos_warped + p_rect.position);
|
||||
//OS::get_singleton()->warp_mouse_position(pos_warped + p_rect.position);
|
||||
//}
|
||||
|
||||
return rel_warped;
|
||||
@ -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() {
|
||||
@ -981,7 +974,7 @@ Input::Input() {
|
||||
}
|
||||
|
||||
// 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 != "") {
|
||||
Vector<String> entries = env_mapping.split("\n");
|
||||
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()) {
|
||||
Vector<String> entries = env_ignore_devices.split(",");
|
||||
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_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();
|
||||
}
|
||||
|
||||
@ -1439,7 +1430,7 @@ String Input::_hex_str(uint8_t p_byte) {
|
||||
Input *Input::singleton = nullptr;
|
||||
|
||||
void Input::SpeedTrack::update(const Vector2 &p_delta_p) {
|
||||
uint64_t tick = STime::time_us();
|
||||
uint64_t tick = STime::time_us();
|
||||
uint32_t tdiff = tick - last_tick;
|
||||
float delta_t = tdiff / 1000000.0;
|
||||
last_tick = tick;
|
||||
|
@ -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 "e_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;
|
||||
|
Loading…
Reference in New Issue
Block a user