Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emscripten: Add Api and remove Asynchify #2862

Merged
merged 5 commits into from
Dec 20, 2023
Merged

Conversation

Ghabry
Copy link
Member

@Ghabry Ghabry commented Nov 21, 2022

Already wrote this in February but never gave it the final touch... Better for post 0.7.1, removing asynchify can cause problems.

Had to remove Asynchify because it breaks the Api (you cannot call back into the Player while the endless async loop is running). Previously we had bad experiences when the browser does the mainloop timing. I hope this works better by now...

New feature: Save upload and download (fix: #1025).

Screenshot_20221121_231818

New feature: Api under "easyrpgPlayer.api". Currently can do: Reset, Refresh (called after upload to refresh the scene), Upload, Download. Can be used as an extension point if you have a website that wants to access certain functionality of the Player.

Renamed constants from EASYRPG_GAME to game and EASYRPG_FS to saveFs as this is now cleaner to do with a module.

Fix passing of custom game name.
Adding an Interface class for this.
For ensuring that savegames are immediately available scene refresh is now possible.

The refresh will be also useful for other use cases later.
Is required for the Api extensions, otherwise they will crash because a async call is active the entire time.
@Ghabry Ghabry added this to the 0.7.2 milestone Nov 21, 2022
@fdelapena fdelapena added Emscripten WebAssembly/JavaScript port for web browsers Awaiting Rebase Pull requests with conflicting files due to former merge Async Enhancement labels Dec 3, 2022
@fdelapena
Copy link
Contributor

Looking great 👍. There's a good chance for an emscripten PR to include a couple of changes:

Pause/resume option via SDL_WINDOWEVENT_FOCUS_GAINED or whichever supports Emscripten as suggested by catinbag and Carsten:
https://community.easyrpg.org/t/pause-and-resume-on-web-version-of-player/1151/2

OSD button html shell updates by Joke:
https://community.easyrpg.org/t/web-player-on-mobile-more-touch-screen-buttons/1032/17

Module = {
EASYRPG_GAME: "",

// Do not replace Object.assign with "...Module". Breaks the Acorn optimizer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acorn optimizer was just fixed :) emscripten-core/emscripten#18461

@johannschopplich
Copy link
Contributor

Wow, this PR looks promising. Unfortunately, I'm not able to help technically.
Just am here to cheer and applaud for the work. 🙂

@Ghabry Ghabry modified the milestones: 0.8.2, 0.8.1 Oct 28, 2023
@Ghabry Ghabry modified the milestones: 0.8.1, 0.8.2 Dec 4, 2023
@Ghabry Ghabry removed the Awaiting Rebase Pull requests with conflicting files due to former merge label Dec 5, 2023
@Ghabry Ghabry marked this pull request as ready for review December 19, 2023 22:09
@Ghabry
Copy link
Member Author

Ghabry commented Dec 20, 2023

Also added a screenshot API now that offers the screen for download. Will bind it to the screenshot key.

Is suitable for ynonline which currently downloads the canvas for screenshots and this can be black for a brief moment due to how rendering works.

- emscripten_sleep requires asynchify.
- Final fixes for save and load scene.
- New function for taking a screenshot and downloading it.
- Fix unit tests
@Ghabry
Copy link
Member Author

Ghabry commented Dec 20, 2023

okay rebound the Screenshot button to do a download :)

This PR is ready now. Will use the work in this PR to enhance the Fluidsynth and Font settings scene: I want to allow file "upload" so you can have custom (sound)fonts in the browser.

@fdelapena fdelapena merged commit 77d97bb into EasyRPG:master Dec 20, 2023
13 checks passed
@johannschopplich
Copy link
Contributor

Waited a year for this. Thank you all for your work. Is it Christmas already? 🎄

@Ghabry
Copy link
Member Author

Ghabry commented Jan 7, 2024

I have to amend this.

  1. hulje from https://archive.yumeboo.ru/play/ found a regression:

The ?game argument is now always lowercased. When the game is in games/Yume_Nikki the game is not found anymore because the request goes to games/yume_nikki.

  1. papancakes from https://ynoproject.net/ noticed that the canvas steals now all input events on the entire page.

The problem is here that emscripten_set_main_loop grabs all the input (related issue is emscripten-core/emscripten#10542). Before we used Asynchify which didn`t use this funciton.

Solution seems to be:

In sdl2_ui.cpp:

#ifdef EMSCRIPTEN
    // Only handle keyboard events when the canvas has focus
    SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT, "#canvas");
#endif

Changes on the website:

// Launch the Player and configure it
window.addEventListener('load', (event) => {
    createEasyRpgPlayer({
      game: undefined,
      saveFs: undefined
    }).then(function(Module) {
      // Module is ready
      easyrpgPlayer = Module;
      easyrpgPlayer.initApi();
      canvas.focus(); // <-- NEW

      // Custom code here
    });
});

// Make EasyRPG player embeddable
canvas.addEventListener('mouseenter', () => canvas.focus()); // <- CHANGED
canvas.addEventListener('click', () => canvas.focus()); // <- CHANGED
  1. @Mimigris noticed that it is hard to upload a savegame when the game has 0 because "Load" on the title scene cannot be accessed. Will likely alter this so it can be still opened with SHIFT even if disabled.

@thenick775
Copy link

thenick775 commented Apr 15, 2024

Bump, I too am experiencing this regression related to inputs.

All of my text input fields are locked up, and this no longer works to disable:

EMSCRIPTEN_KEEPALIVE void setEventEnable(bool toggle) {
    int state = toggle ? SDL_ENABLE : SDL_DISABLE;
    SDL_EventState(SDL_TEXTINPUT, state);
    SDL_EventState(SDL_KEYDOWN, state);
    SDL_EventState(SDL_KEYUP, state);
    SDL_EventState(SDL_MOUSEMOTION, state);
    SDL_EventState(SDL_MOUSEBUTTONDOWN, state);
    SDL_EventState(SDL_MOUSEBUTTONUP, state);
}

@Ghabry Ghabry deleted the emscripten2 branch April 15, 2024 07:14
@Ghabry
Copy link
Member Author

Ghabry commented Apr 15, 2024

The fix for this is already merged.

When you use a custom shell file do not forget to update it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Async Emscripten WebAssembly/JavaScript port for web browsers Enhancement
Development

Successfully merging this pull request may close these issues.

Emscripten: provide a way to handle savegame files
4 participants