diff --git a/src/java/org/lwjgl/opengl/Display.java b/src/java/org/lwjgl/opengl/Display.java index 70e55915..3c158af8 100644 --- a/src/java/org/lwjgl/opengl/Display.java +++ b/src/java/org/lwjgl/opengl/Display.java @@ -50,6 +50,9 @@ import java.security.PrivilegedAction; import java.util.Arrays; import java.util.HashSet; import java.awt.Canvas; +import java.awt.event.ComponentListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; @@ -111,6 +114,16 @@ public final class Display { private static boolean window_created = false; + private static boolean parent_resized; + + private static ComponentListener component_listener = new ComponentAdapter() { + public final void componentResized(ComponentEvent e) { + synchronized (GlobalLock.lock) { + parent_resized = true; + } + } + }; + static { Sys.initialize(); display_impl = createDisplayImplementation(); @@ -238,6 +251,36 @@ public final class Display { } } + private static DisplayMode getEffectiveMode() { + return !fullscreen && parent != null ? new DisplayMode(parent.getWidth(), parent.getHeight()) : current_mode; + } + + private static int getWindowX() { + if (!fullscreen && parent == null) { + // if no display location set, center window + if (x == -1) { + return Math.max(0, (initial_mode.getWidth() - current_mode.getWidth()) / 2); + } else { + return x; + } + } else { + return 0; + } + } + + private static int getWindowY() { + if (!fullscreen && parent == null) { + // if no display location set, center window + if (y == -1) { + return Math.max(0, (initial_mode.getHeight() - current_mode.getHeight()) / 2); + } else { + return y; + } + } else { + return 0; + } + } + /** * Create the native window peer from the given mode and fullscreen flag. * A native context must exist, and it will be attached to the window. @@ -246,25 +289,14 @@ public final class Display { if (window_created) { return; } - int window_x; - int window_y; - if (!fullscreen && parent == null) { - // if no display location set, center window - if (x == -1 && y == -1) { - window_x = Math.max(0, (initial_mode.getWidth() - current_mode.getWidth()) / 2); - window_y = Math.max(0, (initial_mode.getHeight() - current_mode.getHeight()) / 2); - } else { - window_x = x; - window_y = y; - } - } else { - window_x = 0; - window_y = 0; - } Canvas tmp_parent = fullscreen ? null : parent; if (tmp_parent != null && !tmp_parent.isDisplayable()) // Only a best effort check, since the parent can turn undisplayable hereafter throw new LWJGLException("Parent.isDisplayable() must be true"); - display_impl.createWindow(current_mode, fullscreen, tmp_parent, window_x, window_y); + if (tmp_parent != null) { + tmp_parent.addComponentListener(component_listener); + } + DisplayMode mode = getEffectiveMode(); + display_impl.createWindow(mode, fullscreen, tmp_parent, getWindowX(), getWindowY()); window_created = true; setTitle(title); @@ -282,6 +314,9 @@ public final class Display { if (!window_created) { return; } + if (parent != null) { + parent.removeComponentListener(component_listener); + } try { if (context != null && context.isCurrent()) { Context.releaseCurrentContext(); @@ -676,6 +711,10 @@ public final class Display { } pollDevices(); + if (parent_resized) { + reshape(); + parent_resized = false; + } } } @@ -985,11 +1024,16 @@ public final class Display { // offset if already created if(isCreated()) { - display_impl.reshape(x, y, current_mode.getWidth(), current_mode.getHeight()); + reshape(); } } } + private static void reshape() { + DisplayMode mode = getEffectiveMode(); + display_impl.reshape(getWindowX(), getWindowY(), mode.getWidth(), mode.getHeight()); + } + /** * Get the driver adapter string. This is a unique string describing the actual card's hardware, eg. "Geforce2", "PS2", * "Radeon9700". If the adapter cannot be determined, this function returns null. diff --git a/src/java/org/lwjgl/test/opengl/awt/DisplayParentTest.java b/src/java/org/lwjgl/test/opengl/awt/DisplayParentTest.java index b209d86b..682c4d37 100644 --- a/src/java/org/lwjgl/test/opengl/awt/DisplayParentTest.java +++ b/src/java/org/lwjgl/test/opengl/awt/DisplayParentTest.java @@ -83,13 +83,7 @@ public class DisplayParentTest extends Frame { int current_width = 0; while (isVisible()) { angle += 1.0f; - if (getWidth() != current_width || getHeight() != current_height) { - current_width = getWidth(); - current_height = getHeight(); - Display.setDisplayMode(new DisplayMode(getWidth(), getHeight())); - GL11.glViewport(0, 0, current_width, current_height); - } - GL11.glViewport(0, 0, getWidth(), getHeight()); + GL11.glViewport(0, 0, display_parent.getWidth(), display_parent.getHeight()); GL11.glClearColor(0.0f, 1.0f, 0.0f, 1.0f); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); GL11.glMatrixMode(GL11.GL_PROJECTION); diff --git a/src/native/linux/org_lwjgl_opengl_Display.c b/src/native/linux/org_lwjgl_opengl_Display.c index a237f934..1658def5 100644 --- a/src/native/linux/org_lwjgl_opengl_Display.c +++ b/src/native/linux/org_lwjgl_opengl_Display.c @@ -240,6 +240,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nReshape(JNIEnv *env, Display *disp = (Display *)(intptr_t)display; Window window = (Window)window_ptr; XMoveWindow(disp, window, x, y); + XResizeWindow(disp, window, width, height); } JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_LinuxDisplay_getRootWindow(JNIEnv *env, jclass clazz, jlong display, jint screen) {