diff --git a/GameEngine/resources/test.png b/GameEngine/resources/test.png new file mode 100644 index 0000000..c0e8f4b --- /dev/null +++ b/GameEngine/resources/test.png Binary files differ diff --git a/GameEngine/src/main/java/Main.java b/GameEngine/src/main/java/Main.java index d19dfc9..db7e308 100644 --- a/GameEngine/src/main/java/Main.java +++ b/GameEngine/src/main/java/Main.java @@ -1,4 +1,4 @@ -import gameEngine.*; +import gameEngine.views.Window; public class Main { public static void main(String[] args) { diff --git a/GameEngine/src/main/java/gameEngine/Window.java b/GameEngine/src/main/java/gameEngine/Window.java deleted file mode 100644 index 0ff3001..0000000 --- a/GameEngine/src/main/java/gameEngine/Window.java +++ /dev/null @@ -1,156 +0,0 @@ -package gameEngine; -import gameEngine.gameComponent.ColorController; -import gameEngine.gameComponent.GameComponent; -import gameEngine.input.*; -import gameEngine.scene.*; - -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 static Scene currentScene; - private int width; - private int height; - private String title; - private long glfwWindow; - 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() { - if (window == null) { - window = new Window(); - } - - return window; - } - - public void run() { - System.out.println("Hello LWJGL " + Version.getVersion() + "!"); - init(); - loop(); - glfwFreeCallbacks(glfwWindow); - glfwDestroyWindow(glfwWindow); - glfwTerminate(); - glfwSetErrorCallback(null).free(); - } - - private void init() { - GLFWErrorCallback.createPrint(System.err).set(); - 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); - - 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); - glfwSetScrollCallback(glfwWindow, MouseInput::mouseScrollCallBack); - glfwSetKeyCallback(glfwWindow, KeyInput::keyCallback); - - // 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(glfwWindow, pWidth, pHeight); - - // Get the resolution of the primary monitor - GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); - - // Center the window - glfwSetWindowPos( - glfwWindow, - (vidmode.width() - pWidth.get(0)) / 2, - (vidmode.height() - pHeight.get(0)) / 2 - ); - } - - glfwMakeContextCurrent(glfwWindow); - glfwSwapInterval(1); - glfwShowWindow(glfwWindow); - - gameComponents.add(new ColorController()); - for (GameComponent gameComponent : gameComponents) { - gameComponent.init(); - } - GL.createCapabilities(); - - Window.changeScene(0); - } - - private void loop() { - double beginTime = Time.getTime(); - double endTime; - double dt = -1.0f; - - while (!glfwWindowShouldClose(glfwWindow)) { - - glfwPollEvents(); // ウィンドウイベントをポーリング - glClearColor(r, g, b, a); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア - - for (GameComponent gameComponents : gameComponents) { - gameComponents.update(); - } - - if(dt >= 0){ - currentScene.update((float)dt); - } - - glfwSwapBuffers(glfwWindow); // カラーバッファを交換 - - endTime = Time.getTime(); - dt = endTime - beginTime; - beginTime = endTime; - } - - } - -} \ No newline at end of file diff --git a/GameEngine/src/main/java/gameEngine/entites/Camera.java b/GameEngine/src/main/java/gameEngine/entites/Camera.java new file mode 100644 index 0000000..99900bc --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/entites/Camera.java @@ -0,0 +1,5 @@ +package gameEngine.entites; + +public class Camera extends Entity{ + +} diff --git a/GameEngine/src/main/java/gameEngine/entites/Entity.java b/GameEngine/src/main/java/gameEngine/entites/Entity.java new file mode 100644 index 0000000..4a30fcb --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/entites/Entity.java @@ -0,0 +1,12 @@ +package gameEngine.entites; + +import gameEngine.geometry.Transform; + +public class Entity { + + public Transform transform = new Transform(); + + public Entity() { + } + +} diff --git a/GameEngine/src/main/java/gameEngine/entites/GameObject.java b/GameEngine/src/main/java/gameEngine/entites/GameObject.java new file mode 100644 index 0000000..0101214 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/entites/GameObject.java @@ -0,0 +1,31 @@ +package gameEngine.entites; + +import gameEngine.entites.gameComponents.GameComponent; + +import java.util.ArrayList; +import java.util.List; + +public class GameObject extends Entity { + public boolean active = true; + private final List gameComponents = new ArrayList<>(); + + public void addComponent(GameComponent component) { + this.gameComponents.add(component); + } + + public void initComponents() { + for (GameComponent component : gameComponents) { + component.init(); + } + } + + public void updateComponents() { + for (GameComponent component : gameComponents) { + component.update(); + } + } + + public void setActive(boolean active) { + this.active = active; + } +} \ No newline at end of file diff --git a/GameEngine/src/main/java/gameEngine/entites/gameComponents/ColorController.java b/GameEngine/src/main/java/gameEngine/entites/gameComponents/ColorController.java new file mode 100644 index 0000000..81bea9d --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/entites/gameComponents/ColorController.java @@ -0,0 +1,68 @@ +package gameEngine.entites.gameComponents; +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() { + System.out.println("in Component"); + 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/entites/gameComponents/GameComponent.java b/GameEngine/src/main/java/gameEngine/entites/gameComponents/GameComponent.java new file mode 100644 index 0000000..cc72d5a --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/entites/gameComponents/GameComponent.java @@ -0,0 +1,11 @@ +package gameEngine.entites.gameComponents; + +public abstract class GameComponent { + public void init() { + + } + + public void update() { + + } +} diff --git a/GameEngine/src/main/java/gameEngine/entites/gameComponents/Mesh.java b/GameEngine/src/main/java/gameEngine/entites/gameComponents/Mesh.java new file mode 100644 index 0000000..1f7c955 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/entites/gameComponents/Mesh.java @@ -0,0 +1,86 @@ +package gameEngine.entites.gameComponents; + +import gameEngine.entites.GameObject; +import gameEngine.views.Window; +import static org.lwjgl.opengl.GL11.*; + +public class Mesh extends GameComponent { + + public enum MeshType { + SPRITE, CUBE + } + + private final MeshType type; + private Texture texture; // スプライトのテクスチャを保持 + private final GameObject parent; // 親のオブジェクトを持つ + + private int spriteWidth = 64; // スプライト幅(今後指定可能にする) + private int spriteHeight = 64; // スプライト高さ(今後指定可能にする) + + public Mesh(GameObject parent, MeshType type, String texturePath) { + this.parent = parent; + this.type = type; + if (type == MeshType.SPRITE) { + this.texture = new Texture("GameEngine/resources/" + texturePath); // Load the image + } + } + + @Override + public void init() { + } + + @Override + public void update() { + if (type == MeshType.SPRITE) { + renderSprite(); // スプライトの描画 + } + } + + private void renderSprite() { + // 2Dの描画モードに切り替え + glMatrixMode(GL_PROJECTION);// 投影行列の設定モードに切り替え + glPushMatrix(); // 現在の投影行列を保存 + glLoadIdentity(); // 単位行列をロードして投影行列をリセット + + // ピクセル単位の座標系に設定 (画面左上が原点、画面サイズはWindowの幅と高さ) + /* + x軸: 左が0、右がWindow.get().width(ウィンドウの幅) + y軸: 上が0、下がWindow.get().height(ウィンドウの高さ) + z軸: -1から1の範囲で設定 + */ + glOrtho(0, Window.get().width, Window.get().height, 0, -1, 1); + + // モデルビュー行列に切り替えて、描画する位置を決める + glMatrixMode(GL_MODELVIEW); // モデルビュー行列の設定モードに切り替え + glPushMatrix(); // 現在のモデルビュー行列を保存 + glLoadIdentity(); // 単位行列をロードしてモデルビュー行列をリセット + + // テクスチャをバインド (テクスチャIDをOpenGLに設定) + glBindTexture(GL_TEXTURE_2D, texture.getId()); + + // テクスチャを有効にして、イメージの座標とサイズを指定 + glEnable(GL_TEXTURE_2D); // テクスチャを有効化 + glColor4f(1f, 1f, 1f, 1f);// 色を白に設定 (テクスチャそのままの色で表示) + + // 親のGameObjectの座標を取得 + float x = parent.transform.position.x; + float y = parent.transform.position.y; + + // 四角形を描画 (テクスチャ座標と画面座標を対応付け) + glBegin(GL_QUADS); // 四角形を描画する + glTexCoord2f(0, 0); glVertex2f(x, y); // 左上の頂点 (テクスチャ座標0,0と画面座標) + glTexCoord2f(1, 0); glVertex2f(x + spriteWidth, y); // 右上の頂点 (テクスチャ座標1,0と画面座標) + glTexCoord2f(1, 1); glVertex2f(x + spriteWidth, y + spriteHeight); // 右下の頂点 (テクスチャ座標1,1と画面座標) + glTexCoord2f(0, 1); glVertex2f(x, y + spriteHeight); // 左下の頂点 (テクスチャ座標0,1と画面座標) + glEnd(); // 四角形の描画を終了 + + // テクスチャの使用を終了 + glDisable(GL_TEXTURE_2D); // テクスチャの無効化 + glBindTexture(GL_TEXTURE_2D, 0); // テクスチャのバインドを解除 + + // 行列を元に戻す (PushしたものをPopで復元) + glPopMatrix(); // モデルビュー行列を元に戻す + glMatrixMode(GL_PROJECTION); // 投影行列の設定モードに切り替え + glPopMatrix(); // 投影行列を元に戻す + } +} \ No newline at end of file diff --git a/GameEngine/src/main/java/gameEngine/entites/gameComponents/Texture.java b/GameEngine/src/main/java/gameEngine/entites/gameComponents/Texture.java new file mode 100644 index 0000000..5cb612d --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/entites/gameComponents/Texture.java @@ -0,0 +1,94 @@ +package gameEngine.entites.gameComponents; + +import org.lwjgl.BufferUtils; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; + +import static org.lwjgl.opengl.GL11.*; + +public class Texture { + + /* + 指定された画像ファイルを読み込み、そのピクセルデータをOpenGLにアップロードして + テクスチャとして利用できるようにする + */ + + private int id; //OpenGLのテクスチャID + private int width; // 読み込んだ画像の幅 + private int height; // 読み込んだ画像の高さ + + public Texture(String path) { + BufferedImage bi; + try { + //ImageIO.read(new File(path)): 画像ファイルを読み込み、BufferedImageオブジェクトを作成 + //BufferedImageはJava標準ライブラリのクラス + bi = ImageIO.read(new File(path)); + width = bi.getWidth(); + height = bi.getHeight(); + + //画像のピクセルデータをRGB形式で取得 + //pixelsRawは整数配列で、各ピクセルの色データが格納される + int[] pixelsRaw = bi.getRGB(0, 0, width, height, null, 0, width); + + //BufferUtils.createByteBuffer(width * height * 4): OpenGLに送信するためのバッファ(ByteBuffer)を作成 + ByteBuffer pixels = BufferUtils.createByteBuffer(width * height * 4); + + // 各ピクセルを取り出し、色成分(R, G, B, A)を抽出してByteBufferに格納 + // (pixel >> 16) & 0xFF: ピクセルデータの上位16ビットを右にシフトし、赤色成分を抽出。 + // 同様に緑(8ビット右シフト)、青(そのまま)、アルファ(24ビット右シフト)も抽出 + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + int pixel = pixelsRaw[y * width + x]; + pixels.put((byte) ((pixel >> 16) & 0xFF)); // Red + pixels.put((byte) ((pixel >> 8) & 0xFF)); // Green + pixels.put((byte) (pixel & 0xFF)); // Blue + pixels.put((byte) ((pixel >> 24) & 0xFF)); // Alpha + } + } + // ByteBufferを読み込み可能な状態に + pixels.flip(); + + // 新しいテクスチャIDを生成 + id = glGenTextures(); + + // 生成したテクスチャIDを現在のテクスチャとしてバインド + // 以降のテクスチャ関連の操作はこのテクスチャに対して行われる + glBindTexture(GL_TEXTURE_2D, id); + + // テクスチャデータをOpenGLに送信 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + + //テクスチャのフィルタリング方法を設定 + //ニアレストフィルタリング:拡大縮小時のぼやけがなくなる + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + // テクスチャのバインディングを解除 + glBindTexture(GL_TEXTURE_2D, 0); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + public int getId() { + return id; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public void delete() { + //テクスチャを削除し、OpenGLからリソースを解放します。 + glDeleteTextures(id); + } +} \ No newline at end of file diff --git a/GameEngine/src/main/java/gameEngine/gameComponent/ColorController.java b/GameEngine/src/main/java/gameEngine/gameComponent/ColorController.java deleted file mode 100644 index 15f1abd..0000000 --- a/GameEngine/src/main/java/gameEngine/gameComponent/ColorController.java +++ /dev/null @@ -1,67 +0,0 @@ -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 deleted file mode 100644 index 1b509be..0000000 --- a/GameEngine/src/main/java/gameEngine/gameComponent/GameComponent.java +++ /dev/null @@ -1,11 +0,0 @@ -package gameEngine.gameComponent; - -public abstract class GameComponent { - public void init() { - - } - - public void update() { - - } -} diff --git a/GameEngine/src/main/java/gameEngine/geometry/Transform.java b/GameEngine/src/main/java/gameEngine/geometry/Transform.java new file mode 100644 index 0000000..5243b02 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/geometry/Transform.java @@ -0,0 +1,34 @@ +package gameEngine.geometry; + +import org.joml.Vector3f; + +public class Transform { + + public Vector3f position; + public Vector3f rotation; + public Vector3f scale; + + public Transform() { + this.position = new Vector3f(0, 0, 0); + this.rotation = new Vector3f(0, 0, 0); + this.scale = new Vector3f(1, 1, 1); + } + + public void setPosition(float x, float y, float z) { + position.x = x; + position.y = y; + position.z = z; + } + + public void setRotation(float x, float y, float z) { + rotation.x = x; + rotation.y = y; + rotation.z = z; + } + + public void setScale(float x, float y, float z) { + scale.x = x; + scale.y = y; + scale.z = z; + } +} diff --git a/GameEngine/src/main/java/gameEngine/scene/EditorScene.java b/GameEngine/src/main/java/gameEngine/scene/EditorScene.java deleted file mode 100644 index 3145b1c..0000000 --- a/GameEngine/src/main/java/gameEngine/scene/EditorScene.java +++ /dev/null @@ -1,34 +0,0 @@ -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 deleted file mode 100644 index 89a9dba..0000000 --- a/GameEngine/src/main/java/gameEngine/scene/GameScene.java +++ /dev/null @@ -1,18 +0,0 @@ -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 deleted file mode 100644 index d53c91d..0000000 --- a/GameEngine/src/main/java/gameEngine/scene/Scene.java +++ /dev/null @@ -1,10 +0,0 @@ -package gameEngine.scene; - -public abstract class Scene { - - public Scene(){ - - } - - public abstract void update(float dt); -} diff --git a/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java b/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java new file mode 100644 index 0000000..4a0aaa9 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java @@ -0,0 +1,43 @@ +package gameEngine.scenes; + +import gameEngine.input.*; +import gameEngine.views.Window; + +import java.awt.event.KeyEvent; + +import static org.lwjgl.opengl.GL11.glClearColor; + +public class EditorScene extends Scene { + + private boolean changingScene = false; + private float timeToChangeScene = 2.0f; + public float r,g,b,a; + public EditorScene(){ + System.out.println("Active Editor scene"); + r = 1; + g = 1; + b = 1; + a = 0; + } + + @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; + r -= dt * 5.0f; + g -= dt * 5.0f; + b -= dt * 5.0f; + } + else if(changingScene){ + Window.changeScene(1); + } + + glClearColor(r, g, b, a); + } +} diff --git a/GameEngine/src/main/java/gameEngine/scenes/GameScene.java b/GameEngine/src/main/java/gameEngine/scenes/GameScene.java new file mode 100644 index 0000000..21d40b6 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/scenes/GameScene.java @@ -0,0 +1,44 @@ +package gameEngine.scenes; + +import gameEngine.entites.GameObject; +import gameEngine.entites.gameComponents.ColorController; +import gameEngine.entites.gameComponents.Mesh; +import gameEngine.input.Input; + +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.glClearColor; + +public class GameScene extends Scene { + public GameScene(){ + System.out.println("Active Game scene"); + glClearColor(1, 1, 1, 0); + + gameObjects.put("0", new GameObject()); + gameObjects.get("0").addComponent(new ColorController()); + + gameObjects.put("sprite", new GameObject()); + gameObjects.get("sprite").addComponent(new Mesh(gameObjects.get("sprite"), Mesh.MeshType.SPRITE, "test.png")); + } + + @Override + public void update(float dt) { + + if (Input.GetKey(GLFW_KEY_W)) { + float y = gameObjects.get("sprite").transform.position.y; + gameObjects.get("sprite").transform.setPosition(gameObjects.get("sprite").transform.position.x, y - 1, 0); + } + if (Input.GetKey(GLFW_KEY_A)) { + float x = gameObjects.get("sprite").transform.position.x; + gameObjects.get("sprite").transform.setPosition(x - 1, gameObjects.get("sprite").transform.position.y, 0); + } + if (Input.GetKey(GLFW_KEY_S)) { + float y = gameObjects.get("sprite").transform.position.y; + gameObjects.get("sprite").transform.setPosition(gameObjects.get("sprite").transform.position.x, y + 1, 0); + } + if (Input.GetKey(GLFW_KEY_D)) { + float x = gameObjects.get("sprite").transform.position.x; + gameObjects.get("sprite").transform.setPosition(x + 1, gameObjects.get("sprite").transform.position.y, 0); + } + } + +} diff --git a/GameEngine/src/main/java/gameEngine/scenes/Scene.java b/GameEngine/src/main/java/gameEngine/scenes/Scene.java new file mode 100644 index 0000000..d91a8c0 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/scenes/Scene.java @@ -0,0 +1,15 @@ +package gameEngine.scenes; + +import gameEngine.entites.GameObject; + +import java.util.HashMap; + +public abstract class Scene { + + public HashMap gameObjects = new HashMap<>(); + + public Scene(){ + } + + public abstract void update(float dt); +} diff --git a/GameEngine/src/main/java/gameEngine/views/Window.java b/GameEngine/src/main/java/gameEngine/views/Window.java new file mode 100644 index 0000000..beeff65 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/views/Window.java @@ -0,0 +1,154 @@ +package gameEngine.views; +import gameEngine.Time; +import gameEngine.entites.GameObject; +import gameEngine.input.*; +import gameEngine.scenes.*; + +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 Window { + private static Window window; + private static Scene currentScene; + public int width; + public int height; + private String title; + private long glfwWindow; + + + private Window() { + this.width = 1200; + this.height = 900; + this.title = "HelloWorld"; + + } + + public static void changeScene(int newScene){ + switch (newScene){ + case 0: + currentScene = new EditorScene(); + //現在のシーンのゲームコンポーネントのinit + for (GameObject obj : currentScene.gameObjects.values()) { + obj.initComponents(); + } + break; + case 1: + currentScene = new GameScene(); + //現在のシーンのゲームコンポーネントのinit + for (GameObject obj : currentScene.gameObjects.values()) { + obj.initComponents(); + } + break; + default: + assert false : "Unknown Scene [" + newScene + "]"; + break; + } + } + + 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(glfwWindow); + glfwDestroyWindow(glfwWindow); + glfwTerminate(); + glfwSetErrorCallback(null).free(); + } + + private void init() { + GLFWErrorCallback.createPrint(System.err).set(); + 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); + + 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); + glfwSetScrollCallback(glfwWindow, MouseInput::mouseScrollCallBack); + glfwSetKeyCallback(glfwWindow, KeyInput::keyCallback); + + // 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(glfwWindow, pWidth, pHeight); + + // Get the resolution of the primary monitor + GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); + + // Center the window + glfwSetWindowPos( + glfwWindow, + (vidmode.width() - pWidth.get(0)) / 2, + (vidmode.height() - pHeight.get(0)) / 2 + ); + } + + glfwMakeContextCurrent(glfwWindow); + glfwSwapInterval(1); + glfwShowWindow(glfwWindow); + + GL.createCapabilities(); + + Window.changeScene(0); + } + + private void loop() { + double beginTime = Time.getTime(); + double endTime; + double dt = -1.0f; + + while (!glfwWindowShouldClose(glfwWindow)) { + + glfwPollEvents(); // ウィンドウイベントをポーリング + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア + + if(dt >= 0){ + currentScene.update((float)dt); + + //現在のシーンのゲームコンポーネントのUpdate + for (GameObject obj : currentScene.gameObjects.values()) { + obj.updateComponents(); + } + } + + glfwSwapBuffers(glfwWindow); // カラーバッファを交換 + + endTime = Time.getTime(); + dt = endTime - beginTime; + beginTime = endTime; + } + + } + +} \ No newline at end of file