added Emscripten support for running R'n'D in a web browser
authorHolger Schemel <info@artsoft.org>
Thu, 26 Nov 2020 22:33:56 +0000 (23:33 +0100)
committerHolger Schemel <info@artsoft.org>
Thu, 26 Nov 2020 22:39:17 +0000 (23:39 +0100)
Makefile
src/Makefile
src/libgame/misc.c
src/libgame/platform.h
src/libgame/system.h
src/libgame/zip/ioapi.h

index 3e5877f063d467b511c5bcff015b9361542789a4..5dd17c24025faf37cbb80391e642918d2813c873 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -80,6 +80,9 @@ android-clean:
 
 android: android-package
 
+emscripten:
+       @emmake $(MAKE_CMD) PLATFORM=emscripten
+
 clean:
        @$(MAKE_CMD) clean
 
@@ -166,6 +169,9 @@ dist-build-mac:
 dist-build-android:
        @BUILD_DIST=TRUE $(MAKE) android
 
+dist-build-emscripten:
+       @BUILD_DIST=TRUE $(MAKE) emscripten
+
 dist-package-linux:
        $(MAKE_DIST) package linux
 
index 6885c601ac19af7726a91fa73fc1007d4b615574..0db8f54d511017105f573f6f2242f89ea5e5245b 100644 (file)
@@ -58,6 +58,14 @@ PROGNAME = ../$(PROGBASE).exe
 EXTRA_LDFLAGS = -lshfolder -lwsock32
 endif
 
+ifeq ($(PLATFORM),emscripten)          # compiling with Emscripten
+PROGNAME = ../$(PROGBASE).js
+CC = emcc
+AR = emar
+RANLIB = emranlib
+STRIP = true
+endif
+
 ifdef BUILD_DIST                       # distribution build
 ifeq ($(shell uname -s),Darwin)                # compiling on Mac OS X
 MAC_TARGET_VERSION_MIN = 10.7
@@ -85,7 +93,15 @@ endif
 # $(info Using SDL version $(SDL_VERSION) [TARGET == $(TARGET)])
 
 ifeq ($(TARGET),sdl2)                  # compiling for SDL2 target
+ifeq ($(PLATFORM),emscripten)
+SDL_LIBS = -s USE_SDL_IMAGE=2 -s USE_SDL_MIXER=2 -s USE_SDL_NET=2 -s USE_ZLIB=1
+SDL_FMTS = -s SDL2_IMAGE_FORMATS='["bmp","png","pcx","xpm"]'
+EXTRA_CFLAGS = $(SDL_LIBS)
+EXTRA_LDFLAGS = $(SDL_FMTS) -s INITIAL_MEMORY=65536000 -s ALLOW_MEMORY_GROWTH=1 --preload-file ../graphics/ --preload-file ../sounds/ --preload-file ../levels/ --preload-file ../music/ -s NO_EXIT_RUNTIME=0 -s ASYNCIFY -O2
+else
 SDL_LIBS = -lSDL2_image -lSDL2_mixer -lSDL2_net
+endif
+
 SYS_CFLAGS := -DTARGET_SDL2 $(shell sdl2-config --cflags)
 SYS_LDFLAGS := $(SDL_LIBS) $(shell sdl2-config --libs) -lm -lz
 endif
@@ -317,7 +333,10 @@ clean-ico:
 
 clean-bin:
        $(RM) $(PROGNAME)
-       $(RM) ../*.exe
+       $(RM) $(PROGNAME).exe
+       $(RM) $(PROGNAME).js
+       $(RM) $(PROGNAME).wasm
+       $(RM) $(PROGNAME).data
 
 clean: clean-obj clean-ico clean-bin
 
index b50abca74f18ac64e7e8580d93d7c131acace21c..bf2d2c42fb4b8c84b0dbdcf317ef76ad7af1f4b1 100644 (file)
@@ -786,7 +786,12 @@ static char *get_corrected_real_name(char *real_name)
 #if defined(PLATFORM_UNIX)
 static struct passwd *getPasswdEntry(void)
 {
+#if defined(PLATFORM_EMSCRIPTEN)
+  // currently not fully supported; force fallback to default values
+  return NULL;
+#else
   return getpwuid(getuid());
+#endif
 }
 
 char *getUnixLoginName(void)
index 60ca20f4734cd98960bdd970cf0462dbe2066b33..438e8de1c9a2a3029800038a7110b2e2e1bbbef9 100644 (file)
 #define PLATFORM_STRING "Android"
 #endif
 
+#if defined(__EMSCRIPTEN__)
+#define PLATFORM_EMSCRIPTEN
+#undef  PLATFORM_STRING
+#define PLATFORM_STRING "Emscripten"
+#endif
+
 
 // ============================================================================
 // define additional platform keywords for CPU architecture
index 1bfed54352d6aaeab474cc6fa3c1c2c9988955f1..f02578be4ecfb61b8ef55536660a281a14b4a09d 100644 (file)
 #define STR_SPECIAL_RENDERING_TARGET   "target_texture_only"
 #define STR_SPECIAL_RENDERING_DOUBLE   "stream_and_target_texture"
 
+#if defined(PLATFORM_EMSCRIPTEN)
+#define STR_SPECIAL_RENDERING_DEFAULT  STR_SPECIAL_RENDERING_BITMAP
+#else
 #define STR_SPECIAL_RENDERING_DEFAULT  STR_SPECIAL_RENDERING_DOUBLE
+#endif
 
 #define SPECIAL_RENDERING_OFF          0
 #define SPECIAL_RENDERING_BITMAP       1
index 0741de44153f17fb2175f7fcc459e42c4b67fe21..0b6555d69c74df3efb48658549609607a90c54ce 100644 (file)
@@ -33,7 +33,7 @@
 #  define ftello64 ftell
 #  define fseeko64 fseek
 #else
-#  if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__ANDROID__) || defined(__linux__)
+#  if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__ANDROID__) || defined(__linux__) || defined(__EMSCRIPTEN__)
 #    define fopen64 fopen
 #    define ftello64 ftello
 #    define fseeko64 fseeko