2019-10-14 13:48:20 +11:00
|
|
|
use std::env;
|
2019-10-07 19:00:33 +11:00
|
|
|
use std::time::Instant;
|
|
|
|
|
2019-10-02 12:31:18 +10:00
|
|
|
use pixels::{Error, Pixels, SurfaceTexture};
|
2019-10-07 19:00:33 +11:00
|
|
|
use simple_invaders::{Controls, Direction, World, SCREEN_HEIGHT, SCREEN_WIDTH};
|
2019-10-22 16:34:12 +11:00
|
|
|
use winit::event::{Event, VirtualKeyCode, WindowEvent};
|
2019-09-26 16:07:30 +10:00
|
|
|
use winit::event_loop::{ControlFlow, EventLoop};
|
2019-10-22 16:34:12 +11:00
|
|
|
use winit_input_helper::WinitInputHelper;
|
2019-09-26 16:07:30 +10:00
|
|
|
|
2019-09-26 16:47:04 +10:00
|
|
|
fn main() -> Result<(), Error> {
|
2019-09-26 16:07:30 +10:00
|
|
|
env_logger::init();
|
|
|
|
let event_loop = EventLoop::new();
|
2019-10-22 16:34:12 +11:00
|
|
|
let mut input = WinitInputHelper::new();
|
2019-09-26 16:07:30 +10:00
|
|
|
|
2019-10-14 13:48:20 +11:00
|
|
|
// Enable debug mode with `DEBUG=true` environment variable
|
|
|
|
let debug = env::var("DEBUG")
|
|
|
|
.unwrap_or_else(|_| "false".to_string())
|
|
|
|
.parse()
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
2019-10-15 19:09:33 +11:00
|
|
|
let (window, surface, width, height, mut hidpi_factor) = {
|
2019-10-05 15:48:29 +10:00
|
|
|
let scale = 3.0;
|
|
|
|
let width = SCREEN_WIDTH as f64 * scale;
|
|
|
|
let height = SCREEN_HEIGHT as f64 * scale;
|
|
|
|
|
|
|
|
let window = winit::window::WindowBuilder::new()
|
|
|
|
.with_inner_size(winit::dpi::LogicalSize::new(width, height))
|
2019-10-07 07:53:08 +11:00
|
|
|
.with_title("pixel invaders")
|
2019-10-05 15:48:29 +10:00
|
|
|
.build(&event_loop)
|
|
|
|
.unwrap();
|
2019-10-15 16:17:42 +11:00
|
|
|
let surface = pixels::wgpu::Surface::create(&window);
|
2019-10-15 19:09:33 +11:00
|
|
|
let hidpi_factor = window.hidpi_factor();
|
|
|
|
let size = window.inner_size().to_physical(hidpi_factor);
|
|
|
|
|
|
|
|
(
|
|
|
|
window,
|
|
|
|
surface,
|
|
|
|
size.width.round() as u32,
|
|
|
|
size.height.round() as u32,
|
|
|
|
hidpi_factor,
|
|
|
|
)
|
2019-09-26 16:07:30 +10:00
|
|
|
};
|
|
|
|
|
2019-10-15 16:17:42 +11:00
|
|
|
let surface_texture = SurfaceTexture::new(width, height, surface);
|
2019-10-22 16:34:12 +11:00
|
|
|
let mut pixels = Pixels::new(224, 256, surface_texture)?;
|
2019-10-14 13:48:20 +11:00
|
|
|
let mut invaders = World::new(debug);
|
2019-10-22 16:34:12 +11:00
|
|
|
let mut time = Instant::now();
|
2019-10-08 18:35:53 +11:00
|
|
|
let mut controls = Controls::default();
|
2019-10-03 13:22:20 +10:00
|
|
|
|
2019-10-22 16:34:12 +11:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
// The one and only event that winit_input_helper doesn't have for us...
|
|
|
|
match event {
|
|
|
|
Event::WindowEvent {
|
|
|
|
event: WindowEvent::RedrawRequested,
|
2019-09-26 16:07:30 +10:00
|
|
|
..
|
2019-10-22 16:34:12 +11:00
|
|
|
} => {
|
|
|
|
pixels.render(invaders.draw());
|
2019-09-26 16:07:30 +10:00
|
|
|
}
|
2019-10-22 16:34:12 +11:00
|
|
|
_ => (),
|
|
|
|
}
|
2019-10-08 18:35:53 +11:00
|
|
|
|
2019-10-22 16:34:12 +11:00
|
|
|
// For everything else, for let winit_input_helper collect events to build its state.
|
|
|
|
// It returns `true` when it is time to update our game state and request a redraw.
|
|
|
|
if input.update(event) {
|
|
|
|
// Close events
|
|
|
|
if input.key_pressed(VirtualKeyCode::Escape) || input.quit() {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
return;
|
|
|
|
}
|
2019-10-08 18:35:53 +11:00
|
|
|
|
2019-10-22 16:34:12 +11:00
|
|
|
// Keyboard controls
|
|
|
|
controls.direction = if input.key_held(VirtualKeyCode::Left) {
|
|
|
|
Direction::Left
|
|
|
|
} else if input.key_held(VirtualKeyCode::Right) {
|
|
|
|
Direction::Right
|
|
|
|
} else {
|
|
|
|
Direction::Still
|
|
|
|
};
|
|
|
|
controls.fire = input.key_pressed(VirtualKeyCode::Space);
|
2019-10-08 18:35:53 +11:00
|
|
|
|
2019-10-15 19:09:33 +11:00
|
|
|
// Adjust high DPI factor
|
2019-10-22 16:34:12 +11:00
|
|
|
if let Some(factor) = input.hidpi_changed() {
|
|
|
|
hidpi_factor = factor;
|
|
|
|
}
|
2019-10-15 19:09:33 +11:00
|
|
|
|
2019-10-15 16:17:42 +11:00
|
|
|
// Resize the window
|
2019-10-22 16:34:12 +11:00
|
|
|
if let Some(size) = input.window_resized() {
|
2019-10-15 19:09:33 +11:00
|
|
|
let size = size.to_physical(hidpi_factor);
|
|
|
|
let width = size.width.round() as u32;
|
|
|
|
let height = size.height.round() as u32;
|
2019-10-15 16:17:42 +11:00
|
|
|
|
2019-10-22 16:34:12 +11:00
|
|
|
pixels.resize(width, height);
|
2019-10-15 16:17:42 +11:00
|
|
|
}
|
|
|
|
|
2019-10-07 19:00:33 +11:00
|
|
|
// Get a new delta time.
|
|
|
|
let now = Instant::now();
|
2019-10-22 16:34:12 +11:00
|
|
|
let dt = now.duration_since(time);
|
|
|
|
time = now;
|
2019-10-14 13:48:20 +11:00
|
|
|
|
2019-10-08 18:35:53 +11:00
|
|
|
// Update the game logic and request redraw
|
2019-10-13 08:26:47 +11:00
|
|
|
invaders.update(&dt, &controls);
|
2019-10-05 15:48:29 +10:00
|
|
|
window.request_redraw();
|
|
|
|
}
|
2019-09-26 16:07:30 +10:00
|
|
|
});
|
|
|
|
}
|