moved filesystem code for Emscripten platform to separate functions
[rocksndiamonds.git] / src / libgame / system.c
index db9d72b1ef5bc4e377543fa05b2fe9e0a1c08b0c..3d23f5fad085a43384e3f091935e9f89d89ef266 100644 (file)
@@ -104,25 +104,6 @@ void InitProgramInfo(char *argv0, char *config_filename, char *userdata_subdir,
   program.log_file[LOG_ERR_ID] = program.log_file_default[LOG_ERR_ID] = stderr;
 
   program.headless = FALSE;
-
-#if defined(PLATFORM_EMSCRIPTEN)
-  EM_ASM
-  (
-    Module.sync_done = 0;
-
-    FS.mkdir('/persistent');           // create persistent data directory
-    FS.mount(IDBFS, {}, '/persistent');        // mount with IDBFS filesystem type
-    FS.syncfs(true, function(err)      // sync persistent data into memory
-    {
-      assert(!err);
-      Module.sync_done = 1;
-    });
-  );
-
-  // wait for persistent data to be synchronized to memory
-  while (emscripten_run_script_int("Module.sync_done") == 0)
-    Delay(20);
-#endif
 }
 
 void InitNetworkInfo(boolean enabled, boolean connected, boolean serveronly,
@@ -206,6 +187,8 @@ void InitExitFunction(void (*exit_function)(int))
 
 void InitPlatformDependentStuff(void)
 {
+  InitEmscriptenFilesystem();
+
   // this is initialized in GetOptions(), but may already be used before
   options.verbose = TRUE;
 
@@ -1267,6 +1250,7 @@ void ReCreateGameTileSizeBitmap(Bitmap **bitmaps)
 
   if (gfx.game_tile_size == gfx.standard_tile_size)
   {
+    // set game bitmap pointer to standard sized bitmap (already existing)
     bitmaps[IMG_BITMAP_PTR_GAME] = bitmaps[IMG_BITMAP_STANDARD];
 
     return;
@@ -1276,10 +1260,10 @@ void ReCreateGameTileSizeBitmap(Bitmap **bitmaps)
   int width  = bitmap->width  * gfx.game_tile_size / gfx.standard_tile_size;;
   int height = bitmap->height * gfx.game_tile_size / gfx.standard_tile_size;;
 
-  Bitmap *bitmap_new = ZoomBitmap(bitmap, width, height);
+  bitmaps[IMG_BITMAP_CUSTOM] = ZoomBitmap(bitmap, width, height);
 
-  bitmaps[IMG_BITMAP_CUSTOM] = bitmap_new;
-  bitmaps[IMG_BITMAP_PTR_GAME] = bitmap_new;
+  // set game bitmap pointer to custom sized bitmap (newly created)
+  bitmaps[IMG_BITMAP_PTR_GAME] = bitmaps[IMG_BITMAP_CUSTOM];
 }
 
 static void CreateScaledBitmaps(Bitmap **bitmaps, int zoom_factor,
@@ -1478,6 +1462,9 @@ static void CreateScaledBitmaps(Bitmap **bitmaps, int zoom_factor,
 
     // set original bitmap pointer to corresponding sized bitmap
     bitmaps[IMG_BITMAP_PTR_ORIGINAL] = bitmaps[IMG_BITMAP_32x32];
+
+    if (old_bitmap != tmp_bitmap_1)
+      FreeBitmap(old_bitmap);
   }
 
   UPDATE_BUSY_STATE();
@@ -1916,3 +1903,43 @@ void ClearJoystickState(void)
 {
   SDLClearJoystickState();
 }
+
+
+// ============================================================================
+// Emscripten functions
+// ============================================================================
+
+void InitEmscriptenFilesystem(void)
+{
+#if defined(PLATFORM_EMSCRIPTEN)
+  EM_ASM
+  (
+    Module.sync_done = 0;
+
+    FS.mkdir('/persistent');           // create persistent data directory
+    FS.mount(IDBFS, {}, '/persistent');        // mount with IDBFS filesystem type
+    FS.syncfs(true, function(err)      // sync persistent data into memory
+    {
+      assert(!err);
+      Module.sync_done = 1;
+    });
+  );
+
+  // wait for persistent data to be synchronized to memory
+  while (emscripten_run_script_int("Module.sync_done") == 0)
+    Delay(20);
+#endif
+}
+
+void SyncEmscriptenFilesystem(void)
+{
+#if defined(PLATFORM_EMSCRIPTEN)
+  EM_ASM
+  (
+    FS.syncfs(function(err)
+    {
+      assert(!err);
+    });
+  );
+#endif
+}