|
| 1 | +/// Minimal GLFW helper for Processing examples |
| 2 | +use glfw::{Glfw, GlfwReceiver, PWindow, WindowEvent, WindowMode}; |
| 3 | +use processing::prelude::error::Result; |
| 4 | + |
| 5 | +pub struct GlfwContext { |
| 6 | + glfw: Glfw, |
| 7 | + window: PWindow, |
| 8 | + events: GlfwReceiver<(f64, WindowEvent)>, |
| 9 | +} |
| 10 | + |
| 11 | +impl GlfwContext { |
| 12 | + pub fn new(width: u32, height: u32) -> Result<Self> { |
| 13 | + let mut glfw = glfw::init(glfw::fail_on_errors).unwrap(); |
| 14 | + |
| 15 | + glfw.window_hint(glfw::WindowHint::ClientApi(glfw::ClientApiHint::NoApi)); |
| 16 | + glfw.window_hint(glfw::WindowHint::Visible(false)); |
| 17 | + |
| 18 | + let (mut window, events) = glfw |
| 19 | + .create_window(width, height, "Processing", WindowMode::Windowed) |
| 20 | + .unwrap(); |
| 21 | + |
| 22 | + window.set_all_polling(true); |
| 23 | + window.show(); |
| 24 | + |
| 25 | + Ok(Self { |
| 26 | + glfw, |
| 27 | + window, |
| 28 | + events, |
| 29 | + }) |
| 30 | + } |
| 31 | + |
| 32 | + #[cfg(target_os = "macos")] |
| 33 | + pub fn get_window(&self) -> u64 { |
| 34 | + self.window.get_cocoa_window() as u64 |
| 35 | + } |
| 36 | + |
| 37 | + #[cfg(target_os = "windows")] |
| 38 | + pub fn get_window(&self) -> u64 { |
| 39 | + self.window.get_win32_window() as u64 |
| 40 | + } |
| 41 | + |
| 42 | + #[cfg(target_os = "linux")] |
| 43 | + pub fn get_window(&self) -> u64 { |
| 44 | + self.window.get_wayland_window() as u64 |
| 45 | + } |
| 46 | + |
| 47 | + #[cfg(not(target_os = "linux"))] |
| 48 | + pub fn get_display(&self) -> u64 { |
| 49 | + 0 |
| 50 | + } |
| 51 | + |
| 52 | + #[cfg(target_os = "linux")] |
| 53 | + pub fn get_display(&self) -> u64 { |
| 54 | + self.glfw.get_wayland_display() as u64 |
| 55 | + } |
| 56 | + |
| 57 | + pub fn poll_events(&mut self) -> bool { |
| 58 | + self.glfw.poll_events(); |
| 59 | + |
| 60 | + for (_, event) in glfw::flush_messages(&self.events) { |
| 61 | + match event { |
| 62 | + WindowEvent::Close => return false, |
| 63 | + _ => {} |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + !self.window.should_close() |
| 68 | + } |
| 69 | +} |
0 commit comments