From 0d70fefc8b4891c82680dede7080e65c637f39b8 Mon Sep 17 00:00:00 2001 From: "Mahlon E. Smith" Date: Wed, 20 Aug 2025 10:40:16 -0700 Subject: [PATCH] Experiment with resource loading (in the form of a config file). Change compile time checks from "if" to "when", for proper type definitions. --- README.md | 5 +++-- config.nims | 3 ++- resources/config.ini | 5 +++++ src/nim_wasm_test.nim | 26 +++++++++++++++++++------- 4 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 resources/config.ini diff --git a/README.md b/README.md index d8bd782..710368d 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,6 @@ References: - https://github.com/planetis-m/naylib -May need to add `/usr/lib/emscripten` to your path, and hard link `emar.py` to -`emar`. +If on arch, may need to add `/usr/lib/emscripten` to your path, and hard link +`emar.py` to `emar`, along with `tools/file_packager.py` to +`tools/file_packager` for some reason. diff --git a/config.nims b/config.nims index 33fdd22..55a64b3 100644 --- a/config.nims +++ b/config.nims @@ -23,7 +23,7 @@ when defined( emscripten ): --panics:on --exceptions:goto --define:noSignalHandler - --passL:"-Oz" # Production bu ld + # --passL:"-Oz" # Production bu ld # --passL:"-O0 -gDebug -gsource-map" # Debug build --passL:"-s EXPORT_ES6" --passL:"-s EXPORT_NAME=NimTest" @@ -31,5 +31,6 @@ when defined( emscripten ): # --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:"--preload-file resources" --passL:"-o build/nimtest.js" diff --git a/resources/config.ini b/resources/config.ini new file mode 100644 index 0000000..f56f523 --- /dev/null +++ b/resources/config.ini @@ -0,0 +1,5 @@ + +[Performance] +# In milliseconds +tick = 1000 + diff --git a/src/nim_wasm_test.nim b/src/nim_wasm_test.nim index 4d2834c..935a87e 100644 --- a/src/nim_wasm_test.nim +++ b/src/nim_wasm_test.nim @@ -2,9 +2,23 @@ import std/os, - std/strformat + std/parsecfg, + std/strformat, + std/strutils import raylib +var conf: Config +try: + conf = loadConfig( "resources/config.ini" ) +except IOError as err: + echo "No config file, using defaults." + conf = newConfig() + +when defined( emscripten ): + var tick = conf.getSectionValue( "Performance", "tick", defaultVal = "10").parseInt.cuint +else: + var tick = conf.getSectionValue( "Performance", "tick", defaultVal = "10").parseInt + # Emscripten specific imports proc emscripten_set_main_loop( f: proc() {.cdecl.}, a: cint, b: bool ) {.importc.} proc emscripten_sleep( a: cuint ) {.importc.} @@ -48,15 +62,13 @@ proc mainLoop() {.cdecl.} = count = count + 1 - if defined( emscripten ): - emscripten_sleep 10 - echo "wasm: hi" + when defined( emscripten ): + emscripten_sleep( tick ) else: - sleep 10 - echo "native: hi" + sleep( tick ) -if defined( emscripten ): +when defined( emscripten ): emscripten_set_main_loop( mainLoop, 0, true ); else: mainLoop()