Search in sources :

Example 1 with GLFWVidMode

use of org.lwjgl.glfw.GLFWVidMode in project libgdx by libgdx.

the class Lwjgl3Application method createGlfwWindow.

static long createGlfwWindow(Lwjgl3ApplicationConfiguration config, long sharedContextWindow) {
    GLFW.glfwDefaultWindowHints();
    GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
    GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, config.windowResizable ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
    GLFW.glfwWindowHint(GLFW.GLFW_MAXIMIZED, config.windowMaximized ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
    if (sharedContextWindow == 0) {
        GLFW.glfwWindowHint(GLFW.GLFW_RED_BITS, config.r);
        GLFW.glfwWindowHint(GLFW.GLFW_GREEN_BITS, config.g);
        GLFW.glfwWindowHint(GLFW.GLFW_BLUE_BITS, config.b);
        GLFW.glfwWindowHint(GLFW.GLFW_ALPHA_BITS, config.a);
        GLFW.glfwWindowHint(GLFW.GLFW_STENCIL_BITS, config.stencil);
        GLFW.glfwWindowHint(GLFW.GLFW_DEPTH_BITS, config.depth);
        GLFW.glfwWindowHint(GLFW.GLFW_SAMPLES, config.samples);
    }
    if (config.useGL30) {
        GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, config.gles30ContextMajorVersion);
        GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, config.gles30ContextMinorVersion);
        if (SharedLibraryLoader.isMac) {
            // hints mandatory on OS X for GL 3.2+ context creation, but fail on Windows if the
            // WGL_ARB_create_context extension is not available
            // see: http://www.glfw.org/docs/latest/compat.html
            GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE);
            GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
        }
    }
    if (config.debug) {
        GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_DEBUG_CONTEXT, GLFW.GLFW_TRUE);
    }
    long windowHandle = 0;
    if (config.fullscreenMode != null) {
        // glfwWindowHint(GLFW.GLFW_REFRESH_RATE, config.fullscreenMode.refreshRate);
        windowHandle = GLFW.glfwCreateWindow(config.fullscreenMode.width, config.fullscreenMode.height, config.title, config.fullscreenMode.getMonitor(), sharedContextWindow);
    } else {
        GLFW.glfwWindowHint(GLFW.GLFW_DECORATED, config.windowDecorated ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
        windowHandle = GLFW.glfwCreateWindow(config.windowWidth, config.windowHeight, config.title, 0, sharedContextWindow);
    }
    if (windowHandle == 0) {
        throw new GdxRuntimeException("Couldn't create window");
    }
    Lwjgl3Window.setSizeLimits(windowHandle, config.windowMinWidth, config.windowMinHeight, config.windowMaxWidth, config.windowMaxHeight);
    if (config.fullscreenMode == null && !config.windowMaximized) {
        if (config.windowX == -1 && config.windowY == -1) {
            int windowWidth = Math.max(config.windowWidth, config.windowMinWidth);
            int windowHeight = Math.max(config.windowHeight, config.windowMinHeight);
            if (config.windowMaxWidth > -1)
                windowWidth = Math.min(windowWidth, config.windowMaxWidth);
            if (config.windowMaxHeight > -1)
                windowHeight = Math.min(windowHeight, config.windowMaxHeight);
            GLFWVidMode vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
            GLFW.glfwSetWindowPos(windowHandle, vidMode.width() / 2 - windowWidth / 2, vidMode.height() / 2 - windowHeight / 2);
        } else {
            GLFW.glfwSetWindowPos(windowHandle, config.windowX, config.windowY);
        }
    }
    if (config.windowIconPaths != null) {
        Lwjgl3Window.setIcon(windowHandle, config.windowIconPaths, config.windowIconFileType);
    }
    GLFW.glfwMakeContextCurrent(windowHandle);
    GLFW.glfwSwapInterval(config.vSyncEnabled ? 1 : 0);
    GL.createCapabilities();
    initiateGL();
    if (!glVersion.isVersionEqualToOrHigher(2, 0))
        throw new GdxRuntimeException("OpenGL 2.0 or higher with the FBO extension is required. OpenGL version: " + GL11.glGetString(GL11.GL_VERSION) + "\n" + glVersion.getDebugVersionString());
    if (!supportsFBO()) {
        throw new GdxRuntimeException("OpenGL 2.0 or higher with the FBO extension is required. OpenGL version: " + GL11.glGetString(GL11.GL_VERSION) + ", FBO extension: false\n" + glVersion.getDebugVersionString());
    }
    if (config.debug) {
        glDebugCallback = GLUtil.setupDebugMessageCallback(config.debugStream);
        setGLDebugMessageControl(GLDebugMessageSeverity.NOTIFICATION, false);
    }
    return windowHandle;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode)

