From c6293cd7d1f5fe3d51192141880bb5658633752d Mon Sep 17 00:00:00 2001 From: Mirko Covizzi Date: Tue, 30 Jun 2020 21:25:53 +0200 Subject: [PATCH 1/3] Add Timer --- src/win/window.rs | 48 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/src/win/window.rs b/src/win/window.rs index 912afda..abca234 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -13,14 +13,7 @@ use self::winapi::um::wingdi::{ SwapBuffers, PFD_DOUBLEBUFFER, PFD_DRAW_TO_WINDOW, PFD_MAIN_PLANE, PFD_SUPPORT_OPENGL, PFD_TYPE_RGBA, PIXELFORMATDESCRIPTOR, }; -use self::winapi::um::winuser::{ - AdjustWindowRectEx, CreateWindowExA, DefWindowProcA, DestroyWindow, DispatchMessageA, GetDC, - GetWindowLongPtrA, MessageBoxA, PeekMessageA, PostMessageA, RegisterClassA, ReleaseDC, - SetWindowLongPtrA, TranslateMessage, UnregisterClassA, CS_OWNDC, GWLP_USERDATA, MB_ICONERROR, - MB_OK, MB_TOPMOST, MSG, PM_REMOVE, WM_CREATE, WM_QUIT, WM_SHOWWINDOW, WNDCLASSA, WS_CAPTION, - WS_CHILD, WS_CLIPSIBLINGS, WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_POPUPWINDOW, WS_SIZEBOX, - WS_VISIBLE, -}; +use self::winapi::um::winuser::{AdjustWindowRectEx, CreateWindowExA, DefWindowProcA, DestroyWindow, DispatchMessageA, GetDC, GetWindowLongPtrA, MessageBoxA, PeekMessageA, PostMessageA, RegisterClassA, ReleaseDC, SetWindowLongPtrA, TranslateMessage, UnregisterClassA, CS_OWNDC, GWLP_USERDATA, MB_ICONERROR, MB_OK, MB_TOPMOST, MSG, PM_REMOVE, WM_CREATE, WM_QUIT, WM_SHOWWINDOW, WNDCLASSA, WS_CAPTION, WS_CHILD, WS_CLIPSIBLINGS, WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_POPUPWINDOW, WS_SIZEBOX, WS_VISIBLE, SetTimer, GetMessageA}; use self::winapi::ctypes::c_void; use crate::Parent::WithParent; @@ -52,22 +45,6 @@ unsafe fn generate_guid() -> String { ) } -fn handle_msg(_window: HWND) -> bool { - unsafe { - let mut msg: MSG = std::mem::zeroed(); - loop { - if PeekMessageA(&mut msg, 0 as HWND, 0, 0, PM_REMOVE) == 0 { - return true; - } - if msg.message == WM_QUIT { - return false; - } - TranslateMessage(&msg); - DispatchMessageA(&msg); - } - } -} - unsafe extern "system" fn wnd_proc( hwnd: HWND, msg: UINT, @@ -239,17 +216,28 @@ impl Window { SetWindowLongPtrA(hwnd, GWLP_USERDATA, &Arc::clone(&win) as *const _ as _); + SetTimer(hwnd, 4242, 13, None); + // todo: decide what to do with the message pump if parent.is_null() { + let mut msg: MSG = std::mem::zeroed(); loop { - if !handle_msg(null_mut()) { - break; + let status = GetMessageA(&mut msg, hwnd, 0, 0); + if status == -1 { + break + } + TranslateMessage(&mut msg); + + match msg.message { + WM_TIMER => { + // todo: pass callback rendering function instead + gl::ClearColor(0.3, 0.8, 0.3, 1.0); + gl::Clear(gl::COLOR_BUFFER_BIT); + SwapBuffers(hdc); + } } - // todo: pass callback rendering function instead - gl::ClearColor(0.3, 0.8, 0.3, 1.0); - gl::Clear(gl::COLOR_BUFFER_BIT); - SwapBuffers(hdc); + DefWindowProcA(hwnd, msg.message, msg.wParam, msg.lParam); } } From 0bd1ae52c54669b97374715a34f006b29ded243c Mon Sep 17 00:00:00 2001 From: Mirko Covizzi Date: Tue, 30 Jun 2020 21:29:00 +0200 Subject: [PATCH 2/3] Cargo fmt --- src/win/window.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/win/window.rs b/src/win/window.rs index abca234..048a0ef 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -13,7 +13,14 @@ use self::winapi::um::wingdi::{ SwapBuffers, PFD_DOUBLEBUFFER, PFD_DRAW_TO_WINDOW, PFD_MAIN_PLANE, PFD_SUPPORT_OPENGL, PFD_TYPE_RGBA, PIXELFORMATDESCRIPTOR, }; -use self::winapi::um::winuser::{AdjustWindowRectEx, CreateWindowExA, DefWindowProcA, DestroyWindow, DispatchMessageA, GetDC, GetWindowLongPtrA, MessageBoxA, PeekMessageA, PostMessageA, RegisterClassA, ReleaseDC, SetWindowLongPtrA, TranslateMessage, UnregisterClassA, CS_OWNDC, GWLP_USERDATA, MB_ICONERROR, MB_OK, MB_TOPMOST, MSG, PM_REMOVE, WM_CREATE, WM_QUIT, WM_SHOWWINDOW, WNDCLASSA, WS_CAPTION, WS_CHILD, WS_CLIPSIBLINGS, WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_POPUPWINDOW, WS_SIZEBOX, WS_VISIBLE, SetTimer, GetMessageA}; +use self::winapi::um::winuser::{ + AdjustWindowRectEx, CreateWindowExA, DefWindowProcA, DestroyWindow, DispatchMessageA, GetDC, + GetMessageA, GetWindowLongPtrA, MessageBoxA, PeekMessageA, PostMessageA, RegisterClassA, + ReleaseDC, SetTimer, SetWindowLongPtrA, TranslateMessage, UnregisterClassA, CS_OWNDC, + GWLP_USERDATA, MB_ICONERROR, MB_OK, MB_TOPMOST, MSG, PM_REMOVE, WM_CREATE, WM_QUIT, + WM_SHOWWINDOW, WM_TIMER, WNDCLASSA, WS_CAPTION, WS_CHILD, WS_CLIPSIBLINGS, WS_MAXIMIZEBOX, + WS_MINIMIZEBOX, WS_POPUPWINDOW, WS_SIZEBOX, WS_VISIBLE, +}; use self::winapi::ctypes::c_void; use crate::Parent::WithParent; @@ -224,7 +231,7 @@ impl Window { loop { let status = GetMessageA(&mut msg, hwnd, 0, 0); if status == -1 { - break + break; } TranslateMessage(&mut msg); @@ -235,6 +242,7 @@ impl Window { gl::Clear(gl::COLOR_BUFFER_BIT); SwapBuffers(hdc); } + _ => (), } DefWindowProcA(hwnd, msg.message, msg.wParam, msg.lParam); From 46dacdfc3704b64a2f2f2612ecd67b097f7101fb Mon Sep 17 00:00:00 2001 From: Mirko Covizzi Date: Tue, 30 Jun 2020 21:53:04 +0200 Subject: [PATCH 3/3] Event management refactoring --- src/win/event.rs | 27 +++++++++++++++++++++++---- src/win/window.rs | 33 +++++++++++++++------------------ 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/win/event.rs b/src/win/event.rs index 03d68cf..768844a 100644 --- a/src/win/event.rs +++ b/src/win/event.rs @@ -3,21 +3,40 @@ use std::sync::{Arc, Mutex}; use winapi::shared::minwindef::{LPARAM, LRESULT, UINT, WPARAM}; use crate::Window; -use winapi::um::winuser::WM_MOUSEMOVE; +use winapi::um::winuser::{DefWindowProcA, WM_MOUSEMOVE, WM_TIMER}; + +const WIN_FRAME_TIMER: usize = 4242; + +unsafe fn handle_timer(win: Arc>, timer_id: usize) { + match timer_id { + WIN_FRAME_TIMER => { + win.lock().unwrap().draw_frame(); + } + _ => (), + } +} pub(crate) unsafe fn handle_message( win: Arc>, - msg: UINT, + message: UINT, wparam: WPARAM, lparam: LPARAM, ) -> LRESULT { - match msg { + let hwnd; + { + hwnd = win.lock().unwrap().hwnd; + } + match message { WM_MOUSEMOVE => { let x = (lparam & 0xFFFF) as i32; let y = ((lparam >> 16) & 0xFFFF) as i32; win.lock().unwrap().handle_mouse_motion(x, y); 0 } - _ => 0, + WM_TIMER => { + handle_timer(win, wparam); + 0 + } + _ => DefWindowProcA(hwnd, message, wparam, lparam), } } diff --git a/src/win/window.rs b/src/win/window.rs index 048a0ef..4686b73 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -58,22 +58,23 @@ unsafe extern "system" fn wnd_proc( wparam: WPARAM, lparam: LPARAM, ) -> LRESULT { - let win = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void; + let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void; match msg { WM_CREATE => { PostMessageA(hwnd, WM_SHOWWINDOW, 0, 0); 0 } _ => { - if !win.is_null() { - let win: &Arc> = std::mem::transmute(win); - let win = Arc::clone(win); + if !win_ptr.is_null() { + let win_ref: &Arc> = std::mem::transmute(win_ptr); + let win = Arc::clone(win_ref); let ret = handle_message(win, msg, wparam, lparam); // todo: need_reconfigure thing? - // return ret + return ret; } + return DefWindowProcA(hwnd, msg, wparam, lparam); } } @@ -106,7 +107,7 @@ unsafe fn unregister_wnd_class(wnd_class: ATOM) { unsafe fn init_gl_context() {} pub struct Window { - hwnd: HWND, + pub(crate) hwnd: HWND, hdc: HDC, gl_context: HGLRC, window_class: ATOM, @@ -234,18 +235,7 @@ impl Window { break; } TranslateMessage(&mut msg); - - match msg.message { - WM_TIMER => { - // todo: pass callback rendering function instead - gl::ClearColor(0.3, 0.8, 0.3, 1.0); - gl::Clear(gl::COLOR_BUFFER_BIT); - SwapBuffers(hdc); - } - _ => (), - } - - DefWindowProcA(hwnd, msg.message, msg.wParam, msg.lParam); + handle_message(Arc::clone(&win), msg.message, msg.wParam, msg.lParam); } } @@ -264,6 +254,13 @@ impl Window { } } + pub(crate) unsafe fn draw_frame(&self) { + // todo: pass callback rendering function instead + gl::ClearColor(0.3, 0.8, 0.3, 1.0); + gl::Clear(gl::COLOR_BUFFER_BIT); + SwapBuffers(self.hdc); + } + pub(crate) fn handle_mouse_motion(&self, x: i32, y: i32) { println!("{}, {}", x, y); }