diff options
| author | LLLL Colonq <llll@colonq> | 2024-10-14 03:17:46 -0400 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2024-10-14 03:17:46 -0400 |
| commit | 92c18ba3b9d6252094cbe04ae750713327526ec3 (patch) | |
| tree | d4fb5fca7def1ff66f6ec6a5bb0e962ba9093411 /src | |
| parent | 3efd3026d22c71e95a853985f3f50f52147d287e (diff) | |
Add nonsense to poll an async HTTP request in the game loop
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 20 | ||||
| -rw-r--r-- | src/mesh.rs | 2 | ||||
| -rw-r--r-- | src/state.rs | 57 |
3 files changed, 66 insertions, 13 deletions
@@ -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, G>(f: F) +pub fn contextualize<F, G>(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(); diff --git a/src/mesh.rs b/src/mesh.rs index aba9957..cfbbf91 100644 --- a/src/mesh.rs +++ b/src/mesh.rs @@ -1,5 +1,3 @@ -use std::io::BufRead; - use glow::HasContext; use crate::context; diff --git a/src/state.rs b/src/state.rs index de22d13..89d1a13 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,3 +1,4 @@ +#![allow(dead_code, unused_variables)] use std::collections::HashMap; use bimap::BiHashMap; use enum_map::{enum_map, Enum, EnumMap}; @@ -6,14 +7,27 @@ use crate::{context, framebuffer, shader, audio}; const DELTA_TIME: f64 = 1.0 / 60.0; +pub struct WinitWaker {} + +impl WinitWaker { + fn new() -> Self { Self {} } +} +impl std::task::Wake for WinitWaker { + fn wake(self: std::sync::Arc<Self>) {} +} + pub trait Game { fn initialize_audio(&self, ctx: &context::Context, st: &State, actx: &audio::Context) -> - HashMap<String, audio::Audio>; - fn finish_title(&mut self, st: &mut State); - fn mouse_move(&mut self, ctx: &context::Context, st: &mut State, x: i32, y: i32); - fn mouse_press(&mut self, ctx: &context::Context, st: &mut State); - fn update(&mut self, ctx: &context::Context, st: &mut State) -> Option<()>; - fn render(&mut self, ctx: &context::Context, st: &mut State) -> Option<()>; + HashMap<String, audio::Audio> + { + HashMap::new() + } + fn finish_title(&mut self, st: &mut State) {} + fn mouse_move(&mut self, ctx: &context::Context, st: &mut State, x: i32, y: i32) {} + fn mouse_press(&mut self, ctx: &context::Context, st: &mut State) {} + fn request_return(&mut self, ctx: &context::Context, st: &mut State, res: reqwest::Response) {} + fn update(&mut self, ctx: &context::Context, st: &mut State) -> Option<()> { Some(()) } + fn render(&mut self, ctx: &context::Context, st: &mut State) -> Option<()> { Some(()) } } #[derive(Debug, Enum, Clone, Copy, PartialEq, Eq, Hash)] @@ -93,6 +107,10 @@ pub struct State { pub lighting: (glam::Vec3, glam::Vec3, glam::Vec3), pub point_lights: Vec<PointLight>, + pub waker_ctx: std::task::Context<'static>, + pub http_client: reqwest::Client, + pub request: Option<std::pin::Pin<Box<dyn std::future::Future<Output = reqwest::Response>>>>, + pub log: Vec<(u64, String)>, } @@ -129,6 +147,10 @@ impl State { include_str!("assets/shaders/scale/frag.glsl"), ); + let waker = std::sync::Arc::new(WinitWaker::new()); + let cwaker = Box::leak(Box::new(waker.into())); + let waker_ctx = std::task::Context::from_waker(cwaker); + Self { acc: 0.0, last: now(ctx), @@ -160,6 +182,10 @@ impl State { ), point_lights: Vec::new(), + waker_ctx, + http_client: reqwest::Client::new(), + request: None, + log: Vec::new(), } } @@ -362,6 +388,25 @@ impl State { self.rebinding = Some(*k); } + pub fn request<F>(&mut self, ctx: &context::Context, f: F) + where F: Fn(&reqwest::Client) -> reqwest::RequestBuilder + { + let builder = f(&self.http_client); + let fut = async { + builder.send().await.expect("failed to send HTTP request") + }; + self.request = Some(Box::pin(fut)); + } + + pub fn requesting(&self) -> bool { self.request.is_some() } + + pub fn request_returned<G>(&mut self, ctx: &context::Context, game: &mut G, res: reqwest::Response) + where G: Game + { + game.request_return(ctx, self, res); + self.request = None; + } + pub fn run_update<G>(&mut self, ctx: &context::Context, game: &mut G) where G: Game { let now = now(ctx); let diff = now - self.last; |
