diff --git a/GameEngine/src/main/java/Main.java b/GameEngine/src/main/java/Main.java index 7a4dbfa..6b57f28 100644 --- a/GameEngine/src/main/java/Main.java +++ b/GameEngine/src/main/java/Main.java @@ -1,4 +1,4 @@ -import org.example.*; +import gameEngine.Window; public class Main { public static void main(String[] args) { diff --git a/GameEngine/src/main/java/Swing.java b/GameEngine/src/main/java/Swing.java new file mode 100644 index 0000000..c1de740 --- /dev/null +++ b/GameEngine/src/main/java/Swing.java @@ -0,0 +1,63 @@ +import gameEngine.ColorController; + +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Objects; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; + +public class Swing { + public Swing() { + } + + public static void swing() { + JFrame frame = new JFrame("Swing Example"); + frame.setDefaultCloseOperation(3); + frame.setSize(300, 200); + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(4, 2)); + JLabel keyInputLabel = new JLabel("KeyInput:"); + String[] keyInputOptions = new String[]{"KeyPress", "KeyDown", "KeyUp"}; + final JComboBox keyInputDropdown = new JComboBox(keyInputOptions); + JLabel keyCodeLabel = new JLabel("KeyCode:"); + String[] keyCodeOptions = new String[]{"A", "S", "D"}; + final JComboBox keyCodeDropdown = new JComboBox(keyCodeOptions); + JLabel colorLabel = new JLabel("Color:"); + String[] colorOptions = new String[]{"Red", "Blue", "Green", "Yellow"}; + final JComboBox colorDropdown = new JComboBox(colorOptions); + JButton updateButton = new JButton("Update"); + updateButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String selectedKeyInput = (String)keyInputDropdown.getSelectedItem(); + String selectedKeyCode = (String)keyCodeDropdown.getSelectedItem(); + String selectedColor = (String)colorDropdown.getSelectedItem(); + if (Objects.equals(selectedKeyCode, "A")) { + ColorController.setCustomKeyCode(65); + } + + if (Objects.equals(selectedKeyCode, "S")) { + ColorController.setCustomKeyCode(83); + } + + if (Objects.equals(selectedKeyCode, "D")) { + ColorController.setCustomKeyCode(68); + } + + } + }); + panel.add(keyInputLabel); + panel.add(keyInputDropdown); + panel.add(keyCodeLabel); + panel.add(keyCodeDropdown); + panel.add(colorLabel); + panel.add(colorDropdown); + panel.add(new JLabel()); + panel.add(updateButton); + frame.add(panel); + frame.setVisible(true); + } +} \ No newline at end of file diff --git a/GameEngine/src/main/java/gameEngine/ColorController.java b/GameEngine/src/main/java/gameEngine/ColorController.java new file mode 100644 index 0000000..6e0eae8 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/ColorController.java @@ -0,0 +1,71 @@ +package gameEngine; + +import gameEngine.*; +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; + +public class ColorController implements IGameComponent { + private float red; + private float green; + private float blue; + private static int customKeyCode; + + public ColorController() { + } + + public void init() { + this.setColor(0.0F, 0.0F, 0.0F); + } + + public void update() { + if (KeyInput.KeyDown(GLFW_KEY_R)) { + this.setColor(1.0F, 0.0F, 0.0F); + System.out.println("R"); + } + + if (KeyInput.KeyPress(GLFW_KEY_G)) { + this.setColor(0.0F, 1.0F, 0.0F); + System.out.println("G"); + } + + if (KeyInput.KeyUp(GLFW_KEY_B)) { + this.setColor(0.0F, 0.0F, 1.0F); + System.out.println("B"); + } + + if (KeyInput.KeyUp(GLFW_KEY_Y)) { + this.setColor(1.0F, 1.0F, 0.0F); + System.out.println("Y"); + } + + if (KeyInput.KeyPress(customKeyCode)) { + this.setColor(0.0F, 1.0F, 1.0F); + System.out.println("Custom"); + } + + glClearColor(this.getRed(), this.getGreen(), this.getBlue(), 0.0F); + } + + public float getRed() { + return this.red; + } + + public float getGreen() { + return this.green; + } + + public float getBlue() { + return this.blue; + } + + public void setColor(float r, float g, float b) { + this.red = r; + this.green = g; + this.blue = b; + } + + public static void setCustomKeyCode(int value) { + customKeyCode = value; + } + +} diff --git a/GameEngine/src/main/java/gameEngine/IGameComponent.java b/GameEngine/src/main/java/gameEngine/IGameComponent.java new file mode 100644 index 0000000..6a04e9a --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/IGameComponent.java @@ -0,0 +1,6 @@ +package gameEngine; + +public interface IGameComponent { + void init(); + void update (); +} diff --git a/GameEngine/src/main/java/gameEngine/KeyInput.java b/GameEngine/src/main/java/gameEngine/KeyInput.java new file mode 100644 index 0000000..f98dc47 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/KeyInput.java @@ -0,0 +1,54 @@ +package gameEngine; + +import static org.lwjgl.glfw.GLFW.*; + +public class KeyInput { + private static KeyInput instance; + private static long window; + 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; + } + + public static void init(long window) { + if (instance == null) { + instance = new KeyInput(window); + } + + } + + public static boolean KeyPress(int keyCode) { + return isValidKeyCode(keyCode) ? currentKeyStates[keyCode] : false; + } + + public static boolean KeyDown(int keyCode) { + if (!isValidKeyCode(keyCode)) { + return false; + } else { + return currentKeyStates[keyCode] && !lastKeyStates[keyCode]; + } + } + + public static boolean KeyUp(int keyCode) { + if (!isValidKeyCode(keyCode)) { + return false; + } else { + return !currentKeyStates[keyCode] && lastKeyStates[keyCode]; + } + } + + public static void updateKeyStates() { + System.arraycopy(currentKeyStates, 0, lastKeyStates, 0, currentKeyStates.length); + + 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 >= GLFW_KEY_SPACE && keyCode <= GLFW_KEY_LAST; + } +} diff --git a/GameEngine/src/main/java/gameEngine/Window.java b/GameEngine/src/main/java/gameEngine/Window.java new file mode 100644 index 0000000..4b4e344 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/Window.java @@ -0,0 +1,121 @@ + +package gameEngine; + +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.List; + +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; + private int width = 800; + private int height = 600; + String title = "HelloWorld"; + private long glfwWindow; + private final List iGameComponents = new ArrayList(); + + private Window() { + } + + public static Window get() { + if (window == null) { + window = new Window(); + } + + return window; + } + + public void run() { + System.out.println("Hello LWJGL " + Version.getVersion() + "!"); + init(); + loop(); + glfwFreeCallbacks(this.glfwWindow); + glfwDestroyWindow(this.glfwWindow); + glfwTerminate(); + glfwSetErrorCallback(null).free(); + } + + private void init() { + GLFWErrorCallback.createPrint(System.err).set(); + if ( !glfwInit() ) + throw new IllegalStateException("Unable to initialize GLFW"); + + glfwDefaultWindowHints(); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); + glfwWindowHint(GLFW_MAXIMIZED, GLFW_FALSE); + + this.glfwWindow = glfwCreateWindow(this.width, this.height, this.title, NULL, NULL); + if ( this.glfwWindow == NULL ) + throw new RuntimeException("Failed to create the GLFW window"); + + glfwSetKeyCallback(this.glfwWindow, (window, key, scancode, action, mods) -> { + if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) + glfwSetWindowShouldClose(window, true); + }); + + // 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(this.glfwWindow, pWidth, pHeight); + + // 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 (!glfwWindowShouldClose(this.glfwWindow)) { + //--------------------------------------------------------------- + // 毎フレームのキー状態を更新 + KeyInput.updateKeyStates(); + + //GameComponentのupdate処理 + for (IGameComponent gameComponents : iGameComponents) { + gameComponents.update(); + } + //--------------------------------------------------------------- + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア + glfwSwapBuffers(this.glfwWindow); // カラーバッファを交換 + glfwPollEvents(); // ウィンドウイベントをポーリング + } + + } + +} \ 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 deleted file mode 100644 index 949ce7b..0000000 --- a/GameEngine/src/main/java/org/example/ColorController.java +++ /dev/null @@ -1,79 +0,0 @@ -package org.example; - -import static org.lwjgl.glfw.GLFW.*; -import static org.lwjgl.opengl.GL11.*; - -public class ColorController implements IGameComponent { - private float red; - private float green; - private float blue; - private int customKeyInput; - private static int customKeyCode; - private int customColor; - - public ColorController() { - } - - public void init() { - this.setColor(0.0F, 0.0F, 0.0F); - } - - public void update() { - if (KeyInput.KeyDown(GLFW_KEY_R)) { - this.setColor(1.0F, 0.0F, 0.0F); - System.out.println("R"); - } - - if (KeyInput.KeyPress(GLFW_KEY_G)) { - this.setColor(0.0F, 1.0F, 0.0F); - System.out.println("G"); - } - - if (KeyInput.KeyUp(GLFW_KEY_B)) { - this.setColor(0.0F, 0.0F, 1.0F); - System.out.println("B"); - } - - if (KeyInput.KeyUp(GLFW_KEY_Y)) { - this.setColor(1.0F, 1.0F, 0.0F); - System.out.println("Y"); - } - - if (KeyInput.KeyPress(customKeyCode)) { - this.setColor(0.0F, 1.0F, 1.0F); - System.out.println("Custom"); - } - - glClearColor(this.getRed(), this.getGreen(), this.getBlue(), 0.0F); - } - - public float getRed() { - return this.red; - } - - public float getGreen() { - return this.green; - } - - public float getBlue() { - return this.blue; - } - - public void setColor(float r, float g, float b) { - this.red = r; - this.green = g; - this.blue = b; - } - - public void setCustomKeyInput(int value) { - this.customKeyInput = value; - } - - public static void setCustomKeyCode(int value) { - customKeyCode = value; - } - - public void setCustomColor(int value) { - this.customColor = value; - } -} diff --git a/GameEngine/src/main/java/org/example/IGameComponent.java b/GameEngine/src/main/java/org/example/IGameComponent.java deleted file mode 100644 index 03acee5..0000000 --- a/GameEngine/src/main/java/org/example/IGameComponent.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.example; - -public interface IGameComponent { - void init(); - void update (); -} diff --git a/GameEngine/src/main/java/org/example/KeyInput.java b/GameEngine/src/main/java/org/example/KeyInput.java deleted file mode 100644 index 2502f16..0000000 --- a/GameEngine/src/main/java/org/example/KeyInput.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.example; - -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[GLFW_KEY_LAST+1]; - private static final boolean[] currentKeyStates = new boolean[GLFW_KEY_LAST+1]; - - private KeyInput(long window) { - KeyInput.window = window; - } - - public static void init(long window) { - if (instance == null) { - instance = new KeyInput(window); - } - - } - - public static boolean KeyPress(int keyCode) { - return isValidKeyCode(keyCode) ? currentKeyStates[keyCode] : false; - } - - public static boolean KeyDown(int keyCode) { - if (!isValidKeyCode(keyCode)) { - return false; - } else { - return currentKeyStates[keyCode] && !lastKeyStates[keyCode]; - } - } - - public static boolean KeyUp(int keyCode) { - if (!isValidKeyCode(keyCode)) { - return false; - } else { - return !currentKeyStates[keyCode] && lastKeyStates[keyCode]; - } - } - - public static void updateKeyStates() { - System.arraycopy(currentKeyStates, 0, lastKeyStates, 0, currentKeyStates.length); - - 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 >= GLFW_KEY_SPACE && keyCode <= GLFW_KEY_LAST; - } -} diff --git a/GameEngine/src/main/java/org/example/Swing.java b/GameEngine/src/main/java/org/example/Swing.java deleted file mode 100644 index bb7499e..0000000 --- a/GameEngine/src/main/java/org/example/Swing.java +++ /dev/null @@ -1,64 +0,0 @@ - -package org.example; - -import java.awt.GridLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.Objects; -import javax.swing.JButton; -import javax.swing.JComboBox; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; - -public class Swing { - public Swing() { - } - - public static void swing() { - JFrame frame = new JFrame("Swing Example"); - frame.setDefaultCloseOperation(3); - frame.setSize(300, 200); - JPanel panel = new JPanel(); - panel.setLayout(new GridLayout(4, 2)); - JLabel keyInputLabel = new JLabel("KeyInput:"); - String[] keyInputOptions = new String[]{"KeyPress", "KeyDown", "KeyUp"}; - final JComboBox keyInputDropdown = new JComboBox(keyInputOptions); - JLabel keyCodeLabel = new JLabel("KeyCode:"); - String[] keyCodeOptions = new String[]{"A", "S", "D"}; - final JComboBox keyCodeDropdown = new JComboBox(keyCodeOptions); - JLabel colorLabel = new JLabel("Color:"); - String[] colorOptions = new String[]{"Red", "Blue", "Green", "Yellow"}; - final JComboBox colorDropdown = new JComboBox(colorOptions); - JButton updateButton = new JButton("Update"); - updateButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - String selectedKeyInput = (String)keyInputDropdown.getSelectedItem(); - String selectedKeyCode = (String)keyCodeDropdown.getSelectedItem(); - String selectedColor = (String)colorDropdown.getSelectedItem(); - if (Objects.equals(selectedKeyCode, "A")) { - ColorController.setCustomKeyCode(65); - } - - if (Objects.equals(selectedKeyCode, "S")) { - ColorController.setCustomKeyCode(83); - } - - if (Objects.equals(selectedKeyCode, "D")) { - ColorController.setCustomKeyCode(68); - } - - } - }); - panel.add(keyInputLabel); - panel.add(keyInputDropdown); - panel.add(keyCodeLabel); - panel.add(keyCodeDropdown); - panel.add(colorLabel); - panel.add(colorDropdown); - panel.add(new JLabel()); - panel.add(updateButton); - frame.add(panel); - frame.setVisible(true); - } -} \ 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 deleted file mode 100644 index 546f4f6..0000000 --- a/GameEngine/src/main/java/org/example/Window.java +++ /dev/null @@ -1,121 +0,0 @@ - -package org.example; - -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.List; - -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; - private int width = 800; - private int height = 600; - String title = "HelloWorld"; - private long glfwWindow; - private final List iGameComponents = new ArrayList(); - - private Window() { - } - - public static Window get() { - if (window == null) { - window = new Window(); - } - - return window; - } - - public void run() { - System.out.println("Hello LWJGL " + Version.getVersion() + "!"); - init(); - loop(); - glfwFreeCallbacks(this.glfwWindow); - glfwDestroyWindow(this.glfwWindow); - glfwTerminate(); - glfwSetErrorCallback(null).free(); - } - - private void init() { - GLFWErrorCallback.createPrint(System.err).set(); - if ( !glfwInit() ) - throw new IllegalStateException("Unable to initialize GLFW"); - - glfwDefaultWindowHints(); - glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); - glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); - glfwWindowHint(GLFW_MAXIMIZED, GLFW_FALSE); - - this.glfwWindow = glfwCreateWindow(this.width, this.height, this.title, NULL, NULL); - if ( this.glfwWindow == NULL ) - throw new RuntimeException("Failed to create the GLFW window"); - - glfwSetKeyCallback(this.glfwWindow, (window, key, scancode, action, mods) -> { - if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) - glfwSetWindowShouldClose(window, true); - }); - - // 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(this.glfwWindow, pWidth, pHeight); - - // 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 (!glfwWindowShouldClose(this.glfwWindow)) { - //--------------------------------------------------------------- - // 毎フレームのキー状態を更新 - KeyInput.updateKeyStates(); - - //GameComponentのupdate処理 - for (IGameComponent gameComponents : iGameComponents) { - gameComponents.update(); - } - //--------------------------------------------------------------- - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア - glfwSwapBuffers(this.glfwWindow); // カラーバッファを交換 - glfwPollEvents(); // ウィンドウイベントをポーリング - } - - } - -} \ No newline at end of file