Skip to content

Commit 8a33647

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

1 file changed

Lines changed: 56 additions & 9 deletions

File tree

src/lib.rs

Lines changed: 56 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

@@ -411,6 +413,7 @@ impl Context {
411413
self.quit_requested = false;
412414

413415
self.textures.garbage_collect(get_quad_context());
416+
self.atlas.delete_old_textures();
414417

415418
// remove all touches that were Ended or Cancelled
416419
self.touches.retain(|_, touch| {
@@ -554,7 +557,7 @@ impl EventHandler for Stage {
554557
context.mouse_position = Vec2::new(x, y);
555558
}
556559

557-
if miniquad::window::blocking_event_loop() {
560+
if context.update_on.mouse_down {
558561
miniquad::window::schedule_update();
559562
}
560563
}
@@ -573,7 +576,7 @@ impl EventHandler for Stage {
573576
if !context.cursor_grabbed {
574577
context.mouse_position = Vec2::new(x, y);
575578
}
576-
if miniquad::window::blocking_event_loop() {
579+
if context.update_on.mouse_up {
577580
miniquad::window::schedule_update();
578581
}
579582
}
@@ -639,7 +642,12 @@ impl EventHandler for Stage {
639642
repeat,
640643
})
641644
});
642-
if miniquad::window::blocking_event_loop() {
645+
if context
646+
.update_on
647+
.specific_key
648+
.as_ref()
649+
.map_or(context.update_on.key_down, |keys| keys.contains(&keycode))
650+
{
643651
miniquad::window::schedule_update();
644652
}
645653
}
@@ -655,7 +663,7 @@ impl EventHandler for Stage {
655663
.for_each(|arr| arr.push(MiniquadInputEvent::KeyUp { keycode, modifiers }));
656664

657665
if miniquad::window::blocking_event_loop() {
658-
miniquad::window::schedule_update();
666+
//miniquad::window::schedule_update();
659667
}
660668
}
661669

@@ -755,6 +763,36 @@ impl EventHandler for Stage {
755763
}
756764
}
757765

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

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

782829
Box::new(Stage {})
783830
});

0 commit comments

Comments
 (0)