1
0
Fork 0

Successful execution and rendering on Windows host

This commit is contained in:
Mirko Covizzi 2020-07-08 23:03:03 +02:00
parent 46dacdfc37
commit df72b11c74
3 changed files with 35 additions and 18 deletions

View file

@ -11,6 +11,7 @@ edition = "2018"
[dependencies] [dependencies]
gl = "0.14.0" gl = "0.14.0"
log = "0.4.8"
[target.'cfg(target_os="linux")'.dependencies] [target.'cfg(target_os="linux")'.dependencies]
xcb = { version = "0.9", features = ["thread", "xlib_xcb", "dri2"] } xcb = { version = "0.9", features = ["thread", "xlib_xcb", "dri2"] }

View file

@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex};
use winapi::shared::minwindef::{LPARAM, LRESULT, UINT, WPARAM}; use winapi::shared::minwindef::{LPARAM, LRESULT, UINT, WPARAM};
use crate::Window; use crate::Window;
use winapi::um::winuser::{DefWindowProcA, WM_MOUSEMOVE, WM_TIMER}; use winapi::um::winuser::{DefWindowProcA, WM_MOUSEMOVE, WM_TIMER, WM_PAINT};
const WIN_FRAME_TIMER: usize = 4242; const WIN_FRAME_TIMER: usize = 4242;
@ -37,6 +37,10 @@ pub(crate) unsafe fn handle_message(
handle_timer(win, wparam); handle_timer(win, wparam);
0 0
} }
WM_PAINT => {
win.lock().unwrap().draw_frame();
0
}
_ => DefWindowProcA(hwnd, message, wparam, lparam), _ => DefWindowProcA(hwnd, message, wparam, lparam),
} }
} }

View file

@ -66,12 +66,17 @@ unsafe extern "system" fn wnd_proc(
} }
_ => { _ => {
if !win_ptr.is_null() { if !win_ptr.is_null() {
let win_ref: &Arc<Mutex<Window>> = std::mem::transmute(win_ptr); let win_ref: Arc<Mutex<Window>> = Arc::from_raw(win_ptr as *mut Mutex<Window>);
let win = Arc::clone(win_ref); let win = Arc::clone(&win_ref);
let ret = handle_message(win, msg, wparam, lparam); let ret = handle_message(win, msg, wparam, lparam);
// todo: need_reconfigure thing? // 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; return ret;
} }
@ -111,16 +116,22 @@ pub struct Window {
hdc: HDC, hdc: HDC,
gl_context: HGLRC, gl_context: HGLRC,
window_class: ATOM, window_class: ATOM,
r: f32,
g: f32,
b: f32
} }
impl Window { impl Window {
pub fn open(options: WindowOpenOptions) -> Arc<Mutex<Self>> { pub fn open(options: WindowOpenOptions) {
unsafe { unsafe {
let mut window = Window { let mut window = Window {
hwnd: null_mut(), hwnd: null_mut(),
hdc: null_mut(), hdc: null_mut(),
gl_context: null_mut(), gl_context: null_mut(),
window_class: 0, window_class: 0,
r: 0.3,
g: 0.8,
b: 0.3
}; };
let title = (options.title.to_owned() + "\0").as_ptr() as *const i8; let title = (options.title.to_owned() + "\0").as_ptr() as *const i8;
@ -171,8 +182,6 @@ impl Window {
window.hdc = GetDC(window.hwnd); window.hdc = GetDC(window.hwnd);
//init_gl_context(&window, title);
let mut pfd: PIXELFORMATDESCRIPTOR = std::mem::zeroed(); let mut pfd: PIXELFORMATDESCRIPTOR = std::mem::zeroed();
pfd.nSize = std::mem::size_of::<PIXELFORMATDESCRIPTOR>() as u16; pfd.nSize = std::mem::size_of::<PIXELFORMATDESCRIPTOR>() as u16;
pfd.nVersion = 1; pfd.nVersion = 1;
@ -188,26 +197,26 @@ impl Window {
if pf_id == 0 { if pf_id == 0 {
// todo: use a more useful return like an Option // todo: use a more useful return like an Option
// todo: also launch error message boxes // 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 { if SetPixelFormat(window.hdc, pf_id, &pfd) == 0 {
// todo: use a more useful return like an Option // todo: use a more useful return like an Option
// todo: also launch error message boxes // todo: also launch error message boxes
return Arc::new(Mutex::new(window)); //return Arc::new(Mutex::new(window));
} }
window.gl_context = wglCreateContext(window.hdc); window.gl_context = wglCreateContext(window.hdc);
if window.gl_context == 0 as HGLRC { if window.gl_context == 0 as HGLRC {
// todo: use a more useful return like an Option // todo: use a more useful return like an Option
// todo: also launch error message boxes // 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 { if wglMakeCurrent(window.hdc, window.gl_context) == 0 {
// todo: use a more useful return like an Option // todo: use a more useful return like an Option
// todo: also launch error message boxes // 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); let h = LoadLibraryA("opengl32.dll\0".as_ptr() as *const i8);
@ -221,8 +230,9 @@ impl Window {
let hdc = window.hdc; let hdc = window.hdc;
let win = Arc::new(Mutex::new(window)); 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); SetTimer(hwnd, 4242, 13, None);
@ -235,11 +245,9 @@ impl Window {
break; break;
} }
TranslateMessage(&mut msg); 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) { pub(crate) unsafe fn draw_frame(&mut self) {
// todo: pass callback rendering function instead // todo: pass callback rendering function instead?
gl::ClearColor(0.3, 0.8, 0.3, 1.0); gl::ClearColor(self.r, self.g, self.b, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT); gl::Clear(gl::COLOR_BUFFER_BIT);
SwapBuffers(self.hdc); 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); println!("{}, {}", x, y);
let r = (x as f32) / 1000.0;
let g = (y as f32) / 1000.0;
self.r = r;
self.g = g;
} }
} }