From 0f97ae9293d10c67519a7bcd7d4ce92f3e90fd7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adriano=20Anast=C3=A1cio?= Date: Mon, 27 Jul 2026 16:46:46 +0100 Subject: [PATCH] changed projects structure --- build.bat | 1 + build.sh | 6 ++--- lib/js/consoleApi.js | 26 +++++++++++++++++++ lib/odin/js_console/console_api.odin | 13 ++++++++++ .../js_console/logger.odin} | 23 +++++----------- src/game.odin | 4 +-- template/main.odin | 7 +++-- template/web/index.html | 17 ++++++++++-- template/web/index.js | 20 -------------- 9 files changed, 69 insertions(+), 48 deletions(-) create mode 100644 lib/js/consoleApi.js create mode 100644 lib/odin/js_console/console_api.odin rename lib/{js_log/js_log.odin => odin/js_console/logger.odin} (57%) delete mode 100644 template/web/index.js diff --git a/build.bat b/build.bat index 45b906c..b40e4a4 100644 --- a/build.bat +++ b/build.bat @@ -1,3 +1,4 @@ del /s /f /q .\out\* xcopy /s /e /y .\template\web .\out +xcopy /s /e /y .\lib\js .\out odin build template -target:js_wasm32 -out:out\game.wasm \ No newline at end of file diff --git a/build.sh b/build.sh index cff4fee..fb946ea 100644 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ -yes | del -r ./out\* -# shellcheck disable=SC2216 -yes | cp -r ./template\web ./out +yes | del -r ./out/* +cp -r ./template/web ./out +cp -r ./lib/js/ ./out odin build template -target:js_wasm32 -out:out/game.wasm \ No newline at end of file diff --git a/lib/js/consoleApi.js b/lib/js/consoleApi.js new file mode 100644 index 0000000..5cafbca --- /dev/null +++ b/lib/js/consoleApi.js @@ -0,0 +1,26 @@ +class ConsoleApi { + constructor(mem) { + this.mem = mem; + } + + trace = (str_ptr, str_len) => { + console.trace(this.mem.loadString(str_ptr, str_len)); + }; + + debug = (str_ptr, str_len) => { + console.debug(this.mem.loadString(str_ptr, str_len)); + }; + info = (str_ptr, str_len) => { + console.info(this.mem.loadString(str_ptr, str_len)); + }; + + warn = function(str_ptr, str_len) { + console.warn(this.mem.loadString(str_ptr, str_len)); + }; + + error = (str_ptr, str_len) => { + console.error(this.mem.loadString(str_ptr, str_len)); + }; +} + + diff --git a/lib/odin/js_console/console_api.odin b/lib/odin/js_console/console_api.odin new file mode 100644 index 0000000..cbbbaac --- /dev/null +++ b/lib/odin/js_console/console_api.odin @@ -0,0 +1,13 @@ +package js_console + +foreign import "console_api" + +@(default_calling_convention="contextless") +foreign console_api { +// you can pass values to the JS side too. + trace :: proc(str: string) --- + debug :: proc(str: string) --- + info :: proc(str: string) --- + warn :: proc(str: string) --- + error :: proc(str: string) --- +} \ No newline at end of file diff --git a/lib/js_log/js_log.odin b/lib/odin/js_console/logger.odin similarity index 57% rename from lib/js_log/js_log.odin rename to lib/odin/js_console/logger.odin index 267fc10..5b5d39d 100644 --- a/lib/js_log/js_log.odin +++ b/lib/odin/js_console/logger.odin @@ -1,23 +1,12 @@ -package js_log +package js_console import "core:log" -import "core:mem" - -foreign import "js_log" - -@(default_calling_convention="contextless") -foreign js_log { -// you can pass values to the JS side too. - trace :: proc(str: string) --- - debug :: proc(str: string) --- - info :: proc(str: string) --- - warn :: proc(str: string) --- - error :: proc(str: string) --- -} // Beware that .FATAL is logged as .Error as it is not supported by this interface // Unknown log levels will be logged by calling 'console.trace' js_console_logger_proc :: proc(data: rawptr, level: log.Level, text: string, options: log.Options, location := #caller_location) { +// right now options are ignored, maybe revisit it later + switch level { case .Debug: debug(text) case .Info: info(text) @@ -28,11 +17,11 @@ js_console_logger_proc :: proc(data: rawptr, level: log.Level, text: string, opt } } -create_js_console_logger :: proc(options := log.Default_Console_Logger_Opts) -> log.Logger { +create_js_console_logger :: proc() -> log.Logger { return log.Logger{ lowest_level = .Debug, procedure = js_console_logger_proc, - options = options, + options = {}, data = nil } -} +} \ No newline at end of file diff --git a/src/game.odin b/src/game.odin index 6e57d2b..9781290 100644 --- a/src/game.odin +++ b/src/game.odin @@ -1,11 +1,11 @@ package game -import js_log "../lib/js_log" +import js_console "../lib/odin/js_console" import log "core:log" import "base:runtime" init :: proc(ctx: ^runtime.Context) { - ctx.logger = js_log.create_js_console_logger() + ctx.logger = js_console.create_js_console_logger() log.info("Initialized") } diff --git a/template/main.odin b/template/main.odin index 83a8445..2c9cb8e 100644 --- a/template/main.odin +++ b/template/main.odin @@ -28,10 +28,9 @@ step :: proc(delta_time: f64) -> bool { is_init = true } - if !update_hook() { + keep_running := update_hook() + if !keep_running { exit_hook() - return false } - - return true + return keep_running } \ No newline at end of file diff --git a/template/web/index.html b/template/web/index.html index 1301148..a502cb5 100644 --- a/template/web/index.html +++ b/template/web/index.html @@ -5,10 +5,23 @@ - Example 1 + frigg - + + + \ No newline at end of file diff --git a/template/web/index.js b/template/web/index.js deleted file mode 100644 index b3e546a..0000000 --- a/template/web/index.js +++ /dev/null @@ -1,20 +0,0 @@ -"use strict"; - -(async () => { - await import("./odin.js"); // from the 'sys/wasm/js' - - let mem = new odin.WasmMemoryInterface() - - let js_log = { - trace: (str_ptr, str_len) => console.trace(mem.loadString(str_ptr, str_len)), - debug: (str_ptr, str_len) => console.debug(mem.loadString(str_ptr, str_len)), - info : (str_ptr, str_len) => console.info(mem.loadString(str_ptr, str_len)), - warn : (str_ptr, str_len) => console.warn(mem.loadString(str_ptr, str_len)), - error: (str_ptr, str_len) => console.error(mem.loadString(str_ptr, str_len)), - }; - - let extra = { - 'js_log' : js_log - }; - await odin.runWasm("./game.wasm", null, extra, mem); -})(); \ No newline at end of file