From 92c18ba3b9d6252094cbe04ae750713327526ec3 Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Mon, 14 Oct 2024 03:17:46 -0400 Subject: Add nonsense to poll an async HTTP request in the game loop --- src/lib.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 453b0dc..f860dc2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,10 +18,10 @@ static mut CTX: Option<*const context::Context> = None; static mut ST: Option<*mut state::State> = None; static mut G: Option<*mut std::ffi::c_void> = None; -pub fn contextualize(f: F) +pub fn contextualize(mut f: F) where G: state::Game + 'static, - F: Fn(&context::Context, &mut state::State, &mut G) { + F: FnMut(&context::Context, &mut state::State, &mut G) { unsafe { match (CTX, ST, G) { (Some(c), Some(s), Some(g)) => f(&*c, &mut*s, &mut*(g as *mut G)), @@ -39,7 +39,7 @@ where console_log::init_with_level(log::Level::Debug).unwrap(); console_error_panic_hook::set_once(); tracing_wasm::set_as_global_default(); - log::info!("HELLO COMPUTER HELLO CLONKHEAD :)"); + log::info!("hello computer, starting up..."); let event_loop = winit::event_loop::EventLoop::new() .expect("failed to initialize event loop"); @@ -54,6 +54,9 @@ where ctx.maximize_canvas(); let game = Box::leak(Box::new(gnew(ctx).await)); let st = Box::leak(Box::new(state::State::new(&ctx))); + // request = Some(Box::new(async { + // "foo".to_owned() + // })); unsafe { CTX = Some(ctx as _); @@ -61,8 +64,6 @@ where G = Some(game as *mut G as *mut std::ffi::c_void); } - st.write_log("test"); - event_loop.set_control_flow(winit::event_loop::ControlFlow::Wait); event_loop.spawn(|event, elwt| { contextualize(|ctx, st, game: &mut G| { @@ -120,6 +121,15 @@ where ctx.maximize_canvas(); st.handle_resize(&ctx); } + if let Some(f) = &mut st.request { + match std::future::Future::poll(f.as_mut(), &mut st.waker_ctx) { + std::task::Poll::Pending => {}, + std::task::Poll::Ready(res) => { + st.request_returned(&ctx, game, res); + }, + } + // f.poll(); + } st.run_update(&ctx, game); st.run_render(&ctx, game); ctx.window.request_redraw(); -- cgit v1.2.3