From: Brendan Hansen Date: Mon, 1 Nov 2021 22:20:27 +0000 (-0500) Subject: Changed the way that hb functions are defined X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=8228da28c2eec667cb71bb1fc37d56702b1f6c2e;p=heartbreak.git Changed the way that hb functions are defined --- diff --git a/include/heartbreak.h b/include/heartbreak.h index 0255e7b..4f69072 100644 --- a/include/heartbreak.h +++ b/include/heartbreak.h @@ -37,14 +37,18 @@ typedef struct { #define CONCAT3(a, b, c) a ## _ ## b ## _ ## c #define HEARTBREAK_MODULE_NAME_GEN(m) CONCAT2(__heartbreak_module, m) #define HEARTBREAK_FUNC_NAME(m, n) CONCAT3(__heartbreak_internal, m, n) +#define HEARTBREAK_DEF_NAME(m, n) CONCAT3(__heartbreak_internal_def, m, n) #define HEARTBREAK_IMPORT_NAME(m, n) STRINGIFY1(m) "_" #n -#define HEARTBREAK_DEF(name) wasm_trap_t* HEARTBREAK_FUNC_NAME(HEARTBREAK_MODULE_NAME, name)(const wasm_val_vec_t* params, wasm_val_vec_t* results) -#define HEARTBREAK_FUNC(name, params_types, result_types) (WasmFuncDefinition) { HEARTBREAK_IMPORT_NAME(HEARTBREAK_MODULE_NAME, name), HEARTBREAK_FUNC_NAME(HEARTBREAK_MODULE_NAME, name), _VALS params_types, _VALS result_types }, +#define HEARTBREAK_DEF(name, params_types, result_types) \ + wasm_trap_t* HEARTBREAK_FUNC_NAME(HEARTBREAK_MODULE_NAME, name)(const wasm_val_vec_t* params, wasm_val_vec_t* results); \ + static const WasmFuncDefinition HEARTBREAK_DEF_NAME(HEARTBREAK_MODULE_NAME, name) = { HEARTBREAK_IMPORT_NAME(HEARTBREAK_MODULE_NAME, name), HEARTBREAK_FUNC_NAME(HEARTBREAK_MODULE_NAME, name), _VALS params_types, _VALS result_types }; \ + \ + wasm_trap_t* HEARTBREAK_FUNC_NAME(HEARTBREAK_MODULE_NAME, name)(const wasm_val_vec_t* params, wasm_val_vec_t* results) +#define HEARTBREAK_FUNC(name) HEARTBREAK_DEF_NAME(HEARTBREAK_MODULE_NAME, name), #define HEARTBREAK_MODULE WasmFuncDefinition HEARTBREAK_MODULE_NAME_GEN(HEARTBREAK_MODULE_NAME) [] = - // The Heartbreak modules extern WasmFuncDefinition __heartbreak_module_system[]; extern WasmFuncDefinition __heartbreak_module_graphics[]; diff --git a/misc/onyx/heartbreak.onyx b/misc/onyx/heartbreak.onyx index ce375c0..3966396 100644 --- a/misc/onyx/heartbreak.onyx +++ b/misc/onyx/heartbreak.onyx @@ -20,4 +20,6 @@ run :: (use funcs: HeartbreakFuncs) { update(0); draw(); } + + system.destroy(); } \ No newline at end of file diff --git a/misc/onyx/heartbreak_graphics.onyx b/misc/onyx/heartbreak_graphics.onyx index d18f80b..0716e15 100644 --- a/misc/onyx/heartbreak_graphics.onyx +++ b/misc/onyx/heartbreak_graphics.onyx @@ -1,6 +1,6 @@ package heartbreak.graphics -setClearColor :: (r, g, b, a: f32) -> void #foreign "heartbreak" "graphics_set_clear_color" --- +setClearColor :: (r, g, b: f32, a: f32 = 1) -> void #foreign "heartbreak" "graphics_set_clear_color" --- clear :: () -> void #foreign "heartbreak" "graphics_clear" --- FillMode :: enum { diff --git a/src/heartbreak.c b/src/heartbreak.c index 307759a..cf508cf 100644 --- a/src/heartbreak.c +++ b/src/heartbreak.c @@ -138,7 +138,7 @@ void run_wasm_file(bh_buffer wasm_bytes) { goto bad_import; import_found: - bh_printf("Found import %b.%b.\n", module_name->data, module_name->size, import_name->data, import_name->size); + // bh_printf("Found import %b.%b.\n", module_name->data, module_name->size, import_name->data, import_name->size); imports.data[i] = import; continue; diff --git a/src/heartbreak_graphics.c b/src/heartbreak_graphics.c index c5898ab..afb4ea0 100644 --- a/src/heartbreak_graphics.c +++ b/src/heartbreak_graphics.c @@ -3,7 +3,7 @@ static f32 clear_r, clear_g, clear_b, clear_a; -HEARTBREAK_DEF(set_clear_color) { +HEARTBREAK_DEF(set_clear_color, (WASM_F32,WASM_F32,WASM_F32,WASM_F32), ()) { clear_r = params->data[0].of.f32; clear_g = params->data[1].of.f32; clear_b = params->data[2].of.f32; @@ -11,27 +11,27 @@ HEARTBREAK_DEF(set_clear_color) { return NULL; } -HEARTBREAK_DEF(clear) { +HEARTBREAK_DEF(clear, (), ()) { glClearColor(clear_r, clear_g, clear_b, clear_a); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); return NULL; } -HEARTBREAK_DEF(set_color) { +HEARTBREAK_DEF(set_color, (WASM_F32,WASM_F32,WASM_F32,WASM_F32), ()) { return NULL; } -HEARTBREAK_DEF(rectangle) { +HEARTBREAK_DEF(rectangle, (WASM_I32,WASM_F32,WASM_F32,WASM_F32,WASM_F32), ()) { return NULL; } HEARTBREAK_MODULE { - HEARTBREAK_FUNC(set_clear_color, (WASM_F32,WASM_F32,WASM_F32,WASM_F32), ()) - HEARTBREAK_FUNC(clear, (), ()) - HEARTBREAK_FUNC(set_color, (WASM_F32,WASM_F32,WASM_F32,WASM_F32), ()) - HEARTBREAK_FUNC(rectangle, (WASM_I32,WASM_F32,WASM_F32,WASM_F32,WASM_F32), ()) + HEARTBREAK_FUNC(set_clear_color) + HEARTBREAK_FUNC(clear) + HEARTBREAK_FUNC(set_color) + HEARTBREAK_FUNC(rectangle) { NULL } }; \ No newline at end of file diff --git a/src/heartbreak_system.c b/src/heartbreak_system.c index 135ba53..f91b25f 100644 --- a/src/heartbreak_system.c +++ b/src/heartbreak_system.c @@ -1,7 +1,8 @@ -#define HEARTBREAK_MODULE_NAME system #include "heartbreak.h" -HEARTBREAK_DEF(init) { +#define HEARTBREAK_MODULE_NAME system + +HEARTBREAK_DEF(init, (), (WASM_I32)) { if (!glfwInit()) { bh_printf("Failed to initialize GLFW.\n"); results->data[0] = WASM_I32_VAL(0); @@ -23,13 +24,16 @@ HEARTBREAK_DEF(init) { return NULL; } -HEARTBREAK_DEF(destroy) { +HEARTBREAK_DEF(destroy, (), ()) { glfwDestroyWindow(glfw_window); glfwTerminate(); return NULL; } -HEARTBREAK_DEF(end_frame) { +// Called at the end of every frame. Every library using Heartbreak needs to call this in the +// main loop. This function also returns if the application should stay running. +// +HEARTBREAK_DEF(end_frame, (), (WASM_I32)) { glfwSwapBuffers(glfw_window); glfwPollEvents(); @@ -38,9 +42,9 @@ HEARTBREAK_DEF(end_frame) { } HEARTBREAK_MODULE { - HEARTBREAK_FUNC(init, (), (WASM_I32)) - HEARTBREAK_FUNC(destroy, (), ()) - HEARTBREAK_FUNC(end_frame, (), (WASM_I32)) + HEARTBREAK_FUNC(init) + HEARTBREAK_FUNC(destroy) + HEARTBREAK_FUNC(end_frame) { NULL } }; \ No newline at end of file diff --git a/tests/simp.onyx b/tests/simp.onyx index d38276a..47f1284 100644 --- a/tests/simp.onyx +++ b/tests/simp.onyx @@ -21,4 +21,6 @@ main :: (_) => { printf("Simp test is working!\n"); hb.run(.{ update, draw }); + + printf("Leaving..."); } \ No newline at end of file