}
// This is one place where top-level macros would be nice.
-#if #defined(symbol_source.keydown) { #export "__heartbreak_keydown" symbol_source.keydown }
-#if #defined(symbol_source.keyup) { #export "__heartbreak_keyup" symbol_source.keyup }
-#if #defined(symbol_source.resize) { #export "__heartbreak_resize" symbol_source.resize }
+#if #defined(symbol_source.keydown) { #export "__heartbreak_keydown" symbol_source.keydown }
+#if #defined(symbol_source.keyup) { #export "__heartbreak_keyup" symbol_source.keyup }
+#if #defined(symbol_source.mousedown) { #export "__heartbreak_mousedown" symbol_source.mousedown }
+#if #defined(symbol_source.mouseup) { #export "__heartbreak_mouseup" symbol_source.mouseup }
+#if #defined(symbol_source.mousemove) { #export "__heartbreak_mousemove" symbol_source.mousemove }
+#if #defined(symbol_source.mouseenter) { #export "__heartbreak_mouseenter" symbol_source.mouseenter }
+#if #defined(symbol_source.mouseleave) { #export "__heartbreak_mouseleave" symbol_source.mouseleave }
+#if #defined(symbol_source.wheelmoved) { #export "__heartbreak_wheelmoved" symbol_source.wheelmoved }
+#if #defined(symbol_source.resize) { #export "__heartbreak_resize" symbol_source.resize }
main :: (_) => hb.run(.{ symbol_source.load, symbol_source.update, symbol_source.draw });
return NULL;
}
-HEARTBREAK_DEF(mouse_get_x, (), (WASM_I32)) {
+HEARTBREAK_DEF(mouse_get_x, (), (WASM_F32)) {
f64 x_pos;
glfwGetCursorPos(glfw_window, &x_pos, NULL);
- results->data[0] = WASM_I32_VAL((i32) floor(x_pos));
+ results->data[0] = WASM_F32_VAL(x_pos);
return NULL;
}
-HEARTBREAK_DEF(mouse_get_y, (), (WASM_I32)) {
+HEARTBREAK_DEF(mouse_get_y, (), (WASM_F32)) {
f64 y_pos;
glfwGetCursorPos(glfw_window, NULL, &y_pos);
- results->data[0] = WASM_I32_VAL((i32) floor(y_pos));
+ results->data[0] = WASM_F32_VAL(y_pos);
return NULL;
}
HEARTBREAK_FUNC(mouse_is_down)
{ NULL }
-};
\ No newline at end of file
+};
#define HEARTBREAK_MODULE_NAME system
#define HEARTBREAK_EVENTS \
- HB_EVENT(keydown, (WASM_I32, WASM_I32)) \
- HB_EVENT(keyup, (WASM_I32)) \
- HB_EVENT(mousedown, ()) \
- HB_EVENT(mouseup, ()) \
- HB_EVENT(mousemove, ()) \
- HB_EVENT(resize, (WASM_I32, WASM_I32)) \
+ HB_EVENT(keydown, (WASM_I32, WASM_I32)) \
+ HB_EVENT(keyup, (WASM_I32)) \
+ HB_EVENT(mousedown, (WASM_I32, WASM_I32)) \
+ HB_EVENT(mouseup, (WASM_I32)) \
+ HB_EVENT(mousemove, (WASM_F32, WASM_F32)) \
+ HB_EVENT(mouseenter, ()) \
+ HB_EVENT(mouseleave, ()) \
+ HB_EVENT(wheelmoved, (WASM_F32, WASM_F32)) \
+ HB_EVENT(resize, (WASM_I32, WASM_I32)) \
#define HB_EVENT(name, pt) static wasm_func_t *__heartbreak_event_##name;
HEARTBREAK_EVENTS
HEARTBREAK_EVENT_CALL(resize, new_width, new_height);
}
-static void __glfw_window_key_callback(GLFWwindow *w, i32 key, int scancode, int action, int mods) {
+static void __glfw_key_callback(GLFWwindow *w, i32 key, int scancode, int action, int mods) {
switch (action) {
case GLFW_PRESS: HEARTBREAK_EVENT_CALL(keydown, key, mods); break;
case GLFW_RELEASE: HEARTBREAK_EVENT_CALL(keyup, key); break;
}
}
+static void __glfw_mouse_button_callback(GLFWwindow *w, i32 button, i32 action, i32 mods) {
+ switch (action) {
+ case GLFW_PRESS: HEARTBREAK_EVENT_CALL(mousedown, button, mods); break;
+ case GLFW_RELEASE: HEARTBREAK_EVENT_CALL(mouseup, button); break;
+ }
+}
+
+static void __glfw_mouse_position_callback(GLFWwindow *w, f64 xpos, f64 ypos) {
+ HEARTBREAK_EVENT_CALL(mousemove, (f32) xpos, (f32) ypos);
+}
+
+static void __glfw_mouse_enter_callback(GLFWwindow *w, i32 entered) {
+ if (entered == GLFW_TRUE) {
+ HEARTBREAK_EVENT_CALL(mouseenter, 0);
+ } else {
+ HEARTBREAK_EVENT_CALL(mouseleave, 0);
+ }
+}
+
+static void __glfw_scroll_callback(GLFWwindow *w, f64 xoff, f64 yoff) {
+ HEARTBREAK_EVENT_CALL(wheelmoved, xoff, yoff);
+}
+
typedef struct HeartbreakEventLink {
const char *name;
wasm_func_t **func;
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwMakeContextCurrent(glfw_window);
glfwSetWindowSizeCallback(glfw_window, __glfw_window_size_callback);
- glfwSetKeyCallback(glfw_window, __glfw_window_key_callback);
+ glfwSetKeyCallback(glfw_window, __glfw_key_callback);
+ glfwSetMouseButtonCallback(glfw_window, __glfw_mouse_button_callback);
+ glfwSetCursorPosCallback(glfw_window, __glfw_mouse_position_callback);
+ glfwSetCursorEnterCallback(glfw_window, __glfw_mouse_enter_callback);
+ glfwSetScrollCallback(glfw_window, __glfw_scroll_callback);
glfwSwapInterval(1);