initial loading/progress screen for emscripten
authorTaylor Richards <taylor@icculus.org>
Fri, 16 Feb 2018 00:35:47 +0000 (19:35 -0500)
committerTaylor Richards <taylor@icculus.org>
Sat, 17 Mar 2018 15:31:15 +0000 (11:31 -0400)
CMakeLists.txt
dist/fs2demo_shell.html [moved from dist/fs_shell.html with 63% similarity]
dist/fsdemo_shell.html [new file with mode: 0644]
src/freespace2/freespace.cpp

index cf7608b..64414e6 100644 (file)
@@ -234,7 +234,7 @@ if(EMSCRIPTEN)
     "-s FULL_ES2=1"
   # "-s WASM=1"
     "-s TOTAL_MEMORY=184549376"
-    "--shell-file ${CMAKE_SOURCE_DIR}/dist/fs_shell.html"
+    "--shell-file ${CMAKE_SOURCE_DIR}/dist/${FS_BINARY}_shell.html"
     "--preload-file game@/"
     "-s SAFE_HEAP=1"
     "-s DEMANGLE_SUPPORT=1"
similarity index 63%
rename from dist/fs_shell.html
rename to dist/fs2demo_shell.html
index 768629e..729cdb8 100644 (file)
@@ -4,15 +4,51 @@
     <meta charset="utf-8">
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
-    <title>FreeSpace</title>
+    <title>FreeSpace 2 Demo</title>
     <style>
+      html {
+        box-sizing: border-box;
+      }
+      * {
+        box-sizing: inherit;
+      }
       html, body {
         height: 100%;
         margin: 0;
         padding: 0;
         background-color: #222;
       }
-      canvas {
+      #progress {
+        background: url(demotitle.jpg);
+        background-repeat: no-repeat;
+        background-size: cover;
+        z-index: 10;
+      }
+      #progress_container {
+        width: 70%;
+        margin: 0 auto;
+        position: relative;
+        top: 50%;
+      }
+      #progress_bar_back {
+        width: 100%;
+        background-color: #457573;
+        margin: 0 auto;
+        padding: 3px;
+      }
+      #progress_bar {
+        padding-left: 20px;
+        line-height: 30px;
+        height: 30px;
+        width: 0%;
+        background-color: #182928;
+      }
+      #progress_text, #progress_bar {
+        font-family: 'Noto Sans', sans-serif;
+        color: #fff;
+
+      }
+      canvas, #progress {
         width: 100vw;
         height: 75vw;
         background-color: #000;
     </style>
   </head>
   <body>
+    <div id="progress">
+      <div id="progress_container">
+        <div id="progress_bar_back"><div id="progress_bar"></div></div>
+        <div id="progress_text"></div>
+      </div>
+    </div>
     <canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
     <script type='text/javascript'>
