From bf32c78dd3f79683a0f4ebb6f47f5627ffbde736 Mon Sep 17 00:00:00 2001 From: TadaTeruki Date: Mon, 19 Aug 2024 19:06:13 +0900 Subject: [PATCH] wip: fetch --- graphics/Cargo.toml | 6 +++++- graphics/Makefile | 3 +++ graphics/src/fetch.rs | 35 +++++++++++++++++++++++++++++++++++ graphics/src/lib.rs | 5 ++++- graphics/src/state.rs | 5 ++--- view/public/test.txt | 1 + 6 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 graphics/src/fetch.rs create mode 100644 view/public/test.txt diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index 2cdd206..017957e 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -18,8 +18,12 @@ wgpu = "22.1.0" web-sys = { version = "0.3", features = [ "Document", "Window", - "HtmlCanvasElement", "Element", + "HtmlCanvasElement", + "Request", + "RequestInit", + "RequestMode", + "Response", "KeyboardEvent", ]} wasm-logger = "0.2.0" diff --git a/graphics/Makefile b/graphics/Makefile index be1c83f..4287df7 100644 --- a/graphics/Makefile +++ b/graphics/Makefile @@ -1,2 +1,5 @@ build: + mkdir -p ../view/public/resources + cp -r resources ../view/public + echo "*" > ../view/public/resources/.gitignore wasm-pack build --target web --out-dir ../view/pkg \ No newline at end of file diff --git a/graphics/src/fetch.rs b/graphics/src/fetch.rs new file mode 100644 index 0000000..2549269 --- /dev/null +++ b/graphics/src/fetch.rs @@ -0,0 +1,35 @@ +use std::any::Any; + +use log::info; +use wasm_bindgen::{convert::IntoWasmAbi, JsCast, JsValue}; +use wasm_bindgen_futures::JsFuture; +use web_sys::{Request, RequestInit, RequestMode, Response}; + +pub async fn fetch() -> Result<(), JsValue> { + + info!("fetching"); + + let opts = RequestInit::new(); + opts.set_method("GET"); + opts.set_mode(RequestMode::Cors); + + let url = "localhost:5173/test.txt"; + + let request = Request::new_with_str_and_init(&url, &opts)?; + + let window = web_sys::window().unwrap(); + let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?; + + + + // `resp_value` is a `Response` object. + assert!(resp_value.is_instance_of::()); + let resp: Response = resp_value.dyn_into().unwrap(); + info!("conv"); + let json = JsFuture::from(resp.array_buffer()?).await?.as_string().unwrap(); + + info!("{:?}", json); + info!("ok"); + + Ok(()) +} diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index a388b77..6a60b48 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -1,4 +1,5 @@ -use log::error; +use fetch::fetch; +use log::{error, info}; use state::State; use wasm_bindgen::prelude::*; @@ -8,6 +9,7 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; mod camera; mod key; mod model; +mod fetch; mod state; #[wasm_bindgen(start)] @@ -20,6 +22,7 @@ pub async fn create_state( canvas: web_sys::HtmlCanvasElement, use_gl_instead: bool, ) -> Option { + info!("{:?}", fetch().await); match State::new(canvas, use_gl_instead).await { Ok(state) => Some(state), Err(e) => { diff --git a/graphics/src/state.rs b/graphics/src/state.rs index 2058bfd..da3a78a 100644 --- a/graphics/src/state.rs +++ b/graphics/src/state.rs @@ -19,11 +19,8 @@ pub struct State { device: wgpu::Device, queue: wgpu::Queue, config: wgpu::SurfaceConfiguration, - key_states: KeyStateMap, - camera: Camera, - render_pipeline: wgpu::RenderPipeline, models: Vec, } @@ -129,6 +126,8 @@ impl State { ], }); + + let main_model = Model::create( &device, &queue, diff --git a/view/public/test.txt b/view/public/test.txt new file mode 100644 index 0000000..05a682b --- /dev/null +++ b/view/public/test.txt @@ -0,0 +1 @@ +Hello! \ No newline at end of file