diff --git a/src/main/java/org/lwjglx/opengl/ContextGL.java b/src/main/java/org/lwjglx/opengl/ContextGL.java index 58c194f..5d76351 100644 --- a/src/main/java/org/lwjglx/opengl/ContextGL.java +++ b/src/main/java/org/lwjglx/opengl/ContextGL.java @@ -31,6 +31,8 @@ */ package org.lwjglx.opengl; +import org.lwjgl.glfw.GLFW; +import org.lwjgl.opengl.GL; import org.lwjglx.LWJGLException; import org.lwjglx.PointerBuffer; @@ -47,25 +49,50 @@ import org.lwjglx.PointerBuffer; */ final class ContextGL implements Context { - public void releaseCurrent() throws LWJGLException {} + public long glfwWindow = Long.MIN_VALUE; + public final boolean shared; + + public ContextGL(long glfwWindow, boolean shared) { + this.glfwWindow = glfwWindow; + this.shared = shared; + } + + public void releaseCurrent() throws LWJGLException { + GLFW.glfwMakeContextCurrent(0); + GL.setCapabilities(null); + } public synchronized void releaseDrawable() throws LWJGLException {} public synchronized void update() {} - public static void swapBuffers() throws LWJGLException {} - - public synchronized void makeCurrent() throws LWJGLException {} - - public synchronized boolean isCurrent() throws LWJGLException { - return false; + public static void swapBuffers() throws LWJGLException { + GLFW.glfwSwapBuffers(Display.getWindow()); } - public static void setSwapInterval(int value) {} + public synchronized void makeCurrent() throws LWJGLException { + GLFW.glfwMakeContextCurrent(glfwWindow); + GL.createCapabilities(); + } - public synchronized void forceDestroy() throws LWJGLException {} + public synchronized boolean isCurrent() throws LWJGLException { + return GLFW.glfwGetCurrentContext() == glfwWindow; + } - public synchronized void destroy() throws LWJGLException {} + public static void setSwapInterval(int value) { + GLFW.glfwSwapInterval(value); + } + + public synchronized void forceDestroy() throws LWJGLException { + destroy(); + } + + public synchronized void destroy() throws LWJGLException { + if (shared && glfwWindow > 0) { + GLFW.glfwDestroyWindow(glfwWindow); + glfwWindow = -1; + } + } public synchronized void setCLSharingProperties(final PointerBuffer properties) throws LWJGLException {} } diff --git a/src/main/java/org/lwjglx/opengl/Display.java b/src/main/java/org/lwjglx/opengl/Display.java index ddbee5a..f02740a 100644 --- a/src/main/java/org/lwjglx/opengl/Display.java +++ b/src/main/java/org/lwjglx/opengl/Display.java @@ -69,6 +69,20 @@ public class Display { desktopDisplayMode = new DisplayMode(monitorWidth, monitorHeight, monitorBitPerPixel, monitorRefreshRate); } + /** + * Create the OpenGL context with the given minimum parameters. If isFullscreen() is true or if windowed + * context are not supported on the platform, the display mode will be switched to the mode returned by + * getDisplayMode(), and a fullscreen context will be created. If isFullscreen() is false, a windowed context + * will be created with the dimensions given in the mode returned by getDisplayMode(). If a context can't be + * created with the given parameters, a LWJGLException will be thrown. + *

+ *

The window created will be set up in orthographic 2D projection, with 1:1 pixel ratio with GL coordinates. + * + * @param pixel_format Describes the minimum specifications the context must fulfill. + * @param shared_drawable The Drawable to share context with. (optional, may be null) + * + * @throws org.lwjglx.LWJGLException + */ public static void create(PixelFormat pixel_format, Drawable shared_drawable) { System.out.println("TODO: Implement Display.create(PixelFormat, Drawable)"); // TODO create(); @@ -85,6 +99,10 @@ public class Display { } public static void create() { + if (displayCreated) { + return; + } + long monitor = glfwGetPrimaryMonitor(); GLFWVidMode vidmode = glfwGetVideoMode(monitor); @@ -98,6 +116,8 @@ public class Display { glfwDefaultWindowHints(); glfwWindowHint(GLFW_VISIBLE, GL_FALSE); glfwWindowHint(GLFW_RESIZABLE, displayResizable ? GL_TRUE : GL_FALSE); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); Window.handle = glfwCreateWindow(mode.getWidth(), mode.getHeight(), windowTitle, NULL, NULL); @@ -226,6 +246,7 @@ public class Display { displayY = (monitorHeight - mode.getHeight()) / 2; glfwMakeContextCurrent(Window.handle); + drawable = new DrawableGL(); GL.createCapabilities(); if (savedIcons != null) { @@ -461,8 +482,10 @@ public class Display { Sync.sync(fps); } + protected static DrawableGL drawable = null; + public static Drawable getDrawable() { - return null; + return drawable; } static DisplayImplementation getImplementation() { diff --git a/src/main/java/org/lwjglx/opengl/DrawableGL.java b/src/main/java/org/lwjglx/opengl/DrawableGL.java index d57a413..3e7c0d4 100644 --- a/src/main/java/org/lwjglx/opengl/DrawableGL.java +++ b/src/main/java/org/lwjglx/opengl/DrawableGL.java @@ -33,12 +33,13 @@ package org.lwjglx.opengl; import static org.lwjgl.opengl.GL11.*; +import org.lwjgl.glfw.GLFW; import org.lwjglx.LWJGLException; import org.lwjglx.LWJGLUtil; import org.lwjglx.PointerBuffer; /** @author Spasi */ -abstract class DrawableGL implements DrawableLWJGL { +class DrawableGL implements DrawableLWJGL { /** The PixelFormat used to create the drawable. */ protected PixelFormat pixel_format; @@ -49,7 +50,9 @@ abstract class DrawableGL implements DrawableLWJGL { /** The OpenGL Context. */ protected ContextGL context; - protected DrawableGL() {} + protected DrawableGL() { + context = new ContextGL(Display.getWindow(), false); + } public void setPixelFormat(final PixelFormatLWJGL pf) throws LWJGLException { throw new UnsupportedOperationException(); @@ -72,9 +75,11 @@ abstract class DrawableGL implements DrawableLWJGL { public ContextGL createSharedContext() throws LWJGLException { synchronized (GlobalLock.lock) { - // checkDestroyed(); - // return new ContextGL(peer_info, context.getContextAttribs(), context); - return null; + long hiddenWindow = GLFW.glfwCreateWindow(16, 16, "MC - Shared Drawable", 0, this.context.glfwWindow); + if (hiddenWindow == 0) { + throw new LWJGLException("Couldn't create shared context hidden window"); + } + return new ContextGL(hiddenWindow, true); } } diff --git a/src/main/java/org/lwjglx/opengl/SharedDrawable.java b/src/main/java/org/lwjglx/opengl/SharedDrawable.java new file mode 100644 index 0000000..7c7e0e9 --- /dev/null +++ b/src/main/java/org/lwjglx/opengl/SharedDrawable.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2002-2008 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjglx.opengl; + +import org.lwjglx.LWJGLException; + +/** + * @author Spasi + */ + +/** + * A Drawable implementation that shares its context with another Drawable. This is useful + * for background loading of resources. See org.lwjgl.test.opengl.multithread.BackgroundLoad + * for an example. + * + * @author Spasi + */ +public final class SharedDrawable extends DrawableGL { + + public SharedDrawable(final Drawable drawable) throws LWJGLException { + this.context = (ContextGL) ((DrawableLWJGL) drawable).createSharedContext(); + } + + public ContextGL createSharedContext() { + throw new UnsupportedOperationException(); + } +}