minecrab/src/main.rs

82 lines
2.7 KiB
Rust
Raw Normal View History

2021-05-28 00:54:06 +00:00
mod camera;
2021-05-28 01:13:07 +00:00
mod cube;
mod state;
2021-05-28 00:54:06 +00:00
mod texture;
mod uniforms;
2021-05-28 01:09:00 +00:00
mod vertex;
2021-05-28 00:54:06 +00:00
2021-05-28 01:13:07 +00:00
use std::time::Instant;
2021-05-28 00:54:06 +00:00
use winit::{
2021-05-28 01:06:27 +00:00
event::{Event, WindowEvent},
2021-05-28 00:54:06 +00:00
event_loop::{ControlFlow, EventLoop},
2021-05-28 01:13:07 +00:00
window::WindowBuilder,
2021-05-28 00:54:06 +00:00
};
2021-05-28 01:13:07 +00:00
use crate::state::State;
2021-05-28 00:54:06 +00:00
fn main() {
env_logger::init();
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_title("\u{1f980}\u{1f980}\u{1f980}\u{1f980}\u{1f980}")
.build(&event_loop)
.unwrap();
use futures::executor::block_on;
// Since main can't be async, we're going to need to block
let mut state = block_on(State::new(&window));
let mut frames = 0;
let mut instant = Instant::now();
event_loop.run(move |event, _, control_flow| {
match event {
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => {
if !state.input(event) {
match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::Resized(physical_size) => {
state.resize(*physical_size);
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
// new_inner_size is &mut so w have to dereference it twice
state.resize(**new_inner_size);
}
_ => {}
}
}
}
Event::RedrawRequested(_) => {
frames += 1;
if frames % 1000 == 0 {
let frametime = instant.elapsed() / 1000;
let fps = 1_000_000 / frametime.as_micros();
println!("{:?} - {} fps", frametime, fps);
instant = Instant::now();
}
state.update();
match state.render() {
Ok(_) => {}
// Recreate the swap_chain if lost
Err(wgpu::SwapChainError::Lost) => state.resize(state.size),
// The system is out of memory, we should probably quit
Err(wgpu::SwapChainError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// All other errors (Outdated, Timeout) should be resolved by the next frame
Err(e) => eprintln!("{:?}", e),
}
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
window.request_redraw();
}
_ => {}
}
});
}