diff --git a/src/main/java/org/hirw/game/Keyboard.java b/src/main/java/org/hirw/game/Keyboard.java new file mode 100644 index 0000000..fefcdba --- /dev/null +++ b/src/main/java/org/hirw/game/Keyboard.java @@ -0,0 +1,37 @@ +package org.hirw.game; + +import static org.lwjgl.glfw.GLFW.*; + +import java.util.HashMap; + +public class Keyboard { + private static Keyboard instance; + private HashMap buttons; + + private Keyboard() { + this.buttons = new HashMap(); + } + + public static Keyboard getInstance() { + if (instance == null) { + synchronized (Keyboard.class) { + if (instance == null) { + instance = new Keyboard(); + } + } + } + return instance; + } + + public static void keyCallback(long window, int keyCode, int _scan, int action, int _modifiers) { + if (action == GLFW_PRESS) { + getInstance().buttons.put(keyCode, true); + } else if (action == GLFW_RELEASE) { + getInstance().buttons.put(keyCode, false); + } + } + + public static boolean isPressed(int keyCode) { + return getInstance().buttons.getOrDefault(keyCode, false); + } +} diff --git a/src/main/java/org/hirw/game/Mouse.java b/src/main/java/org/hirw/game/Mouse.java new file mode 100644 index 0000000..960008a --- /dev/null +++ b/src/main/java/org/hirw/game/Mouse.java @@ -0,0 +1,67 @@ +package org.hirw.game; + +import static org.lwjgl.glfw.GLFW.*; + +import java.util.HashMap; + +public class Mouse { + private double x, y, oldX, oldY; + private HashMap buttons; + + private static Mouse instance; + + public interface Buttons { + public final int LEFT = GLFW_MOUSE_BUTTON_LEFT; + public final int RIGHT = GLFW_MOUSE_BUTTON_RIGHT; + } + + private Mouse() { + this.buttons = new HashMap(); + } + + public static Mouse getInstance() { + if (instance == null) { + synchronized (Mouse.class) { // Double checked locking + if (instance == null) { + instance = new Mouse(); + } + } + } + return instance; + } + + public static void cursorPositionCallback(long window, double newX, double newY) { + getInstance().oldX = getInstance().x; + getInstance().oldY = getInstance().y; + getInstance().x = newX; + getInstance().y = newY; + } + + public static void mouseButtonCallback(long window, int keyCode, int action, int _modifiers) { + if (action == GLFW_PRESS) { + getInstance().buttons.put(keyCode, true); + } else if (action == GLFW_RELEASE) { + getInstance().buttons.put(keyCode, false); + } + } + + public static double getX() { + return getInstance().x; + } + + public static double getY() { + return getInstance().y; + } + + public static double getDeltaX() { + return getInstance().oldX - getInstance().x; + } + + public static double getDeltaY() { + return getInstance().oldY - getInstance().y; + } + + public static boolean isPressed(int keyCode) { + return getInstance().buttons.getOrDefault(keyCode, false); + } +} diff --git a/src/main/java/org/hirw/game/Window.java b/src/main/java/org/hirw/game/Window.java index fc9d929..b0d7352 100644 --- a/src/main/java/org/hirw/game/Window.java +++ b/src/main/java/org/hirw/game/Window.java @@ -31,7 +31,6 @@ public class Window { } public void blastOff() { - logVersion(); setup(); loop(); cleanUp(); @@ -42,6 +41,8 @@ public class Window { } private void setup() { + logVersion(); + GLFWErrorCallback.createPrint(System.err).set(); if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW"); @@ -66,18 +67,26 @@ public class Window { // GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); // } + glfwSetCursorPosCallback(glfwWindow, Mouse::cursorPositionCallback); + glfwSetMouseButtonCallback(glfwWindow, Mouse::mouseButtonCallback); + glfwSetKeyCallback(glfwWindow, Keyboard::keyCallback); + glfwSetWindowTitle(glfwWindow, this.title); glfwMakeContextCurrent(glfwWindow); glfwSwapInterval(1); glfwShowWindow(glfwWindow); + GL.createCapabilities(); + glClearColor(1.0f, 0.0f, 2.0f, 0.0f); } private void loop() { - // GL.createCapabilities(); // Does this maybe go in here? - glClearColor(1.0f, 0.0f, 0.0f, 0.0f); - while (!glfwWindowShouldClose(glfwWindow)) { + + if (Mouse.isPressed(Mouse.Buttons.LEFT)) { + System.out.println("LEFT CLICKED"); + } + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwSwapBuffers(glfwWindow); glfwPollEvents();