2003-03-27 20:30:34 -05:00
|
|
|
/*
|
|
|
|
* 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 <cix_foo@users.sourceforge.net>
|
|
|
|
* @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
|
|
|
|
LPDIRECTINPUT lpdi = NULL; // DirectInput
|
|
|
|
bool isFullScreen = false; // Whether we're fullscreen or not
|
2003-03-28 14:02:24 -05:00
|
|
|
bool isMinimized = false; // Whether we're minimized or not
|
2003-03-28 16:04:43 -05:00
|
|
|
JNIEnv * environment = NULL; // Cached environment
|
2003-03-27 20:30:34 -05:00
|
|
|
jobject window; // Cached Java Window instance handle
|
|
|
|
extern HINSTANCE dll_handle; // Handle to the LWJGL dll
|
2003-05-16 14:39:46 -04:00
|
|
|
RECT clientSize;
|
2003-03-27 20:30:34 -05:00
|
|
|
|
2003-06-07 15:28:53 -04:00
|
|
|
extern void tempRestoreDisplayMode();
|
|
|
|
extern void tempResetDisplayMode();
|
|
|
|
|
2003-03-27 20:30:34 -05:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2003-03-29 16:52:14 -05:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2003-03-27 20:30:34 -05:00
|
|
|
/*
|
|
|
|
* Create DirectInput.
|
|
|
|
* Returns true for success, or false for failure
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
void closeWindow()
|
|
|
|
{
|
|
|
|
// Release DirectInput
|
|
|
|
if (lpdi != NULL) {
|
2003-03-28 18:16:15 -05:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Destroying directinput\n");
|
|
|
|
#endif
|
2003-03-27 20:30:34 -05:00
|
|
|
lpdi->Release();
|
|
|
|
lpdi = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release device context
|
2003-03-28 16:04:43 -05:00
|
|
|
if (hdc != NULL && hwnd != NULL) {
|
2003-03-28 18:16:15 -05:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Releasing DC\n");
|
|
|
|
#endif
|
2003-03-27 20:30:34 -05:00
|
|
|
ReleaseDC(hwnd, hdc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the window
|
|
|
|
if (hwnd != NULL) {
|
2003-03-28 18:16:15 -05:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Destroy window\n");
|
|
|
|
#endif
|
2003-03-27 20:30:34 -05:00
|
|
|
// Vape the window
|
|
|
|
DestroyWindow(hwnd);
|
2003-03-28 18:16:15 -05:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Destroyed window\n");
|
|
|
|
#endif
|
2003-03-27 20:30:34 -05:00
|
|
|
hwnd = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when the application is alt-tabbed to or from
|
|
|
|
*/
|
|
|
|
void appActivate(bool active)
|
|
|
|
{
|
2003-06-12 10:08:10 -04:00
|
|
|
if (!active) {
|
|
|
|
tempResetDisplayMode();
|
|
|
|
}
|
2003-03-27 20:30:34 -05:00
|
|
|
if (active) {
|
|
|
|
SetForegroundWindow(hwnd);
|
|
|
|
ShowWindow(hwnd, SW_RESTORE);
|
2003-05-16 14:39:46 -04:00
|
|
|
} else if (isFullScreen) {
|
2003-03-27 20:30:34 -05:00
|
|
|
ShowWindow(hwnd, SW_MINIMIZE);
|
2003-05-16 14:39:46 -04:00
|
|
|
}
|
2003-06-12 10:08:10 -04:00
|
|
|
if (active) {
|
|
|
|
tempRestoreDisplayMode();
|
|
|
|
}
|
2003-03-27 20:30:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* WindowProc for the GL window.
|
|
|
|
*/
|
|
|
|
LRESULT CALLBACK lwjglWindowProc(HWND hWnd,
|
|
|
|
UINT msg,
|
|
|
|
WPARAM wParam,
|
|
|
|
LPARAM lParam)
|
|
|
|
{
|
2003-03-28 16:04:43 -05:00
|
|
|
if (environment == NULL) {
|
|
|
|
return DefWindowProc(hWnd, msg, wParam, lParam);
|
|
|
|
}
|
2003-03-27 20:30:34 -05:00
|
|
|
|
|
|
|
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:
|
2003-03-30 14:26:39 -05:00
|
|
|
environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), JNI_TRUE);
|
2003-03-27 20:30:34 -05:00
|
|
|
appActivate(true);
|
|
|
|
break;
|
|
|
|
case SC_RESTORE:
|
2003-03-30 14:26:39 -05:00
|
|
|
environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), JNI_FALSE);
|
2003-03-27 20:30:34 -05:00
|
|
|
appActivate(false);
|
|
|
|
break;
|
|
|
|
case SC_CLOSE:
|
2003-03-30 14:26:39 -05:00
|
|
|
environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "closeRequested", "Z"), JNI_TRUE);
|
2003-03-27 20:30:34 -05:00
|
|
|
//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:
|
2003-03-30 14:26:39 -05:00
|
|
|
environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), JNI_FALSE);
|
2003-03-28 14:02:24 -05:00
|
|
|
isMinimized = false;
|
2003-06-07 15:28:53 -04:00
|
|
|
|
2003-03-27 20:30:34 -05:00
|
|
|
break;
|
|
|
|
case WA_INACTIVE:
|
2003-03-30 14:26:39 -05:00
|
|
|
environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "minimized", "Z"), JNI_TRUE);
|
2003-03-27 20:30:34 -05:00
|
|
|
isMinimized = true;
|
2003-06-07 15:28:53 -04:00
|
|
|
|
2003-03-27 20:30:34 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
appActivate(!isMinimized);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WM_QUIT:
|
|
|
|
{
|
2003-03-30 14:26:39 -05:00
|
|
|
environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "closeRequested", "Z"), JNI_TRUE);
|
2003-03-27 20:30:34 -05:00
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
case WM_PAINT:
|
|
|
|
{
|
2003-03-30 14:26:39 -05:00
|
|
|
environment->SetBooleanField(window, environment->GetFieldID(environment->GetObjectClass(window), "dirty", "Z"), JNI_TRUE);
|
2003-03-27 20:30:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// default action
|
|
|
|
return DefWindowProc(hWnd, msg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register the LWJGL window class.
|
|
|
|
* Returns true for success, or false for failure
|
|
|
|
*/
|
|
|
|
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);
|
2003-05-20 08:20:13 -04:00
|
|
|
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
2003-03-27 20:30:34 -05:00
|
|
|
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;
|
|
|
|
}
|
2003-03-28 18:16:15 -05:00
|
|
|
#ifdef _DEBUG
|
2003-03-28 14:02:24 -05:00
|
|
|
printf("Window registered\n");
|
2003-03-28 18:16:15 -05:00
|
|
|
#endif
|
2003-03-27 20:30:34 -05:00
|
|
|
oneShotInitialised = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
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) {
|
2003-06-07 08:48:21 -04:00
|
|
|
exstyle = WS_EX_APPWINDOW | WS_EX_TOPMOST;
|
2003-03-27 20:30:34 -05:00
|
|
|
windowflags = WS_POPUP | WS_VISIBLE;
|
|
|
|
} else {
|
2003-06-07 08:48:21 -04:00
|
|
|
exstyle = WS_EX_APPWINDOW;
|
2003-03-27 20:30:34 -05:00
|
|
|
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,
|
2003-05-16 14:39:46 -04:00
|
|
|
x, y, clientSize.right - clientSize.left, clientSize.bottom - clientSize.top,
|
2003-03-27 20:30:34 -05:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
dll_handle,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (hwnd == NULL) {
|
|
|
|
printf("Failed to create window\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-03-28 16:04:43 -05:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Created window\n");
|
|
|
|
#endif
|
|
|
|
|
2003-06-07 08:48:21 -04:00
|
|
|
ShowWindow(hwnd, SW_SHOWNORMAL);
|
|
|
|
//ShowWindow(hwnd, SW_SHOW);
|
2003-03-27 20:30:34 -05:00
|
|
|
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
|
2003-03-28 18:16:15 -05:00
|
|
|
isFullScreen = fullscreen == JNI_TRUE;
|
2003-03-27 20:30:34 -05:00
|
|
|
|
|
|
|
// 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
|
2003-03-30 14:26:39 -05:00
|
|
|
(JNIEnv * env, jobject obj, jstring title_obj)
|
2003-03-27 20:30:34 -05:00
|
|
|
{
|
2003-03-30 14:26:39 -05:00
|
|
|
const char * title = env->GetStringUTFChars(title_obj, NULL);
|
2003-03-27 20:30:34 -05:00
|
|
|
SetWindowText(hwnd, title);
|
2003-03-30 14:26:39 -05:00
|
|
|
env->ReleaseStringUTFChars(title_obj, title);
|
2003-03-27 20:30:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle native Win32 messages
|
|
|
|
*/
|
|
|
|
void handleMessages(JNIEnv * env, jobject obj)
|
|
|
|
{
|
|
|
|
// Cache env and obj
|
|
|
|
environment = env;
|
|
|
|
window = obj;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2003-03-28 14:02:24 -05:00
|
|
|
))
|
|
|
|
{
|
2003-03-27 20:30:34 -05:00
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
};
|
2003-03-30 14:53:39 -05:00
|
|
|
environment = NULL;
|
|
|
|
window = NULL;
|
2003-03-27 20:30:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_Window
|
|
|
|
* Method: tick
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_Window_tick
|
|
|
|
(JNIEnv * env, jobject obj)
|
|
|
|
{
|
|
|
|
handleMessages(env, obj);
|
2003-03-30 14:26:39 -05:00
|
|
|
}
|
2003-06-04 18:10:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_Window
|
|
|
|
* Method: minimize
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_Window_minimize
|
|
|
|
(JNIEnv * env, jobject obj)
|
|
|
|
{
|
|
|
|
if (isMinimized)
|
|
|
|
return;
|
|
|
|
ShowWindow(hwnd, SW_MINIMIZE);
|
2003-06-12 10:08:10 -04:00
|
|
|
tempResetDisplayMode();
|
2003-06-04 18:10:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_Window
|
|
|
|
* Method: minimize
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_Window_restore
|
|
|
|
(JNIEnv * env, jobject obj)
|
|
|
|
{
|
|
|
|
if (!isMinimized)
|
|
|
|
return;
|
|
|
|
|
2003-06-12 10:08:10 -04:00
|
|
|
tempRestoreDisplayMode();
|
2003-06-04 18:10:24 -04:00
|
|
|
ShowWindow(hwnd, SW_RESTORE);
|
|
|
|
}
|