/* * 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 functionality for GL. * * @author cix_foo * @version $Revision$ */ #include #include "org_lwjgl_opengl_BaseGL.h" #include "extgl.h" #include "Window.h" #include "jni.h" HGLRC hglrc = NULL; // OpenGL rendering context 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; } /* * Class: org_lwjgl_opengl_BaseGL * Method: nCreate * Signature: (Ljava/lang/String;IIIIIIIIZ)V */ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_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) { // 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; } // Stash handle back in Java env->SetIntField(obj, env->GetFieldID(env->GetObjectClass(obj), "handle", "I"), (jint) hglrc); } /* * Class: org_lwjgl_opengl_BaseGL * Method: nDestroy * Signature: ()V */ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nDestroyGL (JNIEnv * env, jobject obj) { 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_BaseGL * Method: swapBuffers * Signature: ()V */ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_swapBuffers (JNIEnv *, jobject) { wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE); }