summaryrefslogtreecommitdiff
path: root/src/state.rs
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2024-10-14 03:17:46 -0400
committerLLLL Colonq <llll@colonq>2024-10-14 03:17:46 -0400
commit92c18ba3b9d6252094cbe04ae750713327526ec3 (patch)
treed4fb5fca7def1ff66f6ec6a5bb0e962ba9093411 /src/state.rs
parent3efd3026d22c71e95a853985f3f50f52147d287e (diff)
Add nonsense to poll an async HTTP request in the game loop
Diffstat (limited to 'src/state.rs')
-rw-r--r--src/state.rs57
1 files changed, 51 insertions, 6 deletions
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;