add callbacks for mouse movement and mouse clicking

This commit is contained in:
2024-07-30 17:10:48 +01:00
parent 75b82fd888
commit e97768b18a
12 changed files with 102 additions and 49 deletions

View File

@@ -157,7 +157,8 @@ typedef Pointer Objptr;
extern "C" {
#endif
MLLIB_PUBLIC(void printFromMLton (Int32 x0, Int32 x1, Int32 x2, Int32 x3);)
MLLIB_PUBLIC(void mltonMouseMoveCallback (Int32 x0, Int32 x1);)
MLLIB_PUBLIC(void mltonMouseClickCallback (Int32 x0, Int32 x1);)
#undef MLLIB_PRIVATE
#undef MLLIB_PUBLIC

25
ffi/glfw-input.c Normal file
View File

@@ -0,0 +1,25 @@
#include "export.h"
#include <GLFW/glfw3.h>
int MOUSE_PRESSED = GLFW_PRESS;
int MOUSE_RELEASED = GLFW_RELEASE;
int LEFT_MOUSE_BUTTON = GLFW_MOUSE_BUTTON_1;
// Calls function exported from SML
void mouseMoveCallback(GLFWwindow *window, double xpos, double ypos) {
mltonMouseMoveCallback((int)xpos, (int)ypos);
}
void mouseClickCallback(GLFWwindow *window, int button, int action, int mods) {
mltonMouseClickCallback(button, action);
}
// Call this from MLton to register key callback with GLFW.
void setMouseMoveCallback(GLFWwindow *window) {
glfwSetCursorPosCallback(window, mouseMoveCallback);
}
void setMouseClickCallback(GLFWwindow *window) {
glfwSetMouseButtonCallback(window, mouseClickCallback);
}

24
ffi/glfw-input.sml Normal file
View File

@@ -0,0 +1,24 @@
structure Input =
struct
type window = MLton.Pointer.t
(* Export function to C. *)
val exportMouseMoveCallback =
_export "mltonMouseMoveCallback" public : (int * int -> unit) -> unit;
(* Import function to set callback for GLFW. *)
val setMouseMoveCallback = _import "setMouseMoveCallback" public reentrant : window -> unit;
val exportMouseClickCallback =
_export "mltonMouseClickCallback" public : (int * int -> unit) -> unit;
val setMouseClickCallback = _import "setMouseClickCallback" public reentrant : window -> unit;
(* Constants for mouse input. *)
val (MOUSE_PRESSED, _) =
_symbol "MOUSE_PRESSED" public : ( unit -> int ) * ( int -> unit );
val (MOUSE_RELEASED, _) =
_symbol "MOUSE_RELEASED" public : ( unit -> int ) * ( int -> unit );
val (LEFT_MOUSE_BUTTON, _) =
_symbol "LEFT_MOUSE_BUTTON" public : ( unit -> int ) * ( int -> unit );
end

View File

@@ -1,14 +0,0 @@
#include "export.h"
#include <GLFW/glfw3.h>
#include <stdbool.h>
// Calls function exported from SML
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
printFromMLton(key, scancode, action, mods);
}
// Call this from MLton to register key callback with GLFW.
void setKeyCallback(GLFWwindow *window) {
glfwSetKeyCallback(window, keyCallback);
}

View File

@@ -1,11 +0,0 @@
structure Key =
struct
type window = MLton.Pointer.t
(* Export function to C. *)
val export =
_export "printFromMLton" public : (int * int * int * int -> unit) -> unit;
(* Import function to set callback for GLFW. *)
val setCallback = _import "setKeyCallback" public reentrant : window -> unit;
end