emscripten: special case to make SDL_ShowSimpleMessageBox() work.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 06 Sep 2016 13:13:03 -0400
changeset 10312943b722a8e61
parent 10311 848951113551
child 10314 722dd7d03f81
emscripten: special case to make SDL_ShowSimpleMessageBox() work.

Browsers don't have the functionality to fully support the generic
SDL_ShowMessageBox(), but this handles the likely most-common case.

Without this, you'd return immediately with a proper error result and no UI,
but probably no one checks that for SDL_ShowSimpleMessageBox. And if they
did: what would they do to handle this anyhow?

We'd need to lobby for an HTML spec of some sort that allows customizable
message boxes--that block!--to properly support SDL message boxes on
Emscripten, but this is probably Good Enough for now.
src/video/SDL_video.c
     1.1 --- a/src/video/SDL_video.c	Tue Sep 06 12:18:52 2016 -0400
     1.2 +++ b/src/video/SDL_video.c	Tue Sep 06 13:13:03 2016 -0400
     1.3 @@ -55,6 +55,10 @@
     1.4  #undef CreateWindow
     1.5  #endif
     1.6  
     1.7 +#ifdef __EMSCRIPTEN__
     1.8 +#include <emscripten.h>
     1.9 +#endif
    1.10 +
    1.11  /* Available video drivers */
    1.12  static VideoBootStrap *bootstrap[] = {
    1.13  #if SDL_VIDEO_DRIVER_COCOA
    1.14 @@ -3722,6 +3726,16 @@
    1.15  int
    1.16  SDL_ShowSimpleMessageBox(Uint32 flags, const char *title, const char *message, SDL_Window *window)
    1.17  {
    1.18 +#ifdef __EMSCRIPTEN__
    1.19 +    /* !!! FIXME: propose a browser API for this, get this #ifdef out of here? */
    1.20 +    /* Web browsers don't (currently) have an API for a custom message box
    1.21 +       that can block, but for the most common case (SDL_ShowSimpleMessageBox),
    1.22 +       we can use the standard Javascript alert() function. */
    1.23 +    EM_ASM_({
    1.24 +        alert(UTF8ToString($0) + "\n\n" + UTF8ToString($1));
    1.25 +    }, title, message);
    1.26 +    return 0;
    1.27 +#else
    1.28      SDL_MessageBoxData data;
    1.29      SDL_MessageBoxButtonData button;
    1.30  
    1.31 @@ -3739,6 +3753,7 @@
    1.32      button.text = "OK";
    1.33  
    1.34      return SDL_ShowMessageBox(&data, NULL);
    1.35 +#endif
    1.36  }
    1.37  
    1.38  SDL_bool