+      var progress = document.getElementById('progress');
+      var progress_bar = document.getElementById('progress_bar');
+      var progress_text = document.getElementById('progress_text');
+
       var Module = {
         preRun: [],
         postRun: [],
           return canvas;
         })(),
         setStatus: function(text) {
+          if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
+          if (text === Module.setStatus.text) return;
+          var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
+          if (m) {
+            text = m[1];
+            var perc = parseInt(m[2]) / parseInt(m[4]) * 100;
+            progress_bar.style.width = perc + '%';
+            progress_bar.innerHTML = perc.toFixed(1) + '%';
+          }
+          progress_text.innerHTML = text;
         },
         totalDependencies: 0,
         monitorRunDependencies: function(left) {
diff --git a/dist/fsdemo_shell.html b/dist/fsdemo_shell.html
new file mode 100644 (file)
index 0000000..efa9968
--- /dev/null
@@ -0,0 +1,139 @@
+<!doctype html>
+<html lang="en-us">
+  <head>
+    <meta charset="utf-8">
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
+    <title>FreeSpace Demo</title>
+    <style>
+      html {
+        box-sizing: border-box;
+      }
+      * {
+        box-sizing: inherit;
+      }
+      html, body {
+        height: 100%;
+        margin: 0;
+        padding: 0;
+        background-color: #222;
+      }
+      #progress {
+        background: url(demotitle.jpg);
+        background-repeat: no-repeat;
+        background-size: cover;
+        z-index: 10;
+      }
+      #progress_container {
+        width: 70%;
+        margin: 0 auto;
+        position: relative;
+        top: 50%;
+      }
+      #progress_bar_back {
+        width: 100%;
+        background-color: #353B4C;
+        margin: 0 auto;
+        padding: 3px;
+      }
+      #progress_bar {
+        padding-left: 20px;
+        line-height: 30px;
+        height: 30px;
+        width: 0%;
+        background-color: #798199;
+      }
+      #progress_text, #progress_bar {
+        font-family: 'Noto Sans', sans-serif;
+        color: #4C442D;
+
+      }
+      canvas, #progress {
+        width: 100vw;
+        height: 75vw;
+        background-color: #000;
+        max-height: 100vh;
+        max-width: 133.333333vh;
+        margin: auto;
+        position: absolute;
+        top: 0;
+        bottom: 0;
+        left: 0;
+        right: 0;
+      }
+    </style>
+  </head>
+  <body>
+    <div id="progress">
+      <div id="progress_container">
+        <div id="progress_bar_back"><div id="progress_bar"></div></div>
+        <div id="progress_text"></div>
+      </div>
+    </div>
+    <canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
+    <script type='text/javascript'>
+      var progress = document.getElementById('progress');
+      var progress_bar = document.getElementById('progress_bar');
+      var progress_text = document.getElementById('progress_text');
+
+      var Module = {
+        preRun: [],
+        postRun: [],
+        print: (function() {
+          return function(text) {
+            if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
+            // These replacements are necessary if you render to raw HTML
+            //text = text.replace(/&/g, "&amp;");
+            //text = text.replace(/</g, "&lt;");
+            //text = text.replace(/>/g, "&gt;");
+            //text = text.replace('\n', '<br>', 'g');
+            console.log(text);
+          };
+        })(),
+        printErr: function(text) {
+          if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
+          if (0) { // XXX disabled for safety typeof dump == 'function') {
+            dump(text + '\n'); // fast, straight to the real console
+          } else {
+            console.error(text);
+          }
+        },
+        canvas: (function() {
+          var canvas = document.getElementById('canvas');
+
+          // As a default initial behavior, pop up an alert when webgl context is lost. To make your
+          // application robust, you may want to override this behavior before shipping!
+          // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
+          canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
+
+          return canvas;
+        })(),
+        setStatus: function(text) {
+          if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
+          if (text === Module.setStatus.text) return;
+          var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
+          if (m) {
+            text = m[1];
+            var perc = parseInt(m[2]) / parseInt(m[4]) * 100;
+            progress_bar.style.width = perc + '%';
+            progress_bar.innerHTML = perc.toFixed(1) + '%';
+          }
+          progress_text.innerHTML = text;
+        },
+        totalDependencies: 0,
+        monitorRunDependencies: function(left) {
+          this.totalDependencies = Math.max(this.totalDependencies, left);
+          Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
+        }
+      };
+      Module.setStatus('Downloading...');
+      window.onerror = function() {
+        Module.setStatus('Exception thrown, see JavaScript console');
+        Module.setStatus = function(text) {
+          if (text) Module.printErr('[post-exception status] ' + text);
+        };
+      };
+    </script>
+    {{{ SCRIPT }}}
+  </body>
+</html>
index e0fafb8..2fa5030 100644 (file)
@@ -6861,6 +6861,11 @@ int game_main(const char *szCmdLine)
        }
 
 #ifdef __EMSCRIPTEN__
+       EM_ASM({
+               var progress = document.getElementById('progress');
+               progress.hidden = true;
+       });
+
        emscripten_set_main_loop(game_loop_caller, 0, 1);
 #else
        while ( game_loop() ) { /* nothing */ }