diff --git a/src/native/win32/Window.h b/src/native/win32/Window.h index 8fa81215..4e721a5d 100644 --- a/src/native/win32/Window.h +++ b/src/native/win32/Window.h @@ -52,16 +52,14 @@ #else #define WINDOW_H_API extern - extern HWND display_hwnd; // Handle to the window -// extern HDC display_hdc; // Device context extern bool isFullScreen; // Whether we're fullscreen or not extern bool isMinimized; // Whether we're minimized or not extern bool isFocused; // Whether we're focused or not extern bool isDirty; // Whether we're dirty or not - extern RECT clientSize; -// extern HGLRC display_hglrc; #endif /* _PRIVATE_WINDOW_H_ */ + WINDOW_H_API HWND getCurrentHWND(); + WINDOW_H_API HDC getCurrentWindowDC(); WINDOW_H_API HGLRC getCurrentContext(); diff --git a/src/native/win32/org_lwjgl_Sys.cpp b/src/native/win32/org_lwjgl_Sys.cpp index 5078d1e7..2e576559 100644 --- a/src/native/win32/org_lwjgl_Sys.cpp +++ b/src/native/win32/org_lwjgl_Sys.cpp @@ -123,7 +123,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Sys_nAlert jboolean copy = JNI_FALSE; const char * eMessageText = env->GetStringUTFChars(message, ©); const char * cTitleBarText = env->GetStringUTFChars(title, ©); - MessageBox(display_hwnd, eMessageText, cTitleBarText, MB_OK | MB_TOPMOST); + MessageBox(getCurrentHWND(), eMessageText, cTitleBarText, MB_OK | MB_TOPMOST); printfDebug("*** Alert ***%s\n%s\n", cTitleBarText, eMessageText); diff --git a/src/native/win32/org_lwjgl_input_Controller.cpp b/src/native/win32/org_lwjgl_input_Controller.cpp index 5e559b7a..5ace79df 100644 --- a/src/native/win32/org_lwjgl_input_Controller.cpp +++ b/src/native/win32/org_lwjgl_input_Controller.cpp @@ -318,7 +318,7 @@ static void SetupController() { } // set the cooperative level - if(cDIDevice->SetCooperativeLevel(display_hwnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND) != DI_OK) { + if(cDIDevice->SetCooperativeLevel(getCurrentHWND(), DISCL_EXCLUSIVE | DISCL_FOREGROUND) != DI_OK) { printfDebug("SetCooperativeLevel failed\n"); cCreate_success = false; return; diff --git a/src/native/win32/org_lwjgl_input_Keyboard.cpp b/src/native/win32/org_lwjgl_input_Keyboard.cpp index 912eb4b1..ee26e65e 100644 --- a/src/native/win32/org_lwjgl_input_Keyboard.cpp +++ b/src/native/win32/org_lwjgl_input_Keyboard.cpp @@ -81,18 +81,13 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nCreate return; } - if (display_hwnd == NULL) { - throwException(env, "No window."); - return; - } - // Create a keyboard device if (lpdi->CreateDevice(GUID_SysKeyboard, &lpdiKeyboard, NULL) != DI_OK) { throwException(env, "Failed to create keyboard."); return; } - if (lpdiKeyboard->SetCooperativeLevel(display_hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK) { + if (lpdiKeyboard->SetCooperativeLevel(getCurrentHWND(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK) { throwException(env, "Failed to set keyboard cooperation mode."); return; } diff --git a/src/native/win32/org_lwjgl_input_Mouse.cpp b/src/native/win32/org_lwjgl_input_Mouse.cpp index 8c6d892a..786aa131 100644 --- a/src/native/win32/org_lwjgl_input_Mouse.cpp +++ b/src/native/win32/org_lwjgl_input_Mouse.cpp @@ -71,25 +71,29 @@ void SetupMouse(); void InitializeMouseFields(); void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer_obj, jobject button_buffer_obj); -static void getScreenClientRect(RECT* clientRect, RECT* windowRect) +/* Return the RECT of the current client area in the current window + * in screen coordinates + */ +static void getScreenClientRect(RECT* screen_client_rect) { - GetClientRect(display_hwnd, clientRect); - // transform clientRect to screen coordinates - clientRect->top = -clientSize.top + windowRect->top; - clientRect->left = -clientSize.left + windowRect->left; - clientRect->bottom += clientRect->top; - clientRect->right += clientRect->left; + /* We can't use GetClientRect directly, because it is in + * local window coordinates and we can't use GetWindowRect + * because it returns the screen coordinate RECT of the entire + * window, inluding decoration. Luckily, the WINDOWINFO structure contains + * the client rect in screen coordinates. + */ + WINDOWINFO window_info; + window_info.cbSize = sizeof(WINDOWINFO); + GetWindowInfo(getCurrentHWND(), &window_info); + *screen_client_rect = window_info.rcClient; } static void resetCursorPos(void) { /* Reset cursor position to middle of the window */ RECT clientRect; - GetWindowRect(display_hwnd, &windowRect); - getScreenClientRect(&clientRect, &windowRect); + getScreenClientRect(&clientRect); cursorPos.x = (clientRect.left + clientRect.right)/2; cursorPos.y = clientRect.bottom - 1 - (clientRect.bottom - clientRect.top)/2; -/* SetCursorPos(cursorPos.x, cursorPos.y); - GetCursorPos(&cursorPos);*/ } JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nHasWheel(JNIEnv *, jclass) { @@ -230,11 +234,10 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nSetNativeCursor if (handle_buffer != NULL) { HCURSOR *cursor_handle = (HCURSOR *)env->GetDirectBufferAddress(handle_buffer); HCURSOR cursor = *cursor_handle; -// HCURSOR cursor = (HCURSOR)cursor_handle; - SetClassLong(display_hwnd, GCL_HCURSOR, (LONG)cursor); + SetClassLong(getCurrentHWND(), GCL_HCURSOR, (LONG)cursor); SetCursor(cursor); } else { - SetClassLong(display_hwnd, GCL_HCURSOR, (LONG)NULL); + SetClassLong(getCurrentHWND(), GCL_HCURSOR, (LONG)NULL); SetCursor(LoadCursor(NULL, IDC_ARROW)); } } @@ -279,7 +282,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nGrabMouse resetCursorPos(); } mDIDevice->Unacquire(); - if(mDIDevice->SetCooperativeLevel(display_hwnd, mouseMask) != DI_OK) { + if(mDIDevice->SetCooperativeLevel(getCurrentHWND(), mouseMask) != DI_OK) { throwException(env, "Could not set the CooperativeLevel."); return; } @@ -375,7 +378,7 @@ static void SetupMouse() { mDIDevice->SetProperty(DIPROP_BUFFERSIZE, &dipropdw.diph); // set the cooperative level - if(mDIDevice->SetCooperativeLevel(display_hwnd, mouseMask) != DI_OK) { + if(mDIDevice->SetCooperativeLevel(getCurrentHWND(), mouseMask) != DI_OK) { printfDebug("SetCooperativeLevel failed\n"); mCreate_success = false; return; @@ -394,18 +397,18 @@ static int cap(int val, int min, int max) { } static void getGDICursorDelta(int* return_dx, int* return_dy) { - int dx = 0; - int dy = 0; + int dx; + int dy; POINT newCursorPos; GetCursorPos(&newCursorPos); RECT clientRect; RECT newWindowRect; - GetWindowRect(display_hwnd, &newWindowRect); + GetWindowRect(getCurrentHWND(), &newWindowRect); cursorPos.x += newWindowRect.left - windowRect.left; cursorPos.y += newWindowRect.top - windowRect.top; windowRect = newWindowRect; - getScreenClientRect(&clientRect, &windowRect); + getScreenClientRect(&clientRect); // Clip the position to the client rect newCursorPos.x = cap(newCursorPos.x, clientRect.left, clientRect.right - 1); newCursorPos.y = cap(newCursorPos.y, clientRect.top, clientRect.bottom - 1); diff --git a/src/native/win32/org_lwjgl_opengl_Display.cpp b/src/native/win32/org_lwjgl_opengl_Display.cpp index 8c102d3a..48647261 100644 --- a/src/native/win32/org_lwjgl_opengl_Display.cpp +++ b/src/native/win32/org_lwjgl_opengl_Display.cpp @@ -49,7 +49,7 @@ static bool oneShotInitialised = false; // Registers the LWJGL window class -HWND display_hwnd = NULL; // Handle to the window +static HWND display_hwnd = NULL; // Handle to the window static HDC display_hdc = NULL; // Device context static HGLRC display_hglrc = NULL; // OpenGL context static bool isFullScreen = false; // Whether we're fullscreen or not @@ -58,7 +58,6 @@ static bool isFocused = false; // whether we're focused or no static bool isDirty = false; // Whether we're dirty or not static bool isUndecorated = false; // Whether we're undecorated or not extern HINSTANCE dll_handle; // Handle to the LWJGL dll -RECT clientSize; static bool closerequested; static int pixel_format_index; @@ -78,6 +77,10 @@ bool applyPixelFormat(HDC hdc, int iPixelFormat) { return true; } +HWND getCurrentHWND() { + return display_hwnd; +} + HGLRC getCurrentContext() { return display_hglrc; } @@ -440,6 +443,7 @@ HWND createWindow(int width, int height, bool fullscreen, bool undecorated) // If we're not a fullscreen window, adjust the height to account for the // height of the title bar (unless undecorated) + RECT clientSize; clientSize.bottom = height; clientSize.left = 0; clientSize.right = width; @@ -573,43 +577,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_nMakeCurrent throwException(env, "Could not make display context current"); } -/* -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_nReshape - (JNIEnv *env, jclass clazz, jint x, jint y, jint width, jint height) -{ - if (isFullScreen) { - return; - } - - int exstyle, windowflags; - - if (isUndecorated) { - exstyle = WS_EX_APPWINDOW; - windowflags = WS_OVERLAPPED; - } else { - exstyle = WS_EX_APPWINDOW; - windowflags = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 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 - ); - - SetWindowPos(hwnd, HWND_TOP, x, y, clientSize.right - clientSize.left, - clientSize.bottom - clientSize.top, SWP_NOZORDER); -} -*/ - JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_opengl_Display_nGetAvailableDisplayModes(JNIEnv *env, jclass clazz) { return getAvailableDisplayModes(env); }