diff --git a/Cargo.toml b/Cargo.toml index 76925cc..b4a1298 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ edition = "2018" [dependencies] gl = "0.14.0" +log = "0.4.8" [target.'cfg(target_os="linux")'.dependencies] xcb = { version = "0.9", features = ["thread", "xlib_xcb", "dri2"] } diff --git a/src/win/event.rs b/src/win/event.rs index 768844a..15fc44a 100644 --- a/src/win/event.rs +++ b/src/win/event.rs @@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex}; use winapi::shared::minwindef::{LPARAM, LRESULT, UINT, WPARAM}; use crate::Window; -use winapi::um::winuser::{DefWindowProcA, WM_MOUSEMOVE, WM_TIMER}; +use winapi::um::winuser::{DefWindowProcA, WM_MOUSEMOVE, WM_PAINT, WM_TIMER}; const WIN_FRAME_TIMER: usize = 4242; @@ -37,6 +37,10 @@ pub(crate) unsafe fn handle_message( handle_timer(win, wparam); 0 } + WM_PAINT => { + win.lock().unwrap().draw_frame(); + 0 + } _ => DefWindowProcA(hwnd, message, wparam, lparam), } } diff --git a/src/win/window.rs b/src/win/window.rs index 4686b73..def2d70 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -66,12 +66,17 @@ unsafe extern "system" fn wnd_proc( } _ => { if !win_ptr.is_null() { - let win_ref: &Arc> = std::mem::transmute(win_ptr); - let win = Arc::clone(win_ref); + let win_ref: Arc> = Arc::from_raw(win_ptr as *mut Mutex); + let win = Arc::clone(&win_ref); let ret = handle_message(win, msg, wparam, lparam); // todo: need_reconfigure thing? + // Needed otherwise it crashes because it drops the userdata + // We basically need to keep the GWLP_USERDATA fresh between calls of the proc + // DO NOT REMOVE + SetWindowLongPtrA(hwnd, GWLP_USERDATA, Arc::into_raw(win_ref) as *const _ as _); + return ret; } @@ -111,16 +116,22 @@ pub struct Window { hdc: HDC, gl_context: HGLRC, window_class: ATOM, + r: f32, + g: f32, + b: f32, } impl Window { - pub fn open(options: WindowOpenOptions) -> Arc> { + pub fn open(options: WindowOpenOptions) { unsafe { let mut window = Window { hwnd: null_mut(), hdc: null_mut(), gl_context: null_mut(), window_class: 0, + r: 0.3, + g: 0.8, + b: 0.3, }; let title = (options.title.to_owned() + "\0").as_ptr() as *const i8; @@ -171,8 +182,6 @@ impl Window { window.hdc = GetDC(window.hwnd); - //init_gl_context(&window, title); - let mut pfd: PIXELFORMATDESCRIPTOR = std::mem::zeroed(); pfd.nSize = std::mem::size_of::() as u16; pfd.nVersion = 1; @@ -188,26 +197,26 @@ impl Window { if pf_id == 0 { // todo: use a more useful return like an Option // todo: also launch error message boxes - return Arc::new(Mutex::new(window)); + //return Arc::new(Mutex::new(window)); } if SetPixelFormat(window.hdc, pf_id, &pfd) == 0 { // todo: use a more useful return like an Option // todo: also launch error message boxes - return Arc::new(Mutex::new(window)); + //return Arc::new(Mutex::new(window)); } window.gl_context = wglCreateContext(window.hdc); if window.gl_context == 0 as HGLRC { // todo: use a more useful return like an Option // todo: also launch error message boxes - return Arc::new(Mutex::new(window)); + //return Arc::new(Mutex::new(window)); } if wglMakeCurrent(window.hdc, window.gl_context) == 0 { // todo: use a more useful return like an Option // todo: also launch error message boxes - return Arc::new(Mutex::new(window)); + //return Arc::new(Mutex::new(window)); } let h = LoadLibraryA("opengl32.dll\0".as_ptr() as *const i8); @@ -221,8 +230,9 @@ impl Window { let hdc = window.hdc; let win = Arc::new(Mutex::new(window)); + let win_p = Arc::clone(&win); - SetWindowLongPtrA(hwnd, GWLP_USERDATA, &Arc::clone(&win) as *const _ as _); + SetWindowLongPtrA(hwnd, GWLP_USERDATA, Arc::into_raw(win) as *const _ as _); SetTimer(hwnd, 4242, 13, None); @@ -235,11 +245,9 @@ impl Window { break; } TranslateMessage(&mut msg); - handle_message(Arc::clone(&win), msg.message, msg.wParam, msg.lParam); + handle_message(Arc::clone(&win_p), msg.message, msg.wParam, msg.lParam); } } - - win } } @@ -254,14 +262,18 @@ 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); + pub(crate) unsafe fn draw_frame(&mut self) { + // todo: pass callback rendering function instead? + gl::ClearColor(self.r, self.g, self.b, 1.0); gl::Clear(gl::COLOR_BUFFER_BIT); SwapBuffers(self.hdc); } - pub(crate) fn handle_mouse_motion(&self, x: i32, y: i32) { + pub(crate) fn handle_mouse_motion(&mut self, x: i32, y: i32) { println!("{}, {}", x, y); + let r = (x as f32) / 1000.0; + let g = (y as f32) / 1000.0; + self.r = r; + self.g = g; } } diff --git a/src/x11/window.rs b/src/x11/window.rs index 0dd697d..76c00c7 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -346,4 +346,4 @@ impl Window { // TODO: choose between `xres` and `yres`? (probably both are the same?) Some(yres) } -} \ No newline at end of file +}