diff --git a/src/main/java/GLWindow.java b/src/main/java/GLWindow.java new file mode 100644 index 0000000..fc174aa --- /dev/null +++ b/src/main/java/GLWindow.java @@ -0,0 +1,81 @@ +import entities.GLConfigVariable; +import org.lwjgl.glfw.GLFWErrorCallback; +import org.lwjgl.opengl.GL; + +import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks; +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.system.MemoryUtil.NULL; + +//--------------------------------------------------------------- +// ウィンドウ +public class GLWindow { + + private long hWnd; // ウィンドウハンドル + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public void init() { + initWindow(); + initRender(); + } + + //--------------------------------------------------------------- + // 画面のスワップ + public void swapWindow() { + glfwSwapBuffers(hWnd); // バッファのスワップ + glfwPollEvents(); // 入力とかイベントの取得 + } + + //--------------------------------------------------------------- + // ウィンドウの破棄 + public void destroyWindow() { + glfwFreeCallbacks(hWnd); // ウィンドウコールバックの解放 + glfwDestroyWindow(hWnd); // ウィンドウの破棄 + glfwTerminate(); // GLFWの破棄 + glfwSetErrorCallback(null).free(); // エラーコールバックの解放 + } + + //--------------------------------------------------------------- + // ウィンドウが起動しているかどうか + public boolean windowShouldClose() { + return glfwWindowShouldClose(hWnd); + } + + //--------------------------------------------------------------- + // ウィンドウの初期化 + private void initWindow() { + GLFWErrorCallback.createPrint(System.err).set(); // エラーコールバックの設定 + + + // GLFWの初期化 + if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW"); + + // ウィンドウの設定 + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + + // ウィンドウの作成 + hWnd = glfwCreateWindow(GLConfigVariable.WIDTH, GLConfigVariable.HEIGHT, GLConfigVariable.TITLE_NAME, GLConfigVariable.IS_FULL_SCREEN ? glfwGetPrimaryMonitor() : NULL, NULL); + if (hWnd == NULL) + throw new RuntimeException("Failed to create the window."); + + glfwMakeContextCurrent(hWnd); //起動したウィンドウをターゲットに + glfwSwapInterval(1); // v-syncの適応 + + glfwShowWindow(hWnd); // ウィンドウの表示 + } + + //--------------------------------------------------------------- + // 描画プロセス初期化 + private void initRender() { + GL.createCapabilities(); + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // 背景色設定 + + // OpenGLの初期化 + glEnable(GL_TEXTURE_2D); // 二次元テクスチャの有効化 + glEnable(GL_BLEND); // アルファブレンディングの有効化 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // ブレンドモードの設定 + } + //--------------------------------------------------------------- +} diff --git a/src/main/java/GameEngine.java b/src/main/java/GameEngine.java new file mode 100644 index 0000000..50627dc --- /dev/null +++ b/src/main/java/GameEngine.java @@ -0,0 +1,46 @@ +import static org.lwjgl.opengl.GL11.*; + +//--------------------------------------------------------------- +// ゲームループ提供 +public abstract class GameEngine { + + private GLWindow glWindow = new GLWindow(); + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // 初期化 + protected void init() { + } + + //--------------------------------------------------------------- + // ゲームループ + protected void update() { + } + + //--------------------------------------------------------------- + // 破棄処理 + protected void destroy() { + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // 実行系の本体 + protected void run() { + + // 初期化 + glWindow.init(); + init(); + + // メインループ + while (!glWindow.windowShouldClose()) { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // バッファのクリア + update(); + glWindow.swapWindow(); + } + + // 終了処理 + destroy(); // デストラクタ + glWindow.destroyWindow(); // ウィンドウの破棄 + } + //--------------------------------------------------------------- +} diff --git a/src/main/java/JumpGame.java b/src/main/java/JumpGame.java index 74404d0..86e3f11 100644 --- a/src/main/java/JumpGame.java +++ b/src/main/java/JumpGame.java @@ -25,7 +25,6 @@ //--------------------------------------------------------------- // public void gravity(double y) { -// this.force.gravity(y); // this.time.gravity(y); PlayerModel playerModel = (PlayerModel) models.get(ModelType.PLAYER_MODEL); playerModel.updateGravity(y); @@ -61,7 +60,7 @@ // // マイフレーム更新 -// gravity(0.01); //重力 + gravity(-256); //重力 } //--------------------------------------------------------------- diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 10c462a..445b251 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,4 +1,4 @@ -import entities.WindowConfig; +import entities.GLConfigVariable; import org.lwjgl.glfw.*; import org.lwjgl.opengl.*; @@ -7,142 +7,40 @@ import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.system.MemoryUtil.*; -public class Main { +//--------------------------------------------------------------- +// 実行本体 +public class Main extends GameEngine { - private long variableYieldTime, lastTime; - // ウィンドウハンドル - private long hWnd; - - // private JumpGame jumpGame = new JumpGame(); - // フルスクリーン - public boolean fullScreen; - //--------------------------------------------------------------- //--------------------------------------------------------------- // エントリーポイント public static void main(String[] args) { - new Main().run(false); + new Main().run(); } //--------------------------------------------------------------- - // メイン関数 - public void run(boolean fullScreen) { - // フルスクリーンの真偽値を貰う - this.fullScreen = fullScreen; - - // 色々初期化 - initWindow(); - initRender(); + //--------------------------------------------------------------- + // 初期化 + @Override + protected void init(){ jumpGame.init(); - - // メインループ - while (!glfwWindowShouldClose(hWnd)) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // バッファのクリア - - jumpGame.update(); - - glfwSwapBuffers(hWnd); // バッファのスワップ - glfwPollEvents(); // 入力とかイベントの取得 - - } - - // 終了処理 - delete(); // デストラクタ - glfwFreeCallbacks(hWnd); // ウィンドウコールバックの解放 - glfwDestroyWindow(hWnd); // ウィンドウの破棄 - glfwTerminate(); // GLFWの破棄 - glfwSetErrorCallback(null).free(); // エラーコールバックの解放 } //--------------------------------------------------------------- - //--------------------------------------------------------------- - // ウィンドウの初期化 - private void initWindow() { - GLFWErrorCallback.createPrint(System.err).set(); // エラーコールバックの設定 - - - // GLFWの初期化 - if (!glfwInit()) - throw new IllegalStateException("Unable to initialize GLFW"); - - // ウィンドウの設定 - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - - // ウィンドウの作成 - hWnd = glfwCreateWindow(WindowConfig.WIDTH, WindowConfig.HEIGHT, WindowConfig.TITLE_NAME, fullScreen ? glfwGetPrimaryMonitor() : NULL, NULL); - if (hWnd == NULL) - throw new RuntimeException("Failed to create the window."); - - // ウィンドウをタゲにするのか? - glfwMakeContextCurrent(hWnd); - // v-syncの適応 - glfwSwapInterval(1); - - // ウィンドウの表示 - glfwShowWindow(hWnd); - - + // 更新処理 + @Override + protected void update(){ + jumpGame.update(); } //--------------------------------------------------------------- - // 描画プロセス初期化 - private void initRender() { - GL.createCapabilities(); - glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // 背景色設定 - - // OpenGLの初期化 - glEnable(GL_TEXTURE_2D); // 二次元テクスチャの有効化 - glEnable(GL_BLEND); // アルファブレンディングの有効化 - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // ブレンドモードの設定 - } - - //--------------------------------------------------------------- - // デストラクタ - public void delete() { + // 破棄 + @Override + protected void destroy() { jumpGame.delete(); } //--------------------------------------------------------------- - // - private void sync(int fps) { - if (fps <= 0) return; - - long sleepTime = 1000000000 / fps; // nanoseconds to sleep this frame - // yieldTime + remainder micro & nano seconds if smaller than sleepTime - long yieldTime = Math.min(sleepTime, variableYieldTime + sleepTime % (1000*1000)); - long overSleep = 0; // time the sync goes over by - - try { - while (true) { - long t = System.nanoTime() - lastTime; - - if (t < sleepTime - yieldTime) { - Thread.sleep(1); - }else if (t < sleepTime) { - // burn the last few CPU cycles to ensure accuracy - Thread.yield(); - }else { - overSleep = t - sleepTime; - break; // exit while loop - } - } - } catch (InterruptedException e) { - e.printStackTrace(); - }finally{ - lastTime = System.nanoTime() - Math.min(overSleep, sleepTime); - - // auto tune the time sync should yield - if (overSleep > variableYieldTime) { - // increase by 200 microseconds (1/5 a ms) - variableYieldTime = Math.min(variableYieldTime + 200*1000, sleepTime); - } - else if (overSleep < variableYieldTime - 200*1000) { - // decrease by 2 microseconds - variableYieldTime = Math.max(variableYieldTime - 2*1000, 0); - } - } - } - //--------------------------------------------------------------- } \ No newline at end of file diff --git a/src/main/java/entities/Acceleration.java b/src/main/java/entities/Acceleration.java index 68ee34c..0d80c79 100644 --- a/src/main/java/entities/Acceleration.java +++ b/src/main/java/entities/Acceleration.java @@ -1,41 +1,47 @@ package entities; public class Acceleration { - private double massValue; - private Pair forceValue; + private double massValue = 1.0; + private Pair forceValue = new Pair<>(0d, 0d); private Velocity velocity; private Onground onground; - private Pair value; + private Pair value = new Pair<>(0d, 0d); + //--------------------------------------------------------------- + // public void updateByMass(double mass) { this.massValue = mass; - Pair temp_l0; - if (this.onground.getOnground()) { - temp_l0 = new Pair((forceValue.getFirst() / mass), 0.0); - } else { - temp_l0 = new Pair((forceValue.getFirst() / mass), (forceValue.getSecond() / mass)); - } - value = temp_l0; + Pair temp_l0; + + if (this.onground.getOnground()) temp_l0 = new Pair((forceValue.getFirst() / mass), 0.0); + else temp_l0 = new Pair((forceValue.getFirst() / mass), (forceValue.getSecond() / mass)); + + this.value = temp_l0; velocity.updateByAcceleration(value); } + //--------------------------------------------------------------- + // public void updateByForce(Pair force) { this.forceValue = force; - Pair temp_l1; - if (this.onground.getOnground()) { - temp_l1 = new Pair((force.getFirst() / massValue), 0.0); - } else { - temp_l1 = new Pair((force.getFirst() / massValue), (force.getSecond() / massValue)); - } + Pair temp_l1; + + if (this.onground.getOnground()) temp_l1 = new Pair((force.getFirst() / massValue), 0.0); + temp_l1 = new Pair((force.getFirst() / massValue), (force.getSecond() / massValue)); + value = temp_l1; velocity.updateByAcceleration(value); } + //--------------------------------------------------------------- + //--------------------------------------------------------------- public Acceleration(Velocity velocity, Onground onground) { this.velocity = velocity; this.onground = onground; } + //--------------------------------------------------------------- + // public Pair getValue() { return value; } diff --git a/src/main/java/entities/Force.java b/src/main/java/entities/Force.java index 9e74af0..c6654b6 100644 --- a/src/main/java/entities/Force.java +++ b/src/main/java/entities/Force.java @@ -2,7 +2,7 @@ public class Force { private Acceleration acceleration; - private Pair value; + private Pair value = new Pair<>(0.0d, 0.0d); public Force(Acceleration acceleration) { this.acceleration = acceleration; diff --git a/src/main/java/entities/GLConfigVariable.java b/src/main/java/entities/GLConfigVariable.java new file mode 100644 index 0000000..adb3001 --- /dev/null +++ b/src/main/java/entities/GLConfigVariable.java @@ -0,0 +1,18 @@ +package entities; + +//--------------------------------------------------------------- +// 画面設定定数 +public class GLConfigVariable { + //--------------------------------------------------------------- + //--------------------------------------------------------------- + private GLConfigVariable() { + } + + //--------------------------------------------------------------- + public static final String TITLE_NAME = "JumpGame"; + public static final int WIDTH = 1280; + public static final int HEIGHT = 720; + public static final int DEPTH = 100; // 追加。深度 + public static final double TARGET_FPS = 60d; //フレームレート + public static final boolean IS_FULL_SCREEN = false; +} \ No newline at end of file diff --git a/src/main/java/entities/Image2D.java b/src/main/java/entities/Image2D.java index e089d0a..bbbc877 100644 --- a/src/main/java/entities/Image2D.java +++ b/src/main/java/entities/Image2D.java @@ -72,10 +72,10 @@ glMatrixMode(GL_PROJECTION); // 行列の設定 glLoadIdentity(); // 単位行列化 - glOrtho(0, WindowConfig.WIDTH, 0, WindowConfig.HEIGHT, -WindowConfig.DEPTH, WindowConfig.DEPTH); // 正射影投影 + glOrtho(0, GLConfigVariable.WIDTH, 0, GLConfigVariable.HEIGHT, -GLConfigVariable.DEPTH, GLConfigVariable.DEPTH); // 正射影投影 // ビューポートの範囲 - glViewport(0, 0, WindowConfig.WIDTH, WindowConfig.HEIGHT); + glViewport(0, 0, GLConfigVariable.WIDTH, GLConfigVariable.HEIGHT); // ポリゴンの色 glColor4d(color.getR(), color.getG(), color.getB(), alpha); diff --git a/src/main/java/entities/Move.java b/src/main/java/entities/Move.java index 18a5b6d..c7e92ac 100644 --- a/src/main/java/entities/Move.java +++ b/src/main/java/entities/Move.java @@ -2,7 +2,7 @@ public class Move { private Velocity velocity; - private Pair value; + private Pair value = new Pair<>(0d, 0d); public Move(Velocity velocity) { this.velocity = velocity; diff --git a/src/main/java/entities/Position.java b/src/main/java/entities/Position.java index 651c13c..4cef46d 100644 --- a/src/main/java/entities/Position.java +++ b/src/main/java/entities/Position.java @@ -4,13 +4,13 @@ // public class Position { private Ground ground; - private Pair value; + private Pair value = new Pair<>(0d, 0d); //--------------------------------------------------------------- //--------------------------------------------------------------- // public void updateByVelocity(Pair velocity) { - Pair temp_l3; + Pair temp_l3; if (((this.ground.getValue() == true) && ((this.value.getSecond() + (0.01 * velocity.getSecond())) < 0.0))) { temp_l3 = new Pair((this.value.getFirst() + (0.01 * velocity.getFirst())), 0.0); } else { diff --git a/src/main/java/entities/Velocity.java b/src/main/java/entities/Velocity.java index 22e9bf1..5c82b80 100644 --- a/src/main/java/entities/Velocity.java +++ b/src/main/java/entities/Velocity.java @@ -12,7 +12,7 @@ //--------------------------------------------------------------- public void updateByMove(Pair move) { this.moveValue = move; - Pair temp_l2; + Pair temp_l2; if ((this.onground.getOnground() && (move.getSecond() >= 0.0))) { temp_l2 = move; } else { diff --git a/src/main/java/entities/WindowConfig.java b/src/main/java/entities/WindowConfig.java deleted file mode 100644 index 0a93afb..0000000 --- a/src/main/java/entities/WindowConfig.java +++ /dev/null @@ -1,16 +0,0 @@ -package entities; - -//--------------------------------------------------------------- -// 画面設定定数 -public class WindowConfig { - //--------------------------------------------------------------- - //--------------------------------------------------------------- - private WindowConfig() { - } - - //--------------------------------------------------------------- - public static final String TITLE_NAME = "JumpGame"; - public static final int WIDTH = 1280; - public static final int HEIGHT = 720; - public static final int DEPTH = 100; // 追加。深度 -} \ No newline at end of file diff --git a/src/main/java/models/PlayerModel.java b/src/main/java/models/PlayerModel.java index e992429..af983bb 100644 --- a/src/main/java/models/PlayerModel.java +++ b/src/main/java/models/PlayerModel.java @@ -40,12 +40,13 @@ //--------------------------------------------------------------- // public void jump() { - this.move.moveY(jumpPower); + this.move.moveX(jumpPower); } //--------------------------------------------------------------- // public void updateGravity(double gravity) { this.force.gravity(gravity); + System.out.println("swapWindow Gravity"); } } diff --git a/src/main/java/views/TileMapRenderer.java b/src/main/java/views/TileMapRenderer.java index 803c731..5dd33e3 100644 --- a/src/main/java/views/TileMapRenderer.java +++ b/src/main/java/views/TileMapRenderer.java @@ -1,6 +1,6 @@ package views; -import entities.WindowConfig; +import entities.GLConfigVariable; import entities.Pair; import entities.TileType; import models.IModel; @@ -40,11 +40,11 @@ switch (tileType) { case CLOSE: - newTile = new TileRenderer("resources/tile.png", new Pair<>((double) WindowConfig.WIDTH, 0d), 2); + newTile = new TileRenderer("resources/tile.png", new Pair<>((double) GLConfigVariable.WIDTH, 0d), 2); break; case OPEN: - newTile = new TileRenderer("resources/hole.png", new Pair<>((double) WindowConfig.WIDTH, 0d),2); + newTile = new TileRenderer("resources/hole.png", new Pair<>((double) GLConfigVariable.WIDTH, 0d),2); break; } @@ -55,7 +55,7 @@ // 初期タイル作成 private void initTiles() { - for (double x = 0; x <= WindowConfig.WIDTH; x += 32 * newTile.getScaleValue()) { + for (double x = 0; x <= GLConfigVariable.WIDTH; x += 32 * newTile.getScaleValue()) { newTile = new TileRenderer("resources/tile.png", new Pair<>(x, offsetY)); newTile.setScaleValue(2); tiles.add(newTile);