Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Jun 15, 2023
1 parent 21bbcbb commit f34bca4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 58 deletions.
20 changes: 16 additions & 4 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::{Duration, Instant};

use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::ControlFlow,
Expand All @@ -7,6 +9,7 @@ use crate::State;
use log::error;

pub fn handle_event_loop(event: &Event<()>, state: &mut State, control_flow: &mut ControlFlow) {
// use std::ops::Add;
// *control_flow = ControlFlow::WaitUntil(Instant::now().add(Duration::from_millis(1000)));
match event {
&Event::WindowEvent {
Expand All @@ -28,15 +31,13 @@ pub fn handle_event_loop(event: &Event<()>, state: &mut State, control_flow: &mu
WindowEvent::KeyboardInput {
event:
KeyEvent {
state: ElementState::Pressed,
physical_key: winit::keyboard::KeyCode::Space,
state: ElementState::Pressed,
..
},
..
} => {
error!("Space pressed - warn");
println!("Space press");
state.window.set_title("SPACE");
}
WindowEvent::Resized(physical_size) => {
log::info!("resize: {:?}", physical_size);
Expand All @@ -45,13 +46,24 @@ pub fn handle_event_loop(event: &Event<()>, state: &mut State, control_flow: &mu
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
state.resize(**new_inner_size);
}
WindowEvent::CursorMoved {
device_id: _,
position,
} => {
state.cursor_position = *position;
}
WindowEvent::MouseInput {
state: winit::event::ElementState::Pressed,
..
} => {
error!("Mouse pressed");
}
_ => {}
}
}
}

&Event::RedrawRequested(window_id) if window_id == state.window.id() => {
state.update();
match state.render() {
Ok(_) => {}
// Reconfigure the surface if lost
Expand Down
92 changes: 38 additions & 54 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rand::prelude::*;
use wasm_bindgen::prelude::*;
use wgpu::util::DeviceExt;
use winit::{
dpi::PhysicalPosition,
event::{ElementState, WindowEvent},
event_loop::EventLoop,
window::{Window, WindowBuilder},
Expand Down Expand Up @@ -83,6 +84,7 @@ pub struct State<'a> {
renderer: Renderer,
size_buffer: wgpu::Buffer,
cells_width: usize,
cursor_position: PhysicalPosition<f64>,
}

pub struct RendererFactory<'a> {
Expand Down Expand Up @@ -163,14 +165,40 @@ impl<'a> RendererFactory<'a> {
cells_width: usize,
texture_format: wgpu::TextureFormat,
) -> Renderer {
let render_pipeline = render_pipeline_from_shader(
device,
&self.pipeline_layout,
&self.shader,
texture_format,
self.cells_stride.clone(),
self.square_stride.clone(),
);
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("render_pipeline"),
layout: Some(&self.pipeline_layout),
vertex: wgpu::VertexState {
buffers: &[self.cells_stride.clone(), self.square_stride.clone()],
entry_point: "vertex_main",
module: &self.shader,
},
fragment: Some(wgpu::FragmentState {
module: &self.shader,
entry_point: "fragment_main",
targets: &[Some(wgpu::ColorTargetState {
blend: Some(wgpu::BlendState::REPLACE),
format: texture_format,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
primitive: wgpu::PrimitiveState {
conservative: false,
cull_mode: Some(wgpu::Face::Back),
front_face: wgpu::FrontFace::Cw,
polygon_mode: wgpu::PolygonMode::Fill,
strip_index_format: None,
topology: wgpu::PrimitiveTopology::TriangleStrip,
unclipped_depth: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
alpha_to_coverage_enabled: false,
count: 1,
mask: !0,
},
multiview: None,
});

let size_bind_group = device.create_bind_group({
{
Expand Down Expand Up @@ -503,8 +531,7 @@ impl<'a> State<'a> {
width: size.width,
height: size.height,
// "present_mode uses wgpu::PresentMode enum which determines how to sync the surface with the display"
// Probably want PresentMode::Fifo (VSync)?
present_mode: wgpu::PresentMode::Fifo, // surface_caps.present_modes[0],
present_mode: wgpu::PresentMode::Fifo,
// "alpha_mode is honestly not something I'm familiar with. I believe it has something to do with
// transparent windows, but feel free to open a pull request"
alpha_mode: surface_caps.alpha_modes[0],
Expand Down Expand Up @@ -556,6 +583,7 @@ impl<'a> State<'a> {
computer_factory,
computer,
cells_width,
cursor_position: PhysicalPosition { x: 0., y: 0. },
}
}

Expand Down Expand Up @@ -662,47 +690,3 @@ impl<'a> State<'a> {
}
}
}

fn render_pipeline_from_shader(
device: &wgpu::Device,
render_pipeline_layout: &wgpu::PipelineLayout,
shader: &wgpu::ShaderModule,
format: wgpu::TextureFormat,
cells_stride: wgpu::VertexBufferLayout,
square_stride: wgpu::VertexBufferLayout,
) -> wgpu::RenderPipeline {
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("render_pipeline"),
layout: Some(render_pipeline_layout),
vertex: wgpu::VertexState {
buffers: &[cells_stride, square_stride],
entry_point: "vertex_main",
module: shader,
},
fragment: Some(wgpu::FragmentState {
module: shader,
entry_point: "fragment_main",
targets: &[Some(wgpu::ColorTargetState {
blend: Some(wgpu::BlendState::REPLACE),
format,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
primitive: wgpu::PrimitiveState {
conservative: false,
cull_mode: Some(wgpu::Face::Back),
front_face: wgpu::FrontFace::Cw,
polygon_mode: wgpu::PolygonMode::Fill,
strip_index_format: None,
topology: wgpu::PrimitiveTopology::TriangleStrip,
unclipped_depth: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
alpha_to_coverage_enabled: false,
count: 1,
mask: !0,
},
multiview: None,
})
}

0 comments on commit f34bca4

Please sign in to comment.