Added Input & Some render code (but in a broken state)

This commit is contained in:
Sebastian Cabrera 2020-03-21 11:22:09 -04:00
parent f9fef39baa
commit 2105610921
10 changed files with 111 additions and 5 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -1,5 +1,75 @@
package bz.bronze.latte.engine.io; 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;
}
} }

View file

@ -2,6 +2,8 @@ package bz.bronze.latte.engine.io;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
public class Window { public class Window {
public int width, height; public int width, height;
@ -11,6 +13,16 @@ public class Window {
public static int frames; public static int frames;
public static long time; 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() { public static boolean shouldClose() {
if (window == 0) { if (window == 0) {
return false; return false;
@ -20,6 +32,11 @@ public class Window {
} }
public static void update() { public static void update() {
if (isInitialized) {
GL11.glClearColor(backgroundR, backgroundG, backgroundB, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
}
GLFW.glfwPollEvents(); GLFW.glfwPollEvents();
frames++; frames++;
@ -57,11 +74,18 @@ public class Window {
GLFW.glfwSetWindowPos(window, (videoMode.width() - width) / 2, (videoMode.height() - height) / 2); GLFW.glfwSetWindowPos(window, (videoMode.width() - width) / 2, (videoMode.height() - height) / 2);
GLFW.glfwMakeContextCurrent(window); 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.glfwShowWindow(window);
GLFW.glfwSwapInterval(2); GLFW.glfwSwapInterval(2);
Window.time = System.currentTimeMillis(); Window.time = System.currentTimeMillis();
isInitialized = true;
} }
public Window(int width, int height, String title) { public Window(int width, int height, String title) {

View file

@ -1,10 +1,15 @@
package bz.bronze.latte.main; package bz.bronze.latte.main;
import org.lwjgl.glfw.GLFW;
import bz.bronze.latte.engine.io.Input;
import bz.bronze.latte.engine.io.Window; import bz.bronze.latte.engine.io.Window;
public class Game implements Runnable { public class Game implements Runnable {
private void update() { private void update() {
Window.update(); Window.update();
if (Input.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) System.out.println("X: " + Input.getMouseX() + ", Y: " + Input.getMouseY());
} }
private void render() { private void render() {
@ -15,6 +20,10 @@ public class Game implements Runnable {
while (!Window.shouldClose()) { while (!Window.shouldClose()) {
update(); update();
render(); render();
if (Input.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) return;
} }
Input.destroy();
} }
} }

View file

@ -16,6 +16,8 @@ public class Main {
game.start(); game.start();
window.create(); window.create();
window.setBackgroundColor(1.0f, 0, 0);
} }
public static void main(String[] args) { public static void main(String[] args) {

View file

@ -1,3 +1,4 @@
module latte { module latte {
requires org.lwjgl.glfw; requires org.lwjgl.glfw;
requires org.lwjgl.opengl;
} }