diff --git a/src/java/org/lwjgl/Window.java b/src/java/org/lwjgl/opengl/Window.java similarity index 94% rename from src/java/org/lwjgl/Window.java rename to src/java/org/lwjgl/opengl/Window.java index f5c6684b..83660b84 100644 --- a/src/java/org/lwjgl/Window.java +++ b/src/java/org/lwjgl/opengl/Window.java @@ -4,7 +4,7 @@ * To change this generated comment go to * Window>Preferences>Java>Code Generation>Code Template */ -package org.lwjgl; +package org.lwjgl.opengl; /** * This is the abstract class for a Window in LWJGL. LWJGL windows have some @@ -20,6 +20,10 @@ package org.lwjgl; * * @author foo */ + +import org.lwjgl.Display; +import org.lwjgl.Sys; + public final class Window { static { @@ -29,18 +33,6 @@ public final class Window { /** Whether the window is currently created, ie. has a native peer */ private static boolean created; - /** Whether the window is currently minimized */ - private static boolean minimized; - - /** Whether the window has focus */ - private static boolean focused = true; - - /** Whether the window has been asked to close by the user or underlying OS */ - private static boolean closeRequested; - - /** Whether the window is dirty, ie. needs painting */ - private static boolean dirty; - /** X coordinate of the window */ private static int x; @@ -142,28 +134,31 @@ public final class Window { */ public static boolean isCloseRequested() { assert isCreated(); - - boolean currentValue = closeRequested; - closeRequested = false; - return currentValue; + return nIsCloseRequested(); } + private static native boolean nIsCloseRequested(); + /** * @return true if the window is minimized or otherwise not visible */ public static boolean isMinimized() { assert isCreated(); - return minimized; + return nIsMinimized(); } + private static native boolean nIsMinimized(); + /** * @return true if window is focused */ public static boolean isFocused() { assert isCreated(); - return focused; + return nIsFocused(); } + private static native boolean nIsFocused(); + /** * Minimize the game and allow the operating system's default display to become * visible. It is the responsibility of LWJGL's native code to restore the display @@ -193,15 +188,16 @@ public final class Window { */ public static boolean isDirty() { assert isCreated(); - return dirty; + return nIsDirty(); } + private static native boolean nIsDirty(); + /** * Paint the window. This clears the dirty flag and swaps the buffers. */ public static void paint() { assert isCreated(); - dirty = false; swapBuffers(); } diff --git a/src/java/org/lwjgl/test/WindowCreationTest.java b/src/java/org/lwjgl/test/WindowCreationTest.java index ac65b16e..22270bb7 100644 --- a/src/java/org/lwjgl/test/WindowCreationTest.java +++ b/src/java/org/lwjgl/test/WindowCreationTest.java @@ -7,7 +7,7 @@ package org.lwjgl.test; import org.lwjgl.*; -import org.lwjgl.opengl.GLWindow; +import org.lwjgl.opengl.Window; /** * @author Brian @@ -15,14 +15,11 @@ import org.lwjgl.opengl.GLWindow; public class WindowCreationTest { public static void main(String[] args) { - GLWindow gl = null; - DisplayMode[] modes = Display.getAvailableDisplayModes(); System.out.println("Found " + modes.length + " display modes"); try { - gl = new GLWindow("WindowCreationTest", 50, 50, 320, 240, 16, 0, 0, 0); - gl.create(); + Window.create("WindowCreationTest", 50, 50, 320, 240, 16, 0, 0, 0); } catch (Exception e) { e.printStackTrace(); } @@ -38,6 +35,6 @@ public class WindowCreationTest { } } - gl.destroy(); + Window.destroy(); } } diff --git a/src/java/org/lwjgl/test/input/ControllerCreationTest.java b/src/java/org/lwjgl/test/input/ControllerCreationTest.java index 335f3b61..15d0db6b 100644 --- a/src/java/org/lwjgl/test/input/ControllerCreationTest.java +++ b/src/java/org/lwjgl/test/input/ControllerCreationTest.java @@ -34,10 +34,9 @@ package org.lwjgl.test.input; import org.lwjgl.Sys; import org.lwjgl.Display; import org.lwjgl.DisplayMode; -import org.lwjgl.Window; import org.lwjgl.input.Controller; import org.lwjgl.opengl.GL; -import org.lwjgl.opengl.GLWindow; +import org.lwjgl.opengl.Window; import org.lwjgl.opengl.GLU; import org.lwjgl.vector.Vector2f; @@ -50,10 +49,6 @@ import org.lwjgl.vector.Vector2f; * @version $Revision$ */ public class ControllerCreationTest { - - /** OpenGL instance */ - private GLWindow gl; - /** position of quad to draw */ private Vector2f position = new Vector2f(320.0f, 240.0f); @@ -79,11 +74,10 @@ public class ControllerCreationTest { try { if(fullscreen) { Display.setDisplayMode(displayMode); - gl = new GLWindow("ControllerCreationTest", 16, 0, 0, 0); + Window.create("ControllerCreationTest", 16, 0, 0, 0); } else { - gl = new GLWindow("ControllerCreationTest", 50, 50, 640, 480, 16, 0, 0, 0); + Window.create("ControllerCreationTest", 50, 50, 640, 480, 16, 0, 0, 0); } - gl.create(); } catch (Exception e) { e.printStackTrace(); @@ -115,12 +109,12 @@ public class ControllerCreationTest { // recreate display in fullscreen mode System.out.print("Destroying display..."); - gl.destroy(); + Window.destroy(); System.out.println("success"); System.out.print("Entering fullscreen mode..."); try { - gl.destroy(); + Window.destroy(); initialize(true); Display.setDisplayMode(displayMode); } catch (Exception e) { @@ -143,7 +137,7 @@ public class ControllerCreationTest { System.out.print("Shutting down..."); Display.resetDisplayMode(); Controller.destroy(); - gl.destroy(); + Window.destroy(); System.out.println("shutdown complete"); } diff --git a/src/java/org/lwjgl/test/input/ControllerTest.java b/src/java/org/lwjgl/test/input/ControllerTest.java index 78cb65f0..960a9a07 100644 --- a/src/java/org/lwjgl/test/input/ControllerTest.java +++ b/src/java/org/lwjgl/test/input/ControllerTest.java @@ -32,11 +32,10 @@ package org.lwjgl.test.input; import org.lwjgl.DisplayMode; -import org.lwjgl.Window; import org.lwjgl.input.Controller; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL; -import org.lwjgl.opengl.GLWindow; +import org.lwjgl.opengl.Window; import org.lwjgl.opengl.GLU; import org.lwjgl.vector.Vector2f; @@ -50,9 +49,6 @@ import org.lwjgl.vector.Vector2f; */ public class ControllerTest { - /** OpenGL instance */ - private GLWindow gl; - /** GLU instance */ private GLU glu; @@ -80,8 +76,7 @@ public class ControllerTest { private void setupDisplay(boolean fullscreen) { try { - gl = new GLWindow("ControllerTest", 50, 50, 640, 480, 16, 0, 0, 0); - gl.create(); + Window.create("ControllerTest", 50, 50, 640, 480, 16, 0, 0, 0); } catch (Exception e) { e.printStackTrace(); System.exit(-1); @@ -104,7 +99,7 @@ public class ControllerTest { Controller.destroy(); Keyboard.destroy(); - gl.destroy(); + Window.destroy(); } private void createController() { diff --git a/src/java/org/lwjgl/test/input/HWCursorTest.java b/src/java/org/lwjgl/test/input/HWCursorTest.java index cb6ea5d3..0477f8c1 100644 --- a/src/java/org/lwjgl/test/input/HWCursorTest.java +++ b/src/java/org/lwjgl/test/input/HWCursorTest.java @@ -50,9 +50,6 @@ public class HWCursorTest { /** Intended deiplay mode */ private DisplayMode mode; - /** GL instance */ - private GLWindow gl; - /** GLU instance */ private GLU glu; @@ -83,8 +80,7 @@ public class HWCursorTest { mode = findDisplayMode(800, 600, 16); // start of in windowed mode - gl = new GLWindow("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); - gl.create(); + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); glInit(); @@ -230,11 +226,10 @@ public class HWCursorTest { } cursor.destroy(); Mouse.destroy(); - gl.destroy(); + Window.destroy(); Display.setDisplayMode(mode); - gl = new GLWindow("Test", mode.bpp, 0, 0, 0); - gl.create(); + Window.create("Test", mode.bpp, 0, 0, 0); glInit(); @@ -257,11 +252,10 @@ public class HWCursorTest { } cursor.destroy(); Mouse.destroy(); - gl.destroy(); + Window.destroy(); Display.resetDisplayMode(); - gl = new GLWindow("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); - gl.create(); + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); glInit(); @@ -304,7 +298,7 @@ public class HWCursorTest { cursor.destroy(); Mouse.destroy(); Display.resetDisplayMode(); - gl.destroy(); + Window.destroy(); } /** diff --git a/src/java/org/lwjgl/test/input/KeyboardTest.java b/src/java/org/lwjgl/test/input/KeyboardTest.java index 65ab45bf..5d60fd4e 100644 --- a/src/java/org/lwjgl/test/input/KeyboardTest.java +++ b/src/java/org/lwjgl/test/input/KeyboardTest.java @@ -32,10 +32,9 @@ package org.lwjgl.test.input; import org.lwjgl.DisplayMode; -import org.lwjgl.Window; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL; -import org.lwjgl.opengl.GLWindow; +import org.lwjgl.opengl.Window; import org.lwjgl.opengl.GLU; import org.lwjgl.vector.Vector2f; @@ -49,9 +48,6 @@ import org.lwjgl.vector.Vector2f; */ public class KeyboardTest { - /** OpenGL instance */ - private GLWindow gl; - /** GLU instance */ private GLU glu; @@ -83,9 +79,7 @@ public class KeyboardTest { private void setupDisplay(boolean fullscreen) { try { - gl = new GLWindow("KeyboardTest", 50, 50, 640, 480, 16, 0, 0, 0); - gl.create(); - + Window.create("KeyboardTest", 50, 50, 640, 480, 16, 0, 0, 0); } catch (Exception e) { e.printStackTrace(); System.exit(-1); @@ -107,7 +101,7 @@ public class KeyboardTest { wiggleKeyboard(); Keyboard.destroy(); - gl.destroy(); + Window.destroy(); } private void createKeyboard() { diff --git a/src/java/org/lwjgl/test/input/MouseCreationTest.java b/src/java/org/lwjgl/test/input/MouseCreationTest.java index 35204b9e..f5d23b3e 100644 --- a/src/java/org/lwjgl/test/input/MouseCreationTest.java +++ b/src/java/org/lwjgl/test/input/MouseCreationTest.java @@ -34,10 +34,9 @@ package org.lwjgl.test.input; import org.lwjgl.Sys; import org.lwjgl.Display; import org.lwjgl.DisplayMode; -import org.lwjgl.Window; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL; -import org.lwjgl.opengl.GLWindow; +import org.lwjgl.opengl.Window; import org.lwjgl.opengl.GLU; import org.lwjgl.vector.Vector2f; @@ -50,10 +49,6 @@ import org.lwjgl.vector.Vector2f; * @version $Revision$ */ public class MouseCreationTest { - - /** OpenGL instance */ - private GLWindow gl; - /** GLU instance */ private GLU glu; @@ -82,11 +77,10 @@ public class MouseCreationTest { try { if(fullscreen) { Display.setDisplayMode(displayMode); - gl = new GLWindow("MouseCreationTest", 16, 0, 0, 0); + Window.create("MouseCreationTest", 16, 0, 0, 0); } else { - gl = new GLWindow("MouseCreationTest", 50, 50, 640, 480, 16, 0, 0, 0); + Window.create("MouseCreationTest", 50, 50, 640, 480, 16, 0, 0, 0); } - gl.create(); } catch (Exception e) { e.printStackTrace(); @@ -123,7 +117,7 @@ public class MouseCreationTest { System.out.print("Entering fullscreen mode..."); try { - gl.destroy(); + Window.destroy(); initialize(true); Display.setDisplayMode(displayMode); } catch (Exception e) { @@ -146,7 +140,7 @@ public class MouseCreationTest { System.out.print("Shutting down..."); Display.resetDisplayMode(); Mouse.destroy(); - gl.destroy(); + Window.destroy(); System.out.println("shutdown complete"); } diff --git a/src/java/org/lwjgl/test/input/MouseTest.java b/src/java/org/lwjgl/test/input/MouseTest.java index 5cec7290..e504b61d 100644 --- a/src/java/org/lwjgl/test/input/MouseTest.java +++ b/src/java/org/lwjgl/test/input/MouseTest.java @@ -32,11 +32,10 @@ package org.lwjgl.test.input; import org.lwjgl.DisplayMode; -import org.lwjgl.Window; import org.lwjgl.input.Mouse; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL; -import org.lwjgl.opengl.GLWindow; +import org.lwjgl.opengl.Window; import org.lwjgl.opengl.GLU; import org.lwjgl.vector.Vector2f; @@ -50,9 +49,6 @@ import org.lwjgl.vector.Vector2f; */ public class MouseTest { - /** OpenGL instance */ - private GLWindow gl; - /** GLU instance */ private GLU glu; @@ -80,9 +76,7 @@ public class MouseTest { private void setupDisplay(boolean fullscreen) { try { - gl = new GLWindow("MouseTest", 50, 50, 640, 480, 16, 0, 0, 0); - gl.create(); - + Window.create("MouseTest", 50, 50, 640, 480, 16, 0, 0, 0); } catch (Exception e) { e.printStackTrace(); System.exit(-1); @@ -105,7 +99,7 @@ public class MouseTest { Mouse.destroy(); Keyboard.destroy(); - gl.destroy(); + Window.destroy(); } private void createMouse() { diff --git a/src/java/org/lwjgl/test/openal/MovingSoundTest.java b/src/java/org/lwjgl/test/openal/MovingSoundTest.java index 346c5086..8cf77eeb 100644 --- a/src/java/org/lwjgl/test/openal/MovingSoundTest.java +++ b/src/java/org/lwjgl/test/openal/MovingSoundTest.java @@ -31,11 +31,10 @@ */ package org.lwjgl.test.openal; -import org.lwjgl.Window; import org.lwjgl.openal.AL; import org.lwjgl.openal.eax.*; import org.lwjgl.input.Keyboard; -import org.lwjgl.opengl.GLWindow; +import org.lwjgl.opengl.Window; import java.nio.IntBuffer; import java.nio.FloatBuffer; @@ -52,7 +51,6 @@ import java.nio.FloatBuffer; public class MovingSoundTest extends BasicTest { public static float MOVEMENT = 50.00f; - private GLWindow gl = new GLWindow("Moving Sound Test", 100, 100, 320, 240, 32, 0 ,0 ,0); /** * Creates an instance of MovingSoundTest @@ -71,7 +69,7 @@ public class MovingSoundTest extends BasicTest { } try { - gl.create(); + Window.create("Moving Sound Test", 100, 100, 320, 240, 32, 0 ,0 ,0); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/java/org/lwjgl/test/opengl/FullScreenWindowedTest.java b/src/java/org/lwjgl/test/opengl/FullScreenWindowedTest.java index 38870012..ee5275dd 100644 --- a/src/java/org/lwjgl/test/opengl/FullScreenWindowedTest.java +++ b/src/java/org/lwjgl/test/opengl/FullScreenWindowedTest.java @@ -49,9 +49,6 @@ public class FullScreenWindowedTest { /** Intended deiplay mode */ private DisplayMode mode; - /** GL instance */ - private GLWindow gl; - /** GLU instance */ private GLU glu; @@ -96,8 +93,7 @@ public class FullScreenWindowedTest { mode = findDisplayMode(800, 600, 16); // start of in windowed mode - gl = new GLWindow("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); - gl.create(); + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); glInit(); @@ -202,11 +198,10 @@ public class FullScreenWindowedTest { try { Keyboard.destroy(); - gl.destroy(); + Window.destroy(); Display.setDisplayMode(mode); - gl = new GLWindow("Test", mode.bpp, 0, 0, 0); - gl.create(); + Window.create("Test", mode.bpp, 0, 0, 0); glInit(); @@ -220,11 +215,10 @@ public class FullScreenWindowedTest { if (Keyboard.isKeyDown(Keyboard.KEY_W)) { try { Keyboard.destroy(); - gl.destroy(); + Window.destroy(); Display.resetDisplayMode(); - gl = new GLWindow("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); - gl.create(); + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); glInit(); @@ -282,7 +276,7 @@ public class FullScreenWindowedTest { */ private void cleanup() { Keyboard.destroy(); - gl.destroy(); + Window.destroy(); } /** diff --git a/src/java/org/lwjgl/test/opengl/Game.java b/src/java/org/lwjgl/test/opengl/Game.java index c070864e..1d2365f5 100644 --- a/src/java/org/lwjgl/test/opengl/Game.java +++ b/src/java/org/lwjgl/test/opengl/Game.java @@ -72,10 +72,9 @@ public final class Game { } } - public static final GLWindow gl = new GLWindow("LWJGL Game Example", 16, 0, 0,0); static { try { - gl.create(); + Window.create("LWJGL Game Example", 16, 0, 0,0); System.out.println("Created OpenGL."); } catch (Exception e) { System.err.println("Failed to create OpenGL due to "+e); @@ -184,7 +183,7 @@ public final class Game { GL.glGetInteger(GL.GL_MAX_TEXTURE_UNITS_ARB, num_tex_units_buf.asIntBuffer()); System.out.println("Number of texture units: " + num_tex_units_buf.getInt()); // Fix the refresh rate to the display frequency. -// gl.wglSwapIntervalEXT(1); +// GL.wglSwapIntervalEXT(1); } /** @@ -193,7 +192,7 @@ public final class Game { private static void cleanup() { Keyboard.destroy(); Mouse.destroy(); - gl.destroy(); + Window.destroy(); try { Display.resetDisplayMode(); } catch (Exception e) { diff --git a/src/java/org/lwjgl/test/opengl/Grass.java b/src/java/org/lwjgl/test/opengl/Grass.java index 67deb44d..77880a7f 100644 --- a/src/java/org/lwjgl/test/opengl/Grass.java +++ b/src/java/org/lwjgl/test/opengl/Grass.java @@ -88,11 +88,9 @@ public class Grass { } } - public static final GLWindow gl = new GLWindow("LWJGL Grass", 50, 50, 640, 480, 16, 0, 0,0); - static { try { - gl.create(); + Window.create("LWJGL Grass", 50, 50, 640, 480, 16, 0, 0,0); Keyboard.create(); Keyboard.enableBuffer(); Mouse.create(); @@ -149,7 +147,7 @@ public class Grass { GL.GL_VERTEX_PROGRAM_NV, program_handle, program_buf); - /*gl.getIntegerv(GL.PROGRAM_ERROR_POSITION_NV, Sys.getDirectBufferAddress(int_buf)); + /*GL.glGetInteger(GL.PROGRAM_ERROR_POSITION_NV, int_buf); System.out.println("error position: " + int_buf.get(0));*/ genMesh(); @@ -205,7 +203,7 @@ public class Grass { } Mouse.destroy(); Keyboard.destroy(); - gl.destroy(); + Window.destroy(); } private static float myrand() { @@ -433,27 +431,6 @@ public class Grass { } - /* private static void ptrDraw() - { - glRotatef((aslod.angle * 180.0f) / 3.1415f, 0, 1, 0); - glTranslatef(0, 4.5, -7.5); - glRotatef(-90, 0, 1, 0); - glRotatef(-45, 0, 0, 1); - - glMaterialfv(GL.FRONT, GL.AMBIENT, vec4f(.1f,.1f,0,1).v); - glMaterialfv(GL.FRONT, GL.DIFFUSE, vec4f(.6f,.6f,.1f,1).v); - glMaterialfv(GL.FRONT, GL.SPECULAR, vec4f(1,1,.75f,1).v); - glMaterialf(GL.FRONT, GL.SHININESS, 128.f); - - glutSolidTeapot(aslod.value*5); - - gl.rotatef(45, 0, 0, 1); - gl.totatef(90, 0, 1, 0); - gl.translatef(0, -4.5, 7.5); - gl.rotatef(-(aslod.angle * 180.0f) / 3.1415f, 0f, 1f, 0f); - - } - */ private static void ptrAnimate(float degree) { aslod.count += degree; aslod.ripple = (float) (java.lang.Math.cos(aslod.count) / 80.0); diff --git a/src/java/org/lwjgl/test/opengl/PbufferTest.java b/src/java/org/lwjgl/test/opengl/PbufferTest.java index 1d60323f..f6230d5d 100644 --- a/src/java/org/lwjgl/test/opengl/PbufferTest.java +++ b/src/java/org/lwjgl/test/opengl/PbufferTest.java @@ -51,9 +51,6 @@ public class PbufferTest { /** Intended deiplay mode */ private DisplayMode mode; - /** GL instance */ - private GLWindow gl; - /** GLU instance */ private GLU glu; @@ -98,9 +95,8 @@ public class PbufferTest { mode = findDisplayMode(800, 600, 16); // start of in windowed mode - gl = new GLWindow("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); // gl = new GLWindow("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); - gl.create(); if ((Pbuffer.getPbufferCaps() & Pbuffer.PBUFFER_SUPPORTED) == 0) { System.out.println("No Pbuffer support!"); System.exit(1); @@ -259,11 +255,10 @@ public class PbufferTest { Keyboard.destroy(); Pbuffer.releaseContext(); pbuffer.destroy(); - gl.destroy(); + Window.destroy(); Display.setDisplayMode(mode); - gl = new GLWindow("Test", mode.bpp, 0, 0, 0); - gl.create(); + Window.create("Test", mode.bpp, 0, 0, 0); glInit(); initPbuffer(); @@ -280,11 +275,10 @@ public class PbufferTest { Keyboard.destroy(); Pbuffer.releaseContext(); pbuffer.destroy(); - gl.destroy(); + Window.destroy(); Display.resetDisplayMode(); - gl = new GLWindow("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); - gl.create(); + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); glInit(); initPbuffer(); @@ -352,7 +346,7 @@ public class PbufferTest { Keyboard.destroy(); Pbuffer.releaseContext(); pbuffer.destroy(); - gl.destroy(); + Window.destroy(); } /** diff --git a/src/java/org/lwjgl/test/opengl/VBOTest.java b/src/java/org/lwjgl/test/opengl/VBOTest.java index 9a613f85..322b210f 100644 --- a/src/java/org/lwjgl/test/opengl/VBOTest.java +++ b/src/java/org/lwjgl/test/opengl/VBOTest.java @@ -72,10 +72,9 @@ public final class VBOTest { } } - public static final GLWindow gl = new GLWindow("LWJGL Game Example", 16, 0, 0,0); static { try { - gl.create(); + Window.create("LWJGL Game Example", 16, 0, 0,0); System.out.println("Created OpenGL."); } catch (Exception e) { System.err.println("Failed to create OpenGL due to "+e); @@ -210,7 +209,7 @@ public final class VBOTest { GL.glDeleteBuffersARB(int_buffer); Keyboard.destroy(); Mouse.destroy(); - gl.destroy(); + Window.destroy(); try { Display.resetDisplayMode(); } catch (Exception e) { diff --git a/src/native/common/org_lwjgl_Window.h b/src/native/common/org_lwjgl_Window.h deleted file mode 100644 index ae259d36..00000000 --- a/src/native/common/org_lwjgl_Window.h +++ /dev/null @@ -1,86 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class org_lwjgl_Window */ - -#ifndef _Included_org_lwjgl_Window -#define _Included_org_lwjgl_Window -#ifdef __cplusplus -extern "C" { -#endif -/* Inaccessible static: _00024assertionsDisabled */ -/* Inaccessible static: created */ -/* Inaccessible static: minimized */ -/* Inaccessible static: focused */ -/* Inaccessible static: closeRequested */ -/* Inaccessible static: dirty */ -/* Inaccessible static: x */ -/* Inaccessible static: y */ -/* Inaccessible static: width */ -/* Inaccessible static: height */ -/* Inaccessible static: title */ -/* Inaccessible static: color */ -/* Inaccessible static: alpha */ -/* Inaccessible static: depth */ -/* Inaccessible static: stencil */ -/* Inaccessible static: fullscreen */ -/* Inaccessible static: class_000240 */ -/* - * Class: org_lwjgl_Window - * Method: nSetTitle - * Signature: (Ljava/lang/String;)V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_nSetTitle - (JNIEnv *, jclass, jstring); - -/* - * Class: org_lwjgl_Window - * Method: minimize - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_minimize - (JNIEnv *, jclass); - -/* - * Class: org_lwjgl_Window - * Method: restore - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_restore - (JNIEnv *, jclass); - -/* - * Class: org_lwjgl_Window - * Method: swapBuffers - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_swapBuffers - (JNIEnv *, jclass); - -/* - * Class: org_lwjgl_Window - * Method: nCreate - * Signature: (Ljava/lang/String;IIIIZIIII)V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_nCreate - (JNIEnv *, jclass, jstring, jint, jint, jint, jint, jboolean, jint, jint, jint, jint); - -/* - * Class: org_lwjgl_Window - * Method: nDestroy - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_nDestroy - (JNIEnv *, jclass); - -/* - * Class: org_lwjgl_Window - * Method: tick - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_tick - (JNIEnv *, jclass); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/native/common/org_lwjgl_opengl_GL.cpp b/src/native/common/org_lwjgl_opengl_GL.cpp index 2fa3a261..764f0192 100644 --- a/src/native/common/org_lwjgl_opengl_GL.cpp +++ b/src/native/common/org_lwjgl_opengl_GL.cpp @@ -47,7 +47,9 @@ #include "extgl.h" #include "checkGLerror.h" +#ifdef _WIN32 extern HDC hdc; +#endif static inline void * safeGetBufferAddress(JNIEnv *env, jobject buffer) { if (buffer == NULL) diff --git a/src/native/common/org_lwjgl_opengl_Window.h b/src/native/common/org_lwjgl_opengl_Window.h new file mode 100644 index 00000000..2f93e5c2 --- /dev/null +++ b/src/native/common/org_lwjgl_opengl_Window.h @@ -0,0 +1,114 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_lwjgl_opengl_Window */ + +#ifndef _Included_org_lwjgl_opengl_Window +#define _Included_org_lwjgl_opengl_Window +#ifdef __cplusplus +extern "C" { +#endif +/* Inaccessible static: _00024assertionsDisabled */ +/* Inaccessible static: created */ +/* Inaccessible static: x */ +/* Inaccessible static: y */ +/* Inaccessible static: width */ +/* Inaccessible static: height */ +/* Inaccessible static: title */ +/* Inaccessible static: color */ +/* Inaccessible static: alpha */ +/* Inaccessible static: depth */ +/* Inaccessible static: stencil */ +/* Inaccessible static: fullscreen */ +/* Inaccessible static: class_00024org_00024lwjgl_00024opengl_00024Window */ +/* + * Class: org_lwjgl_opengl_Window + * Method: nSetTitle + * Signature: (Ljava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nSetTitle + (JNIEnv *, jclass, jstring); + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsCloseRequested + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsCloseRequested + (JNIEnv *, jclass); + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsMinimized + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsMinimized + (JNIEnv *, jclass); + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsFocused + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsFocused + (JNIEnv *, jclass); + +/* + * Class: org_lwjgl_opengl_Window + * Method: minimize + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_minimize + (JNIEnv *, jclass); + +/* + * Class: org_lwjgl_opengl_Window + * Method: restore + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_restore + (JNIEnv *, jclass); + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsDirty + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsDirty + (JNIEnv *, jclass); + +/* + * Class: org_lwjgl_opengl_Window + * Method: swapBuffers + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_swapBuffers + (JNIEnv *, jclass); + +/* + * Class: org_lwjgl_opengl_Window + * Method: nCreate + * Signature: (Ljava/lang/String;IIIIZIIII)V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nCreate + (JNIEnv *, jclass, jstring, jint, jint, jint, jint, jboolean, jint, jint, jint, jint); + +/* + * Class: org_lwjgl_opengl_Window + * Method: nDestroy + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nDestroy + (JNIEnv *, jclass); + +/* + * Class: org_lwjgl_opengl_Window + * Method: tick + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_tick + (JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/native/configure.in b/src/native/configure.in index 014c92f5..c2f1bb91 100644 --- a/src/native/configure.in +++ b/src/native/configure.in @@ -51,8 +51,8 @@ if test "x$JAVA_HOME" = x; then else AC_MSG_RESULT($JAVA_HOME) JAVA_HOME="$JAVA_HOME" - CPPFLAGS="$CPPFLAGS -fno-rtti -fno-exceptions -pthread -D_X11 -Wall -I$JAVA_HOME/include -I$JAVA_HOME/include/linux" - CFLAGS="$CFLAGS -pthread -D_X11 -Wall -I$JAVA_HOME/include -I$JAVA_HOME/include/linux" + CPPFLAGS="$CPPFLAGS -D_DEBUG -fno-rtti -fno-exceptions -pthread -D_X11 -Wall -I$JAVA_HOME/include -I$JAVA_HOME/include/linux" + CFLAGS="$CFLAGS -D_DEBUG -pthread -D_X11 -Wall -I$JAVA_HOME/include -I$JAVA_HOME/include/linux" fi dnl Checks for libraries. diff --git a/src/native/linux/Makefile.am b/src/native/linux/Makefile.am index bbff0d22..4e534277 100644 --- a/src/native/linux/Makefile.am +++ b/src/native/linux/Makefile.am @@ -10,9 +10,8 @@ NATIVE = \ org_lwjgl_input_Keyboard.cpp \ org_lwjgl_input_Mouse.cpp \ org_lwjgl_input_Cursor.cpp \ - org_lwjgl_opengl_GLWindow.cpp \ + org_lwjgl_opengl_Window.cpp \ org_lwjgl_opengl_GLCaps.cpp \ org_lwjgl_opengl_Pbuffer.cpp \ - org_lwjgl_Window.cpp \ extxcursor.cpp diff --git a/src/native/linux/Window.h b/src/native/linux/Window.h index a46dc1bb..671b2af9 100644 --- a/src/native/linux/Window.h +++ b/src/native/linux/Window.h @@ -70,16 +70,6 @@ extern void acquireKeyboard(void); extern void acquirePointer(void); - /* - * destroy the window - */ - extern void destroyWindow(void); - - /* - * create a new X window with the specified visual - */ - extern void createWindow(JNIEnv *env, Display *disp, int screen, XVisualInfo *vis_info, jstring title, int x, int y, int width, int height, bool fullscreen); - /* * get the current window width */ diff --git a/src/native/linux/org_lwjgl_Window.cpp b/src/native/linux/org_lwjgl_Window.cpp deleted file mode 100644 index d018fe32..00000000 --- a/src/native/linux/org_lwjgl_Window.cpp +++ /dev/null @@ -1,262 +0,0 @@ -/* - * Copyright (c) 2002 Light Weight Java Game Library 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 'Light Weight Java Game Library' 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. - */ - -/** - * $Id$ - * - * Linux specific window functions. - * - * @author elias_naur - * @version $Revision$ - */ - - -#include -#include -#include -#include -#include -#include -#include -#include "Window.h" -#include "org_lwjgl_Window.h" - -static Atom delete_atom; -static Display *current_disp; -static Window current_win; -static int current_screen; -static bool current_fullscreen; -static int current_height; -static int current_width; - -static bool input_released; - -static void waitMapped(Display *disp, Window win) { - XEvent event; - - do { - XMaskEvent(disp, StructureNotifyMask, &event); - } while ((event.type != MapNotify) || (event.xmap.event != win)); -} - -static void acquireInput(void) { - if (input_released) { - acquireKeyboard(); - acquirePointer(); - input_released = false; - } -} - -static void doReleaseInput(void) { - releaseKeyboard(); - releasePointer(); - input_released = true; -} - -void updateInput(void) { - if (!input_released) { - doReleaseInput(); - acquireInput(); - } -} - -bool releaseInput(void) { - if (current_fullscreen || input_released) - return false; - doReleaseInput(); - return true; -} - -bool isFullscreen(void) { - return current_fullscreen; -} - -static void handleMessages(JNIEnv *env, jobject window_obj) { - XEvent event; - while (XPending(current_disp) > 0) { - XNextEvent(current_disp, &event); - switch (event.type) { - case ClientMessage: - if ((event.xclient.format == 32) && ((Atom)event.xclient.data.l[0] == delete_atom)) - env->SetBooleanField(window_obj, env->GetFieldID(env->GetObjectClass(window_obj), "closeRequested", "Z"), JNI_TRUE); - break; - case FocusOut: - releaseInput(); - env->SetBooleanField(window_obj, env->GetFieldID(env->GetObjectClass(window_obj), "focused", "Z"), JNI_FALSE); - break; - case FocusIn: - acquireInput(); - env->SetBooleanField(window_obj, env->GetFieldID(env->GetObjectClass(window_obj), "focused", "Z"), JNI_TRUE); - break; - case MapNotify: - env->SetBooleanField(window_obj, env->GetFieldID(env->GetObjectClass(window_obj), "dirty", "Z"), JNI_TRUE); - env->SetBooleanField(window_obj, env->GetFieldID(env->GetObjectClass(window_obj), "minimized", "Z"), JNI_FALSE); - break; - case UnmapNotify: - env->SetBooleanField(window_obj, env->GetFieldID(env->GetObjectClass(window_obj), "minimized", "Z"), JNI_TRUE); - break; - case Expose: -// XSetInputFocus(current_disp, current_win, RevertToParent, CurrentTime); - env->SetBooleanField(window_obj, env->GetFieldID(env->GetObjectClass(window_obj), "dirty", "Z"), JNI_TRUE); - break; - case ButtonPress: - handleButtonPress(&(event.xbutton)); - break; - case ButtonRelease: - handleButtonRelease(&(event.xbutton)); - break; - case MotionNotify: - handlePointerMotion(&(event.xmotion)); - break; - case KeyPress: - case KeyRelease: - handleKeyEvent(&(event.xkey)); - break; - } - } -} - -static void setWindowTitle(const char *title) { - XStoreName(current_disp, current_win, title); -} - -JNIEXPORT void JNICALL Java_org_lwjgl_Window_nSetTitle - (JNIEnv * env, jobject obj, jstring title_obj) -{ - const char * title = env->GetStringUTFChars(title_obj, NULL); - setWindowTitle(title); - env->ReleaseStringUTFChars(title_obj, title); -} - -void createWindow(JNIEnv* env, Display *disp, int screen, XVisualInfo *vis_info, jstring title, int x, int y, int width, int height, bool fullscreen) { - Window root_win; - Window win; - XSetWindowAttributes attribs; - Colormap cmap; - int attribmask; - - current_disp = disp; - current_screen = screen; - input_released = false; - current_fullscreen = fullscreen; - current_width = width; - current_height = height; - - root_win = RootWindow(disp, screen); - cmap = XCreateColormap(disp, root_win, vis_info->visual, AllocNone); - attribs.colormap = cmap; - attribs.event_mask = ExposureMask | FocusChangeMask | VisibilityChangeMask| StructureNotifyMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask; - attribs.background_pixel = 0xFF000000; - attribmask = CWColormap | CWBackPixel | CWEventMask; - if (fullscreen) { - attribmask |= CWOverrideRedirect; - attribs.override_redirect = True; - } - win = XCreateWindow(disp, root_win, x, y, width, height, 0, vis_info->depth, InputOutput, vis_info->visual, attribmask, &attribs); - XFreeColormap(disp, cmap); -#ifdef _DEBUG - printf("Created window\n"); -#endif - current_win = win; - Java_org_lwjgl_Window_nSetTitle(env, NULL, title); - XSizeHints * size_hints = XAllocSizeHints(); - size_hints->flags = PMinSize | PMaxSize; - size_hints->min_width = width; - size_hints->max_width = width; - size_hints->min_height = height; - size_hints->max_height = height; - XSetWMNormalHints(disp, win, size_hints); - XFree(size_hints); - delete_atom = XInternAtom(disp, "WM_DELETE_WINDOW", False); - XSetWMProtocols(disp, win, &delete_atom, 1); - XMapRaised(disp, win); - waitMapped(disp, win); - XClearWindow(disp, win); - XSetInputFocus(current_disp, current_win, RevertToParent, CurrentTime); - XSync(disp, True); -} - -void destroyWindow() { - XDestroyWindow(current_disp, current_win); - current_disp = NULL; -} - -Display *getCurrentDisplay(void) { - return current_disp; -} - -int getCurrentScreen(void) { - return current_screen; -} - -Window getCurrentWindow(void) { - return current_win; -} - -int getWindowWidth(void) { - return current_width; -} - -int getWindowHeight(void) { - return current_height; -} - -/* - * Class: org_lwjgl_Window - * Method: tick - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_tick - (JNIEnv *env, jobject obj) -{ - handleMessages(env, obj); -} - -/* - * Utility function to throw an Exception - */ -void throwException(JNIEnv * env, const char * err) -{ - jclass cls = env->FindClass("java/lang/Exception"); - env->ThrowNew(cls, err); - env->DeleteLocalRef(cls); -} - -/* - * Utility function to throw a RuntimeException - */ -void throwRuntimeException(JNIEnv * env, const char * err) -{ - jclass cls = env->FindClass("java/lang/RuntimeException"); - env->ThrowNew(cls, err); - env->DeleteLocalRef(cls); -} diff --git a/src/native/linux/org_lwjgl_opengl_GLWindow.cpp b/src/native/linux/org_lwjgl_opengl_Window.cpp similarity index 53% rename from src/native/linux/org_lwjgl_opengl_GLWindow.cpp rename to src/native/linux/org_lwjgl_opengl_Window.cpp index 1b21c65b..248a8115 100644 --- a/src/native/linux/org_lwjgl_opengl_GLWindow.cpp +++ b/src/native/linux/org_lwjgl_opengl_Window.cpp @@ -1,53 +1,282 @@ -/* +/* * Copyright (c) 2002 Light Weight Java Game Library Project * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are + * modification, are permitted provided that the following conditions are * met: - * - * * Redistributions of source code must retain the above copyright + * + * * 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 'Light Weight Java Game Library' nor the names of - * its contributors may be used to endorse or promote products derived + * * Neither the name of 'Light Weight Java Game Library' 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 + * 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 + * 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. */ - + /** * $Id$ * - * Base linux functionality for GL. + * Linux specific window functions. * * @author elias_naur * @version $Revision$ */ + +#include +#include +#include +#include +#include +#include +#include #include "extgl.h" #include "Window.h" -#include "org_lwjgl_opengl_GLWindow.h" +#include "org_lwjgl_opengl_Window.h" #define USEGLX13 true static GLXContext context = NULL; // OpenGL rendering context static GLXWindow glx_window; +static Atom delete_atom; +static Display *current_disp; +static Window current_win; +static int current_screen; +static bool current_fullscreen; +static int current_height; +static int current_width; + +static bool input_released; + +static bool dirty; +static bool minimized; +static bool focused; +static bool closerequested; + +static void waitMapped(Display *disp, Window win) { + XEvent event; + + do { + XMaskEvent(disp, StructureNotifyMask, &event); + } while ((event.type != MapNotify) || (event.xmap.event != win)); +} + +static void acquireInput(void) { + if (input_released) { + acquireKeyboard(); + acquirePointer(); + input_released = false; + } +} + +static void doReleaseInput(void) { + releaseKeyboard(); + releasePointer(); + input_released = true; +} + +void updateInput(void) { + if (!input_released) { + doReleaseInput(); + acquireInput(); + } +} + +bool releaseInput(void) { + if (current_fullscreen || input_released) + return false; + doReleaseInput(); + return true; +} + +bool isFullscreen(void) { + return current_fullscreen; +} + +static void handleMessages() { + XEvent event; + while (XPending(current_disp) > 0) { + XNextEvent(current_disp, &event); + switch (event.type) { + case ClientMessage: + if ((event.xclient.format == 32) && ((Atom)event.xclient.data.l[0] == delete_atom)) + closerequested = true; + break; + case FocusOut: + releaseInput(); + focused = false; + break; + case FocusIn: + acquireInput(); + focused = true; + break; + case MapNotify: + dirty = true; + minimized = false; + break; + case UnmapNotify: + minimized = true; + break; + case Expose: +// XSetInputFocus(current_disp, current_win, RevertToParent, CurrentTime); + dirty = true; + break; + case ButtonPress: + handleButtonPress(&(event.xbutton)); + break; + case ButtonRelease: + handleButtonRelease(&(event.xbutton)); + break; + case MotionNotify: + handlePointerMotion(&(event.xmotion)); + break; + case KeyPress: + case KeyRelease: + handleKeyEvent(&(event.xkey)); + break; + } + } +} + +static void setWindowTitle(const char *title) { + XStoreName(current_disp, current_win, title); +} + +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nSetTitle + (JNIEnv * env, jclass clazz, jstring title_obj) +{ + const char * title = env->GetStringUTFChars(title_obj, NULL); + setWindowTitle(title); + env->ReleaseStringUTFChars(title_obj, title); +} + +static void createWindow(JNIEnv* env, Display *disp, int screen, XVisualInfo *vis_info, jstring title, int x, int y, int width, int height, bool fullscreen) { + dirty = true; + focused = true; + minimized = false; + closerequested = false; + + Window root_win; + Window win; + XSetWindowAttributes attribs; + Colormap cmap; + int attribmask; + + current_disp = disp; + current_screen = screen; + input_released = false; + current_fullscreen = fullscreen; + current_width = width; + current_height = height; + + root_win = RootWindow(disp, screen); + cmap = XCreateColormap(disp, root_win, vis_info->visual, AllocNone); + attribs.colormap = cmap; + attribs.event_mask = ExposureMask | FocusChangeMask | VisibilityChangeMask| StructureNotifyMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask; + attribs.background_pixel = 0xFF000000; + attribmask = CWColormap | CWBackPixel | CWEventMask; + if (fullscreen) { + attribmask |= CWOverrideRedirect; + attribs.override_redirect = True; + } + win = XCreateWindow(disp, root_win, x, y, width, height, 0, vis_info->depth, InputOutput, vis_info->visual, attribmask, &attribs); + XFreeColormap(disp, cmap); +#ifdef _DEBUG + printf("Created window\n"); +#endif + current_win = win; + Java_org_lwjgl_opengl_Window_nSetTitle(env, NULL, title); + XSizeHints * size_hints = XAllocSizeHints(); + size_hints->flags = PMinSize | PMaxSize; + size_hints->min_width = width; + size_hints->max_width = width; + size_hints->min_height = height; + size_hints->max_height = height; + XSetWMNormalHints(disp, win, size_hints); + XFree(size_hints); + delete_atom = XInternAtom(disp, "WM_DELETE_WINDOW", False); + XSetWMProtocols(disp, win, &delete_atom, 1); + XMapRaised(disp, win); + waitMapped(disp, win); + XClearWindow(disp, win); + XSetInputFocus(current_disp, current_win, RevertToParent, CurrentTime); + XSync(disp, True); +} + +static void destroyWindow() { + XDestroyWindow(current_disp, current_win); + current_disp = NULL; +} + +Display *getCurrentDisplay(void) { + return current_disp; +} + +int getCurrentScreen(void) { + return current_screen; +} + +Window getCurrentWindow(void) { + return current_win; +} + +int getWindowWidth(void) { + return current_width; +} + +int getWindowHeight(void) { + return current_height; +} + +/* + * Class: org_lwjgl_Window + * Method: tick + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_tick + (JNIEnv *env, jclass clazz) +{ + handleMessages(); +} + +/* + * Utility function to throw an Exception + */ +void throwException(JNIEnv * env, const char * err) +{ + jclass cls = env->FindClass("java/lang/Exception"); + env->ThrowNew(cls, err); + env->DeleteLocalRef(cls); +} + +/* + * Utility function to throw a RuntimeException + */ +void throwRuntimeException(JNIEnv * env, const char * err) +{ + jclass cls = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(cls, err); + env->DeleteLocalRef(cls); +} + void makeCurrent(void) { if (USEGLX13 && extgl_Extensions.glx.GLX13) glXMakeContextCurrent(getCurrentDisplay(), glx_window, glx_window, context); @@ -208,8 +437,8 @@ static bool initWindowGLX(JNIEnv *env, Display *disp, int screen, jstring title, * Method: nCreate * Signature: (IIII)Z */ -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLWindow_nCreate - (JNIEnv * env, jobject obj, jstring title, jint x, jint y, jint width, jint height, jint bpp, jint alpha, jint depth, jint stencil, jboolean fullscreen) +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nCreate + (JNIEnv * env, jclass clazz, jstring title, jint x, jint y, jint width, jint height, jboolean fullscreen, jint bpp, jint alpha, jint depth, jint stencil) { int screen; Display *disp; @@ -260,8 +489,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLWindow_nCreate * Method: nDestroy * Signature: ()V */ -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLWindow_nDestroyGL - (JNIEnv * env, jobject obj) +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nDestroy + (JNIEnv *env, jclass clazz) { destroy(); } @@ -271,10 +500,71 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLWindow_nDestroyGL * Method: swapBuffers * Signature: ()V */ -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLWindow_swapBuffers(JNIEnv * env, jobject obj) +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_swapBuffers(JNIEnv * env, jclass clazz) { + dirty = false; if (USEGLX13 && extgl_Extensions.glx.GLX13) glXSwapBuffers(getCurrentDisplay(), glx_window); else glXSwapBuffers(getCurrentDisplay(), getCurrentWindow()); } + +/* + * Class: org_lwjgl_opengl_Window + * Method: minimize + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_minimize + (JNIEnv *env, jclass clazz) { +} + +/* + * Class: org_lwjgl_opengl_Window + * Method: restore + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_restore + (JNIEnv *env, jclass clazz) { +} + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsDirty + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsDirty + (JNIEnv *env, jclass clazz) { + return dirty; +} + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsMinimized + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsMinimized + (JNIEnv *env, jclass clazz) { + return minimized; +} + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsCloseRequested + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsCloseRequested + (JNIEnv *, jclass) { + bool saved = closerequested; + closerequested = false; + return saved; +} + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsFocused + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsFocused + (JNIEnv *env, jclass clazz) { + return focused; +} diff --git a/src/native/win32/org_lwjgl_Window.cpp b/src/native/win32/org_lwjgl_opengl_Window.cpp old mode 100644 new mode 100755 similarity index 85% rename from src/native/win32/org_lwjgl_Window.cpp rename to src/native/win32/org_lwjgl_opengl_Window.cpp index ef80af6f..098fd9d9 --- a/src/native/win32/org_lwjgl_Window.cpp +++ b/src/native/win32/org_lwjgl_opengl_Window.cpp @@ -1,600 +1,643 @@ -/* - * Copyright (c) 2002 Light Weight Java Game Library 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 'Light Weight Java Game Library' 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. - */ - -/** - * $Id: org_lwjglWindow.cpp,v 1.0 2003/02/12 09:29:07 cix_foo Exp $ - * - * Base Win32 window - * - * @author cix_foo - * @version $Revision: 1.0 $ - */ - -#define _PRIVATE_WINDOW_H_ -#include "Window.h" -#include "org_lwjgl_Window.h" - -bool oneShotInitialised = false; // Registers the LWJGL window class -HWND hwnd = NULL; // Handle to the window -HDC hdc = NULL; // Device context -HGLRC hglrc = NULL; // OpenGL context -LPDIRECTINPUT lpdi = NULL; // DirectInput -bool isFullScreen = false; // Whether we're fullscreen or not -bool isMinimized = false; // Whether we're minimized or not -JNIEnv * environment = NULL; // Cached environment -jclass window; // Cached Java Window class -extern HINSTANCE dll_handle; // Handle to the LWJGL dll -RECT clientSize; - -//CAS: commented these out as no longer used -//extern void tempRestoreDisplayMode(); -//extern void tempResetDisplayMode(); - -#define WINDOWCLASSNAME "LWJGL" - -/* - * Utility function to throw an Exception - */ -void throwException(JNIEnv * env, const char * err) -{ - jclass cls = env->FindClass("java/lang/Exception"); - env->ThrowNew(cls, err); - env->DeleteLocalRef(cls); -} - -/* - * Utility function to throw a RuntimeException - */ -void throwRuntimeException(JNIEnv * env, const char * err) -{ - jclass cls = env->FindClass("java/lang/RuntimeException"); - env->ThrowNew(cls, err); - env->DeleteLocalRef(cls); -} - -/* - * Find an appropriate pixel format - */ -static int findPixelFormat(JNIEnv *env, unsigned int flags, int bpp, int alpha, int depth, int stencil) -{ - PIXELFORMATDESCRIPTOR pfd = { - sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd - 1, // version number - flags, // RGBA type - PFD_TYPE_RGBA, - (BYTE)bpp, - 0, 0, 0, 0, 0, 0, // color bits ignored - (BYTE)alpha, - 0, // shift bit ignored - 0, // no accumulation buffer - 0, 0, 0, 0, // accum bits ignored - (BYTE)depth, - (BYTE)stencil, - 0, // No auxiliary buffer - PFD_MAIN_PLANE, // main layer - 0, // reserved - 0, 0, 0 // layer masks ignored - }; - - // get the best available match of pixel format for the device context - int iPixelFormat = ChoosePixelFormat(hdc, &pfd); - if (iPixelFormat == 0) { - throwException(env, "Failed to choose pixel format"); - return -1; - } - -#ifdef _DEBUG - printf("Pixel format is %d\n", iPixelFormat); -#endif - - // make that the pixel format of the device context - if (SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE) { - printf("Failed to set pixel format\n"); - throwException(env, "Failed to choose pixel format"); - return -1; - } - - // 3. Check the chosen format matches or exceeds our specifications - PIXELFORMATDESCRIPTOR desc; - if (DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &desc) == 0) { - throwException(env, "Could not describe pixel format"); - return -1; - } - - if (desc.cColorBits < bpp) { - throwException(env, "This application requires a greater colour depth"); - return -1; - } - - if (desc.cAlphaBits < alpha) { - throwException(env, "This application requires a greater alpha depth"); - return -1; - } - - if (desc.cStencilBits < stencil) { - throwException(env, "This application requires a greater stencil depth"); - return -1; - } - - if (desc.cDepthBits < depth) { - throwException(env, "This application requires a greater depth buffer depth"); - return -1; - } - - if ((desc.dwFlags & PFD_GENERIC_FORMAT) != 0 || (desc.dwFlags & PFD_GENERIC_ACCELERATED) != 0) { - throwException(env, "Mode not supported by hardware"); - return -1; - } - - if ((desc.dwFlags & flags) != flags) { - throwException(env, "Capabilities not supported"); - return -1; - } - - // 4. Initialise other things now - if (extgl_Open() != 0) { - throwException(env, "Failed to open extgl"); - return -1; - } - return iPixelFormat; -} -/* - * Create DirectInput. - * Returns true for success, or false for failure - */ -static bool createDirectInput() -{ - // Create input - HRESULT ret = DirectInputCreate(dll_handle, DIRECTINPUT_VERSION, &lpdi, NULL); - if (ret != DI_OK && ret != DIERR_BETADIRECTINPUTVERSION ) { - printf("Failed to create directinput"); - switch (ret) { - case DIERR_INVALIDPARAM : - printf(" - Invalid parameter\n"); - break; - case DIERR_OLDDIRECTINPUTVERSION : - printf(" - Old Version\n"); - break; - case DIERR_OUTOFMEMORY : - printf(" - Out Of Memory\n"); - break; - default: - printf(" - Unknown failure\n"); - } - return false; - } else { - return true; - } -} - -/* - * Close the window - */ -static void closeWindow() -{ - // Release DirectInput - if (lpdi != NULL) { -#ifdef _DEBUG - printf("Destroying directinput\n"); -#endif - lpdi->Release(); - lpdi = NULL; - } - - // Release device context - if (hdc != NULL && hwnd != NULL) { -#ifdef _DEBUG - printf("Releasing DC\n"); -#endif - ReleaseDC(hwnd, hdc); - } - - // Close the window - if (hwnd != NULL) { -#ifdef _DEBUG - printf("Destroy window\n"); -#endif - // Vape the window - DestroyWindow(hwnd); -#ifdef _DEBUG - printf("Destroyed window\n"); -#endif - hwnd = NULL; - } -} - -/* - * Called when the application is alt-tabbed to or from - */ -static void appActivate(bool active) -{ -// if (!active) { -// tempResetDisplayMode(); -// } - if (active) { - SetForegroundWindow(hwnd); - ShowWindow(hwnd, SW_RESTORE); - } else if (isFullScreen) { - ShowWindow(hwnd, SW_MINIMIZE); - } -// if (active) { -// tempRestoreDisplayMode(); -// } -} - -/* - * WindowProc for the GL window. - */ -LRESULT CALLBACK lwjglWindowProc(HWND hWnd, - UINT msg, - WPARAM wParam, - LPARAM lParam) -{ - if (environment == NULL) { - return DefWindowProc(hWnd, msg, wParam, lParam); - } - - switch (msg) { - // disable screen saver and monitor power down messages which wreak havoc - case WM_SYSCOMMAND: - { - switch (wParam) { - case SC_SCREENSAVE: - case SC_MONITORPOWER: - return 0L; - case SC_MINIMIZE: - environment->SetStaticBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), JNI_TRUE); - appActivate(false); - break; - case SC_RESTORE: - environment->SetStaticBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), JNI_FALSE); - appActivate(true); - break; - case SC_CLOSE: - environment->SetStaticBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "closeRequested", "Z"), JNI_TRUE); - //don't continue processing this command since this - //would shutdown the window, which the application might not want to - return 0L; - } - } - break; - case WM_ACTIVATE: - { - switch(LOWORD(wParam)) { - case WA_ACTIVE: - case WA_CLICKACTIVE: - environment->SetStaticBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), JNI_FALSE); - isMinimized = false; - - break; - case WA_INACTIVE: - environment->SetStaticBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), JNI_TRUE); - isMinimized = true; - - break; - } - appActivate(!isMinimized); - } - break; - case WM_QUIT: - { - environment->SetStaticBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "closeRequested", "Z"), JNI_TRUE); - return 0L; - } - case WM_PAINT: - { - environment->SetStaticBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "dirty", "Z"), JNI_TRUE); - } - } - - // default action - return DefWindowProc(hWnd, msg, wParam, lParam); -} - -/* - * Register the LWJGL window class. - * Returns true for success, or false for failure - */ -static bool registerWindow() -{ - if (!oneShotInitialised) { - WNDCLASS windowClass; - - windowClass.style = CS_GLOBALCLASS | CS_OWNDC; - windowClass.lpfnWndProc = lwjglWindowProc; - windowClass.cbClsExtra = 0; - windowClass.cbWndExtra = 0; - windowClass.hInstance = dll_handle; - windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); - windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); - windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); - windowClass.lpszMenuName = NULL; - windowClass.lpszClassName = WINDOWCLASSNAME; - - if (RegisterClass(&windowClass) == 0) { - printf("Failed to register window class\n"); - return false; - } -#ifdef _DEBUG - printf("Window registered\n"); -#endif - oneShotInitialised = true; - } - - return true; -} - -/* - * Handle native Win32 messages - */ -static void handleMessages(JNIEnv * env, jclass clazz) -{ - // Cache env and obj - environment = env; - window = clazz; - - /* - * Now's our chance to deal with Windows messages that are - * otherwise just piling up and causing everything not to - * work properly - */ - MSG msg; - while (PeekMessage( - &msg, // message information - hwnd, // handle to window - 0, // first message - 0, // last message - PM_REMOVE // removal options - )) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - }; - environment = NULL; - window = NULL; -} - -/* - * Create a window with the specified title, position, size, and - * fullscreen attribute. The window will have DirectInput associated - * with it. - * - * Returns true for success, or false for failure - */ -static bool createWindow(const char * title, int x, int y, int width, int height, bool fullscreen) -{ - // 1. Register window class if necessary - if (!registerWindow()) - return false; - - // 2. Create the window - int exstyle, windowflags; - - if (fullscreen) { - exstyle = WS_EX_APPWINDOW | WS_EX_TOPMOST; - windowflags = WS_POPUP | WS_VISIBLE; - } else { - exstyle = WS_EX_APPWINDOW; - windowflags = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_VISIBLE | WS_MINIMIZEBOX | WS_SYSMENU; - } - - // If we're not a fullscreen window, adjust the height to account for the - // height of the title bar: - clientSize.bottom = height; - clientSize.left = 0; - clientSize.right = width; - clientSize.top = 0; - - AdjustWindowRectEx( - &clientSize, // client-rectangle structure - windowflags, // window styles - FALSE, // menu-present option - exstyle // extended window style - ); - - // Create the window now, using that class: - hwnd = CreateWindowEx ( - exstyle, - WINDOWCLASSNAME, - title, - windowflags, - x, y, clientSize.right - clientSize.left, clientSize.bottom - clientSize.top, - NULL, - NULL, - dll_handle, - NULL); - - if (hwnd == NULL) { - printf("Failed to create window\n"); - return false; - } - -#ifdef _DEBUG - printf("Created window\n"); -#endif - -// ShowWindow(hwnd, SW_SHOWNORMAL); - ShowWindow(hwnd, SW_SHOW); - UpdateWindow(hwnd); - SetForegroundWindow(hwnd); - SetFocus(hwnd); - - hdc = GetWindowDC(hwnd); - - // Success! Now you need to initialize a GL object, which creates a GL rendering context; - // and then to issue commands to it, you need to call gl::makeCurrent(). - - // 3. Hide the mouse if necessary - isFullScreen = fullscreen == JNI_TRUE; - - // 4. Create DirectInput - if (!createDirectInput()) { - // Close the window - closeWindow(); - return false; - - } - - return true; -} - -/* - * Class: org_lwjgl_Window - * Method: nSetTitle - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_nSetTitle - (JNIEnv * env, jclass clazz, jstring title_obj) -{ - const char * title = env->GetStringUTFChars(title_obj, NULL); - SetWindowText(hwnd, title); - env->ReleaseStringUTFChars(title_obj, title); -} - -/* - * Class: org_lwjgl_Window - * Method: tick - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_tick - (JNIEnv * env, jclass clazz) -{ - handleMessages(env, clazz); -} - - -/* - * Class: org_lwjgl_Window - * Method: minimize - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_minimize - (JNIEnv * env, jclass clazz) -{ - if (isMinimized) - return; - ShowWindow(hwnd, SW_MINIMIZE); -} - -/* - * Class: org_lwjgl_Window - * Method: minimize - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_restore - (JNIEnv * env, jclass clazz) -{ - if (!isMinimized) - return; - - ShowWindow(hwnd, SW_RESTORE); -} - -/* - * Class: org_lwjgl_Window - * Method: swapBuffers - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_swapBuffers - (JNIEnv * env, jclass clazz) -{ - wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); -} - -/* - * Class: org_lwjgl_Window - * Method: nCreate - * Signature: (Ljava/lang/String;IIIIZIIII)V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_nCreate - (JNIEnv * env, jclass clazz, jstring title, jint x, jint y, jint width, jint height, jboolean fullscreen, jint bpp, jint alpha, jint depth, jint stencil) -{ - // 1. Create a window - const char * titleString = env->GetStringUTFChars(title, NULL); - if (!createWindow(titleString, x, y, width, height, fullscreen == JNI_TRUE ? true : false)) { - env->ReleaseStringUTFChars((jstring) title, titleString); - closeWindow(); - throwException(env, "Failed to create the window."); - return; - } - env->ReleaseStringUTFChars(title, titleString); - - // 2. Choose a pixel format and set it - unsigned int flags = PFD_DRAW_TO_WINDOW | // support window - PFD_SUPPORT_OPENGL | // support OpenGL - PFD_DOUBLEBUFFER; // double buffered - - int iPixelFormat = findPixelFormat(env, flags, bpp, alpha, depth, stencil); - if (iPixelFormat == -1) { - closeWindow(); - return; - } - // Create a rendering context - hglrc = wglCreateContext(hdc); - if (hglrc == NULL) { - throwException(env, "Failed to create OpenGL rendering context"); - closeWindow(); - return; - } - - // Automatically make it the current context - wglMakeCurrent(hdc, hglrc); - - // Initialise GL extensions - if (extgl_Initialize() != 0) { - closeWindow(); - throwException(env, "Failed to initialize GL extensions"); - return; - } -} - -/* - * Class: org_lwjgl_Window - * Method: doDestroy - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Window_nDestroy - (JNIEnv * env, jclass clazz) -{ - wglMakeCurrent(NULL, NULL); - - // Delete the rendering context - if (hglrc != NULL) { -#ifdef _DEBUG - printf("Delete GL context\n"); -#endif - wglDeleteContext(hglrc); - hglrc = NULL; - } - closeWindow(); - extgl_Close(); -} +/* + * Copyright (c) 2002 Light Weight Java Game Library 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 'Light Weight Java Game Library' 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. + */ + +/** + * $Id$ + * + * Base Win32 window + * + * @author cix_foo + * @version $Revision$ + */ + +#define _PRIVATE_WINDOW_H_ +#include "Window.h" +#include "org_lwjgl_opengl_Window.h" + +bool oneShotInitialised = false; // Registers the LWJGL window class +HWND hwnd = NULL; // Handle to the window +HDC hdc = NULL; // Device context +HGLRC hglrc = NULL; // OpenGL context +LPDIRECTINPUT lpdi = NULL; // DirectInput +bool isFullScreen = false; // Whether we're fullscreen or not +bool isMinimized = false; // Whether we're minimized or not +/*JNIEnv * environment = NULL; // Cached environment +jclass window; // Cached Java Window class*/ +extern HINSTANCE dll_handle; // Handle to the LWJGL dll +RECT clientSize; + +static bool closerequested; +static bool minimized; +static bool focused; +static bool dirty; + +//CAS: commented these out as no longer used +//extern void tempRestoreDisplayMode(); +//extern void tempResetDisplayMode(); + +#define WINDOWCLASSNAME "LWJGL" + +/* + * Utility function to throw an Exception + */ +void throwException(JNIEnv * env, const char * err) +{ + jclass cls = env->FindClass("java/lang/Exception"); + env->ThrowNew(cls, err); + env->DeleteLocalRef(cls); +} + +/* + * Utility function to throw a RuntimeException + */ +void throwRuntimeException(JNIEnv * env, const char * err) +{ + jclass cls = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(cls, err); + env->DeleteLocalRef(cls); +} + +/* + * Find an appropriate pixel format + */ +static int findPixelFormat(JNIEnv *env, unsigned int flags, int bpp, int alpha, int depth, int stencil) +{ + PIXELFORMATDESCRIPTOR pfd = { + sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd + 1, // version number + flags, // RGBA type + PFD_TYPE_RGBA, + (BYTE)bpp, + 0, 0, 0, 0, 0, 0, // color bits ignored + (BYTE)alpha, + 0, // shift bit ignored + 0, // no accumulation buffer + 0, 0, 0, 0, // accum bits ignored + (BYTE)depth, + (BYTE)stencil, + 0, // No auxiliary buffer + PFD_MAIN_PLANE, // main layer + 0, // reserved + 0, 0, 0 // layer masks ignored + }; + + // get the best available match of pixel format for the device context + int iPixelFormat = ChoosePixelFormat(hdc, &pfd); + if (iPixelFormat == 0) { + throwException(env, "Failed to choose pixel format"); + return -1; + } + +#ifdef _DEBUG + printf("Pixel format is %d\n", iPixelFormat); +#endif + + // make that the pixel format of the device context + if (SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE) { + printf("Failed to set pixel format\n"); + throwException(env, "Failed to choose pixel format"); + return -1; + } + + // 3. Check the chosen format matches or exceeds our specifications + PIXELFORMATDESCRIPTOR desc; + if (DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &desc) == 0) { + throwException(env, "Could not describe pixel format"); + return -1; + } + + if (desc.cColorBits < bpp) { + throwException(env, "This application requires a greater colour depth"); + return -1; + } + + if (desc.cAlphaBits < alpha) { + throwException(env, "This application requires a greater alpha depth"); + return -1; + } + + if (desc.cStencilBits < stencil) { + throwException(env, "This application requires a greater stencil depth"); + return -1; + } + + if (desc.cDepthBits < depth) { + throwException(env, "This application requires a greater depth buffer depth"); + return -1; + } + + if ((desc.dwFlags & PFD_GENERIC_FORMAT) != 0 || (desc.dwFlags & PFD_GENERIC_ACCELERATED) != 0) { + throwException(env, "Mode not supported by hardware"); + return -1; + } + + if ((desc.dwFlags & flags) != flags) { + throwException(env, "Capabilities not supported"); + return -1; + } + + // 4. Initialise other things now + if (extgl_Open() != 0) { + throwException(env, "Failed to open extgl"); + return -1; + } + return iPixelFormat; +} +/* + * Create DirectInput. + * Returns true for success, or false for failure + */ +static bool createDirectInput() +{ + // Create input + HRESULT ret = DirectInputCreate(dll_handle, DIRECTINPUT_VERSION, &lpdi, NULL); + if (ret != DI_OK && ret != DIERR_BETADIRECTINPUTVERSION ) { + printf("Failed to create directinput"); + switch (ret) { + case DIERR_INVALIDPARAM : + printf(" - Invalid parameter\n"); + break; + case DIERR_OLDDIRECTINPUTVERSION : + printf(" - Old Version\n"); + break; + case DIERR_OUTOFMEMORY : + printf(" - Out Of Memory\n"); + break; + default: + printf(" - Unknown failure\n"); + } + return false; + } else { + return true; + } +} + +/* + * Close the window + */ +static void closeWindow() +{ + // Release DirectInput + if (lpdi != NULL) { +#ifdef _DEBUG + printf("Destroying directinput\n"); +#endif + lpdi->Release(); + lpdi = NULL; + } + + // Release device context + if (hdc != NULL && hwnd != NULL) { +#ifdef _DEBUG + printf("Releasing DC\n"); +#endif + ReleaseDC(hwnd, hdc); + } + + // Close the window + if (hwnd != NULL) { +#ifdef _DEBUG + printf("Destroy window\n"); +#endif + // Vape the window + DestroyWindow(hwnd); +#ifdef _DEBUG + printf("Destroyed window\n"); +#endif + hwnd = NULL; + } +} + +/* + * Called when the application is alt-tabbed to or from + */ +static void appActivate(bool active) +{ +// if (!active) { +// tempResetDisplayMode(); +// } + if (active) { + SetForegroundWindow(hwnd); + ShowWindow(hwnd, SW_RESTORE); + } else if (isFullScreen) { + ShowWindow(hwnd, SW_MINIMIZE); + } +// if (active) { +// tempRestoreDisplayMode(); +// } +} + +/* + * WindowProc for the GL window. + */ +LRESULT CALLBACK lwjglWindowProc(HWND hWnd, + UINT msg, + WPARAM wParam, + LPARAM lParam) +{ + switch (msg) { + // disable screen saver and monitor power down messages which wreak havoc + case WM_SYSCOMMAND: + { + switch (wParam) { + case SC_SCREENSAVE: + case SC_MONITORPOWER: + return 0L; + case SC_MINIMIZE: + minimized = true; + appActivate(false); + break; + case SC_RESTORE: + minimized = false; + appActivate(true); + break; + case SC_CLOSE: + closerequested = true; + //don't continue processing this command since this + //would shutdown the window, which the application might not want to + return 0L; + } + } + break; + case WM_ACTIVATE: + { + switch(LOWORD(wParam)) { + case WA_ACTIVE: + case WA_CLICKACTIVE: + minimized = false; + isMinimized = false; + + break; + case WA_INACTIVE: + minimized = true; + isMinimized = true; + + break; + } + appActivate(!isMinimized); + } + break; + case WM_QUIT: + { + closerequested = true; + return 0L; + } + case WM_PAINT: + { + dirty = true; + } + } + + // default action + return DefWindowProc(hWnd, msg, wParam, lParam); +} + +/* + * Register the LWJGL window class. + * Returns true for success, or false for failure + */ +static bool registerWindow() +{ + if (!oneShotInitialised) { + WNDCLASS windowClass; + + windowClass.style = CS_GLOBALCLASS | CS_OWNDC; + windowClass.lpfnWndProc = lwjglWindowProc; + windowClass.cbClsExtra = 0; + windowClass.cbWndExtra = 0; + windowClass.hInstance = dll_handle; + windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); + windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); + windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + windowClass.lpszMenuName = NULL; + windowClass.lpszClassName = WINDOWCLASSNAME; + + if (RegisterClass(&windowClass) == 0) { + printf("Failed to register window class\n"); + return false; + } +#ifdef _DEBUG + printf("Window registered\n"); +#endif + oneShotInitialised = true; + } + + return true; +} + +/* + * Handle native Win32 messages + */ +static void handleMessages(JNIEnv * env, jclass clazz) +{ + /* + * Now's our chance to deal with Windows messages that are + * otherwise just piling up and causing everything not to + * work properly + */ + MSG msg; + while (PeekMessage( + &msg, // message information + hwnd, // handle to window + 0, // first message + 0, // last message + PM_REMOVE // removal options + )) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + }; +} + +/* + * Create a window with the specified title, position, size, and + * fullscreen attribute. The window will have DirectInput associated + * with it. + * + * Returns true for success, or false for failure + */ +static bool createWindow(const char * title, int x, int y, int width, int height, bool fullscreen) +{ + // 1. Register window class if necessary + if (!registerWindow()) + return false; + + // 2. Create the window + int exstyle, windowflags; + + if (fullscreen) { + exstyle = WS_EX_APPWINDOW | WS_EX_TOPMOST; + windowflags = WS_POPUP | WS_VISIBLE; + } else { + exstyle = WS_EX_APPWINDOW; + windowflags = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_VISIBLE | WS_MINIMIZEBOX | WS_SYSMENU; + } + + // If we're not a fullscreen window, adjust the height to account for the + // height of the title bar: + clientSize.bottom = height; + clientSize.left = 0; + clientSize.right = width; + clientSize.top = 0; + + AdjustWindowRectEx( + &clientSize, // client-rectangle structure + windowflags, // window styles + FALSE, // menu-present option + exstyle // extended window style + ); + + // Create the window now, using that class: + hwnd = CreateWindowEx ( + exstyle, + WINDOWCLASSNAME, + title, + windowflags, + x, y, clientSize.right - clientSize.left, clientSize.bottom - clientSize.top, + NULL, + NULL, + dll_handle, + NULL); + + if (hwnd == NULL) { + printf("Failed to create window\n"); + return false; + } + +#ifdef _DEBUG + printf("Created window\n"); +#endif + +// ShowWindow(hwnd, SW_SHOWNORMAL); + ShowWindow(hwnd, SW_SHOW); + UpdateWindow(hwnd); + SetForegroundWindow(hwnd); + SetFocus(hwnd); + + hdc = GetWindowDC(hwnd); + + // Success! Now you need to initialize a GL object, which creates a GL rendering context; + // and then to issue commands to it, you need to call gl::makeCurrent(). + + // 3. Hide the mouse if necessary + isFullScreen = fullscreen == JNI_TRUE; + + // 4. Create DirectInput + if (!createDirectInput()) { + // Close the window + closeWindow(); + return false; + + } + + return true; +} + +/* + * Class: org_lwjgl_Window + * Method: nSetTitle + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nSetTitle + (JNIEnv * env, jclass clazz, jstring title_obj) +{ + const char * title = env->GetStringUTFChars(title_obj, NULL); + SetWindowText(hwnd, title); + env->ReleaseStringUTFChars(title_obj, title); +} + +/* + * Class: org_lwjgl_Window + * Method: tick + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_tick + (JNIEnv * env, jclass clazz) +{ + handleMessages(env, clazz); +} + + +/* + * Class: org_lwjgl_Window + * Method: minimize + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_minimize + (JNIEnv * env, jclass clazz) +{ + if (isMinimized) + return; + ShowWindow(hwnd, SW_MINIMIZE); +} + +/* + * Class: org_lwjgl_Window + * Method: minimize + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_restore + (JNIEnv * env, jclass clazz) +{ + if (!isMinimized) + return; + + ShowWindow(hwnd, SW_RESTORE); +} + +/* + * Class: org_lwjgl_Window + * Method: swapBuffers + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_swapBuffers + (JNIEnv * env, jclass clazz) +{ + dirty = false; + wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); +} + +/* + * Class: org_lwjgl_Window + * Method: nCreate + * Signature: (Ljava/lang/String;IIIIZIIII)V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nCreate + (JNIEnv * env, jclass clazz, jstring title, jint x, jint y, jint width, jint height, jboolean fullscreen, jint bpp, jint alpha, jint depth, jint stencil) +{ + closerequested = false; + minimized = false; + focused = true; + dirty = true; + + // 1. Create a window + const char * titleString = env->GetStringUTFChars(title, NULL); + if (!createWindow(titleString, x, y, width, height, fullscreen == JNI_TRUE ? true : false)) { + env->ReleaseStringUTFChars((jstring) title, titleString); + closeWindow(); + throwException(env, "Failed to create the window."); + return; + } + env->ReleaseStringUTFChars(title, titleString); + + // 2. Choose a pixel format and set it + unsigned int flags = PFD_DRAW_TO_WINDOW | // support window + PFD_SUPPORT_OPENGL | // support OpenGL + PFD_DOUBLEBUFFER; // double buffered + + int iPixelFormat = findPixelFormat(env, flags, bpp, alpha, depth, stencil); + if (iPixelFormat == -1) { + closeWindow(); + return; + } + // Create a rendering context + hglrc = wglCreateContext(hdc); + if (hglrc == NULL) { + throwException(env, "Failed to create OpenGL rendering context"); + closeWindow(); + return; + } + + // Automatically make it the current context + wglMakeCurrent(hdc, hglrc); + + // Initialise GL extensions + if (extgl_Initialize() != 0) { + closeWindow(); + throwException(env, "Failed to initialize GL extensions"); + return; + } +} + +/* + * Class: org_lwjgl_Window + * Method: doDestroy + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nDestroy + (JNIEnv * env, jclass clazz) +{ + wglMakeCurrent(NULL, NULL); + + // Delete the rendering context + if (hglrc != NULL) { +#ifdef _DEBUG + printf("Delete GL context\n"); +#endif + wglDeleteContext(hglrc); + hglrc = NULL; + } + closeWindow(); + extgl_Close(); +} + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsDirty + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsDirty + (JNIEnv *env, jclass clazz) { + return dirty; +} + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsMinimized + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsMinimized + (JNIEnv *env, jclass clazz) { + return minimized; +} + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsCloseRequested + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsCloseRequested + (JNIEnv *, jclass) { + bool saved = closerequested; + closerequested = false; + return saved; +} + +/* + * Class: org_lwjgl_opengl_Window + * Method: nIsFocused + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsFocused + (JNIEnv *env, jclass clazz) { + return focused; +}