New Window class, and major changes to Display

This commit is contained in:
Caspian Rychlik-Prince 2003-03-28 01:28:04 +00:00
parent cab8f6f62f
commit 419da83107
13 changed files with 475 additions and 882 deletions

View file

@ -167,7 +167,7 @@ public abstract class Window {
/**
* Destroy the window.
*/
public final void destroy() {
public void destroy() {
currentWindow = null;
nDestroy();
}

View file

@ -32,7 +32,8 @@
package org.lwjgl.opengl;
import org.lwjgl.Sys;
import org.lwjgl.*;
import org.lwjgl.Window;
/**
* $Id$
@ -50,306 +51,74 @@ import org.lwjgl.Sys;
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
abstract class BaseGL {
static {
System.loadLibrary(Sys.getLibraryName());
}
public abstract class BaseGL extends Window {
/** The current rendering context */
private static BaseGL currentContext;
/** Has the GL been created yet? */
private boolean created;
/** Handle to the native context */
/** Handle to the native GL rendering context */
protected int handle;
/** This GL will be valid for use in only one thread */
protected Thread renderThread;
/**
* Constructor for BaseGL. The context is not created at this point;
* to create the GL you must call create().
*
* @see #create()
* Construct a windowed instance of GL. If the underlying OS does not
* support windowed mode, then the width and height must match the current
* display resolution, or an Exception will be thrown. Otherwise a fullscreen
* window will be created.
* @param title The title of the window
* @param x, y The position of the window. May be ignored.
* @param width, height The size of the window's client area
* @param bpp Require colour bits
* @param alpha Required alpha bits
* @param depth Required depth bits
* @param stencil Required stencil bits
*/
public BaseGL() {
public BaseGL(String title, int x, int y, int width, int height, int bpp, int alpha, int depth, int stencil) throws Exception {
super(title, x, y, width, height);
nCreate(title, x, y, width, height, false, bpp, alpha, depth, stencil);
}
/**
* Construct a fullscreen instance of GL. If the underlying OS does not
* support fullscreen mode, then a window will be created instead. If this
* fails too then an Exception will be thrown.
* @param title The title of the window
* @param x, y The position of the window. May be ignored.
* @param width, height The size of the window's client area
*/
public BaseGL(String title, int bpp, int alpha, int depth, int stencil) throws Exception {
super(title, 0, 0, Display.getWidth(), Display.getHeight());
nCreate(title, 0, 0, Display.getWidth(), Display.getHeight(), true, bpp, alpha, depth, stencil);
}
/**
* Creates the GL, with the best match it can get for the specified
* parameters. The display must first have been created.
*
* @throws Exception if the GL could not be created for some reason
* Native method to create a windowed GL
*/
public final void create() throws Exception{
if (created)
return;
if (!nCreate())
throw new Exception("GL could not be created.");
created = true;
makeCurrent();
init();
}
private native void nCreate(String title, int x, int y, int width, int height, boolean fullscreen, int bpp, int alpha, int depth, int stencil) throws Exception;
/**
* Override to provide any initialization code after creation.
*/
protected void init() {
}
/**
* Native method to create the GL. If successful then the native handle will
* be set. The GL is created on the current display, which must have been
* created by calling its setDisplayMode() method.
*
* @return true if the GL was created successfully
* @see org.lwjgl.Display#create(org.lwjgl.DisplayMode, boolean)
*/
private native boolean nCreate();
/**
* Destroy the GL context. Does nothing if the GL has not yet been created.
*/
public final void destroy() {
if (!created)
return;
cleanup();
created = false;
renderThread = null;
nDestroy();
}
/**
* Native method to destroy the GL context
*/
private native void nDestroy();
/**
* Provide any cleanup in derived classes here. This method is called
* just before the native context is destroyed.
*/
public void cleanup() {
}
/**
* Finalizer, marked final. To perform specialized cleanup override the
* cleanup() method.
*
* @see #cleanup()
* Finalizer, marked final. Ensures the window is destroyed.
*/
public final void finalize() throws Throwable {
super.finalize();
destroy();
}
/**
* Free the context from the current thread.
/* (non-Javadoc)
* @see org.lwjgl.Window#destroy()
*/
public final void releaseContext() {
assert created : "GL has not been created yet.";
renderThread = null;
currentContext = null;
nReleaseContext();
public void destroy() {
// Do native destroy first
super.destroy();
}
/**
* Make this the current context for the current thread.
* Natively destroy any GL-related stuff
*/
public final void makeCurrent() {
assert created : "GL has not been created yet.";
renderThread = Thread.currentThread();
currentContext = this;
nMakeCurrent();
}
/**
* Swap the buffers
*/
public native void swapBuffers();
/**
* Native method to free the context
*/
private native void nReleaseContext();
/**
* Native method to make this the current thread
*/
private native void nMakeCurrent();
/**
* @return true if this is the current rendering context and the correct
* thread
*/
public final boolean isValid() {
return created
&& currentContext == this
&& Thread.currentThread() == renderThread;
}
/**
* Create a fullscreen display. If the display has already been created then this
* method is a no-op.
*
* Alpha, depth, and stencil will be 0 bits.
*
* @param title The title for the application
* @throws Exception if the window could not be created for any reason
* @see #destroy()
*/
public static void create(String title) throws Exception {
create(title, 0, 0, 0);
}
/**
* Create a fullscreen display. If the display has already been created then this
* method is a no-op.
*
* @param title The title for the application
* @param alpha Minimun number of alpha bits on the display
* @param depth Minimun number of depth bits on the display
* @param stencil Minimun number of stencil bits on the display
* @throws Exception if the window could not be created for any reason
* @see #destroy()
*/
public static void create(String title, int alpha, int depth, int stencil)
throws Exception {
if (Display.created) {
return;
}
if (!nCreateFullscreen(title, alpha, depth, stencil)) {
throw new Exception("Failed to create fullscreen display.");
}
Display.created = true;
}
/**
* Create a windowed display. If the display has already been created then this
* method is a no-op.
*
* The window is not guaranteed to be positioned at (x, y). Nor is it guaranteed
* to have a drag bar, title bar, close button, or minimized button. It cannot be
* resized once created.
*
* The window will have 0 bits alpha, depth, and stencil.
*
* @param title The title for the application
* @param x, y The position of the window
* @param width, height The dimensions of the drawable area of the window
* @throws Exception if the window could not be created for any reason
* @see #destroy()
*/
public static void create(String title, int x, int y, int width, int height)
throws Exception {
create(title, x, y, width, height, 0, 0, 0);
}
/**
* Create a windowed display. If the display has already been created then this
* method is a no-op.
*
* The window is not guaranteed to be positioned at (x, y). Nor is it guaranteed
* to have a drag bar, title bar, close button, or minimized button. It cannot be
* resized once created.
*
* @param title The title for the application
* @param x, y The position of the window
* @param width, height The dimensions of the drawable area of the window
* @param alpha Minimun number of alpha bits on the display
* @param depth Minimun number of depth bits on the display
* @param stencil Minimun number of stencil bits on the display
* @throws Exception if the window could not be created for any reason
* @see #destroy()
*/
public static void create(String title, int x, int y, int width, int height, int alpha, int depth, int stencil)
throws Exception {
if (Display.created) {
return;
}
if (!nCreateWindowed(title, x, y, width, height, alpha, depth, stencil)) {
throw new Exception("Failed to create windowed display.");
}
Display.created = true;
}
/**
* Native method to create the display. This will set the handle if it is
* successful.
* @return true if the display was successfully created
* @see #create(org.lwjgl.DisplayMode, boolean)
*/
private static native boolean nCreateWindowed(
String title,
int x,
int y,
int width,
int height,
int alpha_bits,
int depth_bits,
int stencil_bits);
/**
* Native method to create the display. This will set the handle if it is
* successful.
* @return true if the display was successfully created
* @see #create(org.lwjgl.DisplayMode, boolean)
*/
private static native boolean nCreateFullscreen(
String title,
int alpha_bits,
int depth_bits,
int stencil_bits);
/**
* Destroy the display and return it to normal. If the display has not yet
* been created no action is taken.
*/
public static void destroy() {
if (!Display.created) {
return;
}
nDestroy();
Display.created = false;
Display.mode = null;
}
/**
* Native method to destroy the display. This will reset the handle.
*/
private static native void nDestroy();
/**
* Determines if the display is minimized. When the display is minimized it is
* effectively invisible, and you need perform no rendering in your game loop.
* On the native side, when the application is switched to some other application,
* the display window will minimize; when focus is regained, it will maximize and
* automatically gain focus and become the foreground window again.
* @return true if the display is minimized
*/
public static native boolean isMinimized();
/**
* Determines if the user has requested that the application should close.
* When a user has requested that the application should shutdown, it is up to
* the application to perform the actual shutdown and cleanup of any allocated
* resources.
*
* @return true if the user has requested that the application should close
*/
public static boolean isCloseRequested() {
return Display.closeRequested;
}
private native void nDestroy();
}

View file

@ -42,11 +42,33 @@ package org.lwjgl.opengl;
*/
public class CoreGL extends BaseGL implements CoreGLConstants {
/**
* Constructor for CoreGL.
* @param title
* @param x
* @param y
* @param width
* @param height
* @param bpp
* @param alpha
* @param depth
* @param stencil
* @throws Exception
*/
public CoreGL() {
super();
public CoreGL(String title, int x, int y, int width, int height, int bpp, int alpha, int depth, int stencil) throws Exception {
super(title, x, y, width, height, bpp, alpha, depth, stencil);
}
/**
* @param title
* @param bpp
* @param alpha
* @param depth
* @param stencil
* @throws Exception
*/
public CoreGL(String title, int bpp, int alpha, int depth, int stencil) throws Exception {
super(title, bpp, alpha, depth, stencil);
}
public native void accum(int op, float value);

View file

@ -48,6 +48,36 @@ import org.lwjgl.Display;
* @version $Revision$
*/
public class GL extends CoreGL implements GLConstants {
/**
* @param title
* @param x
* @param y
* @param width
* @param height
* @param bpp
* @param alpha
* @param depth
* @param stencil
* @throws Exception
*/
public GL(String title, int x, int y, int width, int height, int bpp, int alpha, int depth, int stencil) throws Exception {
super(title, x, y, width, height, bpp, alpha, depth, stencil);
determineAvailableExtensions();
}
/**
* @param title
* @param bpp
* @param alpha
* @param depth
* @param stencil
* @throws Exception
*/
public GL(String title, int bpp, int alpha, int depth, int stencil) throws Exception {
super(title, bpp, alpha, depth, stencil);
determineAvailableExtensions();
}
public native void activeStencilFaceEXT(int face);
public native void activeTextureARB(int texture);
@ -1540,13 +1570,6 @@ public class GL extends CoreGL implements GLConstants {
public boolean OpenGL13;
public boolean OpenGL14;
/**
* Constructor for GL.
*/
public GL() {
super();
}
/**
* Determine which extensions are available
*/
@ -1671,17 +1694,5 @@ public class GL extends CoreGL implements GLConstants {
}
}
/* (non-Javadoc)
* @see org.lwjgl.opengl.BaseGL#init()
*/
protected void init() {
super.init();
// Right after creation we can go and find out what extensions are
// available. We can't actually determine this beforehand of course
// because the available extensions can only be determined when there's
// an actual rendering context.
determineAvailableExtensions();
}
}

View file

@ -19,6 +19,14 @@ extern "C" {
#undef org_lwjgl_Display_PLATFORM_AGL
#define org_lwjgl_Display_PLATFORM_AGL 2L
/* Inaccessible static: class_000240 */
/*
* Class: org_lwjgl_Display
* Method: init
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_init
(JNIEnv *, jclass);
/*
* Class: org_lwjgl_Display
* Method: nGetAvailableDisplayModes
@ -29,26 +37,18 @@ JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_nGetAvailableDisplayModes
/*
* Class: org_lwjgl_Display
* Method: nCreate
* Signature: (IIIIIIIZLjava/lang/String;)Z
* Method: setDisplayMode
* Signature: (Lorg/lwjgl/DisplayMode;)V
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate
(JNIEnv *, jclass, jint, jint, jint, jint, jint, jint, jint, jboolean, jstring);
JNIEXPORT void JNICALL Java_org_lwjgl_Display_setDisplayMode
(JNIEnv *, jclass, jobject);
/*
* Class: org_lwjgl_Display
* Method: nDestroy
* Method: resetDisplayMode
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_nDestroy
(JNIEnv *, jclass);
/*
* Class: org_lwjgl_Display
* Method: isMinimized
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_isMinimized
JNIEXPORT void JNICALL Java_org_lwjgl_Display_resetDisplayMode
(JNIEnv *, jclass);
/*
@ -59,6 +59,22 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_isMinimized
JNIEXPORT jint JNICALL Java_org_lwjgl_Display_getPlatform
(JNIEnv *, jclass);
/*
* Class: org_lwjgl_Display
* Method: getGammaRamp
* Signature: ()[I
*/
JNIEXPORT jintArray JNICALL Java_org_lwjgl_Display_getGammaRamp
(JNIEnv *, jclass);
/*
* Class: org_lwjgl_Display
* Method: setGammaRamp
* Signature: ([I)V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_setGammaRamp
(JNIEnv *, jclass, jintArray);
#ifdef __cplusplus
}
#endif

View file

@ -0,0 +1,46 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_lwjgl_Window */
#ifndef _Included_org_lwjgl_Window
#define _Included_org_lwjgl_Window
#ifdef __cplusplus
extern "C" {
#endif
/* Inaccessible static: currentWindow */
/*
* Class: org_lwjgl_Window
* Method: nSetTitle
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Window_nSetTitle
(JNIEnv *, jobject);
/*
* Class: org_lwjgl_Window
* Method: swapBuffers
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Window_swapBuffers
(JNIEnv *, jobject);
/*
* Class: org_lwjgl_Window
* Method: nDestroy
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Window_nDestroy
(JNIEnv *, jobject);
/*
* Class: org_lwjgl_Window
* Method: tick
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Window_tick
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -7,16 +7,15 @@
#ifdef __cplusplus
extern "C" {
#endif
/* Inaccessible static: _00024assertionsDisabled */
/* Inaccessible static: currentWindow */
/* Inaccessible static: currentContext */
/* Inaccessible static: class_00024org_00024lwjgl_00024opengl_00024BaseGL */
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: nCreate
* Signature: (IIII)Z
* Signature: (Ljava/lang/String;IIIIZIIII)V
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate
(JNIEnv *, jobject);
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate
(JNIEnv *, jobject, jstring, jint, jint, jint, jint, jboolean, jint, jint, jint, jint);
/*
* Class: org_lwjgl_opengl_BaseGL
@ -26,30 +25,6 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nDestroy
(JNIEnv *, jobject);
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: swapBuffers
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_swapBuffers
(JNIEnv *, jobject);
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: nReleaseContext
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nReleaseContext
(JNIEnv *, jobject);
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: nMakeCurrent
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nMakeCurrent
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif

View file

@ -39,222 +39,15 @@
* @version $Revision$
*/
#include <windows.h>
#include "Window.h"
#include "org_lwjgl_Display.h"
#undef DIRECTINPUT_VERSION
#define DIRECTINPUT_VERSION 0x0300
#include <dinput.h>
#define WINDOWCLASSNAME "LWJGLWINDOW"
void handleMessages();
extern HINSTANCE dll_handle;
jobjectArray GetAvailableDisplayModesNT(JNIEnv * env);
jobjectArray GetAvailableDisplayModes9x(JNIEnv * env);
// Initialise static variables
bool oneShotInitialised = false;
HWND hwnd = NULL; // Handle to the window
HDC hdc = NULL; // Device context
LPDIRECTINPUT lpdi = NULL;
bool isMinimized = false;
bool isFullscreen = false;
JNIEnv* environment;
jclass clsDisplay;
jfieldID fidclose;
void destroyDI(void)
{
lpdi->Release();
lpdi = NULL;
}
void destroyWindow(void)
{
// Reset the display if necessary
if (isFullscreen)
ChangeDisplaySettings(NULL, 0);
//release dc
if (hdc != NULL) {
ReleaseDC(hwnd, hdc);
}
if (hwnd != NULL) {
// Vape the window
DestroyWindow(hwnd);
hwnd = NULL;
}
#ifdef _DEBUG
printf("Destroyed display\n");
#endif
// Show the mouse
if (isFullscreen)
ShowCursor(TRUE);
}
void destroyAll(void)
{
destroyDI();
destroyWindow();
}
void dumpLastError(void) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
printf("System error: %s\n", lpMsgBuf);
LocalFree(lpMsgBuf);
}
/*
* Called when the application is alt-tabbed to or from
*/
void appActivate(bool active)
{
if (active) {
SetForegroundWindow(hwnd);
ShowWindow(hwnd, SW_RESTORE);
} else if (isFullscreen)
ShowWindow(hwnd, SW_MINIMIZE);
}
/*
* A dummy WindowProc which does nothing. Used so we can have an invisible OpenGL window
*/
LRESULT CALLBACK WindowProc(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:
isMinimized = true;
appActivate(true);
break;
case SC_RESTORE:
isMinimized = false;
appActivate(false);
break;
case SC_CLOSE:
environment->SetStaticBooleanField(clsDisplay, fidclose, 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:
isMinimized = false;
break;
case WA_INACTIVE:
isMinimized = true;
break;
}
appActivate(!isMinimized);
}
break;
case WM_QUIT:
{
environment->SetStaticBooleanField(clsDisplay, fidclose, true);
return 0L;
}
}
// default action
return DefWindowProc(hWnd, msg, wParam, lParam);
}
/*
* Handle windowing messages sent by the operating system
*/
void handleMessages()
{
/*
* 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);
};
}
/*
* Sets the fullscreen display mode.
* Returns 1 for success and -1 for failure.
*/
int SetDisplayMode(int width, int height, int bpp, int freq)
{
// Set display mode using OpenGL friendly tactics
DEVMODE devmode;
devmode.dmSize = sizeof(DEVMODE);
devmode.dmBitsPerPel = bpp;
devmode.dmPelsWidth = width;
devmode.dmPelsHeight = height;
devmode.dmDisplayFlags = 0;
devmode.dmDisplayFrequency = freq;
devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFLAGS;
if (freq != 0)
devmode.dmFields |= DM_DISPLAYFREQUENCY;
LONG cdsret = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
switch (cdsret) {
case DISP_CHANGE_BADFLAGS :
printf("Failed to set screen mode: bad flags\n");
return -1;
case DISP_CHANGE_FAILED:
printf("Failed to set screen mode: change failed\n");
return -1;
case DISP_CHANGE_BADMODE:
printf("Failed to set screen mode: bad mode\n");
return -1;
case DISP_CHANGE_SUCCESSFUL :
// Success!
break;
default:
printf("Failed to set screen mode: unknown error\n");
return -1;
}
return 1;
}
/*
* Class: org_lwjgl_Display
@ -377,265 +170,6 @@ jobjectArray GetAvailableDisplayModes9x(JNIEnv * env) {
return ret;
}
/*
* Class: org_lwjgl_Display
* Method: nCreate
* Signature: (IIIIIIIZLjava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate
(JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq,
jint alphaBits, jint depthBits, jint stencilBits, jboolean fullscreen, jstring title)
{
environment = env;
clsDisplay = clazz;
fidclose = env->GetStaticFieldID(clsDisplay, "closeRequested", "Z");
#ifdef _DEBUG
printf("Creating display: size %dx%d %dhz %dbpp...\n", width, height, freq, bpp);
#endif
if (fullscreen && SetDisplayMode(width, height, bpp, freq) != 1)
return JNI_FALSE;
/*
Register a window. This window does nothing, it's just a requirement that we get
a handle to it so we can do other things
*/
if (!oneShotInitialised) {
WNDCLASS windowClass;
windowClass.style = CS_GLOBALCLASS | CS_OWNDC;
windowClass.lpfnWndProc = WindowProc;
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) {
dumpLastError();
printf("Failed to register window class\n");
return JNI_FALSE;
}
oneShotInitialised = true;
}
int exstyle, windowflags;
if (fullscreen) {
exstyle = WS_EX_TOPMOST;
windowflags = WS_POPUP | WS_VISIBLE;
} else {
exstyle = 0;
windowflags = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_VISIBLE | WS_MINIMIZEBOX | WS_SYSMENU;
}
isFullscreen = fullscreen == JNI_TRUE;
const char* titleString = env->GetStringUTFChars(title, NULL);
// If we're not a fullscreen window, adjust the height to account for the
// height of the title bar:
RECT clientSize;
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
);
clientSize.bottom -= clientSize.top;
clientSize.right -= clientSize.left;
// Create the window now, using that class:
hwnd = CreateWindowEx (
exstyle,
WINDOWCLASSNAME,
titleString,
windowflags,
0, 0, clientSize.right, clientSize.bottom,
NULL,
NULL,
dll_handle,
NULL);
env->ReleaseStringUTFChars(title, titleString);
// And we never look at windowClass again...
ShowWindow(hwnd, SW_SHOWNORMAL);
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().
#ifdef _DEBUG
printf("Created display\n");
#endif
// Hide the mouse in fullscreen
if (fullscreen)
ShowCursor(FALSE);
// Create input
HRESULT ret = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &lpdi, NULL);
if (ret != DI_OK && ret != DIERR_BETADIRECTINPUTVERSION ) {
printf("Failed to create directinput");
switch (ret) {
case DIERR_BETADIRECTINPUTVERSION :
printf(" - Beta version\n");
break;
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("\n");
}
destroyWindow();
return JNI_FALSE;
}
unsigned int flags = PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
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, // No auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
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.cAlphaBits < alphaBits) {
printf("This application requires a greater alpha 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 & PFD_GENERIC_FORMAT) != 0 || (desc.dwFlags & PFD_GENERIC_ACCELERATED) != 0) {
printf("Mode not supported by hardware.\n");
destroyAll();
return JNI_FALSE;
}
if ((desc.dwFlags & flags) != flags) {
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;
}
jfieldID fid_handle = env->GetStaticFieldID(clazz, "handle", "I");
env->SetStaticIntField(clazz, fid_handle, (jint) hwnd);
return JNI_TRUE;
}
/*
* Class: org_lwjgl_Display
* Method: nDestroy
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_nDestroy
(JNIEnv * env, jclass clazz)
{
destroyAll();
}
/*
* Class: org_lwjgl_Display
* Method: isMinimized
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_isMinimized
(JNIEnv * env, jclass clazz)
{
// Make sure that messages are being processed. Because
// you shouldn't be calling swapBuffers when the window is
// minimized, you won't get your window messages processed
// there, so we'll do it here too, just to be sure.
if (isMinimized)
handleMessages();
return isMinimized ? JNI_TRUE : JNI_FALSE;
}
/*
* Class: org_lwjgl_Display
@ -648,3 +182,148 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_Display_getPlatform
return org_lwjgl_Display_PLATFORM_WGL;
}
/*
* Class: org_lwjgl_Display
* Method: setDisplayMode
* Signature: (Lorg/lwjgl/DisplayMode;)V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_setDisplayMode
(JNIEnv * env, jclass clazz, jobject mode)
{
jfieldID fid_width = env->GetFieldID(clazz, "width", "I");
jfieldID fid_height = env->GetFieldID(clazz, "height", "I");
jfieldID fid_bpp = env->GetFieldID(clazz, "bpp", "I");
jfieldID fid_freq = env->GetFieldID(clazz, "freq", "I");
int width = env->GetIntField(mode, fid_width);
int height = env->GetIntField(mode, fid_height);
int bpp = env->GetIntField(mode, fid_bpp);
int freq = env->GetIntField(mode, fid_freq);
DEVMODE devmode;
devmode.dmSize = sizeof(DEVMODE);
devmode.dmBitsPerPel = bpp;
devmode.dmPelsWidth = width;
devmode.dmPelsHeight = height;
devmode.dmDisplayFlags = 0;
devmode.dmDisplayFrequency = freq;
devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFLAGS;
if (freq != 0)
devmode.dmFields |= DM_DISPLAYFREQUENCY;
LONG cdsret = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
if (cdsret != DISP_CHANGE_SUCCESSFUL) {
// Failed: so let's check to see if it's a wierd dual screen display
#ifdef _DEBUG
printf("Failed to set display mode... assuming dual monitors\n");
#endif
devmode.dmPelsWidth = width * 2;
cdsret = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
if (cdsret != DISP_CHANGE_SUCCESSFUL) {
#ifdef _DEBUG
printf("Failed to set display mode using dual monitors\n");
#endif
throwException(env, "Failed to set display mode.");
}
}
// The change was successful but might not be the exact change we were expecting.
// Now we'll construct a new DisplayMode instance and stash it back in the Display
// class's mode instance variable.
// Get the screen
HDC screenDC = CreateCompatibleDC(NULL);
// Get the device caps
width = GetDeviceCaps(screenDC, HORZRES);
height = GetDeviceCaps(screenDC, VERTRES);
bpp = GetDeviceCaps(screenDC, COLORRES);
freq = GetDeviceCaps(screenDC, VREFRESH);
if (freq <= 1)
freq = 0; // Unknown
ReleaseDC(NULL, screenDC);
jclass jclass_DisplayMode = env->FindClass("org/lwjgl/DisplayMode");
jmethodID ctor = env->GetMethodID(jclass_DisplayMode, "<init>", "(IIII)V");
jobject newMode = env->NewObject(jclass_DisplayMode, ctor, width, height, bpp, freq);
jfieldID fid_mode= env->GetStaticFieldID(clazz, "mode", "[org/lwjgl/DisplayMode;");
env->SetStaticObjectField(clazz, fid_mode, newMode);
env->DeleteLocalRef(newMode);
}
/*
* Class: org_lwjgl_Display
* Method: resetDisplayMode
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_resetDisplayMode
(JNIEnv * env, jclass clazz)
{
// Under Win32, all we have to do is:
ChangeDisplaySettings(NULL, 0);
// And we'll call init() again to put the correct mode back in Display
Java_org_lwjgl_Display_init(env, clazz);
}
/*
* Class: org_lwjgl_Display
* Method: getGammaRamp
* Signature: ()[I
*/
JNIEXPORT jintArray JNICALL Java_org_lwjgl_Display_getGammaRamp
(JNIEnv * env, jclass clazz)
{
return NULL;
}
/*
* Class: org_lwjgl_Display
* Method: setGammaRamp
* Signature: ([I)V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_setGammaRamp
(JNIEnv * env, jclass clazz, jintArray gamma)
{
}
/*
* Class: org_lwjgl_Display
* Method: init
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Display_init
(JNIEnv * env, jclass clazz)
{
// Determine the current screen resolution
// Get the screen
HDC screenDC = CreateCompatibleDC(NULL);
// Get the device caps
int width = GetDeviceCaps(screenDC, HORZRES);
int height = GetDeviceCaps(screenDC, VERTRES);
int bpp = GetDeviceCaps(screenDC, COLORRES);
int freq = GetDeviceCaps(screenDC, VREFRESH);
if (freq <= 1)
freq = 0; // Unknown
ReleaseDC(NULL, screenDC);
jclass jclass_DisplayMode = env->FindClass("org/lwjgl/DisplayMode");
jmethodID ctor = env->GetMethodID(jclass_DisplayMode, "<init>", "(IIII)V");
jobject newMode = env->NewObject(jclass_DisplayMode, ctor, width, height, bpp, freq);
jfieldID fid_initialMode = env->GetFieldID(clazz, "initialMode", "[org/lwjgl/DisplayMode;");
env->SetStaticObjectField(clazz, fid_initialMode, newMode);
env->DeleteLocalRef(newMode);
}

View file

@ -44,14 +44,13 @@
#undef DIRECTINPUT_VERSION
#define DIRECTINPUT_VERSION 0x0500
#include <dinput.h>
#include "Window.h"
#define CONTROLLER_AXISMAX 1000 // Maxmimum range to which we'll gauge the swing
#define CONTROLLER_AXISMIN -1000 // Minimum range to which we'll gauge the swing
extern HINSTANCE dll_handle;
extern HWND hwnd; // Handle to window
IDirectInput* cDI; // DI instance
IDirectInputDevice2* cDIDevice; // DI Device instance
DIJOYSTATE2 cJS; // State of Controller

View file

@ -45,16 +45,14 @@
#define DIRECTINPUT_VERSION 0x0300
#include <dinput.h>
#include "org_lwjgl_input_Keyboard.h"
#include "Window.h"
#define KEYBOARD_BUFFER_SIZE 50
BYTE readBuffer[KEYBOARD_BUFFER_SIZE*4];
LPDIRECTINPUTDEVICE lpdiKeyboard = NULL;
jfieldID fid_readBuffer;
bool translationEnabled;
extern LPDIRECTINPUT lpdi;
extern HWND hwnd;
extern void handleMessages(void);
/*
* Class: org_lwjgl_input_Keyboard
@ -214,7 +212,8 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nRead
*buf++ = (unsigned char) rgdod[i].dwOfs;
*buf++ = (unsigned char) rgdod[i].dwData;
if (translationEnabled) {
handleMessages();
// Cas: shouldn't need to call handleMessages any more
// handleMessages();
UINT virt_key = MapVirtualKey(rgdod[i].dwOfs, 1);
if (virt_key != 0) {
if (!GetKeyboardState(state))

View file

@ -43,13 +43,10 @@
#include <windows.h>
#undef DIRECTINPUT_VERSION
#define DIRECTINPUT_VERSION 0x0300
#include "Window.h"
#include <dinput.h>
extern HINSTANCE dll_handle;
extern HWND hwnd; // Handle to window
extern LPDIRECTINPUT lpdi; // DI instance
LPDIRECTINPUTDEVICE mDIDevice; // DI Device instance
DIMOUSESTATE diMouseState; // State of Mouse

View file

@ -42,51 +42,146 @@
#include <windows.h>
#include "org_lwjgl_opengl_BaseGL.h"
#include "extgl.h"
#include "Window.h"
HGLRC hglrc = NULL; // OpenGL rendering context
extern HDC hdc;
extern HWND hwnd;
extern void handleMessages();
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: nCreate
* Signature: (IIII)Z
* Signature: (Ljava/lang/String;IIIIZ)V
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate
(JNIEnv * env, jobject obj)
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate
(JNIEnv * env, jobject obj, jstring title, jint x, jint y, jint width, jint height, jboolean fullscreen, jint bpp, jint alpha, jint depth, jint stencil)
{
if (!hwnd) {
printf("No window handle\n");
return JNI_FALSE;
// 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(title, titleString);
closeWindow();
throwException(env, "Failed to create the window.");
return;
}
if (extgl_Open() != 0)
return JNI_FALSE;
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
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");
closeWindow();
return;
}
#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");
closeWindow();
return;
}
// 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");
closeWindow();
return;
}
if (desc.cColorBits < bpp) {
throwException(env, "This application requires a greater colour depth");
closeWindow();
return;
}
if (desc.cAlphaBits < alpha) {
throwException(env, "This application requires a greater alpha depth");
closeWindow();
return;
}
if (desc.cStencilBits < stencil) {
throwException(env, "This application requires a greater stencil depth");
closeWindow();
return;
}
if (desc.cDepthBits < depth) {
throwException(env, "This application requires a greater depth buffer depth");
closeWindow();
return;
}
if ((desc.dwFlags & PFD_GENERIC_FORMAT) != 0 || (desc.dwFlags & PFD_GENERIC_ACCELERATED) != 0) {
throwException(env, "Mode not supported by hardware");
closeWindow();
return;
}
if ((desc.dwFlags & flags) != flags) {
throwException(env, "Capabilities not supported");
closeWindow();
return;
}
// 4. Initialise other things now
if (extgl_Open() != 0) {
closeWindow();
throwException(env, "Failed to open extgl");
return;
}
// Create a rendering context
hglrc = wglCreateContext(hdc);
if (hglrc == NULL) {
printf("Failed to create device context.\n");
return JNI_FALSE;
closeWindow();
throwException(env, "Failed to create OpenGL rendering context");
return;
}
// Automatically make it the current context
wglMakeCurrent(hdc, hglrc);
// Initialise GL extensions
if (extgl_Initialize() != 0) {
printf("Failed to initialize GL\n");
return JNI_FALSE;
closeWindow();
throwException(env, "Failed to initialize GL extensions");
return;
}
#ifdef _DEBUG
char * p = (char *) glGetString(GL_EXTENSIONS);
if (NULL == p) {
printf("NO extensions available\n");
} else {
printf("Available extensions:\n%s\n", p);
}
#endif
return JNI_TRUE;
// Stash handle back in Java
env->SetIntField(obj, env->GetFieldID(env->GetObjectClass(obj), "handle", "I"), (jint) hglrc);
}
/*
@ -104,41 +199,3 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nDestroy
wglDeleteContext(hglrc);
extgl_Close();
}
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: swapBuffers
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_swapBuffers
(JNIEnv *, jobject)
{
// Handle OS messages here
handleMessages();
// Then do the flip
SwapBuffers(hdc);
}
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: nFreeContext
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nReleaseContext
(JNIEnv *env, jobject obj)
{
wglMakeCurrent(hdc, NULL);
}
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: nMakeCurrent
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nMakeCurrent
(JNIEnv * env, jobject obj)
{
wglMakeCurrent(hdc, hglrc);
}

View file

@ -0,0 +1,23 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_lwjgl_opengl_BaseGL */
#ifndef _Included_org_lwjgl_opengl_BaseGL
#define _Included_org_lwjgl_opengl_BaseGL
#ifdef __cplusplus
extern "C" {
#endif
/* Inaccessible static: currentWindow */
/* Inaccessible static: currentContext */
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: nCreate
* Signature: (Ljava/lang/String;IIIIZ)V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate
(JNIEnv *, jobject, jstring, jint, jint, jint, jint, jboolean);
#ifdef __cplusplus
}
#endif
#endif