Added Mouse/Keyboard classes

This commit is contained in:
2025-08-17 15:11:42 +01:00
parent 31d8cac12a
commit 1b7b9e4ffd
3 changed files with 117 additions and 4 deletions

View File

@@ -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<Integer, Boolean> buttons;
private Keyboard() {
this.buttons = new HashMap<Integer, Boolean>();
}
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);
}
}

View File

@@ -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<Integer, Boolean> 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<Integer, Boolean>();
}
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);
}
}

View File

@@ -31,7 +31,6 @@ public class Window {
} }
public void blastOff() { public void blastOff() {
logVersion();
setup(); setup();
loop(); loop();
cleanUp(); cleanUp();
@@ -42,6 +41,8 @@ public class Window {
} }
private void setup() { private void setup() {
logVersion();
GLFWErrorCallback.createPrint(System.err).set(); GLFWErrorCallback.createPrint(System.err).set();
if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW"); if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
@@ -66,18 +67,26 @@ public class Window {
// GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); // GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// } // }
glfwSetCursorPosCallback(glfwWindow, Mouse::cursorPositionCallback);
glfwSetMouseButtonCallback(glfwWindow, Mouse::mouseButtonCallback);
glfwSetKeyCallback(glfwWindow, Keyboard::keyCallback);
glfwSetWindowTitle(glfwWindow, this.title); glfwSetWindowTitle(glfwWindow, this.title);
glfwMakeContextCurrent(glfwWindow); glfwMakeContextCurrent(glfwWindow);
glfwSwapInterval(1); glfwSwapInterval(1);
glfwShowWindow(glfwWindow); glfwShowWindow(glfwWindow);
GL.createCapabilities(); GL.createCapabilities();
glClearColor(1.0f, 0.0f, 2.0f, 0.0f);
} }
private void loop() { private void loop() {
// GL.createCapabilities(); // Does this maybe go in here?
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
while (!glfwWindowShouldClose(glfwWindow)) { while (!glfwWindowShouldClose(glfwWindow)) {
if (Mouse.isPressed(Mouse.Buttons.LEFT)) {
System.out.println("LEFT CLICKED");
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(glfwWindow); glfwSwapBuffers(glfwWindow);
glfwPollEvents(); glfwPollEvents();