Simplify index wrapper, namespacing.

Experiment with function exports.  Not quite working.
This commit is contained in:
Mahlon E. Smith 2025-08-19 22:35:50 -07:00
parent 740211ed4c
commit b891520ff9
Signed by: mahlon
SSH key fingerprint: SHA256:dP84sRGKZRpOOiPD/+GuOq+SHSxEw9qi5BWLQobaHm0
5 changed files with 71 additions and 95 deletions

View file

@ -23,12 +23,13 @@ when defined( emscripten ):
--panics:on
--exceptions:goto
--define:noSignalHandler
--passL:"-Oz" # Production build
--passL:"-Oz" # Production bu ld
# --passL:"-O0 -gDebug -gsource-map" # Debug build
# --passL:"-s EXPORT_NAME=Test1Module"
# --passL:"-s MODULARIZE=1"
--passL:"-s EXPORT_ES6"
--passL:"-s EXPORT_NAME=NimTest"
--passL:"-s ASYNCIFY" # async support
# --passL:"-s JSPI" # async support
--passL:"-o build/index.html"
--passL:"--shell-file minshell.html"
# --passL:"-s EXPORT_ALL=1" # make all public methods available to JS
# --passL:"-s JSPI" # async support (new)
# --passL:"-s ALLOW_MEMORY_GROWTH=1"
--passL:"-o build/nimtest.js"

41
index.html Normal file
View file

@ -0,0 +1,41 @@
<!doctype html>
<html lang="EN-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>raylib web game</title>
<meta name="title" content="raylib web game">
<meta name="description" content="New raylib web videogame, developed using raylib videogames library">
<meta name="keywords" content="raylib, programming, examples, html5, C, C++, library, learn, games, videogames">
<meta name="viewport" content="width=device-width">
<style>
body { margin: 0px; overflow: hidden; background-color: white; }
canvas.emscripten { border: 0px none; background-color: white; }
</style>
</head>
<body>
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex="-1"></canvas>
<script type="module">
import NimTest from './nimtest.js'
NimTest({
print: (function( text ) {
if ( text === undefined ) return
console.log( text )
})(),
canvas: (function() {
return document.getElementById('canvas');
})()
}).then((nimtest) => {
console.log( "Loaded WASM!" )
// nimtest._nativeGreet()
})
</script>
</body>
</html>

View file

@ -1,85 +0,0 @@
<!doctype html>
<html lang="EN-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>raylib web game</title>
<meta name="title" content="raylib web game">
<meta name="description" content="New raylib web videogame, developed using raylib videogames library">
<meta name="keywords" content="raylib, programming, examples, html5, C, C++, library, learn, games, videogames">
<meta name="viewport" content="width=device-width">
<!-- Open Graph metatags for sharing -->
<meta property="og:type" content="website" />
<meta property="og:title" content="raylib web game">
<meta property="og:image:type" content="image/png">
<meta property="og:image" content="https://www.raylib.com/common/raylib_logo.png">
<meta property="og:image:alt" content="New raylib web videogame, developed using raylib videogames library" />
<meta property="og:site_name" content="raylib - example">
<meta property="og:url" content="https://www.raylib.com/games.html">
<meta property="og:description" content="New raylib web videogame, developed using raylib videogames library">
<!-- Twitter metatags for sharing -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@raysan5">
<meta name="twitter:title" content="raylib web game">
<meta name="twitter:image" content="https://www.raylib.com/common/raylib_logo.png">
<meta name="twitter:image:alt" content="New raylib web videogame, developed using raylib videogames library">
<meta name="twitter:url" content="https://www.raylib.com/games.html">
<meta name="twitter:description" content="New raylib web videogame, developed using raylib videogames library">
<!-- Favicon -->
<link rel="shortcut icon" href="https://www.raylib.com/favicon.ico">
<style>
body { margin: 0px; overflow: hidden; background-color: black; }
canvas.emscripten { border: 0px none; background-color: black; }
</style>
<script type='text/javascript' src="https://cdn.jsdelivr.net/gh/eligrey/FileSaver.js/dist/FileSaver.min.js"> </script>
<script type='text/javascript'>
function saveFileFromMEMFSToDisk(memoryFSname, localFSname) // This can be called by C/C++ code
{
var isSafari = false; // Not supported, navigator.userAgent access is being restricted
//var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
var data = FS.readFile(memoryFSname);
var blob;
if (isSafari) blob = new Blob([data.buffer], { type: "application/octet-stream" });
else blob = new Blob([data.buffer], { type: "application/octet-binary" });
// NOTE: SaveAsDialog is a browser setting. For example, in Google Chrome,
// in Settings/Advanced/Downloads section you have a setting:
// 'Ask where to save each file before downloading' - which you can set true/false.
// If you enable this setting it would always ask you and bring the SaveAsDialog
saveAs(blob, localFSname);
}
</script>
</head>
<body>
<canvas class=emscripten id=canvas oncontextmenu=event.preventDefault() tabindex=-1></canvas>
<p id="output" />
<script>
var Module = {
print: (function() {
var element = document.getElementById('output');
if (element) element.value = ''; // clear browser cache
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
if (element) {
element.value += text + "\n";
element.scrollTop = element.scrollHeight; // focus on bottom
}
};
})(),
canvas: (function() {
var canvas = document.getElementById('canvas');
return canvas;
})()
};
</script>
{{{ SCRIPT }}}
</body>
</html>

View file

@ -14,7 +14,10 @@ task make, "Native build":
exec "mkdir -p build && nim c --outdir:build -d:release --opt:speed src/nim_wasm_test.nim"
task makeweb, "Emscripten build":
exec "mkdir -p build && nim c -d:emscripten -d:release src/nim_wasm_test.nim"
exec """
mkdir -p build && cp index.html build/
nim c -d:emscripten -d:release src/nim_wasm_test.nim
"""
task runweb, "Run a local webserver for the wasm build":
exec "nimhttpd -H:'Cross-Origin-Opener-Policy: same-origin' -H:'Cross-Origin-Embedder-Policy: require-corp' build/"

View file

@ -1,6 +1,8 @@
# vim: set et sta sw=4 ts=4 :
import std/os
import
std/os,
std/strformat
import raylib
# Emscripten specific imports
@ -15,23 +17,37 @@ var camera = Camera(
projection: Perspective # Defines projection type, see CameraProjection
)
# This only works with:
# --passL:"-s EXPORT_FUNCTIONS=_nativeGreet", but it breaks raylib loading.
# hmmmmm. EXPORT_ALL=1 is also a no go.
# Maybe it won't be necessary with raylib/opengl input
#
#[ proc nativeGreet*(): int {.exportc.} =
echo "Oh jeez, hello from native nim!"
return 0 ]#
proc mainLoop() {.cdecl.} =
defer: closeWindow()
initWindow( 800, 600, "raylib nim playground" )
var count = 0
while not windowShouldClose():
beginDrawing();
clearBackground( RAYWHITE );
clearBackground( WHITE );
beginMode3D( camera );
drawGrid( 10, 1.0f );
endMode3D();
var col = if isCursorOnScreen(): BLUE else: GRAY
drawText( "OH SNAP IT'S WORKING", 10, 10, 20, col );
drawText( "OH SNAP IT'S WORKING\n{count}".fmt, 10, 10, 20, col );
endDrawing();
count = count + 1
if defined( emscripten ):
emscripten_sleep 10
echo "wasm: hi"