Limit FPS in JS by skipping iterations.

This commit is contained in:
Fabio Alessandrelli 2020-06-29 21:00:20 +02:00
parent 07d4513886
commit 1a637b07b1
2 changed files with 13 additions and 0 deletions

View file

@ -36,6 +36,7 @@
#include <emscripten/emscripten.h>
static OS_JavaScript *os = nullptr;
static uint64_t target_ticks = 0;
void exit_callback() {
emscripten_cancel_main_loop(); // After this, we can exit!
@ -47,9 +48,18 @@ void exit_callback() {
}
void main_loop_callback() {
uint64_t current_ticks = os->get_ticks_usec();
bool force_draw = DisplayServerJavaScript::get_singleton()->check_size_force_redraw();
if (force_draw) {
Main::force_redraw();
} else if (current_ticks < target_ticks) {
return; // Skip frame.
}
int target_fps = Engine::get_singleton()->get_target_fps();
if (target_fps > 0) {
target_ticks += (uint64_t)(1000000 / target_fps);
}
if (os->main_loop_iterate()) {
emscripten_cancel_main_loop(); // Cancel current loop and wait for finalize_async.

View file

@ -83,6 +83,9 @@ public:
String get_executable_path() const;
virtual Error shell_open(String p_uri);
virtual String get_name() const;
// Override default OS implementation which would block the main thread with delay_usec.
// Implemented in javascript_main.cpp loop callback instead.
virtual void add_frame_delay(bool p_can_draw) {}
virtual bool can_draw() const;
virtual String get_cache_path() const;