Newer
Older
JumpingGame / src / main / java / GLWindow.java
import entities.config.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); // ブレンドモードの設定
    }
    //---------------------------------------------------------------
}