Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 22, 2019 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/854135bfa2a127800002451e97a8985e to your computer and use it in GitHub Desktop.
Save rust-play/854135bfa2a127800002451e97a8985e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use core::{
mem::zeroed,
ptr::{null, null_mut},
};
use winapi::{
shared::windef::HWND,
um::{
libloaderapi::GetModuleHandleW,
winuser::{
CreateWindowExW, DefWindowProcW, DispatchMessageW, GetMessageW, RegisterClassW,
TranslateMessage, CW_USEDEFAULT, MSG, WNDCLASSW, WS_CAPTION, WS_MINIMIZEBOX,
WS_SYSMENU, WS_VISIBLE,
},
},
};
unsafe extern "system" fn the_proc(win: HWND, msg: u32, wparam: usize, lparam: isize) -> isize {
match msg {
//WM_PAINT => unimplemented!("make Vk draw here I guess?"),
_ => DefWindowProcW(win, msg, wparam, lparam),
}
}
fn wide_null(string: &str) -> Vec<u16> {
string.encode_utf16().chain(Some(0)).collect()
}
fn main() {
let mut class: WNDCLASSW = unsafe { zeroed() };
class.lpfnWndProc = Some(the_proc);
class.hInstance = unsafe { GetModuleHandleW(null()) };
assert!(!class.hInstance.is_null());
let class_name_wide: Vec<u16> = wide_null("ClassName");
class.lpszClassName = class_name_wide.as_ptr();
assert_ne!(unsafe { RegisterClassW(&class) }, 0);
let window_name_wide: Vec<u16> = wide_null("Demo Window");
let win = unsafe {
CreateWindowExW(
0,
class_name_wide.as_ptr(),
window_name_wide.as_ptr(),
WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
null_mut(),
null_mut(),
class.hInstance,
null_mut(),
)
};
assert!(!win.is_null());
// then in here we'd start Vk before hitting the main loop I guess?
loop {
let mut msg: MSG = unsafe { zeroed() };
if unsafe { GetMessageW(&mut msg, win, 0, 0) } > 0 {
unsafe {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
} else {
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment