Limit FPS in JS by skipping iterations.
This commit is contained in:
parent
07d4513886
commit
1a637b07b1
2 changed files with 13 additions and 0 deletions
|
@ -36,6 +36,7 @@
|
||||||
#include <emscripten/emscripten.h>
|
#include <emscripten/emscripten.h>
|
||||||
|
|
||||||
static OS_JavaScript *os = nullptr;
|
static OS_JavaScript *os = nullptr;
|
||||||
|
static uint64_t target_ticks = 0;
|
||||||
|
|
||||||
void exit_callback() {
|
void exit_callback() {
|
||||||
emscripten_cancel_main_loop(); // After this, we can exit!
|
emscripten_cancel_main_loop(); // After this, we can exit!
|
||||||
|
@ -47,9 +48,18 @@ void exit_callback() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void main_loop_callback() {
|
void main_loop_callback() {
|
||||||
|
uint64_t current_ticks = os->get_ticks_usec();
|
||||||
|
|
||||||
bool force_draw = DisplayServerJavaScript::get_singleton()->check_size_force_redraw();
|
bool force_draw = DisplayServerJavaScript::get_singleton()->check_size_force_redraw();
|
||||||
if (force_draw) {
|
if (force_draw) {
|
||||||
Main::force_redraw();
|
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()) {
|
if (os->main_loop_iterate()) {
|
||||||
emscripten_cancel_main_loop(); // Cancel current loop and wait for finalize_async.
|
emscripten_cancel_main_loop(); // Cancel current loop and wait for finalize_async.
|
||||||
|
|
|
@ -83,6 +83,9 @@ public:
|
||||||
String get_executable_path() const;
|
String get_executable_path() const;
|
||||||
virtual Error shell_open(String p_uri);
|
virtual Error shell_open(String p_uri);
|
||||||
virtual String get_name() const;
|
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 bool can_draw() const;
|
||||||
|
|
||||||
virtual String get_cache_path() const;
|
virtual String get_cache_path() const;
|
||||||
|
|
Loading…
Reference in a new issue