Skip to content

Commit f46c5dd

Browse files
committed
Redesign globals, clean codebase from tl::with everywhere
1 parent 89aa66d commit f46c5dd

18 files changed

+968
-1261
lines changed

build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::env;
33
fn main() {
44
let target = env::var("TARGET").unwrap_or_else(|e| panic!("{}", e));
55

6-
if target.contains("darwin") || target.contains("ios"){
6+
if target.contains("darwin") || target.contains("ios") {
77
println!("cargo:rustc-link-lib=framework=MetalKit");
88
}
99
}

examples/instancing.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,31 @@ impl EventHandler for Stage {
167167
}
168168

169169
fn main() {
170-
let mut conf = conf::Conf::default();
170+
// std::thread::spawn(|| {
171+
// let mut conf = conf::Conf::default();
172+
// let metal = std::env::args().nth(1).as_deref() == Some("metal");
173+
// conf.platform.apple_gfx_api = if metal {
174+
// conf::AppleGfxApi::Metal
175+
// } else {
176+
// conf::AppleGfxApi::OpenGl
177+
// };
178+
179+
// miniquad::start(conf, move || Box::new(Stage::new()));
180+
// });
181+
182+
//let mut conf = conf::Conf::default();
183+
let mut conf = conf::Conf {
184+
window_title: "Miniquad".to_string(),
185+
window_width: 1024,
186+
window_height: 768,
187+
fullscreen: true,
188+
platform: conf::Platform {
189+
linux_backend: conf::LinuxBackend::WaylandOnly,
190+
..Default::default()
191+
},
192+
..Default::default()
193+
};
194+
171195
let metal = std::env::args().nth(1).as_deref() == Some("metal");
172196
conf.platform.apple_gfx_api = if metal {
173197
conf::AppleGfxApi::Metal

examples/window_conf.rs

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ fn main() {
1818
window_width: 1024,
1919
window_height: 768,
2020
fullscreen: true,
21+
platform: conf::Platform {
22+
linux_backend: conf::LinuxBackend::WaylandOnly,
23+
..Default::default()
24+
},
2125
..Default::default()
2226
},
2327
|| {

src/graphics/metal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ pub struct MetalContext {
269269
impl MetalContext {
270270
pub fn new() -> MetalContext {
271271
unsafe {
272-
let view = crate::window::apple_view().unwrap();
272+
let view = crate::window::apple_view();
273273
assert!(!view.is_null());
274274
let device: ObjcId = msg_send![view, device];
275275
assert!(!device.is_null());

src/lib.rs

+87-68
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![doc = include_str!("../README.md")]
2+
#![allow(warnings)]
23

34
pub mod conf;
45
mod event;
@@ -36,49 +37,65 @@ pub mod date {
3637
}
3738
}
3839

39-
use std::cell::RefCell;
40-
thread_local! {
41-
static NATIVE_DISPLAY: RefCell<Option<fn (&mut dyn FnMut(&mut dyn crate::native::NativeDisplay))>> = RefCell::new(None);
42-
}
43-
pub(crate) fn with_native_display(f: &mut dyn FnMut(&mut dyn crate::native::NativeDisplay)) {
44-
NATIVE_DISPLAY.with(|d| (d.borrow().as_ref().unwrap())(f))
45-
}
40+
use std::sync::{Mutex, OnceLock};
4641

47-
// I wish "with_native_display" could be generic over return value, but function
48-
// pointer to a generic function requires way too much unsafe :(
49-
macro_rules! with_native_display {
50-
($target:ident, $f:expr) => {{
51-
let mut res = Default::default();
52-
53-
with_native_display(&mut |$target| {
54-
res = $f;
55-
});
42+
static NATIVE_DISPLAY: OnceLock<Mutex<native::NativeDisplayData>> = OnceLock::new();
5643

57-
res
58-
}};
44+
fn set_display(display: native::NativeDisplayData) {
45+
NATIVE_DISPLAY.set(Mutex::new(display));
46+
}
47+
fn native_display() -> &'static Mutex<native::NativeDisplayData> {
48+
NATIVE_DISPLAY
49+
.get()
50+
.expect("Backend did not initialized NATIVE_DISPLAY yet.") //|| Mutex::new(Default::default()))
5951
}
6052

6153
/// Window and associated to window rendering context related functions.
6254
/// in macroquad <= 0.3, it was ctx.screen_size(). Now it is window::screen_size()
6355
pub mod window {
6456
use super::*;
6557

58+
/// The same as
59+
/// ```ignore
60+
/// if metal {
61+
/// Box::new(MetalContext::new())
62+
/// } else {
63+
/// Box::new(GlContext::new())
64+
/// };
65+
/// ```
66+
/// but under #[cfg] gate to avoid MetalContext on non-apple platforms
67+
pub fn new_rendering_backend() -> Box<dyn RenderingBackend> {
68+
#[cfg(target_vendor = "apple")]
69+
{
70+
if window::apple_gfx_api() == conf::AppleGfxApi::Metal {
71+
Box::new(MetalContext::new())
72+
} else {
73+
Box::new(GlContext::new())
74+
}
75+
}
76+
#[cfg(not(target_vendor = "apple"))]
77+
Box::new(GlContext::new())
78+
}
79+
6680
/// The current framebuffer size in pixels
6781
/// NOTE: [High DPI Rendering](../conf/index.html#high-dpi-rendering)
6882
pub fn screen_size() -> (f32, f32) {
69-
with_native_display!(d, d.screen_size())
83+
let d = native_display().lock().unwrap();
84+
(d.screen_width as f32, d.screen_height as f32)
7085
}
7186

7287
/// The dpi scaling factor (window pixels to framebuffer pixels)
7388
/// NOTE: [High DPI Rendering](../conf/index.html#high-dpi-rendering)
7489
pub fn dpi_scale() -> f32 {
75-
with_native_display!(d, d.dpi_scale())
90+
let d = native_display().lock().unwrap();
91+
d.dpi_scale
7692
}
7793

7894
/// True when high_dpi was requested and actually running in a high-dpi scenario
7995
/// NOTE: [High DPI Rendering](../conf/index.html#high-dpi-rendering)
8096
pub fn high_dpi() -> bool {
81-
with_native_display!(d, d.high_dpi())
97+
let d = native_display().lock().unwrap();
98+
d.high_dpi
8299
}
83100

84101
/// This function simply quits the application without
@@ -89,7 +106,13 @@ pub mod window {
89106
/// happen in the order_quit implmentation) and execution might continue for some time after
90107
/// But the window is going to be inevitably closed at some point.
91108
pub fn order_quit() {
92-
with_native_display!(d, d.order_quit())
109+
let mut d = native_display().lock().unwrap();
110+
d.quit_ordered = true;
111+
}
112+
113+
/// Shortcut for `order_quit`. Will add a legacy attribute at some point.
114+
pub fn quit() {
115+
order_quit()
93116
}
94117

95118
/// Calling request_quit() will trigger "quit_requested_event" event , giving
@@ -98,7 +121,8 @@ pub mod window {
98121
/// If the event handler callback does nothing, the application will be quit as usual.
99122
/// To prevent this, call the function "cancel_quit()"" from inside the event handler.
100123
pub fn request_quit() {
101-
with_native_display!(d, d.request_quit())
124+
let mut d = native_display().lock().unwrap();
125+
d.quit_requested = true;
102126
}
103127

104128
/// Cancels a pending quit request, either initiated
@@ -107,98 +131,93 @@ pub mod window {
107131
/// function makes sense is from inside the event handler callback when
108132
/// the "quit_requested_event" event has been received
109133
pub fn cancel_quit() {
110-
with_native_display!(d, d.cancel_quit())
134+
let mut d = native_display().lock().unwrap();
135+
d.quit_requested = false;
111136
}
112-
113137
/// Capture mouse cursor to the current window
114138
/// On WASM this will automatically hide cursor
115139
/// On desktop this will bound cursor to windows border
116140
/// NOTICE: on desktop cursor will not be automatically released after window lost focus
117141
/// so set_cursor_grab(false) on window's focus lost is recommended.
118142
/// TODO: implement window focus events
119143
pub fn set_cursor_grab(grab: bool) {
120-
with_native_display!(d, d.set_cursor_grab(grab))
144+
let mut d = native_display().lock().unwrap();
145+
d.native_requests.send(native::Request::SetCursorGrab(grab));
121146
}
122147

123148
/// Show or hide the mouse cursor
124149
pub fn show_mouse(shown: bool) {
125-
with_native_display!(d, d.show_mouse(shown))
150+
let mut d = native_display().lock().unwrap();
151+
d.native_requests.send(native::Request::ShowMouse(shown));
126152
}
127153

128154
/// Set the mouse cursor icon.
129155
pub fn set_mouse_cursor(cursor_icon: CursorIcon) {
130-
with_native_display!(d, d.set_mouse_cursor(cursor_icon))
156+
let mut d = native_display().lock().unwrap();
157+
d.native_requests
158+
.send(native::Request::SetMouseCursor(cursor_icon));
131159
}
132160

133161
/// Set the application's window size.
134162
pub fn set_window_size(new_width: u32, new_height: u32) {
135-
with_native_display!(d, d.set_window_size(new_width, new_height))
163+
let mut d = native_display().lock().unwrap();
164+
d.native_requests.send(native::Request::SetWindowSize {
165+
new_width,
166+
new_height,
167+
});
136168
}
137169

138170
pub fn set_fullscreen(fullscreen: bool) {
139-
with_native_display!(d, d.set_fullscreen(fullscreen))
171+
let mut d = native_display().lock().unwrap();
172+
d.native_requests
173+
.send(native::Request::SetFullscreen(fullscreen));
140174
}
141175

142176
/// Get current OS clipboard value
143177
pub fn clipboard_get() -> Option<String> {
144-
with_native_display!(d, d.clipboard_get())
178+
let mut d = native_display().lock().unwrap();
179+
d.clipboard.get()
145180
}
146181

147182
/// Save value to OS clipboard
148183
pub fn clipboard_set(data: &str) {
149-
with_native_display!(d, d.clipboard_set(data))
184+
let mut d = native_display().lock().unwrap();
185+
d.clipboard.set(data)
150186
}
151187
pub fn dropped_file_count() -> usize {
152-
with_native_display!(d, d.dropped_file_count())
188+
let d = native_display().lock().unwrap();
189+
d.dropped_files.bytes.len()
153190
}
154191
pub fn dropped_file_bytes(index: usize) -> Option<Vec<u8>> {
155-
with_native_display!(d, d.dropped_file_bytes(index))
192+
let d = native_display().lock().unwrap();
193+
d.dropped_files.bytes.get(index).cloned()
156194
}
157195
pub fn dropped_file_path(index: usize) -> Option<std::path::PathBuf> {
158-
with_native_display!(d, d.dropped_file_path(index))
159-
}
160-
161-
/// Shortcut for `order_quit`. Will add a legacy attribute at some point.
162-
pub fn quit() {
163-
with_native_display!(d, d.order_quit())
196+
let d = native_display().lock().unwrap();
197+
d.dropped_files.paths.get(index).cloned()
164198
}
165199

166200
/// Show/hide onscreen keyboard.
167201
/// Only works on Android right now.
168202
pub fn show_keyboard(show: bool) {
169-
with_native_display!(d, d.show_keyboard(show))
203+
let mut d = native_display().lock().unwrap();
204+
d.native_requests.send(native::Request::ShowKeyboard(show));
170205
}
171206

172-
/// The same as
173-
/// ```ignore
174-
/// if metal {
175-
/// Box::new(MetalContext::new())
176-
/// } else {
177-
/// Box::new(GlContext::new())
178-
/// };
179-
/// ```
180-
/// but under #[cfg] gate to avoid MetalContext on non-apple platforms
181-
pub fn new_rendering_backend() -> Box<dyn RenderingBackend> {
182-
#[cfg(target_vendor = "apple")]
183-
{
184-
if with_native_display!(d, d.apple_gfx_api() == conf::AppleGfxApi::Metal) {
185-
Box::new(MetalContext::new())
186-
} else {
187-
Box::new(GlContext::new())
188-
}
189-
}
190-
#[cfg(not(target_vendor = "apple"))]
191-
Box::new(GlContext::new())
207+
#[cfg(target_vendor = "apple")]
208+
pub fn apple_gfx_api() -> crate::conf::AppleGfxApi {
209+
let d = native_display().lock().unwrap();
210+
d.gfx_api
192211
}
193-
194212
#[cfg(target_vendor = "apple")]
195-
pub unsafe fn apple_view() -> Option<crate::native::apple::frameworks::ObjcId> {
196-
with_native_display!(d, d.apple_view())
213+
pub fn apple_view() -> crate::native::apple::frameworks::ObjcId {
214+
let d = native_display().lock().unwrap();
215+
d.view
197216
}
198-
199-
#[cfg(target_os = "ios")]
200-
pub unsafe fn apple_view_ctrl() -> Option<crate::native::apple::frameworks::ObjcId> {
201-
with_native_display!(d, d.apple_view_ctrl())
217+
#[cfg(target_ios = "ios")]
218+
pub fn apple_view_ctrl() -> crate::native::apple::frameworks::ObjcId {
219+
let d = native_display().lock().unwrap();
220+
d.view_ctrl
202221
}
203222
}
204223

0 commit comments

Comments
 (0)