Example 2 with GLFWVidMode

use of org.lwjgl.glfw.GLFWVidMode in project libgdx by libgdx.

the class Lwjgl3ApplicationConfiguration method getDisplayMode.

/**
	 * @return the currently active {@link DisplayMode} of the given monitor
	 */
public static DisplayMode getDisplayMode(Monitor monitor) {
    Lwjgl3Application.initializeGlfw();
    GLFWVidMode videoMode = GLFW.glfwGetVideoMode(((Lwjgl3Monitor) monitor).monitorHandle);
    return new Lwjgl3Graphics.Lwjgl3DisplayMode(((Lwjgl3Monitor) monitor).monitorHandle, videoMode.width(), videoMode.height(), videoMode.refreshRate(), videoMode.redBits() + videoMode.greenBits() + videoMode.blueBits());
}
Also used : GLFWVidMode(org.lwjgl.glfw.GLFWVidMode)

Example 3 with GLFWVidMode

use of org.lwjgl.glfw.GLFWVidMode in project libgdx by libgdx.

the class Lwjgl3ApplicationConfiguration method getDisplayModes.

/**
	 * @return the available {@link DisplayMode}s of the primary monitor
	 */
public static DisplayMode[] getDisplayModes() {
    Lwjgl3Application.initializeGlfw();
    Buffer videoModes = GLFW.glfwGetVideoModes(GLFW.glfwGetPrimaryMonitor());
    DisplayMode[] result = new DisplayMode[videoModes.limit()];
    for (int i = 0; i < result.length; i++) {
        GLFWVidMode videoMode = videoModes.get(i);
        result[i] = new Lwjgl3Graphics.Lwjgl3DisplayMode(GLFW.glfwGetPrimaryMonitor(), videoMode.width(), videoMode.height(), videoMode.refreshRate(), videoMode.redBits() + videoMode.greenBits() + videoMode.blueBits());
    }
    return result;
}
Also used : Buffer(org.lwjgl.glfw.GLFWVidMode.Buffer) PointerBuffer(org.lwjgl.PointerBuffer) IntBuffer(java.nio.IntBuffer) DisplayMode(com.badlogic.gdx.Graphics.DisplayMode) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode)

Example 4 with GLFWVidMode

use of org.lwjgl.glfw.GLFWVidMode in project libgdx by libgdx.

the class Lwjgl3ApplicationConfiguration method getDisplayMode.

/**
	 * @return the currently active {@link DisplayMode} of the primary monitor
	 */
public static DisplayMode getDisplayMode() {
    Lwjgl3Application.initializeGlfw();
    GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
    return new Lwjgl3Graphics.Lwjgl3DisplayMode(GLFW.glfwGetPrimaryMonitor(), videoMode.width(), videoMode.height(), videoMode.refreshRate(), videoMode.redBits() + videoMode.greenBits() + videoMode.blueBits());
}
Also used : GLFWVidMode(org.lwjgl.glfw.GLFWVidMode)

Example 5 with GLFWVidMode

use of org.lwjgl.glfw.GLFWVidMode 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

GLFWVidMode (org.lwjgl.glfw.GLFWVidMode)8 IntBuffer (java.nio.IntBuffer)4 Buffer (org.lwjgl.glfw.GLFWVidMode.Buffer)3 DisplayMode (com.badlogic.gdx.Graphics.DisplayMode)2 PointerBuffer (org.lwjgl.PointerBuffer)2 Lwjgl3Monitor (com.badlogic.gdx.backends.lwjgl3.Lwjgl3Graphics.Lwjgl3Monitor)1 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)1 PlayerProfile (net.glowstone.entity.meta.profile.PlayerProfile)1 GLFWErrorCallback (org.lwjgl.glfw.GLFWErrorCallback)1 GLFWWindowFocusCallback (org.lwjgl.glfw.GLFWWindowFocusCallback)1 GLFWWindowSizeCallback (org.lwjgl.glfw.GLFWWindowSizeCallback)1 MemoryStack (org.lwjgl.system.MemoryStack)1