Skip to content

Commit

Permalink
Added the state struct and implemented the new function
Browse files Browse the repository at this point in the history
run is now async so needs pollster
  • Loading branch information
ThomasJowett committed Feb 22, 2024
1 parent b17d04d commit cd006eb
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 17 deletions.
23 changes: 12 additions & 11 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"cSpell.words": [
"Clippy",
"Halvar",
"swapchain",
"vulkano",
"winit"
],
"rust-analyzer.linkedProjects": [
"Cargo.toml"
]
}
"cSpell.words": [
"Clippy",
"Halvar",
"srgb",
"swapchain",
"Vsync",
"vulkano",
"webgl",
"winit"
],
"rust-analyzer.linkedProjects": ["Cargo.toml"]
}
106 changes: 103 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,112 @@ pub mod halvar {
use winit::{
event::*,
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
window::{Window, WindowBuilder},
};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

struct State {
surface: wgpu::Surface,
device: wgpu::Device,
queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration,
size: winit::dpi::PhysicalSize<u32>,
window: Window,
}

impl State {
async fn new(window: Window) -> Self {
let size = window.inner_size();

let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::all(),
..Default::default()
});

let surface = unsafe { instance.create_surface(&window) }.unwrap();

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
.await
.unwrap();

let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
features: wgpu::Features::empty(),
limits: if cfg!(target_arch = "wasm32") {
wgpu::Limits::downlevel_webgl2_defaults()
} else {
wgpu::Limits::default()
},
label: None,
},
None, //trace_path
)
.await
.unwrap();

let surface_caps = surface.get_capabilities(&adapter);

let surface_format = surface_caps
.formats
.iter()
.copied()
.find(|f| f.is_srgb())
.unwrap_or(surface_caps.formats[0]);

let modes = &surface_caps.present_modes;

let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: surface_format,
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::AutoVsync,
alpha_mode: surface_caps.alpha_modes[0],
view_formats: vec![],
};
surface.configure(&device, &config);

Self {
surface,
device,
queue,
config,
size,
window,
}
}

pub fn window(&self) -> &Window {
&self.window
}

fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
todo!()
}

fn input(&mut self, event: &WindowEvent) -> bool {
todo!()
}

fn update(&mut self) {
todo!()
}

fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
todo!()
}
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn run() {
pub async fn run() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
Expand Down Expand Up @@ -42,11 +140,13 @@ pub mod halvar {
.expect("Couldn't append canvas to document body");
}

let mut state = State::new(window).await;

event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => match event {
} if window_id == state.window.id() => match event {
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
input:
Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

use halvar::halvar::run;

fn main() {
run();
}
pollster::block_on(run());
}

0 comments on commit cd006eb

Please sign in to comment.