Skip to content

Commit c051e06

Browse files
committed
Update strategies for blocking event loop
1 parent 8c710ee commit c051e06

File tree

1 file changed

+55
-9
lines changed

1 file changed

+55
-9
lines changed

src/lib.rs

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ struct Context {
224224
quad_context: Box<dyn miniquad::RenderingBackend>,
225225

226226
textures: crate::texture::TexturesContext,
227+
update_on: conf::UpdateTrigger,
227228
}
228229

229230
#[derive(Clone)]
@@ -350,6 +351,7 @@ impl Context {
350351

351352
quad_context: ctx,
352353
textures: crate::texture::TexturesContext::new(),
354+
update_on: Default::default(),
353355
}
354356
}
355357

@@ -554,7 +556,7 @@ impl EventHandler for Stage {
554556
context.mouse_position = Vec2::new(x, y);
555557
}
556558

557-
if miniquad::window::blocking_event_loop() {
559+
if context.update_on.mouse_down {
558560
miniquad::window::schedule_update();
559561
}
560562
}
@@ -573,7 +575,7 @@ impl EventHandler for Stage {
573575
if !context.cursor_grabbed {
574576
context.mouse_position = Vec2::new(x, y);
575577
}
576-
if miniquad::window::blocking_event_loop() {
578+
if context.update_on.mouse_up {
577579
miniquad::window::schedule_update();
578580
}
579581
}
@@ -639,7 +641,12 @@ impl EventHandler for Stage {
639641
repeat,
640642
})
641643
});
642-
if miniquad::window::blocking_event_loop() {
644+
if context
645+
.update_on
646+
.specific_key
647+
.as_ref()
648+
.map_or(context.update_on.key_down, |keys| keys.contains(&keycode))
649+
{
643650
miniquad::window::schedule_update();
644651
}
645652
}
@@ -655,7 +662,7 @@ impl EventHandler for Stage {
655662
.for_each(|arr| arr.push(MiniquadInputEvent::KeyUp { keycode, modifiers }));
656663

657664
if miniquad::window::blocking_event_loop() {
658-
miniquad::window::schedule_update();
665+
//miniquad::window::schedule_update();
659666
}
660667
}
661668

@@ -755,6 +762,36 @@ impl EventHandler for Stage {
755762
}
756763
}
757764

765+
pub mod conf {
766+
#[derive(Default, Debug)]
767+
pub struct UpdateTrigger {
768+
pub resize: bool,
769+
pub key_down: bool,
770+
pub mouse_down: bool,
771+
pub mouse_up: bool,
772+
pub specific_key: Option<Vec<crate::KeyCode>>,
773+
}
774+
775+
#[derive(Default, Debug)]
776+
pub struct Conf {
777+
pub miniquad_conf: miniquad::conf::Conf,
778+
/// With miniquad_conf.platform.blocking_event_loop = true,
779+
/// next_frame().await will never finish and will wait forever with
780+
/// zero CPU usage.
781+
/// update_on will tell macroquad when to proceed with the event loop.
782+
pub update_on: Option<UpdateTrigger>,
783+
}
784+
}
785+
786+
impl From<miniquad::conf::Conf> for conf::Conf {
787+
fn from(conf: miniquad::conf::Conf) -> conf::Conf {
788+
conf::Conf {
789+
miniquad_conf: conf,
790+
update_on: None,
791+
}
792+
}
793+
}
794+
758795
/// Not meant to be used directly, only from the macro.
759796
#[doc(hidden)]
760797
pub struct Window {}
@@ -763,21 +800,30 @@ impl Window {
763800
pub fn new(label: &str, future: impl Future<Output = ()> + 'static) {
764801
Window::from_config(
765802
conf::Conf {
766-
window_title: label.to_string(),
767-
//high_dpi: true,
803+
miniquad_conf: miniquad::conf::Conf {
804+
window_title: label.to_string(),
805+
//high_dpi: true,
806+
..Default::default()
807+
},
768808
..Default::default()
769809
},
770810
future,
771811
);
772812
}
773813

774-
pub fn from_config(config: conf::Conf, future: impl Future<Output = ()> + 'static) {
775-
miniquad::start(config, move || {
814+
pub fn from_config(config: impl Into<conf::Conf>, future: impl Future<Output = ()> + 'static) {
815+
let conf::Conf {
816+
miniquad_conf,
817+
update_on,
818+
} = config.into();
819+
miniquad::start(miniquad_conf, move || {
776820
thread_assert::set_thread_id();
777821
unsafe {
778822
MAIN_FUTURE = Some(Box::pin(future));
779823
}
780-
unsafe { CONTEXT = Some(Context::new()) };
824+
let mut context = Context::new();
825+
context.update_on = update_on.unwrap_or_default();
826+
unsafe { CONTEXT = Some(context) };
781827

782828
Box::new(Stage {})
783829
});

0 commit comments

Comments
 (0)