-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fe00ac
commit f0cc3c0
Showing
3 changed files
with
130 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/main/java/com/github/incognitojam/redpacket/engine/Window.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.github.incognitojam.redpacket.engine; | ||
|
||
import org.lwjgl.glfw.GLFWVidMode; | ||
import org.lwjgl.system.MemoryStack; | ||
|
||
import java.nio.IntBuffer; | ||
|
||
import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks; | ||
import static org.lwjgl.glfw.GLFW.*; | ||
import static org.lwjgl.system.MemoryStack.stackPush; | ||
import static org.lwjgl.system.MemoryUtil.NULL; | ||
|
||
public class Window { | ||
private String title; | ||
private int width; | ||
private int height; | ||
private final long handle; | ||
|
||
public Window(String title, int width, int height) { | ||
this.title = title; | ||
this.width = width; | ||
this.height = height; | ||
|
||
// Configure GLFW | ||
glfwDefaultWindowHints(); // optional, the current window hints are already the default | ||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation | ||
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable | ||
|
||
// Create the window | ||
handle = glfwCreateWindow(width, height, title, NULL, NULL); | ||
if (handle == NULL) | ||
throw new RuntimeException("Failed to create the GLFW window"); | ||
|
||
// Setup a key callback. It will be called every time a key is pressed, repeated or released. | ||
glfwSetKeyCallback(handle, (window, key, scancode, action, mods) -> { | ||
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) | ||
glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop | ||
}); | ||
|
||
// Setup a window size callback. It will be called every time the window is resized. | ||
glfwSetWindowSizeCallback(handle, (window, w, h) -> { | ||
this.width = w; | ||
this.height = h; | ||
}); | ||
|
||
// Get the thread stack and push a new frame | ||
try (MemoryStack stack = stackPush()) { | ||
IntBuffer pWidth = stack.mallocInt(1); // int* | ||
IntBuffer pHeight = stack.mallocInt(1); // int* | ||
|
||
// Get the window size passed to glfwCreateWindow | ||
glfwGetWindowSize(handle, pWidth, pHeight); | ||
|
||
// Get the resolution of the primary monitor | ||
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); | ||
|
||
// Center the window | ||
glfwSetWindowPos( | ||
handle, | ||
(vidmode.width() - pWidth.get(0)) / 2, | ||
(vidmode.height() - pHeight.get(0)) / 2 | ||
); | ||
} // the stack frame is popped automatically | ||
|
||
// Make the OpenGL context current | ||
glfwMakeContextCurrent(handle); | ||
// Enable v-sync | ||
// TODO: add argument to disable | ||
glfwSwapInterval(1); | ||
|
||
// Make the window visible | ||
glfwShowWindow(handle); | ||
} | ||
|
||
public void destroy() { | ||
// Free the window callbacks and destroy the window | ||
glfwFreeCallbacks(handle); | ||
glfwDestroyWindow(handle); | ||
} | ||
|
||
public boolean isKeyPressed(int keyCode) { | ||
return glfwGetKey(handle, keyCode) == GLFW_PRESS; | ||
} | ||
|
||
public boolean shouldClose() { | ||
return glfwWindowShouldClose(handle); | ||
} | ||
|
||
public void update() { | ||
glfwSwapBuffers(handle); | ||
glfwPollEvents(); | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
glfwSetWindowTitle(handle, title); | ||
} | ||
|
||
public int getWidth() { | ||
return width; | ||
} | ||
|
||
public void setWidth(int width) { | ||
this.width = width; | ||
glfwSetWindowSize(handle, width, height); | ||
} | ||
|
||
public int getHeight() { | ||
return height; | ||
} | ||
|
||
public void setHeight(int height) { | ||
this.height = height; | ||
glfwSetWindowSize(handle, width, height); | ||
} | ||
} |