From: Brendan Hansen Date: Mon, 1 Nov 2021 23:18:48 +0000 (-0500) Subject: started input and window modules X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=fe59d342e8b7536c89f428fd434cdb07aabebc85;p=heartbreak.git started input and window modules --- diff --git a/Makefile b/Makefile index bda92c9..d1a06d9 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ WARNINGS=-Wimplicit -Wmisleading-indentation -Wparentheses -Wsequence-point -Wre FLAGS=$(WARNINGS) -g3 INCLUDES=-I./include -I./lib/linux_x86_64/include -LIBS=-L./lib/linux_x86_64/lib -lwasmer -Wl,-rpath=./lib/linux_x86_64/lib -lglfw -lGL +LIBS=-L./lib/linux_x86_64/lib -lwasmer -Wl,-rpath=./lib/linux_x86_64/lib -lglfw -lGL -lm TARGET=./bin/heartbreak -OBJECT_FILES=./build/heartbreak.o ./build/utils.o ./build/heartbreak_system.o ./build/heartbreak_graphics.o +OBJECT_FILES=./build/heartbreak.o ./build/utils.o ./build/heartbreak_system.o ./build/heartbreak_graphics.o ./build/heartbreak_input.o ./build/heartbreak_window.o build/%.o: src/%.c $(CC) $(FLAGS) $(INCLUDES) -o $@ $(LIBS) -c $< diff --git a/include/heartbreak.h b/include/heartbreak.h index 4f69072..ce5c463 100644 --- a/include/heartbreak.h +++ b/include/heartbreak.h @@ -52,5 +52,7 @@ typedef struct { // The Heartbreak modules extern WasmFuncDefinition __heartbreak_module_system[]; extern WasmFuncDefinition __heartbreak_module_graphics[]; +extern WasmFuncDefinition __heartbreak_module_input[]; +extern WasmFuncDefinition __heartbreak_module_window[]; #endif \ No newline at end of file diff --git a/misc/onyx/heartbreak.onyx b/misc/onyx/heartbreak.onyx index 3966396..8d32705 100644 --- a/misc/onyx/heartbreak.onyx +++ b/misc/onyx/heartbreak.onyx @@ -2,10 +2,13 @@ package heartbreak #load "./heartbreak_graphics" #load "./heartbreak_system" +#load "./heartbreak_input" +#load "./heartbreak_window" use package core {println} HeartbreakFuncs :: struct { + init : () -> void; update : (dt: f32) -> void; draw : () -> void; } @@ -16,6 +19,8 @@ run :: (use funcs: HeartbreakFuncs) { return; } + init(); + while system.end_frame() { update(0); draw(); diff --git a/misc/onyx/heartbreak_input.onyx b/misc/onyx/heartbreak_input.onyx new file mode 100644 index 0000000..3c7722e --- /dev/null +++ b/misc/onyx/heartbreak_input.onyx @@ -0,0 +1,148 @@ +package heartbreak.input + +KeyConstant :: enum { + UNKNOWN :: 0; + SPACE :: 32; + APOSTROPHE :: 39; /* ' */ + COMMA :: 44; /* , */ + MINUS :: 45; /* - */ + PERIOD :: 46; /* . */ + SLASH :: 47; /* / */ + + _0 :: 48; + _1 :: 49; + _2 :: 50; + _3 :: 51; + _4 :: 52; + _5 :: 53; + _6 :: 54; + _7 :: 55; + _8 :: 56; + _9 :: 57; + SEMICOLON :: 59; /* ; */ + EQUAL :: 61; /* = */ + + A :: 65; + B :: 66; + C :: 67; + D :: 68; + E :: 69; + F :: 70; + G :: 71; + H :: 72; + I :: 73; + J :: 74; + K :: 75; + L :: 76; + M :: 77; + N :: 78; + O :: 79; + P :: 80; + Q :: 81; + R :: 82; + S :: 83; + T :: 84; + U :: 85; + V :: 86; + W :: 87; + X :: 88; + Y :: 89; + Z :: 90; + LEFT_BRACKET :: 91; /* [ */ + BACKSLASH :: 92; /* \ */ + RIGHT_BRACKET :: 93; /* ] */ + GRAVE_ACCENT :: 96; /* ` */ + WORLD_1 :: 161; /* non-US #1 */ + WORLD_2 :: 162; /* non-US #2 */ + ESCAPE :: 256; + ENTER :: 257; + TAB :: 258; + BACKSPACE :: 259; + INSERT :: 260; + DELETE :: 261; + RIGHT :: 262; + LEFT :: 263; + DOWN :: 264; + UP :: 265; + PAGE_UP :: 266; + PAGE_DOWN :: 267; + HOME :: 268; + END :: 269; + CAPS_LOCK :: 280; + SCROLL_LOCK :: 281; + NUM_LOCK :: 282; + PRINT_SCREEN :: 283; + PAUSE :: 284; + F1 :: 290; + F2 :: 291; + F3 :: 292; + F4 :: 293; + F5 :: 294; + F6 :: 295; + F7 :: 296; + F8 :: 297; + F9 :: 298; + F10 :: 299; + F11 :: 300; + F12 :: 301; + F13 :: 302; + F14 :: 303; + F15 :: 304; + F16 :: 305; + F17 :: 306; + F18 :: 307; + F19 :: 308; + F20 :: 309; + F21 :: 310; + F22 :: 311; + F23 :: 312; + F24 :: 313; + F25 :: 314; + KP_0 :: 320; + KP_1 :: 321; + KP_2 :: 322; + KP_3 :: 323; + KP_4 :: 324; + KP_5 :: 325; + KP_6 :: 326; + KP_7 :: 327; + KP_8 :: 328; + KP_9 :: 329; + KP_DECIMAL :: 330; + KP_DIVIDE :: 331; + KP_MULTIPLY :: 332; + KP_SUBTRACT :: 333; + KP_ADD :: 334; + KP_ENTER :: 335; + KP_EQUAL :: 336; + LEFT_SHIFT :: 340; + LEFT_CONTROL :: 341; + LEFT_ALT :: 342; + LEFT_SUPER :: 343; + RIGHT_SHIFT :: 344; + RIGHT_CONTROL :: 345; + RIGHT_ALT :: 346; + RIGHT_SUPER :: 347; + MENU :: 348; + LAST :: 348; +} + +ButtonConstant :: enum { + BUTTON_1 :: 0; + BUTTON_2 :: 1; + BUTTON_3 :: 2; + BUTTON_4 :: 3; + BUTTON_5 :: 4; + BUTTON_6 :: 5; + BUTTON_7 :: 6; + BUTTON_8 :: 7; + + Left :: 0; + Right :: 1; + Middle :: 2; +} + +keyIsDown :: (key: KeyConstant) -> bool #foreign "heartbreak" "input_key_is_down" --- +mouseGetX :: () -> i32 #foreign "heartbreak" "input_mouse_get_x" --- +mouseGetY :: () -> i32 #foreign "heartbreak" "input_mouse_get_y" --- +mouseIsDown :: (button: ButtonConstant) -> bool #foreign "heartbreak" "input_mouse_is_down" --- diff --git a/misc/onyx/heartbreak_window.onyx b/misc/onyx/heartbreak_window.onyx new file mode 100644 index 0000000..8b3d73e --- /dev/null +++ b/misc/onyx/heartbreak_window.onyx @@ -0,0 +1,6 @@ +package heartbreak.window + +getWidth :: () -> i32 #foreign "heartbreak" "window_get_width" --- +getHeight :: () -> i32 #foreign "heartbreak" "window_get_height" --- +setDimensions :: (width, height: i32) -> void #foreign "heartbreak" "window_set_dimensions" --- +setShouldClose :: (close: bool) -> void #foreign "heartbreak" "window_set_should_close" --- diff --git a/src/heartbreak.c b/src/heartbreak.c index cf508cf..acc952b 100644 --- a/src/heartbreak.c +++ b/src/heartbreak.c @@ -16,6 +16,8 @@ void build_heartbreak_imports(WasmFuncDefinition** out_wfds, i32* out_count) { static WasmFuncDefinition* modules[] = { __heartbreak_module_system, __heartbreak_module_graphics, + __heartbreak_module_input, + __heartbreak_module_window, }; i32 module_count = sizeof(modules) / sizeof(WasmFuncDefinition*); @@ -159,7 +161,13 @@ void run_wasm_file(bh_buffer wasm_bytes) { wasm_val_vec_t results; wasm_val_vec_new_uninitialized(&args, 0); - wasm_func_call(start_func, &args, &results); + wasm_trap_t* result = wasm_func_call(start_func, &args, &results); + + if (result != NULL) { + wasm_message_t err_msg; + wasm_trap_message(result, &err_msg); + bh_printf("Error running: %b\n", err_msg.data, err_msg.size); + } goto cleanup; diff --git a/src/heartbreak_input.c b/src/heartbreak_input.c new file mode 100644 index 0000000..7c8593c --- /dev/null +++ b/src/heartbreak_input.c @@ -0,0 +1,38 @@ +#include "heartbreak.h" +#include + +#define HEARTBREAK_MODULE_NAME input + +HEARTBREAK_DEF(key_is_down, (WASM_I32), (WASM_I32)) { + results->data[0] = WASM_I32_VAL(glfwGetKey(glfw_window, params->data[0].of.i32)); + return NULL; +} + +HEARTBREAK_DEF(mouse_get_x, (), (WASM_I32)) { + f64 x_pos; + glfwGetCursorPos(glfw_window, &x_pos, NULL); + results->data[0] = WASM_I32_VAL((i32) floor(x_pos)); + return NULL; +} + +HEARTBREAK_DEF(mouse_get_y, (), (WASM_I32)) { + f64 y_pos; + glfwGetCursorPos(glfw_window, NULL, &y_pos); + results->data[0] = WASM_I32_VAL((i32) floor(y_pos)); + return NULL; +} + +HEARTBREAK_DEF(mouse_is_down, (WASM_I32), (WASM_I32)) { + i32 value = glfwGetMouseButton(glfw_window, params->data[0].of.i32); + results->data[0] = WASM_I32_VAL(value == GLFW_PRESS); + return NULL; +} + +HEARTBREAK_MODULE { + HEARTBREAK_FUNC(key_is_down) + HEARTBREAK_FUNC(mouse_get_x) + HEARTBREAK_FUNC(mouse_get_y) + HEARTBREAK_FUNC(mouse_is_down) + + { NULL } +}; \ No newline at end of file diff --git a/src/heartbreak_window.c b/src/heartbreak_window.c new file mode 100644 index 0000000..30ba079 --- /dev/null +++ b/src/heartbreak_window.c @@ -0,0 +1,36 @@ +#include "heartbreak.h" + +#define HEARTBREAK_MODULE_NAME window + +HEARTBREAK_DEF(get_width, (), (WASM_I32)) { + i32 width; + glfwGetWindowSize(glfw_window, &width, NULL); + results->data[0] = WASM_I32_VAL(width); + return NULL; +} + +HEARTBREAK_DEF(get_height, (), (WASM_I32)) { + i32 height; + glfwGetWindowSize(glfw_window, NULL, &height); + results->data[0] = WASM_I32_VAL(height); + return NULL; +} + +HEARTBREAK_DEF(set_dimensions, (WASM_I32, WASM_I32), ()) { + glfwSetWindowSize(glfw_window, params->data[0].of.i32, params->data[1].of.i32); + return NULL; +} + +HEARTBREAK_DEF(set_should_close, (WASM_I32), ()) { + glfwSetWindowShouldClose(glfw_window, params->data[0].of.i32); + return NULL; +} + +HEARTBREAK_MODULE { + HEARTBREAK_FUNC(get_width) + HEARTBREAK_FUNC(get_height) + HEARTBREAK_FUNC(set_dimensions) + HEARTBREAK_FUNC(set_should_close) + + { NULL } +}; \ No newline at end of file diff --git a/tests/simp.onyx b/tests/simp.onyx index 47f1284..c5edf27 100644 --- a/tests/simp.onyx +++ b/tests/simp.onyx @@ -4,9 +4,25 @@ use package core hb :: package heartbreak +init :: () { + w := hb.window.getWidth(); + h := hb.window.getHeight(); + printf("The window is {}x{}\n", w, h); + + hb.window.setDimensions(1200, 900); +} + t: f32 = 0; update :: (dt: f32) { t += 0.016; + + if hb.input.keyIsDown(.ESCAPE) { + hb.window.setShouldClose(true); + } + + if hb.input.mouseIsDown(.Left) { + printf("Left mouse button down!\n"); + } } draw :: () { @@ -20,7 +36,7 @@ draw :: () { main :: (_) => { printf("Simp test is working!\n"); - hb.run(.{ update, draw }); + hb.run(.{ init, update, draw }); - printf("Leaving..."); + println("Leaving..."); } \ No newline at end of file