init :: () {
create_window();
+ input_bind_glfw_events(window);
glInit(glfwGetLoadProcAddress());
glEnable(GL_TEXTURE);
input_update();
defer input_post_update();
+ if is_key_down(GLFW_KEY_ESCAPE) {
+ glfwSetWindowShouldClose(window, true);
+ return;
+ }
+
entity_manager->update(dt);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetWindowSizeCallback(window, "on_resize");
- glfwSetKeyCallback(window, "on_key");
- glfwSetMouseButtonCallback(window, "on_mouse_button");
}
#export "on_resize" (window: GLFWwindow_p, width, height: u32) {
update_view_matrix();
}
-#export "on_key" (window: GLFWwindow_p, key, scancode, action, mod: u32) {
- if key == GLFW_KEY_ESCAPE && action == GLFW_PRESS {
- glfwSetWindowShouldClose(window, true);
- }
-
- handle_key_event(key, scancode, action, mod);
-}
-
-#export "on_mouse_button" (window: GLFWwindow_p, button, action, mod: u32) {
- handle_button_event(button, action, mod);
-}
-
-
main :: (args) => {
if !glfwInit() {
println("Failed to initialize GLFW");
last_mouse_y = mouse_y;
}
-handle_key_event :: (key, scancode, action, mod: u32) {
- if action == GLFW_PRESS {
- keys_this_frame << key;
- }
-
- if action == GLFW_RELEASE {
- array.remove(^keys_this_frame, key);
- }
-}
-
is_key_down :: (key) => array.contains(keys_this_frame, key);
is_key_just_down :: (key) => array.contains(keys_this_frame, key) && !array.contains(keys_last_frame, key);
is_key_just_up :: (key) => !array.contains(keys_this_frame, key) && array.contains(keys_last_frame, key);
-handle_button_event :: (button, action, mod: u32) {
- if action == GLFW_PRESS {
- buttons_this_frame[button] = true;
- }
-
- if action == GLFW_RELEASE {
- buttons_this_frame[button] = false;
- }
-}
-
is_button_down :: (button) => buttons_this_frame[button];
is_button_just_down :: (button) => buttons_this_frame[button] && !buttons_last_frame[button];
is_button_just_up :: (button) => !buttons_this_frame[button] && buttons_last_frame[button];
mouse_get_position :: () -> (f64, f64) {
return mouse_x, mouse_y;
-}
\ No newline at end of file
+}
+
+
+input_bind_glfw_events :: (window: GLFWwindow_p) {
+ glfwSetKeyCallback(window, INPUT_KEY_EVENT);
+ glfwSetMouseButtonCallback(window, INPUT_BUTTON_EVENT);
+}
+
+#local {
+ INPUT_BUTTON_EVENT :: "__input_button_event"
+ INPUT_KEY_EVENT :: "__input_key_event"
+}
+
+#export INPUT_BUTTON_EVENT (window: GLFWwindow_p, button, action, mod: u32) {
+ if action == GLFW_PRESS {
+ buttons_this_frame[button] = true;
+ }
+
+ if action == GLFW_RELEASE {
+ buttons_this_frame[button] = false;
+ }
+}
+
+#export INPUT_KEY_EVENT (window: GLFWwindow_p, key, scancode, action, mod: u32) {
+ if action == GLFW_PRESS {
+ keys_this_frame << key;
+ }
+
+ if action == GLFW_RELEASE {
+ array.remove(^keys_this_frame, key);
+ }
+}