Skip to content

Commit

Permalink
refactor: extract Window into class
Browse files Browse the repository at this point in the history
  • Loading branch information
incognitojam committed Apr 22, 2020
1 parent 5fe00ac commit f0cc3c0
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.{gradle,java}]
[*.{gradle, java}]
indent_size = 4

[*.md]
Expand Down
65 changes: 9 additions & 56 deletions src/main/java/com/github/incognitojam/redpacket/RedPacket.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
package com.github.incognitojam.redpacket;

import com.github.incognitojam.redpacket.engine.Window;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

public class RedPacket {
// Window handle
private long window;
private Window window;

public void run() {
System.out.println("Hello LWJGL " + Version.getVersion() + "!");

init();
loop();

// Free the window callbacks and destroy the window
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
window.destroy();

// Terminate GLFW and free the error callback
glfwTerminate();
Expand All @@ -41,48 +33,7 @@ private void init() {
if (!glfwInit())
throw new IllegalStateException("Unable to initialize GLFW");

// 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
window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL);
if (window == 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(window, (window, key, scancode, action, mods) -> {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
});

// 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(window, pWidth, pHeight);

// Get the resolution of the primary monitor
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

// Center the window
glfwSetWindowPos(
window,
(vidmode.width() - pWidth.get(0)) / 2,
(vidmode.height() - pHeight.get(0)) / 2
);
} // the stack frame is popped automatically

// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);

// Make the window visible
glfwShowWindow(window);
window = new Window("Red Packet", 300, 300);
}

private void loop() {
Expand All @@ -98,10 +49,12 @@ private void loop() {

// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
while (!window.shouldClose()) {
// clear the framebuffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glfwSwapBuffers(window); // swap the color buffers
// swap the color buffers
window.update();

// Poll for window events. The key callback above will only be
// invoked during this call.
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/com/github/incognitojam/redpacket/engine/Window.java
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);
}
}

0 comments on commit f0cc3c0

Please sign in to comment.