*** empty log message ***

This commit is contained in:
Caspian Rychlik-Prince 2003-03-28 19:02:24 +00:00
parent 895c751ec6
commit 4924c564d5
11 changed files with 168 additions and 76 deletions

View File

@ -48,11 +48,6 @@ import java.util.Arrays;
public final class Display { public final class Display {
static {
System.loadLibrary(Sys.getLibraryName());
init();
}
/** Has the display been created? */ /** Has the display been created? */
private static boolean created; private static boolean created;
@ -79,6 +74,11 @@ public final class Display {
/** MacOSX platform */ /** MacOSX platform */
public static final int PLATFORM_AGL = 2; public static final int PLATFORM_AGL = 2;
static {
System.loadLibrary(Sys.getLibraryName());
init();
}
/** /**
* No construction allowed. * No construction allowed.
*/ */

View File

@ -26,8 +26,11 @@ public abstract class Window {
System.loadLibrary(Sys.getLibraryName()); System.loadLibrary(Sys.getLibraryName());
} }
/** Whether we have a window already */ /** The currently created window */
private static Window currentWindow; private static Window currentWindow;
/** Whether the window is currently created, ie. has a native peer */
private boolean created;
/** The window's native data structure. On Win32 this is an HWND. */ /** The window's native data structure. On Win32 this is an HWND. */
private int handle; private int handle;
@ -66,7 +69,7 @@ public abstract class Window {
* In this abstract base class, no actual window peer is constructed. This should be * In this abstract base class, no actual window peer is constructed. This should be
* done in specialised derived classes. * done in specialised derived classes.
* *
* Only one Window can be constructed at a time; to create another Window you must * Only one Window can be created() at a time; to create another Window you must
* first destroy() the first window. * first destroy() the first window.
* *
* @param title The window's title * @param title The window's title
@ -76,15 +79,12 @@ public abstract class Window {
* @throws RuntimeException if you attempt to create more than one window at the same time * @throws RuntimeException if you attempt to create more than one window at the same time
*/ */
protected Window(String title, int x, int y, int width, int height) { protected Window(String title, int x, int y, int width, int height) {
if (currentWindow != null)
throw new RuntimeException("Only one LWJGL window may be instantiated at any one time.");
this.title = title; this.title = title;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.width = width; this.width = width;
this.height = height; this.height = height;
currentWindow = this;
} }
/** /**
@ -164,14 +164,40 @@ public abstract class Window {
*/ */
private native void swapBuffers(); private native void swapBuffers();
/**
* Create the window.
*/
public final void create() throws Exception {
if (currentWindow != null)
throw new RuntimeException("Only one LWJGL window may be instantiated at any one time.");
doCreate();
currentWindow = this;
created = true;
}
/**
* Create the window (derived classes).
* @throws Exception
*/
protected abstract void doCreate() throws Exception;
/** /**
* Destroy the window. * Destroy the window.
*/ */
public void destroy() { public final void destroy() {
currentWindow = null; if (!created)
return;
doDestroy();
nDestroy(); nDestroy();
currentWindow = null;
created = false;
} }
/**
* Destroy the window (derived classes)
*/
protected abstract void doDestroy();
/** /**
* Natively destroy the window * Natively destroy the window
*/ */
@ -185,10 +211,10 @@ public abstract class Window {
} }
/** /**
* @return true if a window has been created * @return true if the window's native peer has been created
*/ */
public static boolean isCreated() { public final boolean isCreated() {
return currentWindow != null; return created;
} }
/** /**
@ -197,4 +223,20 @@ public abstract class Window {
*/ */
public final native void tick(); public final native void tick();
/* (non-Javadoc)
* @see java.lang.Object#finalize()
*/
protected void finalize() throws Throwable {
super.finalize();
destroy();
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return "Window["+title+"]";
}
} }

View File

@ -35,7 +35,6 @@ package org.lwjgl.input;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import org.lwjgl.*;
import org.lwjgl.Sys; import org.lwjgl.Sys;
/** /**
@ -249,8 +248,6 @@ public class Keyboard {
public static void create() throws Exception { public static void create() throws Exception {
if (created) if (created)
return; return;
if (!Window.isCreated())
throw new Exception("The display has not yet been created.");
if (!nCreate()) if (!nCreate())
throw new Exception("The keyboard could not be created."); throw new Exception("The keyboard could not be created.");
created = true; created = true;

View File

@ -101,8 +101,6 @@ public class Mouse {
public static void create() throws Exception { public static void create() throws Exception {
if (created) if (created)
return; return;
if (!Window.isCreated())
throw new Exception("The display has not yet been created.");
if (!nCreate()) if (!nCreate())
throw new Exception("The mouse could not be created."); throw new Exception("The mouse could not be created.");
created = true; created = true;

View File

@ -51,16 +51,37 @@ import org.lwjgl.Window;
* @author cix_foo <cix_foo@users.sourceforge.net> * @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$ * @version $Revision$
*/ */
public abstract class BaseGL extends Window { public class BaseGL extends Window {
static {
System.loadLibrary(Sys.getLibraryName());
}
/** The current rendering context */ /** The current rendering context */
private static BaseGL currentContext; //private static BaseGL currentContext;
/** Has the GL been created yet? */ /** Has the GL been created yet? */
private boolean created; private boolean created;
/** Handle to the native GL rendering context */ /** Handle to the native GL rendering context */
protected int handle; protected int handle;
/** Color bits */
protected final int color;
/** Alpha bits */
protected final int alpha;
/** Depth bits */
protected final int depth;
/** Stencil bits */
protected final int stencil;
private int x, y;
/** Fullscreen */
protected final boolean fullscreen;
/** /**
* Construct a windowed instance of GL. If the underlying OS does not * Construct a windowed instance of GL. If the underlying OS does not
@ -78,7 +99,14 @@ public abstract class BaseGL extends Window {
public BaseGL(String title, int x, int y, int width, int height, int bpp, int alpha, int depth, int stencil) throws Exception { 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); super(title, x, y, width, height);
nCreate(title, x, y, width, height, false, bpp, alpha, depth, stencil); this.x = x;
this.y = y;
this.color = bpp;
this.alpha = alpha;
this.depth = depth;
this.stencil = stencil;
this.fullscreen = false;
} }
/** /**
@ -92,33 +120,44 @@ public abstract class BaseGL extends Window {
public BaseGL(String title, int bpp, int alpha, int depth, int stencil) throws Exception { public BaseGL(String title, int bpp, int alpha, int depth, int stencil) throws Exception {
super(title, 0, 0, Display.getWidth(), Display.getHeight()); super(title, 0, 0, Display.getWidth(), Display.getHeight());
nCreate(title, 0, 0, Display.getWidth(), Display.getHeight(), true, bpp, alpha, depth, stencil); this.x = 0;
this.y = 0;
this.color = bpp;
this.alpha = alpha;
this.depth = depth;
this.stencil = stencil;
this.fullscreen = true;
}
protected void doCreate() throws Exception {
nCreate(x, y, getWidth(), getHeight(), color, alpha, depth, stencil, fullscreen);
} }
/** /**
* Native method to create a windowed GL * Native method to create a windowed GL
*/ */
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; private native void nCreate(
int x,
/** int y,
* Finalizer, marked final. Ensures the window is destroyed. int width,
*/ int height,
public final void finalize() throws Throwable { int bpp,
super.finalize(); int alpha,
int depth,
destroy(); int stencil,
} boolean fullscreen) throws Exception;
/* (non-Javadoc)
* @see org.lwjgl.Window#destroy()
*/
public void destroy() {
// Do native destroy first
super.destroy();
}
/* (non-Javadoc)
* @see org.lwjgl.Window#doDestroy()
*/
protected void doDestroy() {
nDestroyGL();
}
/** /**
* Natively destroy any GL-related stuff * Natively destroy any GL-related stuff
*/ */
private native void nDestroy(); private native void nDestroyGL();
} }

View File

@ -62,7 +62,6 @@ public class GL extends CoreGL implements GLConstants {
*/ */
public GL(String title, int x, int y, int width, int height, int bpp, int alpha, int depth, int 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); super(title, x, y, width, height, bpp, alpha, depth, stencil);
determineAvailableExtensions();
} }
/** /**
@ -75,9 +74,18 @@ public class GL extends CoreGL implements GLConstants {
*/ */
public GL(String title, int bpp, int alpha, int depth, int stencil) throws Exception { public GL(String title, int bpp, int alpha, int depth, int stencil) throws Exception {
super(title, bpp, alpha, depth, stencil); super(title, bpp, alpha, depth, stencil);
}
/* (non-Javadoc)
* @see org.lwjgl.opengl.BaseGL#doCreate()
*/
protected void doCreate() throws Exception {
super.doCreate();
determineAvailableExtensions(); determineAvailableExtensions();
} }
public native void activeStencilFaceEXT(int face); public native void activeStencilFaceEXT(int face);
public native void activeTextureARB(int texture); public native void activeTextureARB(int texture);
@ -1573,7 +1581,7 @@ public class GL extends CoreGL implements GLConstants {
/** /**
* Determine which extensions are available * Determine which extensions are available
*/ */
private void determineAvailableExtensions() { public void determineAvailableExtensions() {
determineAvailableWGLExtensions(); determineAvailableWGLExtensions();

View File

@ -8,21 +8,20 @@
extern "C" { extern "C" {
#endif #endif
/* Inaccessible static: currentWindow */ /* Inaccessible static: currentWindow */
/* Inaccessible static: currentContext */
/* /*
* Class: org_lwjgl_opengl_BaseGL * Class: org_lwjgl_opengl_BaseGL
* Method: nCreate * Method: nCreate
* Signature: (Ljava/lang/String;IIIIZIIII)V * Signature: (IIIIIIIIZ)V
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate
(JNIEnv *, jobject, jstring, jint, jint, jint, jint, jboolean, jint, jint, jint, jint); (JNIEnv *, jobject, jint, jint, jint, jint, jint, jint, jint, jint, jboolean);
/* /*
* Class: org_lwjgl_opengl_BaseGL * Class: org_lwjgl_opengl_BaseGL
* Method: nDestroy * Method: nDestroyGL
* Signature: ()V * Signature: ()V
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nDestroy JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nDestroyGL
(JNIEnv *, jobject); (JNIEnv *, jobject);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -58,7 +58,7 @@
extern HDC hdc; // Device context extern HDC hdc; // Device context
extern LPDIRECTINPUT lpdi; // DirectInput extern LPDIRECTINPUT lpdi; // DirectInput
extern bool isFullScreen; // Whether we're fullscreen or not extern bool isFullScreen; // Whether we're fullscreen or not
extern bool isMinimized; // Whether we're minimized or not
#endif /* _PRIVATE_WINDOW_H_ */ #endif /* _PRIVATE_WINDOW_H_ */
/* /*

View File

@ -191,17 +191,17 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Display_setDisplayMode
(JNIEnv * env, jclass clazz, jobject mode) (JNIEnv * env, jclass clazz, jobject mode)
{ {
jfieldID fid_width = env->GetFieldID(clazz, "width", "I"); jclass cls_displayMode = env->FindClass("org/lwjgl/DisplayMode");
jfieldID fid_height = env->GetFieldID(clazz, "height", "I"); jfieldID fid_width = env->GetFieldID(cls_displayMode, "width", "I");
jfieldID fid_bpp = env->GetFieldID(clazz, "bpp", "I"); jfieldID fid_height = env->GetFieldID(cls_displayMode, "height", "I");
jfieldID fid_freq = env->GetFieldID(clazz, "freq", "I"); jfieldID fid_bpp = env->GetFieldID(cls_displayMode, "bpp", "I");
jfieldID fid_freq = env->GetFieldID(cls_displayMode, "freq", "I");
int width = env->GetIntField(mode, fid_width); int width = env->GetIntField(mode, fid_width);
int height = env->GetIntField(mode, fid_height); int height = env->GetIntField(mode, fid_height);
int bpp = env->GetIntField(mode, fid_bpp); int bpp = env->GetIntField(mode, fid_bpp);
int freq = env->GetIntField(mode, fid_freq); int freq = env->GetIntField(mode, fid_freq);
DEVMODE devmode; DEVMODE devmode;
devmode.dmSize = sizeof(DEVMODE); devmode.dmSize = sizeof(DEVMODE);
devmode.dmBitsPerPel = bpp; devmode.dmBitsPerPel = bpp;
@ -229,6 +229,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Display_setDisplayMode
printf("Failed to set display mode using dual monitors\n"); printf("Failed to set display mode using dual monitors\n");
#endif #endif
throwException(env, "Failed to set display mode."); throwException(env, "Failed to set display mode.");
return;
} }
} }
@ -245,13 +246,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Display_setDisplayMode
freq = GetDeviceCaps(screenDC, VREFRESH); freq = GetDeviceCaps(screenDC, VREFRESH);
if (freq <= 1) if (freq <= 1)
freq = 0; // Unknown freq = 0; // Unknown
ReleaseDC(NULL, screenDC); DeleteDC(screenDC);
jclass jclass_DisplayMode = env->FindClass("org/lwjgl/DisplayMode"); jmethodID ctor = env->GetMethodID(cls_displayMode, "<init>", "(IIII)V");
jmethodID ctor = env->GetMethodID(jclass_DisplayMode, "<init>", "(IIII)V"); jobject newMode = env->NewObject(cls_displayMode, ctor, width, height, bpp, freq);
jobject newMode = env->NewObject(jclass_DisplayMode, ctor, width, height, bpp, freq); jfieldID fid_initialMode = env->GetStaticFieldID(clazz, "mode", "Lorg/lwjgl/DisplayMode;");
jfieldID fid_mode= env->GetStaticFieldID(clazz, "mode", "[org/lwjgl/DisplayMode;"); env->SetStaticObjectField(clazz, fid_initialMode, newMode);
env->SetStaticObjectField(clazz, fid_mode, newMode);
env->DeleteLocalRef(newMode); env->DeleteLocalRef(newMode);
} }
@ -302,9 +302,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Display_init
(JNIEnv * env, jclass clazz) (JNIEnv * env, jclass clazz)
{ {
// Determine the current screen resolution // Determine the current screen resolution
// Get the screen // Get the screen
HDC screenDC = CreateCompatibleDC(NULL); HDC screenDC = CreateCompatibleDC(NULL);
if (!screenDC) {
printf("Couldn't get screen DC!\n");
return;
}
// Get the device caps // Get the device caps
int width = GetDeviceCaps(screenDC, HORZRES); int width = GetDeviceCaps(screenDC, HORZRES);
int height = GetDeviceCaps(screenDC, VERTRES); int height = GetDeviceCaps(screenDC, VERTRES);
@ -312,13 +315,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Display_init
int freq = GetDeviceCaps(screenDC, VREFRESH); int freq = GetDeviceCaps(screenDC, VREFRESH);
if (freq <= 1) if (freq <= 1)
freq = 0; // Unknown freq = 0; // Unknown
ReleaseDC(NULL, screenDC); DeleteDC(screenDC);
jclass jclass_DisplayMode = env->FindClass("org/lwjgl/DisplayMode"); jclass jclass_DisplayMode = env->FindClass("org/lwjgl/DisplayMode");
jmethodID ctor = env->GetMethodID(jclass_DisplayMode, "<init>", "(IIII)V"); jmethodID ctor = env->GetMethodID(jclass_DisplayMode, "<init>", "(IIII)V");
jobject newMode = env->NewObject(jclass_DisplayMode, ctor, width, height, bpp, freq); jobject newMode = env->NewObject(jclass_DisplayMode, ctor, width, height, bpp, freq);
jfieldID fid_initialMode = env->GetStaticFieldID(clazz, "mode", "Lorg/lwjgl/DisplayMode;");
jfieldID fid_initialMode = env->GetFieldID(clazz, "initialMode", "[org/lwjgl/DisplayMode;");
env->SetStaticObjectField(clazz, fid_initialMode, newMode); env->SetStaticObjectField(clazz, fid_initialMode, newMode);
env->DeleteLocalRef(newMode); env->DeleteLocalRef(newMode);
} }

View File

@ -48,6 +48,7 @@ HWND hwnd = NULL; // Handle to the window
HDC hdc = NULL; // Device context HDC hdc = NULL; // Device context
LPDIRECTINPUT lpdi = NULL; // DirectInput LPDIRECTINPUT lpdi = NULL; // DirectInput
bool isFullScreen = false; // Whether we're fullscreen or not bool isFullScreen = false; // Whether we're fullscreen or not
bool isMinimized = false; // Whether we're minimized or not
JNIEnv * environment; // Cached environment JNIEnv * environment; // Cached environment
jobject window; // Cached Java Window instance handle jobject window; // Cached Java Window instance handle
extern HINSTANCE dll_handle; // Handle to the LWJGL dll extern HINSTANCE dll_handle; // Handle to the LWJGL dll
@ -169,11 +170,11 @@ LRESULT CALLBACK lwjglWindowProc(HWND hWnd,
break; break;
case WM_ACTIVATE: case WM_ACTIVATE:
{ {
bool isMinimized = false;
switch(LOWORD(wParam)) { switch(LOWORD(wParam)) {
case WA_ACTIVE: case WA_ACTIVE:
case WA_CLICKACTIVE: case WA_CLICKACTIVE:
environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), false); environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), false);
isMinimized = false;
break; break;
case WA_INACTIVE: case WA_INACTIVE:
environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), true); environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), true);
@ -222,6 +223,7 @@ bool registerWindow()
printf("Failed to register window class\n"); printf("Failed to register window class\n");
return false; return false;
} }
printf("Window registered\n");
oneShotInitialised = true; oneShotInitialised = true;
} }
@ -373,8 +375,8 @@ void handleMessages(JNIEnv * env, jobject obj)
0, // first message 0, // first message
0, // last message 0, // last message
PM_REMOVE // removal options PM_REMOVE // removal options
)) { ))
{
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
}; };

View File

@ -43,22 +43,24 @@
#include "org_lwjgl_opengl_BaseGL.h" #include "org_lwjgl_opengl_BaseGL.h"
#include "extgl.h" #include "extgl.h"
#include "Window.h" #include "Window.h"
#include "jni.h"
HGLRC hglrc = NULL; // OpenGL rendering context HGLRC hglrc = NULL; // OpenGL rendering context
/* /*
* Class: org_lwjgl_opengl_BaseGL * Class: org_lwjgl_opengl_BaseGL
* Method: nCreate * Method: nCreate
* Signature: (Ljava/lang/String;IIIIZ)V * Signature: (Ljava/lang/String;IIIIIIIIZ)V
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate 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) (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 // 1. Create a window
const char * titleString = env->GetStringUTFChars(title, NULL); const char * titleString = env->GetStringUTFChars(title, NULL);
if (!createWindow(titleString, x, y, width, height, fullscreen == JNI_TRUE ? true : false)) { if (!createWindow(titleString, x, y, width, height, fullscreen == JNI_TRUE ? true : false)) {
env->ReleaseStringUTFChars(title, titleString); env->ReleaseStringUTFChars((jstring) title, titleString);
closeWindow(); closeWindow();
throwException(env, "Failed to create the window."); throwException(env, "Failed to create the window.");
return; return;
@ -156,16 +158,16 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate
// 4. Initialise other things now // 4. Initialise other things now
if (extgl_Open() != 0) { if (extgl_Open() != 0) {
closeWindow();
throwException(env, "Failed to open extgl"); throwException(env, "Failed to open extgl");
closeWindow();
return; return;
} }
// Create a rendering context // Create a rendering context
hglrc = wglCreateContext(hdc); hglrc = wglCreateContext(hdc);
if (hglrc == NULL) { if (hglrc == NULL) {
closeWindow();
throwException(env, "Failed to create OpenGL rendering context"); throwException(env, "Failed to create OpenGL rendering context");
closeWindow();
return; return;
} }
@ -189,13 +191,16 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate
* Method: nDestroy * Method: nDestroy
* Signature: ()V * Signature: ()V
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nDestroy JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nDestroyGL
(JNIEnv * env, jobject obj) (JNIEnv * env, jobject obj)
{ {
wglMakeCurrent(NULL, NULL); wglMakeCurrent(NULL, NULL);
// Delete the rendering context // Delete the rendering context
if (hglrc != NULL) if (hglrc != NULL) {
wglDeleteContext(hglrc); wglDeleteContext(hglrc);
hglrc = NULL;
}
extgl_Close(); extgl_Close();
} }