Search in sources :

Example 1 with GLFWWindowSizeCallback

use of org.lwjgl.glfw.GLFWWindowSizeCallback in project jmonkeyengine by jMonkeyEngine.

the class LwjglWindow method createContext.

/**
     * Apply the settings, changing resolution, etc.
     *
     * @param settings the settings to apply when creating the context.
     */
protected void createContext(final AppSettings settings) {
    glfwSetErrorCallback(errorCallback = new GLFWErrorCallback() {

        @Override
        public void invoke(int error, long description) {
            final String message = GLFWErrorCallback.getDescription(description);
            listener.handleError(message, new Exception(message));
        }

        @Override
        public void close() {
            super.close();
        }

        @Override
        public void callback(long args) {
            super.callback(args);
        }
    });
    if (!glfwInit()) {
        throw new IllegalStateException("Unable to initialize GLFW");
    }
    glfwDefaultWindowHints();
    if (settings.getRenderer().equals(AppSettings.LWJGL_OPENGL3)) {
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    } else {
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    }
    if (settings.getBoolean("RendererDebug")) {
        glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
    }
    if (settings.isGammaCorrection()) {
        glfwWindowHint(GLFW_SRGB_CAPABLE, GLFW_TRUE);
    }
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, settings.isResizable() ? GLFW_TRUE : GLFW_FALSE);
    glfwWindowHint(GLFW_DEPTH_BITS, settings.getDepthBits());
    glfwWindowHint(GLFW_STENCIL_BITS, settings.getStencilBits());
    glfwWindowHint(GLFW_SAMPLES, settings.getSamples());
    glfwWindowHint(GLFW_STEREO, settings.useStereo3D() ? GLFW_TRUE : GLFW_FALSE);
    glfwWindowHint(GLFW_REFRESH_RATE, settings.getFrequency());
    if (settings.getBitsPerPixel() == 24) {
        glfwWindowHint(GLFW_RED_BITS, 8);
        glfwWindowHint(GLFW_GREEN_BITS, 8);
        glfwWindowHint(GLFW_BLUE_BITS, 8);
    } else if (settings.getBitsPerPixel() == 16) {
        glfwWindowHint(GLFW_RED_BITS, 5);
        glfwWindowHint(GLFW_GREEN_BITS, 6);
        glfwWindowHint(GLFW_BLUE_BITS, 5);
    }
    glfwWindowHint(GLFW_ALPHA_BITS, settings.getAlphaBits());
    // TODO: Add support for monitor selection
    long monitor = NULL;
    if (settings.isFullscreen()) {
        monitor = glfwGetPrimaryMonitor();
    }
    final GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    if (settings.getWidth() <= 0 || settings.getHeight() <= 0) {
        settings.setResolution(videoMode.width(), videoMode.height());
    }
    window = glfwCreateWindow(settings.getWidth(), settings.getHeight(), settings.getTitle(), monitor, NULL);
    if (window == NULL) {
        throw new RuntimeException("Failed to create the GLFW window");
    }
    // Add a resize callback which delegates to the listener
    glfwSetWindowSizeCallback(window, windowSizeCallback = new GLFWWindowSizeCallback() {

        @Override
        public void invoke(final long window, final int width, final int height) {
            settings.setResolution(width, height);
            listener.reshape(width, height);
        }

        @Override
        public void close() {
            super.close();
        }

        @Override
        public void callback(long args) {
            super.callback(args);
        }
    });
    glfwSetWindowFocusCallback(window, windowFocusCallback = new GLFWWindowFocusCallback() {

        @Override
        public void invoke(final long window, final boolean focus) {
            if (wasActive != focus) {
                if (!wasActive) {
                    listener.gainFocus();
                    timer.reset();
                } else {
                    listener.loseFocus();
                }
                wasActive = !wasActive;
            }
        }

        @Override
        public void close() {
            super.close();
        }

        @Override
        public void callback(long args) {
            super.callback(args);
        }
    });
    // Center the window
    if (!settings.isFullscreen()) {
        glfwSetWindowPos(window, (videoMode.width() - settings.getWidth()) / 2, (videoMode.height() - settings.getHeight()) / 2);
    }
    // Make the OpenGL context current
    glfwMakeContextCurrent(window);
    // Enable vsync
    if (settings.isVSync()) {
        glfwSwapInterval(1);
    } else {
        glfwSwapInterval(0);
    }
    setWindowIcon(settings);
    showWindow();
    allowSwapBuffers = settings.isSwapBuffers();
}
Also used : GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) GLFWWindowSizeCallback(org.lwjgl.glfw.GLFWWindowSizeCallback) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode) GLFWWindowFocusCallback(org.lwjgl.glfw.GLFWWindowFocusCallback)

Aggregations

GLFWErrorCallback (org.lwjgl.glfw.GLFWErrorCallback)1 GLFWVidMode (org.lwjgl.glfw.GLFWVidMode)1 GLFWWindowFocusCallback (org.lwjgl.glfw.GLFWWindowFocusCallback)1 GLFWWindowSizeCallback (org.lwjgl.glfw.GLFWWindowSizeCallback)1