Skip to content

Commit

Permalink
Slower progress
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Jun 16, 2023
1 parent 00e7865 commit b7c73f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
22 changes: 4 additions & 18 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use winit::{
};

use crate::State;
use log::error;

pub fn handle_event_loop(event: &Event<()>, state: &mut State, control_flow: &mut ControlFlow) {
// use std::ops::Add;
Expand Down Expand Up @@ -35,7 +34,7 @@ pub fn handle_event_loop(event: &Event<()>, state: &mut State, control_flow: &mu
},
..
} => {
error!("Space pressed - warn");
log::error!("Space pressed - warn");
}
WindowEvent::Resized(physical_size) => {
log::info!("resize: {:?}", physical_size);
Expand All @@ -51,7 +50,7 @@ pub fn handle_event_loop(event: &Event<()>, state: &mut State, control_flow: &mu
state: winit::event::ElementState::Pressed,
..
} => {
error!("Mouse pressed: {:?}", state.cursor_position);
log::error!("Mouse pressed: {:?}", state.cursor_position);
}
_ => {}
}
Expand All @@ -61,32 +60,19 @@ pub fn handle_event_loop(event: &Event<()>, state: &mut State, control_flow: &mu
&Event::RedrawRequested(window_id) if window_id == state.window.id() => {
match state.render() {
Ok(_) => {}
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// All other errors (Outdated, Timeout) should be resolved by the next frame
Err(e) => eprintln!("{:?}", e),
Err(e) => log::error!("{:?}", e),
}
}
//Event::NewEvents(StartCause::Init) => {
// From the winit README:
// "A lot of functionality expects the application to be ready before you start doing anything;
// this includes creating windows, fetching monitors, drawing, and so on, see issues #2238, #2051
// and #2087.
// If you encounter problems, you should try doing your initialization inside
// Event::NewEvents(StartCause::Init)."
//state .window .set_fullscreen(Some(winit::window::Fullscreen::Borderless(None)));
//state.window.focus_window();
//}
Event::MainEventsCleared => {
state.window.request_redraw();
}
&Event::WindowEvent {
ref event,
window_id,
} => {
error!("INFO: {:?}, window = {:?}", event, window_id);
log::error!("INFO: {:?}, window = {:?}", event, window_id);
}
_ => {}
}
Expand Down
25 changes: 23 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod event_loop;

use std::time::Instant;

use rand::prelude::*;
use rand_chacha::ChaCha20Rng;

Expand Down Expand Up @@ -121,6 +123,8 @@ pub async fn run() -> ! {
}

pub struct State<'a> {
elapsed_time: f32,
last_time: Instant,
device: wgpu::Device,
queue: wgpu::Queue,
texture_view_descriptor: wgpu::TextureViewDescriptor<'a>,
Expand Down Expand Up @@ -676,7 +680,12 @@ impl<'a> State<'a> {
surface_format,
);

let last_time = Instant::now();
let elapsed_time = 0.;

Ok(Self {
last_time,
elapsed_time,
seed,
rule_idx,
rule_buffer,
Expand Down Expand Up @@ -766,7 +775,17 @@ impl<'a> State<'a> {
if self.window.inner_size().height < 10 {
return Ok(());
}
self.frame_count += 1;

// Let's clock the game at 1 simulation steps per second
const SIM_DT: f32 = 1.0 / 4.0;
self.elapsed_time += self.last_time.elapsed().as_secs_f32();
let advance_state = self.elapsed_time > SIM_DT;
self.last_time = Instant::now();

if advance_state {
self.elapsed_time = 0.;
self.frame_count += 1;
}
let is_even = self.frame_count % 2 == 0;

let mut encoder = self
Expand All @@ -775,7 +794,9 @@ impl<'a> State<'a> {

let output: wgpu::SurfaceTexture = self.surface.get_current_texture().unwrap();

self.computer.enqueue(is_even, &mut encoder);
if advance_state {
self.computer.enqueue(is_even, &mut encoder);
}
self.renderer.enqueue(
is_even,
&mut encoder,
Expand Down

0 comments on commit b7c73f6

Please sign in to comment.