forked from processing/libprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglfw.rs
More file actions
94 lines (81 loc) · 2.81 KB
/
glfw.rs
File metadata and controls
94 lines (81 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/// Minimal GLFW helper for Processing examples
use bevy::prelude::Entity;
use glfw::{Glfw, GlfwReceiver, PWindow, WindowEvent, WindowMode};
use processing_core::error::Result;
pub struct GlfwContext {
glfw: Glfw,
window: PWindow,
events: GlfwReceiver<(f64, WindowEvent)>,
}
impl GlfwContext {
pub fn new(width: u32, height: u32) -> Result<Self> {
let mut glfw = glfw::init(glfw::fail_on_errors).unwrap();
glfw.window_hint(glfw::WindowHint::ClientApi(glfw::ClientApiHint::NoApi));
glfw.window_hint(glfw::WindowHint::Visible(false));
let (mut window, events) = glfw
.create_window(width, height, "Processing", WindowMode::Windowed)
.unwrap();
window.set_all_polling(true);
window.show();
Ok(Self {
glfw,
window,
events,
})
}
#[cfg(target_os = "macos")]
pub fn create_surface(&self, width: u32, height: u32) -> Result<Entity> {
use processing_render::surface_create_macos;
let (scale_factor, _) = self.window.get_content_scale();
surface_create_macos(
self.window.get_cocoa_window() as u64,
width,
height,
scale_factor,
)
}
#[cfg(target_os = "windows")]
pub fn create_surface(&self, width: u32, height: u32) -> Result<Entity> {
use processing_render::surface_create_windows;
let (scale_factor, _) = self.window.get_content_scale();
surface_create_windows(
self.window.get_win32_window() as u64,
width,
height,
scale_factor,
)
}
#[cfg(all(target_os = "linux", feature = "wayland"))]
pub fn create_surface(&self, width: u32, height: u32) -> Result<Entity> {
use processing_render::surface_create_wayland;
let (scale_factor, _) = self.window.get_content_scale();
surface_create_wayland(
self.window.get_wayland_window() as u64,
self.glfw.get_wayland_display() as u64,
width,
height,
scale_factor,
)
}
#[cfg(all(target_os = "linux", feature = "x11"))]
pub fn create_surface(&self, width: u32, height: u32) -> Result<Entity> {
use processing_render::surface_create_x11;
let (scale_factor, _) = self.window.get_content_scale();
surface_create_x11(
self.window.get_x11_window() as u64,
self.glfw.get_x11_display() as u64,
width,
height,
scale_factor,
)
}
pub fn poll_events(&mut self) -> bool {
self.glfw.poll_events();
for (_, event) in glfw::flush_messages(&self.events) {
if event == WindowEvent::Close {
return false;
}
}
!self.window.should_close()
}
}