diff --git a/src/java/org/lwjgl/Display.java b/src/java/org/lwjgl/Display.java index a43b3870..55269e4f 100644 --- a/src/java/org/lwjgl/Display.java +++ b/src/java/org/lwjgl/Display.java @@ -81,12 +81,18 @@ public final class Display { * destroyed. * * @param displayMode a display mode to choose + * @param alpha_bits number of alpha bits required + * @param depth_bits number of depth bits required + * @param stencil_bits number of stencil bits required * @param fullscreen whether to create the display fullscreen * @throws Exception if the display mode could not be set * @see #destroy() */ public static void create( DisplayMode displayMode, + int alpha_bits, + int depth_bits, + int stencil_bits, boolean fullscreen) throws Exception { @@ -97,6 +103,9 @@ public final class Display { displayMode.height, displayMode.bpp, displayMode.freq, + alpha_bits, + depth_bits, + stencil_bits, fullscreen)) throw new Exception("Failed to set display mode to " + displayMode); @@ -115,6 +124,9 @@ public final class Display { int height, int bpp, int freq, + int alpha_bits, + int depth_bits, + int stencil_bits, boolean fullscreen); /** diff --git a/src/java/org/lwjgl/opengl/BaseGL.java b/src/java/org/lwjgl/opengl/BaseGL.java index 9bd94550..a8d96e92 100644 --- a/src/java/org/lwjgl/opengl/BaseGL.java +++ b/src/java/org/lwjgl/opengl/BaseGL.java @@ -68,33 +68,13 @@ abstract class BaseGL { /** This GL will be valid for use in only one thread */ protected Thread renderThread; - /** The number of color bits */ - protected final int colorBits; - - /** The number of alpha bits */ - protected final int alphaBits; - - /** The number of depth bits */ - protected final int depthBits; - - /** The number of stencil bits */ - protected final int stencilBits; - /** * Constructor for BaseGL. The context is not created at this point; * to create the GL you must call create(). * - * @param colorBits the number of color bits (eg. 16, 24, 32) - * @param alphaBits the number of alpha bits (eg. 0 or 8) - * @param depthBits the number of depth bits (eg. 16 or 24) - * @param stencilBits the number of stencil bits (eg. 0 or 8) * @see #create() */ - public BaseGL(int colorBits, int alphaBits, int depthBits, int stencilBits) { - this.colorBits = colorBits; - this.alphaBits = alphaBits; - this.depthBits = depthBits; - this.stencilBits = stencilBits; + public BaseGL() { } /** @@ -106,7 +86,7 @@ abstract class BaseGL { public final void create() throws Exception{ if (created) return; - if (!nCreate(colorBits, alphaBits, depthBits, stencilBits)) + if (!nCreate()) throw new Exception("GL could not be created."); created = true; makeCurrent(); @@ -127,7 +107,7 @@ abstract class BaseGL { * @return true if the GL was created successfully * @see org.lwjgl.Display#create(org.lwjgl.DisplayMode, boolean) */ - private native boolean nCreate(int colorBits, int alphaBits, int depthBits, int stencilBits); + private native boolean nCreate(); /** * Destroy the GL context. Does nothing if the GL has not yet been created. diff --git a/src/java/org/lwjgl/opengl/CoreGL.java b/src/java/org/lwjgl/opengl/CoreGL.java index 6ef75a05..afd846fc 100644 --- a/src/java/org/lwjgl/opengl/CoreGL.java +++ b/src/java/org/lwjgl/opengl/CoreGL.java @@ -45,12 +45,8 @@ public class CoreGL extends BaseGL implements CoreGLConstants { /** * Constructor for CoreGL. */ - public CoreGL( - int colorBits, - int alphaBits, - int depthBits, - int stencilBits) { - super(colorBits, alphaBits, depthBits, stencilBits); + public CoreGL() { + super(); } public native void accum(int op, float value); diff --git a/src/java/org/lwjgl/opengl/GL.java b/src/java/org/lwjgl/opengl/GL.java index 613a7d68..c0c5a806 100644 --- a/src/java/org/lwjgl/opengl/GL.java +++ b/src/java/org/lwjgl/opengl/GL.java @@ -1535,8 +1535,8 @@ public class GL extends CoreGL implements GLConstants { /** * Constructor for GL. */ - public GL(int colorBits, int alphaBits, int depthBits, int stencilBits) { - super(colorBits, alphaBits, depthBits, stencilBits); + public GL() { + super(); } /** diff --git a/src/java/org/lwjgl/test/opengl/Game.java b/src/java/org/lwjgl/test/opengl/Game.java index 55090dab..28ba529f 100644 --- a/src/java/org/lwjgl/test/opengl/Game.java +++ b/src/java/org/lwjgl/test/opengl/Game.java @@ -55,7 +55,7 @@ public final class Game { for (int i = 0; i < modes.length; i ++) System.out.println(modes[i]); // For now let's just pick a mode we're certain to have - Display.create(new DisplayMode(640, 480, 16, 60), true); + Display.create(new DisplayMode(640, 480, 16, 60), 8, 16, 0, true); System.out.println("Created display."); } catch (Exception e) { System.err.println("Failed to create display due to "+e); @@ -63,7 +63,7 @@ public final class Game { } } - public static final GL gl = new GL(16, 0, 16, 8); + public static final GL gl = new GL(); public static final GLU glu = new GLU(gl); static { try { diff --git a/src/java/org/lwjgl/test/opengl/Grass.java b/src/java/org/lwjgl/test/opengl/Grass.java index 105e3e8a..2913846d 100644 --- a/src/java/org/lwjgl/test/opengl/Grass.java +++ b/src/java/org/lwjgl/test/opengl/Grass.java @@ -26,7 +26,7 @@ public class Grass { for (int i = 0; i < modes.length; i ++) System.out.println(modes[i]); // For now let's just pick a mode we're certain to have - Display.create(new DisplayMode(800, 600, 16, 60), false); + Display.create(new DisplayMode(800, 600, 16, 60), 8, 16, 0, false); System.out.println("Created display."); } catch (Exception e) { System.err.println("Failed to create display due to "+e); @@ -34,7 +34,7 @@ public class Grass { } } - public static final GL gl = new GL(16, 0, 16, 8); + public static final GL gl = new GL(); public static final GLU glu = new GLU(gl); static { try { diff --git a/src/native/common/org_lwjgl_Display.h b/src/native/common/org_lwjgl_Display.h index d8e05718..3ecfe512 100644 --- a/src/native/common/org_lwjgl_Display.h +++ b/src/native/common/org_lwjgl_Display.h @@ -26,7 +26,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_getAvailableDisplayModes * Signature: (IIIIZ)Z */ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate - (JNIEnv *, jclass, jint, jint, jint, jint, jboolean); + (JNIEnv *, jclass, jint, jint, jint, jint, jint, jint, jint, jboolean); /* * Class: org_lwjgl_Display diff --git a/src/native/common/org_lwjgl_opengl_BaseGL.h b/src/native/common/org_lwjgl_opengl_BaseGL.h index 696055ba..ecf8b4f2 100644 --- a/src/native/common/org_lwjgl_opengl_BaseGL.h +++ b/src/native/common/org_lwjgl_opengl_BaseGL.h @@ -16,7 +16,7 @@ extern "C" { * Signature: (IIII)Z */ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate - (JNIEnv *, jobject, jint, jint, jint, jint); + (JNIEnv *, jobject); /* * Class: org_lwjgl_opengl_BaseGL diff --git a/src/native/linux/org_lwjgl_Display.cpp b/src/native/linux/org_lwjgl_Display.cpp index 022f5715..4763349d 100644 --- a/src/native/linux/org_lwjgl_Display.cpp +++ b/src/native/linux/org_lwjgl_Display.cpp @@ -96,28 +96,43 @@ int getDisplayModes(Display *disp, int screen, int *num_modes, XF86VidModeModeIn return 1; } -JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate(JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq, jboolean fullscreen) { +JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate(JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq, jint alpha_bits, jint depth_bits, jint stencil_bits, jboolean fullscreen) { Window root_win; XSetWindowAttributes attribs; Colormap cmap; int attribmask; - int bpe = bpp/4; + int bpe; + switch (bpp) { + case 32: + case 24: + bpe = 8; + break; + case 16: + bpe = 4; + break; + default: + return JNI_FALSE; + } + if (depth_bits == 32) + depth_bits = 24; + int attriblist[] = { GLX_RGBA, GLX_DOUBLEBUFFER, - GLX_DEPTH_SIZE, bpp, + GLX_DEPTH_SIZE, depth_bits, GLX_RED_SIZE, bpe, GLX_GREEN_SIZE, bpe, GLX_BLUE_SIZE, bpe, - GLX_ALPHA_SIZE, bpe, + GLX_ALPHA_SIZE, alpha_bits, + GLX_STENCIL_SIZE, stencil_bits, None }; - int attriblistna[] = { GLX_RGBA, +/* int attriblistna[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, bpp, GLX_RED_SIZE, bpe, GLX_GREEN_SIZE, bpe, GLX_BLUE_SIZE, bpe, None }; - +*/ int num_modes, i; @@ -151,10 +166,10 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate(JNIEnv * env, jclass c vis_info = glXChooseVisual(disp, screen, attriblist); /* might be a better way to handle not being able to set GLX_ALPHA_SIZE... */ - if (vis_info == NULL) { +/* if (vis_info == NULL) { vis_info = glXChooseVisual(disp, screen, attriblistna); } - +*/ if (vis_info == NULL) { XCloseDisplay(disp); #ifdef _DEBUG diff --git a/src/native/linux/org_lwjgl_opengl_BaseGL.cpp b/src/native/linux/org_lwjgl_opengl_BaseGL.cpp index c72b1b28..e9b7056e 100644 --- a/src/native/linux/org_lwjgl_opengl_BaseGL.cpp +++ b/src/native/linux/org_lwjgl_opengl_BaseGL.cpp @@ -61,7 +61,7 @@ void releaseContext(void) { * Signature: (IIII)Z */ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate - (JNIEnv * env, jobject obj, jint colorBits, jint alphaBits, jint depthBits, jint stencilBits) + (JNIEnv * env, jobject obj) { if (!vis_info) { diff --git a/src/native/win32/org_lwjgl_Display.cpp b/src/native/win32/org_lwjgl_Display.cpp index 6ff239cd..e25ebb50 100644 --- a/src/native/win32/org_lwjgl_Display.cpp +++ b/src/native/win32/org_lwjgl_Display.cpp @@ -56,6 +56,37 @@ HWND hwnd = NULL; // Handle to the window HDC hdc = NULL; // Device context LPDIRECTINPUT lpdi = NULL; +void destroyDI(void) +{ + lpdi->Release(); + lpdi = NULL; +} + +void destroyWindow(void) +{ + // Reset the display if necessary + ChangeDisplaySettings(NULL, 0); + + if (hwnd != NULL) { + // Vape the window + DestroyWindow(hwnd); + hwnd = NULL; + } + +#ifdef _DEBUG + printf("Destroyed display\n"); +#endif + + // Show the mouse + ShowCursor(TRUE); +} + +void destroyAll(void) +{ + destroyDI(); + destroyWindow(); +} + void dumpLastError(void) { LPVOID lpMsgBuf; FormatMessage( @@ -192,7 +223,8 @@ JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_getAvailableDisplayModes * Signature: (IIIIZ)Z */ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate - (JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq, jboolean fullscreen) + (JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq, + jint alphaBits, jint depthBits, jint stencilBits, jboolean fullscreen) { #ifdef _DEBUG printf("Creating display: size %dx%d %dhz %dbpp...\n", width, height, freq, bpp); @@ -259,7 +291,7 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate printf("Failed to create directinput"); switch (ret) { case DIERR_BETADIRECTINPUTVERSION : - printf(" - Beta versio0n\n"); + printf(" - Beta version\n"); break; case DIERR_INVALIDPARAM : printf(" - Invalid parameter\n"); @@ -273,6 +305,91 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate default: printf("\n"); } + destroyWindow(); + return JNI_FALSE; + } + + int flags = PFD_DRAW_TO_WINDOW | // support window + PFD_SUPPORT_OPENGL | // support OpenGL + PFD_GENERIC_ACCELERATED | + PFD_DOUBLEBUFFER; // double buffered + + 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)alphaBits, + 0, // shift bit ignored + 0, // no accumulation buffer + 0, 0, 0, 0, // accum bits ignored + (BYTE)depthBits, + (BYTE)stencilBits, + 0, // One auxiliary buffer + PFD_MAIN_PLANE, // main layer + 0, // reserved + 0, 0, 0 // layer masks ignored + }; + + // Ensure desktop color depth is adequate + int availableBitDepth = GetDeviceCaps(hdc, BITSPIXEL); + if (availableBitDepth < bpp) { + printf("This application requires a greater colour depth.\n"); + destroyAll(); + return JNI_FALSE; + }; + + int iPixelFormat; + + // get the best available match of pixel format for the device context + iPixelFormat = ChoosePixelFormat(hdc, &pfd); + if (iPixelFormat == 0) { + printf("Failed to choose pixel format.\n"); + destroyAll(); + return JNI_FALSE; + } + + PIXELFORMATDESCRIPTOR desc; + if (DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &desc) == 0) { + printf("Could not describe pixel format\n"); + destroyAll(); + return JNI_FALSE; + } + + if (desc.cColorBits < bpp) { + printf("This application requires a greater colour depth.\n"); + destroyAll(); + return JNI_FALSE; + } + + if (desc.cStencilBits < stencilBits) { + printf("This application requires a greater stencil depth.\n"); + destroyAll(); + return JNI_FALSE; + } + + if (desc.cDepthBits < depthBits) { + printf("This application requires a greater depth buffer depth.\n"); + destroyAll(); + return JNI_FALSE; + } + + if ((desc.dwFlags & flags) == 0) { + printf("Capabilities not supported.\n"); + destroyAll(); + return JNI_FALSE; + } + +#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"); + destroyAll(); return JNI_FALSE; } @@ -290,19 +407,6 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate JNIEXPORT void JNICALL Java_org_lwjgl_Display_nDestroy (JNIEnv * env, jclass clazz) { - // Reset the display if necessary - ChangeDisplaySettings(NULL, 0); - - if (hwnd != NULL) { - // Vape the window - DestroyWindow(hwnd); - hwnd = NULL; - } - -#ifdef _DEBUG - printf("Destroyed display\n"); -#endif - - // Show the mouse - ShowCursor(TRUE); + destroyAll(); } + diff --git a/src/native/win32/org_lwjgl_input_Keyboard.cpp b/src/native/win32/org_lwjgl_input_Keyboard.cpp index cf97729d..fededee0 100644 --- a/src/native/win32/org_lwjgl_input_Keyboard.cpp +++ b/src/native/win32/org_lwjgl_input_Keyboard.cpp @@ -137,14 +137,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nDestroy lpdiKeyboard->Release(); lpdiKeyboard = NULL; } - - // Release directinput if the mouse is not present - if (lpdi != NULL) { - // Release directinput - lpdi->Release(); - lpdi = NULL; - - } } /* diff --git a/src/native/win32/org_lwjgl_input_Mouse.cpp b/src/native/win32/org_lwjgl_input_Mouse.cpp index 33dba34b..819bfc51 100644 --- a/src/native/win32/org_lwjgl_input_Mouse.cpp +++ b/src/native/win32/org_lwjgl_input_Mouse.cpp @@ -154,12 +154,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy lpdiMouse = NULL; } - // Release directinput - if (lpdi != NULL) { - // Release directinput - lpdi->Release(); - lpdi = NULL; - } } /* diff --git a/src/native/win32/org_lwjgl_opengl_BaseGL.cpp b/src/native/win32/org_lwjgl_opengl_BaseGL.cpp index dd541771..c5d1ddf5 100644 --- a/src/native/win32/org_lwjgl_opengl_BaseGL.cpp +++ b/src/native/win32/org_lwjgl_opengl_BaseGL.cpp @@ -53,88 +53,13 @@ extern HWND hwnd; * Signature: (IIII)Z */ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate - (JNIEnv * env, jobject obj, jint colorBits, jint alphaBits, jint depthBits, jint stencilBits) + (JNIEnv * env, jobject obj) { if (!hwnd) { printf("No window handle\n"); return JNI_FALSE; } - int flags = PFD_DRAW_TO_WINDOW | // support window - PFD_SUPPORT_OPENGL | // support OpenGL - PFD_GENERIC_ACCELERATED | - PFD_DOUBLEBUFFER; // double buffered - - PIXELFORMATDESCRIPTOR pfd = { - sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd - 1, // version number - flags, // RGBA type - PFD_TYPE_RGBA, - (BYTE)colorBits, - 0, 0, 0, 0, 0, 0, // color bits ignored - (BYTE)alphaBits, - 0, // shift bit ignored - 0, // no accumulation buffer - 0, 0, 0, 0, // accum bits ignored - (BYTE)depthBits, - (BYTE)stencilBits, - 0, // One auxiliary buffer - PFD_MAIN_PLANE, // main layer - 0, // reserved - 0, 0, 0 // layer masks ignored - }; - - // Ensure desktop color depth is adequate - int availableBitDepth = GetDeviceCaps(hdc, BITSPIXEL); - if (availableBitDepth < colorBits) { - printf("This application requires a greater colour depth.\n"); - return JNI_FALSE; - }; - - int iPixelFormat; - - // get the best available match of pixel format for the device context - iPixelFormat = ChoosePixelFormat(hdc, &pfd); - if (iPixelFormat == 0) { - printf("Failed to choose pixel format.\n"); - return JNI_FALSE; - } - - PIXELFORMATDESCRIPTOR desc; - if (DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &desc) == 0) { - printf("Could not describe pixel format\n"); - return JNI_FALSE; - } - - if (desc.cColorBits < colorBits) { - printf("This application requires a greater colour depth.\n"); - return JNI_FALSE; - } - - if (desc.cStencilBits < stencilBits) { - printf("This application requires a greater stencil depth.\n"); - return JNI_FALSE; - } - - if (desc.cDepthBits < depthBits) { - printf("This application requires a greater depth buffer depth.\n"); - return JNI_FALSE; - } - - if ((desc.dwFlags & flags) == 0) { - printf("Capabilities not supported.\n"); - return JNI_FALSE; - } - -#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"); - return JNI_FALSE; - } if (extgl_Open() != 0) return JNI_FALSE; // Create a rendering context