diff --git a/GameEngine/src/main/java/Main.java b/GameEngine/src/main/java/Main.java new file mode 100644 index 0000000..7a4dbfa --- /dev/null +++ b/GameEngine/src/main/java/Main.java @@ -0,0 +1,9 @@ +import org.example.*; + +public class Main { + public static void main(String[] args) { + Window window = Window.get(); + Swing.swing(); + window.run(); + } +} \ No newline at end of file diff --git a/GameEngine/src/main/java/org/example/ColorController.java b/GameEngine/src/main/java/org/example/ColorController.java index 52cd4cb..949ce7b 100644 --- a/GameEngine/src/main/java/org/example/ColorController.java +++ b/GameEngine/src/main/java/org/example/ColorController.java @@ -1,6 +1,7 @@ package org.example; -import org.lwjgl.opengl.GL11; +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; public class ColorController implements IGameComponent { private float red; @@ -18,22 +19,22 @@ } public void update() { - if (KeyInput.KeyDown(82)) { + if (KeyInput.KeyDown(GLFW_KEY_R)) { this.setColor(1.0F, 0.0F, 0.0F); System.out.println("R"); } - if (KeyInput.KeyPress(71)) { + if (KeyInput.KeyPress(GLFW_KEY_G)) { this.setColor(0.0F, 1.0F, 0.0F); System.out.println("G"); } - if (KeyInput.KeyUp(66)) { + if (KeyInput.KeyUp(GLFW_KEY_B)) { this.setColor(0.0F, 0.0F, 1.0F); System.out.println("B"); } - if (KeyInput.KeyUp(89)) { + if (KeyInput.KeyUp(GLFW_KEY_Y)) { this.setColor(1.0F, 1.0F, 0.0F); System.out.println("Y"); } @@ -43,7 +44,7 @@ System.out.println("Custom"); } - GL11.glClearColor(this.getRed(), this.getGreen(), this.getBlue(), 0.0F); + glClearColor(this.getRed(), this.getGreen(), this.getBlue(), 0.0F); } public float getRed() { diff --git a/GameEngine/src/main/java/org/example/KeyInput.java b/GameEngine/src/main/java/org/example/KeyInput.java index f90e565..2502f16 100644 --- a/GameEngine/src/main/java/org/example/KeyInput.java +++ b/GameEngine/src/main/java/org/example/KeyInput.java @@ -1,12 +1,13 @@ package org.example; -import org.lwjgl.glfw.GLFW; +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; public class KeyInput { private static KeyInput instance; private static long window; - private static final boolean[] lastKeyStates = new boolean[349]; - private static final boolean[] currentKeyStates = new boolean[349]; + private static final boolean[] lastKeyStates = new boolean[GLFW_KEY_LAST+1]; + private static final boolean[] currentKeyStates = new boolean[GLFW_KEY_LAST+1]; private KeyInput(long window) { KeyInput.window = window; @@ -42,13 +43,13 @@ public static void updateKeyStates() { System.arraycopy(currentKeyStates, 0, lastKeyStates, 0, currentKeyStates.length); - for(int i = 32; i <= 348; ++i) { - currentKeyStates[i] = GLFW.glfwGetKey(window, i) == 1; + for(int i = GLFW_KEY_SPACE; i <= GLFW_KEY_LAST; ++i) { + currentKeyStates[i] = glfwGetKey(window, i) == 1; } } private static boolean isValidKeyCode(int keyCode) { - return keyCode >= 32 && keyCode <= 348; + return keyCode >= GLFW_KEY_SPACE && keyCode <= GLFW_KEY_LAST; } } diff --git a/GameEngine/src/main/java/org/example/Main.java b/GameEngine/src/main/java/org/example/Main.java deleted file mode 100644 index f106395..0000000 --- a/GameEngine/src/main/java/org/example/Main.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.example; - -public class Main { - public Main() { - } - - public static void main(String[] args) { - Window window = Window.get(); - Swing.swing(); - window.run(); - } -} \ No newline at end of file diff --git a/GameEngine/src/main/java/org/example/Window.java b/GameEngine/src/main/java/org/example/Window.java index bca8b40..546f4f6 100644 --- a/GameEngine/src/main/java/org/example/Window.java +++ b/GameEngine/src/main/java/org/example/Window.java @@ -1,19 +1,20 @@ package org.example; -import java.nio.IntBuffer; +import org.lwjgl.*; +import org.lwjgl.glfw.*; +import org.lwjgl.opengl.*; +import org.lwjgl.system.*; + +import java.nio.*; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import org.lwjgl.Version; -import org.lwjgl.glfw.Callbacks; -import org.lwjgl.glfw.GLFW; -import org.lwjgl.glfw.GLFWErrorCallback; -import org.lwjgl.glfw.GLFWErrorCallbackI; -import org.lwjgl.glfw.GLFWVidMode; -import org.lwjgl.opengl.GL; -import org.lwjgl.opengl.GL11; -import org.lwjgl.system.MemoryStack; + +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 Window { private static Window window; @@ -36,89 +37,85 @@ public void run() { System.out.println("Hello LWJGL " + Version.getVersion() + "!"); - this.init(); - this.loop(); - Callbacks.glfwFreeCallbacks(this.glfwWindow); - GLFW.glfwDestroyWindow(this.glfwWindow); - GLFW.glfwTerminate(); - GLFW.glfwSetErrorCallback((GLFWErrorCallbackI)null).free(); + init(); + loop(); + glfwFreeCallbacks(this.glfwWindow); + glfwDestroyWindow(this.glfwWindow); + glfwTerminate(); + glfwSetErrorCallback(null).free(); } private void init() { GLFWErrorCallback.createPrint(System.err).set(); - if (!GLFW.glfwInit()) { - throw new IllegalStateException("GLFWの初期化に失敗しました"); - } else { - GLFW.glfwDefaultWindowHints(); - GLFW.glfwWindowHint(131076, 0); - GLFW.glfwWindowHint(131075, 0); - GLFW.glfwWindowHint(131080, 0); - this.glfwWindow = GLFW.glfwCreateWindow(this.width, this.height, this.title, 0L, 0L); - if (this.glfwWindow == 0L) { - throw new RuntimeException("GLFWウィンドウの作成に失敗しました"); - } else { - GLFW.glfwSetKeyCallback(this.glfwWindow, (window, key, scancode, action, mods) -> { - if (key == 256 && action == 0) { - GLFW.glfwSetWindowShouldClose(window, true); - } + if ( !glfwInit() ) + throw new IllegalStateException("Unable to initialize GLFW"); - }); - MemoryStack stack = MemoryStack.stackPush(); + glfwDefaultWindowHints(); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); + glfwWindowHint(GLFW_MAXIMIZED, GLFW_FALSE); - try { - IntBuffer pWidth = stack.mallocInt(1); - IntBuffer pHeight = stack.mallocInt(1); - GLFW.glfwGetWindowSize(this.glfwWindow, pWidth, pHeight); - GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor()); - GLFW.glfwSetWindowPos(this.glfwWindow, (vidmode.width() - pWidth.get(0)) / 2, (vidmode.height() - pHeight.get(0)) / 2); - } catch (Throwable var6) { - if (stack != null) { - try { - stack.close(); - } catch (Throwable var5) { - var6.addSuppressed(var5); - } - } + this.glfwWindow = glfwCreateWindow(this.width, this.height, this.title, NULL, NULL); + if ( this.glfwWindow == NULL ) + throw new RuntimeException("Failed to create the GLFW window"); - throw var6; - } + glfwSetKeyCallback(this.glfwWindow, (window, key, scancode, action, mods) -> { + if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) + glfwSetWindowShouldClose(window, true); + }); - if (stack != null) { - stack.close(); - } + // 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* - GLFW.glfwMakeContextCurrent(this.glfwWindow); - GLFW.glfwSwapInterval(1); - GLFW.glfwShowWindow(this.glfwWindow); - KeyInput.init(this.glfwWindow); - this.iGameComponents.add(new ColorController()); - Iterator var7 = this.iGameComponents.iterator(); + // Get the window size passed to glfwCreateWindow + glfwGetWindowSize(this.glfwWindow, pWidth, pHeight); - while(var7.hasNext()) { - IGameComponent gameComponents = (IGameComponent)var7.next(); - gameComponents.init(); - } + // Get the resolution of the primary monitor + GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); - } + // Center the window + glfwSetWindowPos( + this.glfwWindow, + (vidmode.width() - pWidth.get(0)) / 2, + (vidmode.height() - pHeight.get(0)) / 2 + ); } + + + glfwMakeContextCurrent(this.glfwWindow); + glfwSwapInterval(1); + glfwShowWindow(this.glfwWindow); + + //-------------------------------------------------------------- + KeyInput.init(this.glfwWindow); + iGameComponents.add(new ColorController()); + for (IGameComponent gameComponent : iGameComponents) { + gameComponent.init(); + } + //-------------------------------------------------------------- } private void loop() { GL.createCapabilities(); - while(!GLFW.glfwWindowShouldClose(this.glfwWindow)) { + while (!glfwWindowShouldClose(this.glfwWindow)) { + //--------------------------------------------------------------- + // 毎フレームのキー状態を更新 KeyInput.updateKeyStates(); - Iterator var1 = this.iGameComponents.iterator(); - while(var1.hasNext()) { - IGameComponent gameComponents = (IGameComponent)var1.next(); + //GameComponentのupdate処理 + for (IGameComponent gameComponents : iGameComponents) { gameComponents.update(); } + //--------------------------------------------------------------- - GL11.glClear(16640); - GLFW.glfwSwapBuffers(this.glfwWindow); - GLFW.glfwPollEvents(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア + glfwSwapBuffers(this.glfwWindow); // カラーバッファを交換 + glfwPollEvents(); // ウィンドウイベントをポーリング } } + } \ No newline at end of file