Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. use std::convert::TryInto;
  2. use std::ffi::{c_void, OsStr};
  3.  
  4. use std::iter::once;
  5. use std::os::windows::ffi::OsStrExt;
  6.  
  7. use winapi::shared::{ntdef::VOID, windef::HDC};
  8.  
  9. use winapi::um::wingdi::{
  10. StretchDIBits, BITMAPINFO, BITMAPINFOHEADER, BI_RGB, DIB_RGB_COLORS, RGBQUAD, SRCCOPY,
  11. };
  12.  
  13. use winapi::um::winnt::{MEM_COMMIT, MEM_RELEASE, PAGE_READWRITE};
  14.  
  15. use kernel32::{GetModuleHandleW, VirtualAlloc, VirtualFree};
  16. use std::mem::{size_of, zeroed};
  17. use std::ptr::null_mut;
  18. use winapi::shared::minwindef::{HINSTANCE, LPARAM, LRESULT, UINT, WPARAM};
  19. use winapi::shared::windef::{HWND, RECT};
  20.  
  21. use winapi::um::winuser::{
  22. BeginPaint, CreateWindowExW, DefWindowProcW, DispatchMessageW, EndPaint, GetClientRect,
  23. GetMessageW, RegisterClassW, TranslateMessage, CS_HREDRAW, CS_OWNDC, CS_VREDRAW, CW_USEDEFAULT,
  24. MSG, PAINTSTRUCT, WNDCLASSW, WS_OVERLAPPEDWINDOW, WS_VISIBLE,
  25. };
  26.  
  27. // TODO: Figure out why removing this line makes the window invisible
  28. use winapi::um::winuser::*;
  29.  
  30. static mut RUNNING: bool = true;
  31.  
  32. static mut BITMAPMEMORY: *mut c_void = 0 as *mut c_void;
  33. static mut bitmap_width: i32 = 0;
  34. static mut bitmap_height: i32 = 0;
  35.  
  36. static mut bitmapinfo: BITMAPINFO = BITMAPINFO {
  37. bmiHeader: BITMAPINFOHEADER {
  38. biSize: 0,
  39. biWidth: 0,
  40. biHeight: 0,
  41. biPlanes: 0,
  42. biBitCount: 0,
  43. biCompression: 0,
  44. biSizeImage: 0,
  45. biXPelsPerMeter: 0,
  46. biYPelsPerMeter: 0,
  47. biClrUsed: 0,
  48. biClrImportant: 0,
  49. },
  50. bmiColors: [RGBQUAD {
  51. rgbBlue: 0,
  52. rgbGreen: 0,
  53. rgbRed: 0,
  54. rgbReserved: 0,
  55. }],
  56. };
  57.  
  58. /* #[cfg(windows)]
  59. fn print_message(msg: &str) -> Result<i32, Error> {
  60. let wide: Vec<u16> = OsStr::new(msg).encode_wide().chain(once(0)).collect();
  61. let ret = unsafe { MessageBoxW(null_mut(), wide.as_ptr(), wide.as_ptr(), MB_OK) };
  62.  
  63. if ret == 0 {
  64. Err(Error::last_os_error())
  65. } else {
  66. Ok(ret)
  67. }
  68. }
  69. #[cfg(not(windows))]
  70. fn print_message(msg: &str) -> Result<(), Error> {
  71. println!("{}", msg);
  72. Ok(())
  73. }
  74. */
  75. fn win32_resize_dibsection(width: i32, height: i32) {
  76. unsafe {
  77. if BITMAPMEMORY != zeroed() {
  78. VirtualFree(BITMAPMEMORY, 0, MEM_RELEASE);
  79. }
  80. bitmap_width = width;
  81. bitmap_height = height;
  82. bitmapinfo.bmiHeader.biSize = size_of::<BITMAPINFOHEADER>() as u32;
  83. bitmapinfo.bmiHeader.biWidth = bitmap_width;
  84. bitmapinfo.bmiHeader.biHeight = -bitmap_height;
  85. bitmapinfo.bmiHeader.biPlanes = 1;
  86. bitmapinfo.bmiHeader.biBitCount = 3;
  87. bitmapinfo.bmiHeader.biCompression = BI_RGB;
  88. }
  89. let bytes_per_pixel = 4;
  90. unsafe {
  91. let bitmapmemorysize = (bitmap_width * bitmap_height) * bytes_per_pixel;
  92. BITMAPMEMORY = VirtualAlloc(
  93. 0 as *mut c_void,
  94. bitmapmemorysize as u64,
  95. MEM_COMMIT,
  96. PAGE_READWRITE,
  97. ) as *mut std::ffi::c_void;
  98.  
  99. let mut row = BITMAPMEMORY as *mut u8;
  100. let pitch = width * bytes_per_pixel;
  101. for _y in 0..bitmap_height {
  102. let mut pixel = row as *mut u8;
  103. for _x in 0..bitmap_width {
  104. *pixel = 255;
  105. pixel = pixel.offset(1);
  106.  
  107. *pixel = 0;
  108. pixel = pixel.offset(1);
  109. //println!("running {:?}", *pixel);
  110. *pixel = 0;
  111. pixel = pixel.offset(1);
  112.  
  113. *pixel = 1;
  114. pixel = pixel.offset(1);
  115. }
  116. row = row.offset(pitch.try_into().unwrap());
  117. }
  118. }
  119. }
  120.  
  121. fn win32_update_window(
  122. device_context: HDC,
  123. window_rect: &RECT,
  124. _x: i32,
  125. _y: i32,
  126. _width: i32,
  127. _height: i32,
  128. ) {
  129. unsafe {
  130. let window_width = window_rect.right - window_rect.left;
  131. let window_height = window_rect.bottom - window_rect.top;
  132. StretchDIBits(
  133. device_context,
  134. /* x,
  135. y,
  136. width,
  137. height,
  138. x,
  139. y,
  140. width,
  141. height, */
  142. 0,
  143. 0,
  144. bitmap_width,
  145. bitmap_height,
  146. 0,
  147. 0,
  148. window_width,
  149. window_height,
  150. 0 as *const VOID,
  151. &bitmapinfo,
  152. DIB_RGB_COLORS,
  153. SRCCOPY,
  154. );
  155. }
  156. }
  157.  
  158. #[no_mangle]
  159. unsafe extern "system" fn wnd_proc(
  160. window: HWND,
  161. message: UINT,
  162. wparam: WPARAM,
  163. lparam: LPARAM,
  164. ) -> LRESULT {
  165. match message {
  166. WM_SIZE => {
  167. let mut client_rect = zeroed::<RECT>();
  168. GetClientRect(window, &mut client_rect);
  169. let width = client_rect.right - client_rect.left;
  170. let height = client_rect.bottom - client_rect.top;
  171. win32_resize_dibsection(width, height);
  172. 0
  173. }
  174. WM_CLOSE => {
  175. RUNNING = false;
  176. 0
  177. }
  178. WM_ACTIVATEAPP => 0,
  179. WM_DESTROY => {
  180. RUNNING = false;
  181. 0
  182. }
  183. WM_PAINT => {
  184. /* let dim = wind32_get_window_dimension(window);
  185. let mut paint = zeroed::<winapi::PAINTSTRUCT>();
  186. let device_context = BeginPaint(window, &mut paint);
  187. win32_update_window(device_context, dim.width, dim.height, &mut global_buffer);
  188. EndPaint(window, &mut paint); */
  189. let mut paint: PAINTSTRUCT = zeroed::<PAINTSTRUCT>();
  190. let device_context = BeginPaint(window, &mut paint);
  191. let x = paint.rcPaint.left;
  192. let y = paint.rcPaint.top;
  193. let width = paint.rcPaint.right - paint.rcPaint.left;
  194. let height = paint.rcPaint.bottom - paint.rcPaint.top;
  195.  
  196. let mut client_rect = zeroed::<RECT>();
  197. GetClientRect(window, &mut client_rect);
  198.  
  199. win32_update_window(device_context, &client_rect, x, y, width, height);
  200.  
  201. EndPaint(window, &mut paint);
  202. 0
  203. }
  204. _ => DefWindowProcW(window, message, wparam, lparam),
  205. }
  206. }
  207.  
  208. fn win32_string(value: &str) -> Vec<u16> {
  209. //use this when passing strings to windows
  210. OsStr::new(value).encode_wide().chain(once(0)).collect()
  211. }
  212.  
  213. fn create_window() {
  214. let name = win32_string("HandmadeheroWindowClass");
  215. let title = win32_string("HandmadeHero");
  216.  
  217. let instance = unsafe { GetModuleHandleW(name.as_ptr() as *const u16) as HINSTANCE };
  218.  
  219. let wnd_class = WNDCLASSW {
  220. style: CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
  221. lpfnWndProc: Some(wnd_proc),
  222. hInstance: instance,
  223. lpszClassName: name.as_ptr(),
  224. cbClsExtra: 0,
  225. cbWndExtra: 0,
  226. hIcon: null_mut(),
  227. hCursor: null_mut(),
  228. hbrBackground: null_mut(),
  229. lpszMenuName: null_mut(),
  230. };
  231.  
  232. unsafe {
  233. RegisterClassW(&wnd_class);
  234. }
  235.  
  236. let _handle = unsafe {
  237. CreateWindowExW(
  238. 0,
  239. name.as_ptr(),
  240. title.as_ptr(),
  241. WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  242. CW_USEDEFAULT,
  243. CW_USEDEFAULT,
  244. CW_USEDEFAULT,
  245. CW_USEDEFAULT,
  246. null_mut(),
  247. null_mut(),
  248. instance,
  249. null_mut(),
  250. )
  251. };
  252.  
  253. unsafe {
  254. let mut message = zeroed::<MSG>();
  255. while RUNNING {
  256. let message_result = GetMessageW(&mut message, 0 as HWND, 0 as u32, 0 as u32);
  257. if message_result > 0 {
  258. TranslateMessage(&message);
  259. DispatchMessageW(&message);
  260. } else {
  261. break;
  262. }
  263. }
  264. }
  265. }
  266.  
  267. fn main() {
  268. //print_message("Hello, world!").unwrap();
  269.  
  270. unsafe {
  271. let mut s = [1, 2, 3];
  272. let ptr: *mut u32 = s.as_mut_ptr();
  273.  
  274. unsafe {
  275. println!("{}", *ptr.offset(1));
  276. println!("{}", *ptr.offset(2));
  277. }
  278. }
  279.  
  280. create_window();
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement