Added State and App Structs

This commit is contained in:
2026-08-04 13:49:48 +01:00
parent 00e40b1b76
commit 09b5b329f4
+52 -2
View File
@@ -1,3 +1,53 @@
fn main() { use std::sync::Arc;
println!("Hello, world!");
use winit::{
application::ApplicationHandler, event::*, event_loop::{ActiveEventLoop, EventLoop}, keyboard::{KeyCode, PhysicalKey}, window::Window
};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::EventLoopExtWebSys;
// This will store the state of our game
pub struct State {
window: Arc<Window>,
}
impl State {
// We don't need this to be async right now,
// but we will in the next tutorial
pub async fn new(window: Arc<Window>) -> anyhow::Result<Self> {
Ok(Self {
window,
})
}
pub fn resize(&mut self, _width: u32, _height: u32) {
// We'll do stuff here in the next tutorial
}
pub fn render(&mut self) {
self.window.request_redraw();
// We'll do more stuff here in the next tutorial
}
}
pub struct App {
#[cfg(target_arch = "wasm32")]
proxy: Option<winit::event_loop::EventLoopProxy<State>>,
state: Option<State>,
}
impl App {
pub fn new(#[cfg(target_arch = "wasm32")] event_loop: &EventLoop<State>) -> Self {
#[cfg(target_arch = "wasm32")]
let proxy = Some(event_loop.create_proxy());
Self {
state: None,
#[cfg(target_arch = "wasm32")]
proxy,
}
}
} }