diff --git a/bin/bz/bronze/latte/engine/io/Input.class b/bin/bz/bronze/latte/engine/io/Input.class index f895a56..a085318 100644 Binary files a/bin/bz/bronze/latte/engine/io/Input.class and b/bin/bz/bronze/latte/engine/io/Input.class differ diff --git a/bin/bz/bronze/latte/engine/io/Window.class b/bin/bz/bronze/latte/engine/io/Window.class index 024ca6e..9b497ad 100644 Binary files a/bin/bz/bronze/latte/engine/io/Window.class and b/bin/bz/bronze/latte/engine/io/Window.class differ diff --git a/bin/bz/bronze/latte/main/Game.class b/bin/bz/bronze/latte/main/Game.class index 7cb348e..bfba1b8 100644 Binary files a/bin/bz/bronze/latte/main/Game.class and b/bin/bz/bronze/latte/main/Game.class differ diff --git a/bin/bz/bronze/latte/main/Main.class b/bin/bz/bronze/latte/main/Main.class index 97485fe..d5b846d 100644 Binary files a/bin/bz/bronze/latte/main/Main.class and b/bin/bz/bronze/latte/main/Main.class differ diff --git a/bin/module-info.class b/bin/module-info.class index 818dafe..09bb165 100644 Binary files a/bin/module-info.class and b/bin/module-info.class differ diff --git a/src/bz/bronze/latte/engine/io/Input.java b/src/bz/bronze/latte/engine/io/Input.java index f46810e..80b3f4e 100644 --- a/src/bz/bronze/latte/engine/io/Input.java +++ b/src/bz/bronze/latte/engine/io/Input.java @@ -1,5 +1,75 @@ package bz.bronze.latte.engine.io; -public class Input { +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWCursorPosCallback; +import org.lwjgl.glfw.GLFWKeyCallback; +import org.lwjgl.glfw.GLFWMouseButtonCallback; -} +public class Input { + private static boolean[] keys = new boolean[GLFW.GLFW_KEY_LAST]; + private static boolean[] buttons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST]; + private static double mouseX, mouseY; + + private static GLFWKeyCallback keyboard; + private static GLFWCursorPosCallback mouseMove; + private static GLFWMouseButtonCallback mouseButtons; + + public Input() { + keyboard = new GLFWKeyCallback() { + public void invoke(long window, int key, int scancode, int action, int mods) { + keys[key] = (action != GLFW.GLFW_RELEASE); + } + }; + + mouseMove = new GLFWCursorPosCallback() { + public void invoke(long window, double xPos, double yPos) { + mouseX = xPos; + mouseY = yPos; + } + }; + + mouseButtons = new GLFWMouseButtonCallback() { + public void invoke(long window, int button, int action, int mods) { + buttons[button] = (action != GLFW.GLFW_RELEASE); + } + }; + } + + public static boolean isKeyDown(int key) { + return keys[key]; + } + + public static boolean isButtonDown(int button) { + return buttons[button]; + } + + public static void destroy() { + keyboard.free(); + mouseMove.free(); + mouseButtons.free(); + + GLFW.glfwWindowShouldClose(Window.window); + GLFW.glfwDestroyWindow(Window.window); + GLFW.glfwTerminate(); + } + + public static double getMouseX() { + return mouseX; + } + + public static double getMouseY() { + return mouseY; + } + + public GLFWKeyCallback getKeyboardCallback() { + return keyboard; + } + + public GLFWCursorPosCallback getMouseMoveCallback() { + return mouseMove; + } + + public GLFWMouseButtonCallback getMouseButtonsCallback() { + return mouseButtons; + } +} \ No newline at end of file diff --git a/src/bz/bronze/latte/engine/io/Window.java b/src/bz/bronze/latte/engine/io/Window.java index 15387bc..a95c47a 100644 --- a/src/bz/bronze/latte/engine/io/Window.java +++ b/src/bz/bronze/latte/engine/io/Window.java @@ -2,6 +2,8 @@ package bz.bronze.latte.engine.io; import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFWVidMode; +import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.GL11; public class Window { public int width, height; @@ -11,6 +13,16 @@ public class Window { public static int frames; public static long time; + private static float backgroundR, backgroundG, backgroundB; + + private static boolean isInitialized; + + public void setBackgroundColor(float r, float g, float b) { + backgroundR = r; + backgroundG = g; + backgroundB = b; + } + public static boolean shouldClose() { if (window == 0) { return false; @@ -20,6 +32,11 @@ public class Window { } public static void update() { + if (isInitialized) { + GL11.glClearColor(backgroundR, backgroundG, backgroundB, 1.0f); + GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); + } + GLFW.glfwPollEvents(); frames++; @@ -57,11 +74,18 @@ public class Window { GLFW.glfwSetWindowPos(window, (videoMode.width() - width) / 2, (videoMode.height() - height) / 2); GLFW.glfwMakeContextCurrent(window); + GL.createCapabilities(); + + GLFW.glfwSetKeyCallback(window, new Input().getKeyboardCallback()); + GLFW.glfwSetCursorPosCallback(window, new Input().getMouseMoveCallback()); + GLFW.glfwSetMouseButtonCallback(window, new Input().getMouseButtonsCallback()); + GLFW.glfwShowWindow(window); GLFW.glfwSwapInterval(2); Window.time = System.currentTimeMillis(); + isInitialized = true; } public Window(int width, int height, String title) { @@ -70,4 +94,4 @@ public class Window { Window.title = title; } -} +} \ No newline at end of file diff --git a/src/bz/bronze/latte/main/Game.java b/src/bz/bronze/latte/main/Game.java index d02fef3..2cbbfbb 100644 --- a/src/bz/bronze/latte/main/Game.java +++ b/src/bz/bronze/latte/main/Game.java @@ -1,10 +1,15 @@ package bz.bronze.latte.main; +import org.lwjgl.glfw.GLFW; + +import bz.bronze.latte.engine.io.Input; import bz.bronze.latte.engine.io.Window; public class Game implements Runnable { private void update() { Window.update(); + + if (Input.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) System.out.println("X: " + Input.getMouseX() + ", Y: " + Input.getMouseY()); } private void render() { @@ -15,6 +20,10 @@ public class Game implements Runnable { while (!Window.shouldClose()) { update(); render(); + + if (Input.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) return; } + + Input.destroy(); } -} +} \ No newline at end of file diff --git a/src/bz/bronze/latte/main/Main.java b/src/bz/bronze/latte/main/Main.java index d438c5a..81b697f 100644 --- a/src/bz/bronze/latte/main/Main.java +++ b/src/bz/bronze/latte/main/Main.java @@ -16,9 +16,11 @@ public class Main { game.start(); window.create(); + + window.setBackgroundColor(1.0f, 0, 0); } public static void main(String[] args) { init(); } -} +} \ No newline at end of file diff --git a/src/module-info.java b/src/module-info.java index 21c0f4a..bd90d70 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -1,3 +1,4 @@ module latte { requires org.lwjgl.glfw; + requires org.lwjgl.opengl; } \ No newline at end of file