minecrab/src/main.rs

115 lines
3.9 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 instance;
mod light;
2021-05-28 01:13:07 +00:00
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::{
dpi::{PhysicalSize, Size},
2021-05-28 21:21:20 +00:00
event::{ElementState, Event, KeyboardInput, MouseButton, VirtualKeyCode, 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(r#"minecrab"#)
.with_inner_size(Size::Physical(PhysicalSize {
width: 1280,
height: 720,
}))
2021-05-28 00:54:06 +00:00
.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();
2021-05-28 21:21:20 +00:00
let mut last_render_time = Instant::now();
2021-05-28 00:54:06 +00:00
event_loop.run(move |event, _, control_flow| {
match event {
2021-05-28 21:21:20 +00:00
Event::DeviceEvent { ref event, .. } => state.input(event),
2021-05-28 00:54:06 +00:00
Event::WindowEvent {
ref event,
window_id,
2021-05-28 21:21:20 +00:00
} if window_id == window.id() => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
..
} => {
let _ = window.set_cursor_grab(false);
window.set_cursor_visible(true);
state.mouse_grabbed = false;
}
WindowEvent::Resized(physical_size) => {
state.resize(*physical_size);
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
state.resize(**new_inner_size);
}
WindowEvent::MouseInput {
state: mouse_state,
button,
..
} => {
if *button == MouseButton::Left && *mouse_state == ElementState::Pressed {
let _ = window.set_cursor_grab(true);
window.set_cursor_visible(false);
state.mouse_grabbed = true;
2021-05-28 00:54:06 +00:00
}
}
2021-05-28 21:21:20 +00:00
_ => {}
},
2021-05-28 00:54:06 +00:00
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();
}
2021-05-28 21:21:20 +00:00
let now = Instant::now();
let dt = now - last_render_time;
last_render_time = now;
state.update(dt);
2021-05-28 00:54:06 +00:00
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();
}
_ => {}
}
});
}