| 1 | #include <cvd/glwindow.h> |
| 2 | #include <exception> |
| 3 | |
| 4 | #include <windows.h> |
| 5 | #include <windowsx.h> |
| 6 | |
| 7 | #include <gl/gl.h> |
| 8 | |
| 9 | #include <cassert> |
| 10 | #include <iostream> |
| 11 | #include <map> |
| 12 | using namespace std; |
| 13 | |
| 14 | namespace CVD |
| 15 | { |
| 16 | |
| 17 | Exceptions::GLWindow::CreationError::CreationError(std::string w) |
| 18 | : CVD::Exceptions::GLWindow::All { "GLWindow creation error: " + w } |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | Exceptions::GLWindow::RuntimeError::RuntimeError(std::string w) |
| 23 | : CVD::Exceptions::GLWindow::All { "GLWindow error: " + w } |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | struct GLWindow::State |
| 28 | { |
| 29 | ImageRef size; |
| 30 | ImageRef position; |
| 31 | ImageRef size_offset; // offsets dictated by windows decoration to get proper MoveWindows arguments |
| 32 | ImageRef position_offset; |
| 33 | std::string title; |
| 34 | |
| 35 | GLWindow* parent; |
| 36 | |
| 37 | HGLRC hRC; |
| 38 | HDC hDC; |
| 39 | HWND hWnd; |
| 40 | |
| 41 | bool has_just_closed; |
| 42 | bool is_closed; |
| 43 | bool needs_repaint; |
| 44 | }; |
| 45 | |
| 46 | static map<HWND, GLWindow::State> windowMap; |
| 47 | |
| 48 | LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); |
| 49 | |
| 50 | static GLWindow::EventHandler* currentHandler = NULL; |
| 51 | |
| 52 | static bool windowClassRegistered = false; |
| 53 | |
| 54 | void GLWindow::init(const ImageRef& size, int bpp, const std::string& title, const std::string&) |
| 55 | { |
| 56 | GLuint PixelFormat; // Holds The Results After Searching For A Match |
| 57 | WNDCLASS wc; // Windows Class Structure |
| 58 | DWORD dwExStyle; // Window Extended Style |
| 59 | DWORD dwStyle; // Window Style |
| 60 | RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values |
| 61 | WindowRect.left = (long)0; // Set Left Value To 0 |
| 62 | WindowRect.right = (long)size.x; // Set Right Value To Requested Width |
| 63 | WindowRect.top = (long)0; // Set Top Value To 0 |
| 64 | WindowRect.bottom = (long)size.y; // Set Bottom Value To Requested Height |
| 65 | |
| 66 | //fullscreen=fullscreenflag; // Set The Global Fullscreen Flag |
| 67 | // Grab An Instance For Our Window |
| 68 | HINSTANCE hInstance = GetModuleHandle(NULL); |
| 69 | if(!windowClassRegistered) |
| 70 | { |
| 71 | wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window. |
| 72 | wc.lpfnWndProc = (WNDPROC)WndProc; // WndProc Handles Messages |
| 73 | wc.cbClsExtra = 0; // No Extra Window Data |
| 74 | wc.cbWndExtra = 0; // No Extra Window Data |
| 75 | wc.hInstance = hInstance; // Set The Instance |
| 76 | wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon |
| 77 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer |
| 78 | wc.hbrBackground = NULL; // No Background Required For GL |
| 79 | wc.lpszMenuName = NULL; // We Don't Want A Menu |
| 80 | wc.lpszClassName = "glwindow"; // Set The Class Name |
| 81 | |
| 82 | if(!RegisterClass(&wc)) |
| 83 | throw Exceptions::GLWindow::CreationError("Failed to register the Window Class."); |
| 84 | windowClassRegistered = true; |
| 85 | } |
| 86 | |
| 87 | #if 0 |
| 88 | if (fullscreen) // Attempt Fullscreen Mode? |
| 89 | { |
| 90 | DEVMODE dmScreenSettings; // Device Mode |
| 91 | memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared |
| 92 | dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure |
| 93 | dmScreenSettings.dmPelsWidth = width; // Selected Screen Width |
| 94 | dmScreenSettings.dmPelsHeight = height; // Selected Screen Height |
| 95 | dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel |
| 96 | dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; |
| 97 | |
| 98 | // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar. |
| 99 | if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) |
| 100 | { |
| 101 | // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode. |
| 102 | if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) |
| 103 | { |
| 104 | fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | // Pop Up A Message Box Letting User Know The Program Is Closing. |
| 109 | MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP); |
| 110 | return FALSE; // Return FALSE |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (fullscreen) // Are We Still In Fullscreen Mode? |
| 116 | { |
| 117 | dwExStyle=WS_EX_APPWINDOW; // Window Extended Style |
| 118 | dwStyle=WS_POPUP; // Windows Style |
| 119 | ShowCursor(FALSE); // Hide Mouse Pointer |
| 120 | } |
| 121 | else |
| 122 | #endif |
| 123 | { |
| 124 | dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style |
| 125 | dwStyle = WS_OVERLAPPEDWINDOW; // Windows Style |
| 126 | } |
| 127 | |
| 128 | RECT oldRect = WindowRect; |
| 129 | AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size |
| 130 | |
| 131 | // Create The Window |
| 132 | HWND hWnd; |
| 133 | if(!(hWnd = CreateWindowEx(dwExStyle, // Extended Style For The Window |
| 134 | "glwindow", // Class Name |
| 135 | NULL, //title.c_str(), // Window Title |
| 136 | dwStyle | // Defined Window Style |
| 137 | WS_CLIPSIBLINGS | // Required Window Style |
| 138 | WS_CLIPCHILDREN, // Required Window Style |
| 139 | 0, 0, // Window Position |
| 140 | WindowRect.right - WindowRect.left, // Calculate Window Width |
| 141 | WindowRect.bottom - WindowRect.top, // Calculate Window Height |
| 142 | NULL, // No Parent Window |
| 143 | NULL, // No Menu |
| 144 | hInstance, // Instance |
| 145 | NULL))) // Dont Pass Anything To WM_CREATE |
| 146 | { |
| 147 | throw Exceptions::GLWindow::CreationError("Window Creation Error."); |
| 148 | } |
| 149 | |
| 150 | static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be |
| 151 | { |
| 152 | sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor |
| 153 | 1, // Version Number |
| 154 | PFD_DRAW_TO_WINDOW | // Format Must Support Window |
| 155 | PFD_SUPPORT_OPENGL | // Format Must Support OpenGL |
| 156 | PFD_DOUBLEBUFFER, // Must Support Double Buffering |
| 157 | PFD_TYPE_RGBA, // Request An RGBA Format |
| 158 | static_cast<unsigned char>(bpp), // Select Our Color Depth |
| 159 | 0, 0, 0, 0, 0, 0, // Color Bits Ignored |
| 160 | 0, // No Alpha Buffer |
| 161 | 0, // Shift Bit Ignored |
| 162 | 0, // No Accumulation Buffer |
| 163 | 0, 0, 0, 0, // Accumulation Bits Ignored |
| 164 | 32, // 32Bit Z-Buffer (Depth Buffer) |
| 165 | 8, // 8bit Stencil Buffer |
| 166 | 0, // No Auxiliary Buffer |
| 167 | PFD_MAIN_PLANE, // Main Drawing Layer |
| 168 | 0, // Reserved |
| 169 | 0, 0, 0 // Layer Masks Ignored |
| 170 | }; |
| 171 | |
| 172 | HDC hDC; |
| 173 | if(!(hDC = GetDC(hWnd))) |
| 174 | throw Exceptions::GLWindow::CreationError("Can't create a GL Device Context."); |
| 175 | |
| 176 | if(!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) |
| 177 | throw Exceptions::GLWindow::CreationError("Can't find a suitable PixelFormat."); |
| 178 | |
| 179 | if(!SetPixelFormat(hDC, PixelFormat, &pfd)) |
| 180 | throw Exceptions::GLWindow::CreationError("Can't Set The PixelFormat."); |
| 181 | |
| 182 | HGLRC hRC; |
| 183 | if(!(hRC = wglCreateContext(hDC))) |
| 184 | throw Exceptions::GLWindow::CreationError("Can't Create A GL Rendering Context."); |
| 185 | |
| 186 | if(!wglMakeCurrent(hDC, hRC)) |
| 187 | throw Exceptions::GLWindow::CreationError("Can't Activate The GL Rendering Context."); |
| 188 | |
| 189 | ShowWindow(hWnd, SW_SHOW); // Show The Window |
| 190 | SetForegroundWindow(hWnd); // Slightly Higher Priority |
| 191 | SetFocus(hWnd); // Sets Keyboard Focus To The Window |
| 192 | |
| 193 | glLoadIdentity(); |
| 194 | glViewport(0, 0, size.x, size.y); |
| 195 | glMatrixMode(GL_PROJECTION); |
| 196 | glLoadIdentity(); |
| 197 | glColor3f(1.0f, 1.0f, 1.0f); |
| 198 | glOrtho(0, size.x, size.y, 0, -1, 1); |
| 199 | glPixelZoom(1, -1); |
| 200 | glRasterPos2f(0, 0); |
| 201 | |
| 202 | state = &windowMap[hWnd]; |
| 203 | state->parent = this; |
| 204 | state->size = size; |
| 205 | state->title = title; |
| 206 | state->hRC = hRC; |
| 207 | state->hDC = hDC; |
| 208 | state->hWnd = hWnd; |
| 209 | |
| 210 | state->size_offset.x = (WindowRect.right - WindowRect.left) - (oldRect.right - oldRect.left); |
| 211 | state->size_offset.y = (WindowRect.bottom - WindowRect.top) - (oldRect.bottom - oldRect.top); |
| 212 | state->position_offset.x = WindowRect.left - oldRect.left; |
| 213 | state->position_offset.y = WindowRect.top - oldRect.top; |
| 214 | |
| 215 | state->position = -state->position_offset; |
| 216 | |
| 217 | state->has_just_closed = false; |
| 218 | state->is_closed = false; |
| 219 | state->needs_repaint = false; |
| 220 | |
| 221 | // handle events to make window appear |
| 222 | EventSummary summary; |
| 223 | get_events(summary); |
| 224 | |
| 225 | // CWK: actually set window title |
| 226 | set_title(title); |
| 227 | } |
| 228 | |
| 229 | void markGLWindowAsClosed(GLWindow::State* state) |
| 230 | { |
| 231 | if(state == NULL || state->is_closed) |
| 232 | { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | if(!state->is_closed) |
| 237 | { |
| 238 | state->has_just_closed = true; |
| 239 | state->is_closed = true; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void closeGLWindow(GLWindow::State* state) |
| 244 | { |
| 245 | if(state == NULL || state->is_closed) |
| 246 | { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | if(state->hRC) |
| 251 | { |
| 252 | if(wglGetCurrentContext() == state->hRC) |
| 253 | if(!wglMakeCurrent(NULL, NULL)) |
| 254 | throw Exceptions::GLWindow::RuntimeError("Release of DC and RC failed."); |
| 255 | if(!wglDeleteContext(state->hRC)) |
| 256 | throw Exceptions::GLWindow::RuntimeError("Release Rendering Context failed."); |
| 257 | |
| 258 | state->hRC = NULL; |
| 259 | } |
| 260 | |
| 261 | if(state->hDC && !ReleaseDC(state->hWnd, state->hDC)) |
| 262 | { |
| 263 | throw Exceptions::GLWindow::RuntimeError("Release Device Context failed."); |
| 264 | |
| 265 | state->hDC = NULL; |
| 266 | } |
| 267 | |
| 268 | if(state->hWnd && !DestroyWindow(state->hWnd)) |
| 269 | { |
| 270 | throw Exceptions::GLWindow::RuntimeError("Destroy Window failed."); |
| 271 | |
| 272 | // We don't want to set hWnd to NULL here, as we need it in the destructor to remove state from windowMap. |
| 273 | } |
| 274 | |
| 275 | #if 0 |
| 276 | if (!UnregisterClass("glwindow",GetModuleHandle(NULL))) |
| 277 | throw Exceptions::GLWindow::RuntimeError("Could not unregister Class."); |
| 278 | #endif |
| 279 | |
| 280 | markGLWindowAsClosed(state); |
| 281 | } |
| 282 | |
| 283 | ImageRef GLWindow::size() const |
| 284 | { |
| 285 | if(state == NULL || state->is_closed) |
| 286 | { |
| 287 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 288 | } |
| 289 | |
| 290 | return state->size; |
| 291 | } |
| 292 | |
| 293 | GLWindow::~GLWindow() |
| 294 | { |
| 295 | closeGLWindow(state); |
| 296 | windowMap.erase(state->hWnd); |
| 297 | state = NULL; |
| 298 | } |
| 299 | |
| 300 | void GLWindow::set_size(const ImageRef& s_) |
| 301 | { |
| 302 | if(state == NULL || state->is_closed) |
| 303 | { |
| 304 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 305 | } |
| 306 | |
| 307 | state->size = s_; |
| 308 | MoveWindow(state->hWnd, state->position.x + state->position_offset.x, state->position.y + state->position_offset.y, state->size.x + state->size_offset.x, state->size.y + state->size_offset.y, FALSE); |
| 309 | } |
| 310 | |
| 311 | ImageRef GLWindow::position() const |
| 312 | { |
| 313 | if(state == NULL || state->is_closed) |
| 314 | { |
| 315 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 316 | } |
| 317 | |
| 318 | return state->position; |
| 319 | } |
| 320 | |
| 321 | void GLWindow::set_position(const ImageRef& p_) |
| 322 | { |
| 323 | if(state == NULL) |
| 324 | { |
| 325 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 326 | } |
| 327 | |
| 328 | state->position = p_; |
| 329 | MoveWindow(state->hWnd, state->position.x + state->position_offset.x, state->position.y + state->position_offset.y, state->size.x + state->size_offset.x, state->size.y + state->size_offset.y, FALSE); |
| 330 | } |
| 331 | |
| 332 | void GLWindow::set_cursor_position(const ImageRef& where) |
| 333 | { |
| 334 | // FIXME |
| 335 | //XWarpPointer(state->display, None, state->window, 0, 0, 0, 0, where.x, where.y); |
| 336 | } |
| 337 | |
| 338 | ImageRef GLWindow::cursor_position() const |
| 339 | { |
| 340 | if(state == NULL || state->is_closed) |
| 341 | { |
| 342 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 343 | } |
| 344 | |
| 345 | ImageRef where; |
| 346 | POINT point; |
| 347 | GetCursorPos(&point); |
| 348 | where.x = point.x - state->position.x; |
| 349 | where.y = point.y - state->position.y; |
| 350 | return where; |
| 351 | } |
| 352 | |
| 353 | void GLWindow::show_cursor(bool show) |
| 354 | { |
| 355 | if(state == NULL || state->is_closed) |
| 356 | { |
| 357 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 358 | } |
| 359 | |
| 360 | if(show) |
| 361 | ShowCursor(TRUE); |
| 362 | else |
| 363 | ShowCursor(FALSE); |
| 364 | } |
| 365 | |
| 366 | std::string GLWindow::title() const |
| 367 | { |
| 368 | if(state == NULL || state->is_closed) |
| 369 | { |
| 370 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 371 | } |
| 372 | |
| 373 | return state->title; |
| 374 | } |
| 375 | |
| 376 | void GLWindow::set_title(const std::string& title) |
| 377 | { |
| 378 | if(state == NULL || state->is_closed) |
| 379 | { |
| 380 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 381 | } |
| 382 | |
| 383 | state->title = title; |
| 384 | SetWindowText(state->hWnd, state->title.c_str()); |
| 385 | } |
| 386 | |
| 387 | void GLWindow::swap_buffers() |
| 388 | { |
| 389 | if(state == NULL || state->is_closed) |
| 390 | { |
| 391 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 392 | } |
| 393 | |
| 394 | SwapBuffers(state->hDC); |
| 395 | } |
| 396 | |
| 397 | inline int convertButtonState(const WPARAM state) |
| 398 | { |
| 399 | int ret = 0; |
| 400 | if(state & MK_LBUTTON) |
| 401 | ret |= GLWindow::BUTTON_LEFT; |
| 402 | if(state & MK_MBUTTON) |
| 403 | ret |= GLWindow::BUTTON_MIDDLE; |
| 404 | if(state & MK_RBUTTON) |
| 405 | ret |= GLWindow::BUTTON_RIGHT; |
| 406 | if(state & MK_CONTROL) |
| 407 | ret |= GLWindow::BUTTON_MOD_CTRL; |
| 408 | if(state & MK_SHIFT) |
| 409 | ret |= GLWindow::BUTTON_MOD_SHIFT; |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | inline ImageRef convertPosition(LPARAM param) |
| 414 | { |
| 415 | return ImageRef(GET_X_LPARAM(param), GET_Y_LPARAM(param)); |
| 416 | } |
| 417 | |
| 418 | void GLWindow::handle_events(EventHandler& handler) |
| 419 | { |
| 420 | if(state != NULL && state->has_just_closed) |
| 421 | { |
| 422 | state->has_just_closed = false; |
| 423 | handler.on_event(*this, EVENT_CLOSE); |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | if(state == NULL || state->is_closed) |
| 428 | { |
| 429 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 430 | } |
| 431 | |
| 432 | if(state->needs_repaint) |
| 433 | { |
| 434 | state->needs_repaint = false; |
| 435 | handler.on_event(*this, EVENT_EXPOSE); |
| 436 | } |
| 437 | |
| 438 | MSG msg; |
| 439 | |
| 440 | currentHandler = &handler; // for events only received in Window Procedure |
| 441 | |
| 442 | while(PeekMessage(&msg, state->hWnd, 0, 0, PM_REMOVE)) |
| 443 | { |
| 444 | //TranslateMessage(&msg); // don't care for WM_CHAR/WM_DEADCHAR messages |
| 445 | switch(msg.message) |
| 446 | { |
| 447 | case WM_LBUTTONDOWN: |
| 448 | handler.on_mouse_down(*this, convertPosition(msg.lParam), convertButtonState(msg.wParam), GLWindow::BUTTON_LEFT); |
| 449 | break; |
| 450 | case WM_LBUTTONUP: |
| 451 | handler.on_mouse_up(*this, convertPosition(msg.lParam), convertButtonState(msg.wParam), GLWindow::BUTTON_LEFT); |
| 452 | break; |
| 453 | case WM_MBUTTONDOWN: |
| 454 | handler.on_mouse_down(*this, convertPosition(msg.lParam), convertButtonState(msg.wParam), GLWindow::BUTTON_MIDDLE); |
| 455 | break; |
| 456 | case WM_MBUTTONUP: |
| 457 | handler.on_mouse_up(*this, convertPosition(msg.lParam), convertButtonState(msg.wParam), GLWindow::BUTTON_MIDDLE); |
| 458 | break; |
| 459 | case WM_RBUTTONDOWN: |
| 460 | handler.on_mouse_down(*this, convertPosition(msg.lParam), convertButtonState(msg.wParam), GLWindow::BUTTON_RIGHT); |
| 461 | break; |
| 462 | case WM_RBUTTONUP: |
| 463 | handler.on_mouse_up(*this, convertPosition(msg.lParam), convertButtonState(msg.wParam), GLWindow::BUTTON_RIGHT); |
| 464 | break; |
| 465 | case WM_MOUSEWHEEL: |
| 466 | // positive forward, negative backward, FIXME check correspondence to X11 implementation |
| 467 | handler.on_mouse_up(*this, convertPosition(msg.lParam), convertButtonState(GET_KEYSTATE_WPARAM(msg.wParam)), (GET_WHEEL_DELTA_WPARAM(msg.wParam) > 0) ? GLWindow::BUTTON_WHEEL_UP : GLWindow::BUTTON_WHEEL_DOWN); |
| 468 | break; |
| 469 | case WM_MOUSEMOVE: |
| 470 | handler.on_mouse_move(*this, convertPosition(msg.lParam), convertButtonState(msg.wParam)); |
| 471 | break; |
| 472 | case WM_KEYDOWN: |
| 473 | { |
| 474 | unsigned char state[256]; |
| 475 | GetKeyboardState(state); |
| 476 | char buffer[4]; |
| 477 | int res = ToAscii((UINT)msg.wParam, (UINT)(msg.lParam & 0xffffff) >> 16, state, (LPWORD)buffer, 0); |
| 478 | if(res == 1) |
| 479 | { |
| 480 | handler.on_key_down(*this, buffer[0]); |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | handler.on_key_down(*this, (int)msg.wParam); |
| 485 | } |
| 486 | } |
| 487 | break; |
| 488 | case WM_KEYUP: |
| 489 | { |
| 490 | unsigned char state[256]; |
| 491 | GetKeyboardState(state); |
| 492 | char buffer[4]; |
| 493 | int res = ToAscii((UINT)msg.wParam, (UINT)(msg.lParam & 0xffffff) >> 16, state, (LPWORD)buffer, 0); |
| 494 | if(res == 1) |
| 495 | { |
| 496 | handler.on_key_up(*this, buffer[0]); |
| 497 | } |
| 498 | else |
| 499 | { |
| 500 | handler.on_key_up(*this, (int)msg.wParam); |
| 501 | } |
| 502 | } |
| 503 | break; |
| 504 | case WM_PAINT: |
| 505 | // This will never be called here, it will always be in WndProc... |
| 506 | handler.on_event(*this, EVENT_EXPOSE); |
| 507 | default: |
| 508 | DispatchMessage(&msg); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | currentHandler = NULL; |
| 513 | } |
| 514 | |
| 515 | class SaveEvents : public GLWindow::EventHandler |
| 516 | { |
| 517 | private: |
| 518 | std::vector<GLWindow::Event>& events; |
| 519 | |
| 520 | public: |
| 521 | SaveEvents(std::vector<GLWindow::Event>& events_) |
| 522 | : events(events_) |
| 523 | { |
| 524 | } |
| 525 | void on_key_down(GLWindow&, int key) |
| 526 | { |
| 527 | GLWindow::Event e; |
| 528 | e.type = GLWindow::Event::KEY_DOWN; |
| 529 | e.which = key; |
| 530 | events.push_back(e); |
| 531 | } |
| 532 | void on_key_up(GLWindow&, int key) |
| 533 | { |
| 534 | GLWindow::Event e; |
| 535 | e.type = GLWindow::Event::KEY_UP; |
| 536 | e.which = key; |
| 537 | events.push_back(e); |
| 538 | } |
| 539 | |
| 540 | void on_mouse_move(GLWindow&, ImageRef where, int state) |
| 541 | { |
| 542 | GLWindow::Event e; |
| 543 | e.type = GLWindow::Event::MOUSE_MOVE; |
| 544 | e.state = state; |
| 545 | e.where = where; |
| 546 | events.push_back(e); |
| 547 | } |
| 548 | |
| 549 | void on_mouse_down(GLWindow&, ImageRef where, int state, int button) |
| 550 | { |
| 551 | GLWindow::Event e; |
| 552 | e.type = GLWindow::Event::MOUSE_DOWN; |
| 553 | e.state = state; |
| 554 | e.which = button; |
| 555 | e.where = where; |
| 556 | events.push_back(e); |
| 557 | } |
| 558 | |
| 559 | void on_mouse_up(GLWindow&, ImageRef where, int state, int button) |
| 560 | { |
| 561 | GLWindow::Event e; |
| 562 | e.type = GLWindow::Event::MOUSE_UP; |
| 563 | e.state = state; |
| 564 | e.which = button; |
| 565 | e.where = where; |
| 566 | events.push_back(e); |
| 567 | } |
| 568 | |
| 569 | void on_resize(GLWindow&, ImageRef size) |
| 570 | { |
| 571 | GLWindow::Event e; |
| 572 | e.type = GLWindow::Event::RESIZE; |
| 573 | e.size = size; |
| 574 | events.push_back(e); |
| 575 | } |
| 576 | |
| 577 | void on_event(GLWindow&, int event) |
| 578 | { |
| 579 | GLWindow::Event e; |
| 580 | e.type = GLWindow::Event::EVENT; |
| 581 | e.which = event; |
| 582 | events.push_back(e); |
| 583 | } |
| 584 | }; |
| 585 | |
| 586 | void GLWindow::get_events(std::vector<Event>& events) |
| 587 | { |
| 588 | SaveEvents saver(events); |
| 589 | handle_events(saver); |
| 590 | } |
| 591 | |
| 592 | bool GLWindow::EventSummary::should_quit() const |
| 593 | { |
| 594 | return key_down.count(VK_ESCAPE) || events.count(GLWindow::EVENT_CLOSE); |
| 595 | } |
| 596 | |
| 597 | class MakeSummary : public GLWindow::EventHandler |
| 598 | { |
| 599 | private: |
| 600 | GLWindow::EventSummary& summary; |
| 601 | |
| 602 | public: |
| 603 | MakeSummary(GLWindow::EventSummary& summary_) |
| 604 | : summary(summary_) |
| 605 | { |
| 606 | } |
| 607 | |
| 608 | void on_key_down(GLWindow&, int key) { ++summary.key_down[key]; } |
| 609 | void on_key_up(GLWindow&, int key) { ++summary.key_up[key]; } |
| 610 | void on_mouse_move(GLWindow&, ImageRef where, int) |
| 611 | { |
| 612 | summary.cursor = where; |
| 613 | summary.cursor_moved = true; |
| 614 | } |
| 615 | void on_mouse_down(GLWindow&, ImageRef where, int state, int button) { summary.mouse_down[button] = std::make_pair(where, state); } |
| 616 | void on_mouse_up(GLWindow&, ImageRef where, int state, int button) { summary.mouse_up[button] = std::make_pair(where, state); } |
| 617 | void on_event(GLWindow&, int event) { ++summary.events[event]; } |
| 618 | }; |
| 619 | |
| 620 | void GLWindow::get_events(EventSummary& summary) |
| 621 | { |
| 622 | if(state != NULL && !state->is_closed) |
| 623 | { |
| 624 | summary.cursor = cursor_position(); |
| 625 | } |
| 626 | |
| 627 | MakeSummary ms(summary); |
| 628 | handle_events(ms); |
| 629 | } |
| 630 | |
| 631 | bool GLWindow::has_events() const |
| 632 | { |
| 633 | if(state == NULL || state->is_closed) |
| 634 | { |
| 635 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 636 | } |
| 637 | |
| 638 | MSG msg; |
| 639 | return PeekMessage(&msg, state->hWnd, 0, 0, PM_NOREMOVE) != 0; |
| 640 | } |
| 641 | |
| 642 | void GLWindow::activate() |
| 643 | { |
| 644 | if(state == NULL || state->is_closed) |
| 645 | { |
| 646 | throw Exceptions::GLWindow::RuntimeError("Window is not open."); |
| 647 | } |
| 648 | |
| 649 | if(!wglMakeCurrent(state->hDC, state->hRC)) |
| 650 | throw Exceptions::GLWindow::RuntimeError("wglMakeCurrent failed"); |
| 651 | } |
| 652 | |
| 653 | LRESULT CALLBACK WndProc(HWND hWnd, // Handle For This Window |
| 654 | UINT uMsg, // Message For This Window |
| 655 | WPARAM wParam, // Additional Message Information |
| 656 | LPARAM lParam) // Additional Message Information |
| 657 | { |
| 658 | switch(uMsg) |
| 659 | { |
| 660 | case WM_WINDOWPOSCHANGED: |
| 661 | if(windowMap.count(hWnd) == 1) |
| 662 | { |
| 663 | GLWindow::State& state = windowMap[hWnd]; |
| 664 | WINDOWPOS* pos = (WINDOWPOS*)lParam; |
| 665 | ImageRef newSize(pos->cx, pos->cy); |
| 666 | newSize -= state.size_offset; |
| 667 | if(newSize != state.size) |
| 668 | { |
| 669 | state.size = newSize; |
| 670 | state.parent->activate(); |
| 671 | glViewport(0, 0, state.size.x, state.size.y); |
| 672 | if(currentHandler != NULL) |
| 673 | currentHandler->on_resize(*state.parent, state.size); |
| 674 | else |
| 675 | cerr << "Event outside of cvd control for " << state.title << endl; |
| 676 | } |
| 677 | state.position = ImageRef(pos->x, pos->y) - state.position_offset; |
| 678 | return 0; |
| 679 | } |
| 680 | break; |
| 681 | case WM_CLOSE: |
| 682 | if(windowMap.count(hWnd) == 1) |
| 683 | { |
| 684 | GLWindow::State& state = windowMap[hWnd]; |
| 685 | closeGLWindow(&state); |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | break; |
| 691 | case WM_DESTROY: |
| 692 | if(windowMap.count(hWnd) == 1) |
| 693 | { |
| 694 | GLWindow::State& state = windowMap[hWnd]; |
| 695 | // This should happen as part of closeGLWindow when we receive WM_CLOSE, but it doesn't hurt to double-check. |
| 696 | markGLWindowAsClosed(&state); |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | break; |
| 702 | case WM_PAINT: |
| 703 | if(windowMap.count(hWnd) == 1) |
| 704 | { |
| 705 | GLWindow::State& state = windowMap[hWnd]; |
| 706 | state.needs_repaint = true; |
| 707 | // We don't want to return 0 here, as we don't actually paint anything straight away |
| 708 | // (and so Windows will keep sending messages until we do). |
| 709 | } |
| 710 | |
| 711 | break; |
| 712 | } |
| 713 | // Pass All Unhandled Messages To DefWindowProc |
| 714 | return DefWindowProc(hWnd, uMsg, wParam, lParam); |
| 715 | } |
| 716 | |
| 717 | } // namespace CVD |