diff --git a/GameEngine/src/main/java/gameEngine/ColorController.java b/GameEngine/src/main/java/gameEngine/ColorController.java deleted file mode 100644 index f844d3d..0000000 --- a/GameEngine/src/main/java/gameEngine/ColorController.java +++ /dev/null @@ -1,70 +0,0 @@ -package gameEngine; -import gameEngine.input.*; - -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; - - public ColorController() { - } - - public void init() { - this.setColor(0.0F, 0.0F, 0.0F); - } - - public void update() { - if (Input.GetKey(GLFW_KEY_R)) { - this.setColor(1.0F, 0.0F, 0.0F); - System.out.println("R"); - } - - if (Input.GetKeyUp(GLFW_KEY_G)) { - this.setColor(0.0F, 1.0F, 0.0F); - System.out.println("G"); - } - - if (Input.GetKeyDown(GLFW_KEY_B)) { - this.setColor(0.0F, 0.0F, 1.0F); - System.out.println("B"); - } - - if (Input.GetMouseButton(0)) { // Left mouse button - setColor(1.0f, 0.0f, 0.0f); // Set color to red - System.out.println("Left Mouse Button Pressed - Red"); - } - if (Input.GetMouseButtonUp(1)) { // Right mouse button - setColor(0.0f, 0.0f, 1.0f); // Set color to blue - System.out.println("Right Mouse Button Pressed - Blue"); - } - if (Input.GetMouseButtonDown(2)) { // Middle mouse button - setColor(0.0f, 1.0f, 0.0f); // Set color to green - System.out.println("Middle Mouse Button Pressed - Green"); - } - - 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; - } - - -} diff --git a/GameEngine/src/main/java/gameEngine/IGameComponent.java b/GameEngine/src/main/java/gameEngine/IGameComponent.java deleted file mode 100644 index 6a04e9a..0000000 --- a/GameEngine/src/main/java/gameEngine/IGameComponent.java +++ /dev/null @@ -1,6 +0,0 @@ -package gameEngine; - -public interface IGameComponent { - void init(); - void update (); -} diff --git a/GameEngine/src/main/java/gameEngine/Time.java b/GameEngine/src/main/java/gameEngine/Time.java new file mode 100644 index 0000000..46027b5 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/Time.java @@ -0,0 +1,7 @@ +package gameEngine; + +public class Time { + public static float timeStarted = System.nanoTime(); + + public static float getTime(){return (float)((System.nanoTime() - timeStarted) * 1E-9); } +} diff --git a/GameEngine/src/main/java/gameEngine/Window.java b/GameEngine/src/main/java/gameEngine/Window.java index e4f36b0..0ff3001 100644 --- a/GameEngine/src/main/java/gameEngine/Window.java +++ b/GameEngine/src/main/java/gameEngine/Window.java @@ -1,5 +1,8 @@ package gameEngine; +import gameEngine.gameComponent.ColorController; +import gameEngine.gameComponent.GameComponent; import gameEngine.input.*; +import gameEngine.scene.*; import org.lwjgl.*; import org.lwjgl.glfw.*; @@ -18,13 +21,36 @@ public class Window { private static Window window; - private int width = 800; - private int height = 600; - String title = "HelloWorld"; + private static Scene currentScene; + private int width; + private int height; + private String title; private long glfwWindow; - private final List iGameComponents = new ArrayList(); + public float r,g,b,a; + private final List gameComponents = new ArrayList<>(); private Window() { + this.width = 1200; + this.height = 900; + this.title = "HelloWorld"; + r = 1; + g = 1; + b = 1; + a = 0; + } + + public static void changeScene(int newScene){ + switch (newScene){ + case 0: + currentScene = new EditorScene(); + break; + case 1: + currentScene = new GameScene(); + break; + default: + assert false : "Unknown Scene [" + newScene + "]"; + break; + } } public static Window get() { @@ -39,25 +65,28 @@ System.out.println("Hello LWJGL " + Version.getVersion() + "!"); init(); loop(); - glfwFreeCallbacks(this.glfwWindow); - glfwDestroyWindow(this.glfwWindow); + glfwFreeCallbacks(glfwWindow); + glfwDestroyWindow(glfwWindow); glfwTerminate(); glfwSetErrorCallback(null).free(); } private void init() { GLFWErrorCallback.createPrint(System.err).set(); - if ( !glfwInit() ) + if ( !glfwInit() ) { throw new IllegalStateException("Unable to initialize GLFW"); + } + //Configure 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 ) + glfwWindow = glfwCreateWindow(this.width, this.height, this.title, NULL, NULL); + if ( glfwWindow == NULL ) { throw new RuntimeException("Failed to create the GLFW window"); + } glfwSetCursorPosCallback(glfwWindow, MouseInput::mousePosCallback); glfwSetMouseButtonCallback(glfwWindow, MouseInput::mouseButtonCallback); @@ -70,49 +99,56 @@ IntBuffer pHeight = stack.mallocInt(1); // int* // Get the window size passed to glfwCreateWindow - glfwGetWindowSize(this.glfwWindow, pWidth, pHeight); + glfwGetWindowSize(glfwWindow, pWidth, pHeight); // Get the resolution of the primary monitor GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); // Center the window glfwSetWindowPos( - this.glfwWindow, + glfwWindow, (vidmode.width() - pWidth.get(0)) / 2, (vidmode.height() - pHeight.get(0)) / 2 ); } - - glfwMakeContextCurrent(this.glfwWindow); + glfwMakeContextCurrent(glfwWindow); glfwSwapInterval(1); - glfwShowWindow(this.glfwWindow); + glfwShowWindow(glfwWindow); - //-------------------------------------------------------------- - - iGameComponents.add(new ColorController()); - for (IGameComponent gameComponent : iGameComponents) { + gameComponents.add(new ColorController()); + for (GameComponent gameComponent : gameComponents) { gameComponent.init(); } - //-------------------------------------------------------------- + GL.createCapabilities(); + + Window.changeScene(0); } private void loop() { - GL.createCapabilities(); + double beginTime = Time.getTime(); + double endTime; + double dt = -1.0f; - while (!glfwWindowShouldClose(this.glfwWindow)) { - //--------------------------------------------------------------- + while (!glfwWindowShouldClose(glfwWindow)) { + glfwPollEvents(); // ウィンドウイベントをポーリング + glClearColor(r, g, b, a); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア - //GameComponentのupdate処理 - for (IGameComponent gameComponents : iGameComponents) { + for (GameComponent gameComponents : gameComponents) { gameComponents.update(); } - //--------------------------------------------------------------- - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア - glfwSwapBuffers(this.glfwWindow); // カラーバッファを交換 + if(dt >= 0){ + currentScene.update((float)dt); + } + glfwSwapBuffers(glfwWindow); // カラーバッファを交換 + + endTime = Time.getTime(); + dt = endTime - beginTime; + beginTime = endTime; } } diff --git a/GameEngine/src/main/java/gameEngine/gameComponent/ColorController.java b/GameEngine/src/main/java/gameEngine/gameComponent/ColorController.java new file mode 100644 index 0000000..15f1abd --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/gameComponent/ColorController.java @@ -0,0 +1,67 @@ +package gameEngine.gameComponent; +import gameEngine.input.*; + +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; + +public class ColorController extends GameComponent { + private float red; + private float green; + private float blue; + + public void init() { + this.setColor(0.0F, 0.0F, 0.0F); + } + + public void update() { + if (Input.GetKey(GLFW_KEY_R)) { + this.setColor(1.0F, 0.0F, 0.0F); + System.out.println("R"); + } + + if (Input.GetKeyUp(GLFW_KEY_G)) { + this.setColor(0.0F, 1.0F, 0.0F); + System.out.println("G"); + } + + if (Input.GetKeyDown(GLFW_KEY_B)) { + this.setColor(0.0F, 0.0F, 1.0F); + System.out.println("B"); + } + + if (Input.GetMouseButton(0)) { + setColor(1.0f, 0.0f, 0.0f); + System.out.println("Red"); + } + if (Input.GetMouseButtonUp(1)) { + setColor(0.0f, 0.0f, 1.0f); + System.out.println("Blue"); + } + if (Input.GetMouseButtonDown(2)) { + setColor(0.0f, 1.0f, 0.0f); + System.out.println("Green"); + } + + 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; + } + + +} diff --git a/GameEngine/src/main/java/gameEngine/gameComponent/GameComponent.java b/GameEngine/src/main/java/gameEngine/gameComponent/GameComponent.java new file mode 100644 index 0000000..1b509be --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/gameComponent/GameComponent.java @@ -0,0 +1,11 @@ +package gameEngine.gameComponent; + +public abstract class GameComponent { + public void init() { + + } + + public void update() { + + } +} diff --git a/GameEngine/src/main/java/gameEngine/input/MouseInput.java b/GameEngine/src/main/java/gameEngine/input/MouseInput.java index 793dd5b..ab1a2b7 100644 --- a/GameEngine/src/main/java/gameEngine/input/MouseInput.java +++ b/GameEngine/src/main/java/gameEngine/input/MouseInput.java @@ -7,9 +7,9 @@ private static MouseInput instance; private double scrollX,scrollY; private double xPos,yPos,lastX,lastY; - private boolean mouseButtonPressed[] = new boolean[3]; // 現在押されているか - private boolean mouseButtonDown[] = new boolean[3]; // 押された瞬間 - private boolean mouseButtonUp[] = new boolean[3]; // 離された瞬間 + private final boolean[] mouseButtonPressed = new boolean[3]; // 現在押されているか + private final boolean[] mouseButtonDown = new boolean[3]; // 押された瞬間 + private final boolean[] mouseButtonUp = new boolean[3]; // 離された瞬間 private boolean isDragging; private MouseInput(){ diff --git a/GameEngine/src/main/java/gameEngine/scene/EditorScene.java b/GameEngine/src/main/java/gameEngine/scene/EditorScene.java new file mode 100644 index 0000000..3145b1c --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/scene/EditorScene.java @@ -0,0 +1,34 @@ +package gameEngine.scene; + +import gameEngine.*; +import gameEngine.input.*; + +import java.awt.event.KeyEvent; + +public class EditorScene extends Scene { + + private boolean changingScene = false; + private float timeToChangeScene = 2.0f; + public EditorScene(){ + System.out.println("Active Editor scene"); + } + + @Override + public void update(float dt) { + //System.out.println("" + (1.0f / dt) + "FPS"); + + if(!changingScene && Input.GetKeyDown(KeyEvent.VK_SPACE)){ + changingScene = true; + } + + if(changingScene && timeToChangeScene > 0){ + timeToChangeScene -= dt; + Window.get().r -= dt * 5.0f; + Window.get().g -= dt * 5.0f; + Window.get().b -= dt * 5.0f; + } + else if(changingScene){ + Window.changeScene(1); + } + } +} diff --git a/GameEngine/src/main/java/gameEngine/scene/GameScene.java b/GameEngine/src/main/java/gameEngine/scene/GameScene.java new file mode 100644 index 0000000..89a9dba --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/scene/GameScene.java @@ -0,0 +1,18 @@ +package gameEngine.scene; + +import gameEngine.*; + +public class GameScene extends Scene { + public GameScene(){ + System.out.println("Active Game scene"); + Window.get().r = 1; + Window.get().g = 1; + Window.get().b = 1; + } + + @Override + public void update(float dt) { + + } + +} diff --git a/GameEngine/src/main/java/gameEngine/scene/Scene.java b/GameEngine/src/main/java/gameEngine/scene/Scene.java new file mode 100644 index 0000000..d53c91d --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/scene/Scene.java @@ -0,0 +1,10 @@ +package gameEngine.scene; + +public abstract class Scene { + + public Scene(){ + + } + + public abstract void update(float dt); +}