diff options
Diffstat (limited to 'platform/javascript')
| -rw-r--r-- | platform/javascript/javascript_main.cpp | 76 | ||||
| -rw-r--r-- | platform/javascript/os_javascript.cpp | 74 | ||||
| -rw-r--r-- | platform/javascript/os_javascript.h | 16 |
3 files changed, 81 insertions, 85 deletions
diff --git a/platform/javascript/javascript_main.cpp b/platform/javascript/javascript_main.cpp index e122bb2e4..6b1d57449 100644 --- a/platform/javascript/javascript_main.cpp +++ b/platform/javascript/javascript_main.cpp @@ -31,94 +31,50 @@ #include "io/resource_loader.h" #include "main/main.h" #include "os_javascript.h" -#include <GL/glut.h> -#include <string.h> OS_JavaScript *os = NULL; -static void _gfx_init(void *ud, bool gl2, int w, int h, bool fs) { +static void main_loop() { - glutInitWindowSize(w, h); - glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); - glutCreateWindow("godot"); + os->main_loop_iterate(); } -static void _gfx_idle() { +extern "C" void main_after_fs_sync() { - glutPostRedisplay(); -} - -int start_step = 0; - -static void _godot_draw(void) { - - if (start_step == 1) { - start_step = 2; - Main::start(); - os->main_loop_begin(); - } - - if (start_step == 2) { - os->main_loop_iterate(); - } - - glutSwapBuffers(); -} - -extern "C" { - -void main_after_fs_sync() { - - start_step = 1; -} + // Ease up compatibility + ResourceLoader::set_abort_on_missing_resources(false); + Main::start(); + os->main_loop_begin(); + emscripten_set_main_loop(main_loop, 0, false); } int main(int argc, char *argv[]) { - /* Initialize the window */ printf("let it go dude!\n"); - glutInit(&argc, argv); - os = new OS_JavaScript(argv[0], _gfx_init, NULL, NULL); - - Error err = Main::setup(argv[0], argc - 1, &argv[1]); - - ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility - - /* Set up glut callback functions */ - glutIdleFunc(_gfx_idle); - // glutReshapeFunc(gears_reshape); - glutDisplayFunc(_godot_draw); - //glutSpecialFunc(gears_special); - //mount persistent file system + // sync from persistent state into memory and then + // run the 'main_after_fs_sync' function /* clang-format off */ EM_ASM( + Module.noExitRuntime = true; FS.mkdir('/userfs'); FS.mount(IDBFS, {}, '/userfs'); - - // sync from persistent state into memory and then - // run the 'main_after_fs_sync' function FS.syncfs(true, function(err) { - if (err) { Module.setStatus('Failed to load persistent data\nPlease allow (third-party) cookies'); Module.printErr('Failed to populate IDB file system: ' + err.message); - Module.exit(); + Module.noExitRuntime = false; } else { Module.print('Successfully populated IDB file system'); - ccall('main_after_fs_sync', 'void', []); + ccall('main_after_fs_sync', null); } }); ); /* clang-format on */ - glutMainLoop(); + os = new OS_JavaScript(argv[0], NULL); + Error err = Main::setup(argv[0], argc - 1, &argv[1]); return 0; + // continued async in main_after_fs_sync() from syncfs() callback } - -/* - * - *09] <azakai|2__> reduz: yes, define TOTAL_MEMORY on Module. for example var Module = { TOTAL_MEMORY: 12345.. }; before the main - * - */ diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index e44f33f99..1cd199160 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -390,14 +390,18 @@ void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, i print_line("Init OS"); - if (gfx_init_func) - gfx_init_func(gfx_init_ud, use_gl2, p_desired.width, p_desired.height, p_desired.fullscreen); + EmscriptenWebGLContextAttributes attributes; + emscripten_webgl_init_context_attributes(&attributes); + attributes.alpha = false; + attributes.antialias = false; + attributes.majorVersion = 2; + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context(NULL, &attributes); + ERR_FAIL_COND(emscripten_webgl_make_context_current(ctx) != EMSCRIPTEN_RESULT_SUCCESS); - // nothing to do here, can't fulfil fullscreen request due to - // browser security, window size is already set from HTML video_mode = p_desired; + // can't fulfil fullscreen request due to browser security video_mode.fullscreen = false; - _windowed_size = get_window_size(); + set_window_size(Size2(p_desired.width, p_desired.height)); // find locale, emscripten only sets "C" char locale_ptr[16]; @@ -513,20 +517,62 @@ void OS_JavaScript::alert(const String &p_alert, const String &p_title) { /* clang-format on */ } -void OS_JavaScript::set_mouse_show(bool p_show) { +void OS_JavaScript::set_css_cursor(const char *p_cursor) { - //javascript has no mouse... + /* clang-format off */ + EM_ASM_({ + Module.canvas.style.cursor = Module.UTF8ToString($0); + }, p_cursor); + /* clang-format on */ } -void OS_JavaScript::set_mouse_grab(bool p_grab) { +const char *OS_JavaScript::get_css_cursor() const { - //it really has no mouse...! + char cursor[16]; + /* clang-format off */ + EM_ASM_INT({ + Module.stringToUTF8(Module.canvas.style.cursor ? Module.canvas.style.cursor : 'auto', $0, 16); + }, cursor); + /* clang-format on */ + return cursor; } -bool OS_JavaScript::is_mouse_grab_enabled() const { +void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) { - //*sigh* technology has evolved so much since i was a kid.. - return false; + ERR_FAIL_INDEX(p_mode, MOUSE_MODE_CONFINED + 1); + ERR_EXPLAIN("MOUSE_MODE_CONFINED is not supported for the HTML5 platform"); + ERR_FAIL_COND(p_mode == MOUSE_MODE_CONFINED); + if (p_mode == get_mouse_mode()) + return; + + if (p_mode == MOUSE_MODE_VISIBLE) { + + set_css_cursor("auto"); + emscripten_exit_pointerlock(); + + } else if (p_mode == MOUSE_MODE_HIDDEN) { + + set_css_cursor("none"); + emscripten_exit_pointerlock(); + + } else if (p_mode == MOUSE_MODE_CAPTURED) { + + EMSCRIPTEN_RESULT result = emscripten_request_pointerlock("canvas", false); + ERR_EXPLAIN("MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback"); + ERR_FAIL_COND(result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED); + ERR_FAIL_COND(result != EMSCRIPTEN_RESULT_SUCCESS); + set_css_cursor("auto"); + } +} + +OS::MouseMode OS_JavaScript::get_mouse_mode() const { + + if (!strcmp(get_css_cursor(), "none")) + return MOUSE_MODE_HIDDEN; + + EmscriptenPointerlockChangeEvent ev; + emscripten_get_pointerlock_status(&ev); + return ev.isActive && (strcmp(ev.id, "canvas") == 0) ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE; } Point2 OS_JavaScript::get_mouse_position() const { @@ -835,10 +881,8 @@ int OS_JavaScript::get_power_percent_left() { return power_manager->get_power_percent_left(); } -OS_JavaScript::OS_JavaScript(const char *p_execpath, GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) { +OS_JavaScript::OS_JavaScript(const char *p_execpath, GetDataDirFunc p_get_data_dir_func) { set_cmdline(p_execpath, get_cmdline_args()); - gfx_init_func = p_gfx_init_func; - gfx_init_ud = p_gfx_init_ud; main_loop = NULL; gl_extensions = NULL; window_maximized = false; diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h index 0c956ecba..ffd269b51 100644 --- a/platform/javascript/os_javascript.h +++ b/platform/javascript/os_javascript.h @@ -45,16 +45,10 @@ #include <emscripten/html5.h> -typedef void (*GFXInitFunc)(void *ud, bool gl2, int w, int h, bool fs); typedef String (*GetDataDirFunc)(); class OS_JavaScript : public OS_Unix { - GFXInitFunc gfx_init_func; - void *gfx_init_ud; - - bool use_gl2; - int64_t time_to_save_sync; int64_t last_sync_time; @@ -81,6 +75,9 @@ class OS_JavaScript : public OS_Unix { void process_joypads(); + void set_css_cursor(const char *); + const char *get_css_cursor() const; + public: // functions used by main to initialize/deintialize the OS virtual int get_video_driver_count() const; @@ -110,9 +107,8 @@ public: virtual void alert(const String &p_alert, const String &p_title = "ALERT!"); - virtual void set_mouse_show(bool p_show); - virtual void set_mouse_grab(bool p_grab); - virtual bool is_mouse_grab_enabled() const; + virtual void set_mouse_mode(MouseMode p_mode); + virtual MouseMode get_mouse_mode() const; virtual Point2 get_mouse_position() const; virtual int get_mouse_button_state() const; virtual void set_window_title(const String &p_title); @@ -167,7 +163,7 @@ public: virtual int get_power_seconds_left(); virtual int get_power_percent_left(); - OS_JavaScript(const char *p_execpath, GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func); + OS_JavaScript(const char *p_execpath, GetDataDirFunc p_get_data_dir_func); ~OS_JavaScript(); }; |
