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); }