diff options
author | Damien Zammit <damien@zamaudio.com> | 2015-02-17 11:59:35 +1100 |
---|---|---|
committer | Damien Zammit <damien@zamaudio.com> | 2015-02-17 11:59:35 +1100 |
commit | 78883fa7f7ea9d85477a27ed3c569a899ee305fb (patch) | |
tree | f1add63ce6f3af86340d7e4c93092776b5479b14 /libs/dgl/src/pugl/pugl_win.cpp | |
parent | af7a094a0c0e8b658736d77361a2d8d2f99b0006 (diff) | |
parent | 8da011deb152227714196f61eab26e1af37a2890 (diff) |
Merge pull request #24 from falkTX/master
Update DPF, inital file-browser for ZamSFZ
Diffstat (limited to 'libs/dgl/src/pugl/pugl_win.cpp')
-rw-r--r-- | libs/dgl/src/pugl/pugl_win.cpp | 76 |
1 files changed, 49 insertions, 27 deletions
diff --git a/libs/dgl/src/pugl/pugl_win.cpp b/libs/dgl/src/pugl/pugl_win.cpp index 9837671..0837ac1 100644 --- a/libs/dgl/src/pugl/pugl_win.cpp +++ b/libs/dgl/src/pugl/pugl_win.cpp @@ -22,6 +22,7 @@ #include <windowsx.h> #include <GL/gl.h> +#include <ctime> #include <stdio.h> #include <stdlib.h> @@ -37,7 +38,7 @@ # define WHEEL_DELTA 120 #endif -#define PUGL_LOCAL_CLOSE_MSG (WM_USER + 50) +const int LOCAL_CLOSE_MSG = WM_USER + 50; HINSTANCE hInstance = NULL; @@ -51,6 +52,7 @@ struct PuglInternalsImpl { LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); +#if 0 extern "C" { BOOL WINAPI DllMain(HINSTANCE hInst, DWORD, LPVOID) @@ -59,6 +61,7 @@ DllMain(HINSTANCE hInst, DWORD, LPVOID) return 1; } } // extern "C" +#endif PuglInternals* puglInitInternals() @@ -79,7 +82,9 @@ puglCreateWindow(PuglView* view, const char* title) // Should class be a parameter? Does this make sense on other platforms? static int wc_count = 0; char classNameBuf[256]; - _snprintf(classNameBuf, sizeof(classNameBuf), "%s_%d\n", title, wc_count++); + std::srand((std::time(NULL))); + _snprintf(classNameBuf, sizeof(classNameBuf), "%s_%d-%d", title, std::rand(), ++wc_count); + classNameBuf[sizeof(classNameBuf)-1] = '\0'; impl->wc.style = CS_OWNDC; impl->wc.lpfnWndProc = wndProc; @@ -90,36 +95,36 @@ puglCreateWindow(PuglView* view, const char* title) impl->wc.hCursor = LoadCursor(hInstance, IDC_ARROW); impl->wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); impl->wc.lpszMenuName = NULL; - impl->wc.lpszClassName = classNameBuf; - RegisterClass(&impl->wc); + impl->wc.lpszClassName = strdup(classNameBuf); - int winFlags = WS_POPUPWINDOW | WS_CAPTION; - if (view->resizable) { - winFlags |= WS_SIZEBOX; + if (!RegisterClass(&impl->wc)) { + free((void*)impl->wc.lpszClassName); + free(impl); + free(view); + return 1; } // Adjust the overall window size to accomodate our requested client size + const int winFlags = WS_POPUPWINDOW | WS_CAPTION | (view->resizable ? WS_SIZEBOX : 0x0); RECT wr = { 0, 0, view->width, view->height }; - AdjustWindowRectEx(&wr, winFlags, FALSE, WS_EX_TOPMOST); + AdjustWindowRectEx(&wr, view->parent ? WS_CHILD : winFlags, FALSE, WS_EX_TOPMOST); impl->hwnd = CreateWindowEx( WS_EX_TOPMOST, classNameBuf, title, - view->parent ? WS_CHILD : winFlags, + view->parent ? (WS_CHILD | WS_VISIBLE) : winFlags, CW_USEDEFAULT, CW_USEDEFAULT, wr.right-wr.left, wr.bottom-wr.top, (HWND)view->parent, NULL, hInstance, NULL); if (!impl->hwnd) { + UnregisterClass(impl->wc.lpszClassName, NULL); + free((void*)impl->wc.lpszClassName); free(impl); free(view); return 1; } -#ifdef _WIN64 SetWindowLongPtr(impl->hwnd, GWLP_USERDATA, (LONG_PTR)view); -#else - SetWindowLongPtr(impl->hwnd, GWL_USERDATA, (LONG)view); -#endif impl->hdc = GetDC(impl->hwnd); @@ -137,6 +142,16 @@ puglCreateWindow(PuglView* view, const char* title) SetPixelFormat(impl->hdc, format, &pfd); impl->hglrc = wglCreateContext(impl->hdc); + if (!impl->hglrc) { + ReleaseDC (impl->hwnd, impl->hdc); + DestroyWindow (impl->hwnd); + UnregisterClass (impl->wc.lpszClassName, NULL); + free((void*)impl->wc.lpszClassName); + free(impl); + free(view); + return 1; + } + wglMakeCurrent(impl->hdc, impl->hglrc); return 0; @@ -166,6 +181,7 @@ puglDestroy(PuglView* view) ReleaseDC(view->impl->hwnd, view->impl->hdc); DestroyWindow(view->impl->hwnd); UnregisterClass(view->impl->wc.lpszClassName, NULL); + free((void*)view->impl->wc.lpszClassName); free(view->impl); free(view); } @@ -190,13 +206,13 @@ puglDisplay(PuglView* view) { wglMakeCurrent(view->impl->hdc, view->impl->hglrc); + view->redisplay = false; if (view->displayFunc) { view->displayFunc(view); } glFlush(); SwapBuffers(view->impl->hdc); - view->redisplay = false; } static PuglKey @@ -309,17 +325,21 @@ handleMessage(PuglView* view, UINT message, WPARAM wParam, LPARAM lParam) case WM_MOUSEWHEEL: if (view->scrollFunc) { view->event_timestamp_ms = GetMessageTime(); + POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; + ScreenToClient(view->impl->hwnd, &pt); view->scrollFunc( - view, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), - 0.0f, (int16_t)HIWORD(wParam) / (float)WHEEL_DELTA); + view, pt.x, pt.y, + 0, GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA); } break; case WM_MOUSEHWHEEL: if (view->scrollFunc) { view->event_timestamp_ms = GetMessageTime(); + POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; + ScreenToClient(view->impl->hwnd, &pt); view->scrollFunc( - view, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), - (int16_t)HIWORD(wParam) / float(WHEEL_DELTA), 0.0f); + view, pt.x, pt.y, + GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA, 0); } break; case WM_KEYDOWN: @@ -333,11 +353,18 @@ handleMessage(PuglView* view, UINT message, WPARAM wParam, LPARAM lParam) view->specialFunc(view, message == WM_KEYDOWN, key); } } else if (view->keyboardFunc) { - view->keyboardFunc(view, message == WM_KEYDOWN, wParam); + static BYTE kbs[256]; + if (GetKeyboardState(kbs) != FALSE) { + char lb[2]; + UINT scanCode = (lParam >> 8) & 0xFFFFFF00; + if ( 1 == ToAscii(wParam, scanCode, kbs, (LPWORD)lb, 0)) { + view->keyboardFunc(view, message == WM_KEYDOWN, (char)lb[0]); + } + } } break; case WM_QUIT: - case PUGL_LOCAL_CLOSE_MSG: + case LOCAL_CLOSE_MSG: if (view->closeFunc) { view->closeFunc(view); } @@ -358,7 +385,6 @@ puglProcessEvents(PuglView* view) handleMessage(view, msg.message, msg.wParam, msg.lParam); } - if (view->redisplay) { InvalidateRect(view->impl->hwnd, NULL, FALSE); } @@ -369,23 +395,19 @@ puglProcessEvents(PuglView* view) LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { -#ifdef _WIN64 PuglView* view = (PuglView*)GetWindowLongPtr(hwnd, GWLP_USERDATA); -#else - PuglView* view = (PuglView*)GetWindowLongPtr(hwnd, GWL_USERDATA); -#endif switch (message) { case WM_CREATE: PostMessage(hwnd, WM_SHOWWINDOW, TRUE, 0); return 0; case WM_CLOSE: - PostMessage(hwnd, PUGL_LOCAL_CLOSE_MSG, wParam, lParam); + PostMessage(hwnd, LOCAL_CLOSE_MSG, wParam, lParam); return 0; case WM_DESTROY: return 0; default: - if (view) { + if (view && hwnd == view->impl->hwnd) { return handleMessage(view, message, wParam, lParam); } else { return DefWindowProc(hwnd, message, wParam, lParam); |