2019-11-07 16:16:25 +11:00
|
|
|
#![deny(clippy::all)]
|
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
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-28 19:03:03 +11:00
|
|
|
use gilrs::{Button, Gilrs};
|
|
|
|
use log::debug;
|
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-28 10:35:22 +11:00
|
|
|
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
|
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-10-28 19:03:03 +11:00
|
|
|
let mut gilrs = Gilrs::new().unwrap();
|
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-28 10:35:22 +11:00
|
|
|
let (window, surface, width, height, mut hidpi_factor) =
|
|
|
|
create_window("pixel invaders", &event_loop);
|
2019-10-15 16:17:42 +11:00
|
|
|
let surface_texture = SurfaceTexture::new(width, height, surface);
|
2019-10-22 17:17:58 +11:00
|
|
|
let mut pixels = Pixels::new(SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32, surface_texture)?;
|
2019-11-03 09:29:09 +11:00
|
|
|
let mut invaders = World::new(generate_seed(), debug);
|
2019-10-22 16:34:12 +11:00
|
|
|
let mut time = Instant::now();
|
2019-10-28 19:03:03 +11:00
|
|
|
let mut gamepad = None;
|
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...
|
2019-10-24 13:31:42 +11:00
|
|
|
if let Event::WindowEvent {
|
|
|
|
event: WindowEvent::RedrawRequested,
|
|
|
|
..
|
|
|
|
} = event
|
|
|
|
{
|
|
|
|
invaders.draw(pixels.get_frame());
|
|
|
|
pixels.render();
|
2019-10-22 16:34:12 +11:00
|
|
|
}
|
2019-10-08 18:35:53 +11:00
|
|
|
|
2019-10-28 19:03:03 +11:00
|
|
|
// Pump the gilrs event loop and find an active gamepad
|
|
|
|
while let Some(gilrs::Event { id, event, .. }) = gilrs.next_event() {
|
|
|
|
let pad = gilrs.gamepad(id);
|
|
|
|
if gamepad.is_none() {
|
|
|
|
debug!("Gamepad with id {} is connected: {}", id, pad.name());
|
|
|
|
gamepad = Some(id);
|
|
|
|
} else if event == gilrs::ev::EventType::Disconnected {
|
|
|
|
debug!("Gamepad with id {} is disconnected: {}", id, pad.name());
|
|
|
|
gamepad = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-28 19:03:03 +11:00
|
|
|
let controls = {
|
|
|
|
// Keyboard controls
|
|
|
|
let mut left = input.key_held(VirtualKeyCode::Left);
|
|
|
|
let mut right = input.key_held(VirtualKeyCode::Right);
|
|
|
|
let mut fire = input.key_pressed(VirtualKeyCode::Space);
|
|
|
|
|
|
|
|
// Gamepad controls
|
|
|
|
if let Some(id) = gamepad {
|
|
|
|
let gamepad = gilrs.gamepad(id);
|
|
|
|
|
|
|
|
left = left || gamepad.is_pressed(Button::DPadLeft);
|
|
|
|
right = right || gamepad.is_pressed(Button::DPadRight);
|
|
|
|
fire = fire
|
|
|
|
|| gamepad.button_data(Button::South).map_or(false, |button| {
|
|
|
|
button.is_pressed() && button.counter() == gilrs.counter()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let direction = if left {
|
2019-10-27 16:03:12 +11:00
|
|
|
Direction::Left
|
2019-10-28 19:03:03 +11:00
|
|
|
} else if right {
|
2019-10-27 16:03:12 +11:00
|
|
|
Direction::Right
|
|
|
|
} else {
|
|
|
|
Direction::Still
|
2019-10-28 19:03:03 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
Controls { direction, fire }
|
2019-10-22 16:34:12 +11:00
|
|
|
};
|
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
|
|
|
});
|
|
|
|
}
|
2019-10-28 10:35:22 +11:00
|
|
|
|
|
|
|
/// Create a window for the game.
|
|
|
|
///
|
|
|
|
/// Automatically scales the window to cover about 2/3 of the monitor height.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// Tuple of `(window, surface, width, height, hidpi_factor)`
|
2019-11-07 18:15:28 +11:00
|
|
|
/// `width` and `height` are in `PhysicalSize` units.
|
2019-10-28 10:35:22 +11:00
|
|
|
fn create_window(
|
|
|
|
title: &str,
|
|
|
|
event_loop: &EventLoop<()>,
|
2019-10-28 14:27:42 +11:00
|
|
|
) -> (winit::window::Window, pixels::wgpu::Surface, u32, u32, f64) {
|
2019-10-28 10:35:22 +11:00
|
|
|
// Create a hidden window so we can estimate a good default window size
|
|
|
|
let window = winit::window::WindowBuilder::new()
|
|
|
|
.with_visible(false)
|
|
|
|
.with_title(title)
|
|
|
|
.build(&event_loop)
|
|
|
|
.unwrap();
|
|
|
|
let hidpi_factor = window.hidpi_factor();
|
|
|
|
|
|
|
|
// Get dimensions
|
|
|
|
let width = SCREEN_WIDTH as f64;
|
|
|
|
let height = SCREEN_HEIGHT as f64;
|
|
|
|
let (monitor_width, monitor_height) = {
|
|
|
|
let size = window.current_monitor().size();
|
|
|
|
(size.width / hidpi_factor, size.height / hidpi_factor)
|
|
|
|
};
|
|
|
|
let scale = (monitor_height / height * 2.0 / 3.0).round();
|
|
|
|
|
|
|
|
// Resize, center, and display the window
|
|
|
|
let min_size = PhysicalSize::new(width, height).to_logical(hidpi_factor);
|
|
|
|
let default_size = LogicalSize::new(width * scale, height * scale);
|
|
|
|
let center = LogicalPosition::new(
|
|
|
|
(monitor_width - width * scale) / 2.0,
|
|
|
|
(monitor_height - height * scale) / 2.0,
|
|
|
|
);
|
|
|
|
window.set_inner_size(default_size);
|
|
|
|
window.set_min_inner_size(Some(min_size));
|
|
|
|
window.set_outer_position(center);
|
|
|
|
window.set_visible(true);
|
|
|
|
|
|
|
|
let surface = pixels::wgpu::Surface::create(&window);
|
|
|
|
let size = default_size.to_physical(hidpi_factor);
|
|
|
|
|
|
|
|
(
|
|
|
|
window,
|
|
|
|
surface,
|
|
|
|
size.width.round() as u32,
|
|
|
|
size.height.round() as u32,
|
|
|
|
hidpi_factor,
|
|
|
|
)
|
|
|
|
}
|
2019-11-03 09:29:09 +11:00
|
|
|
|
|
|
|
/// Generate a pseudorandom seed for the game's PRNG.
|
|
|
|
fn generate_seed() -> (u64, u64) {
|
|
|
|
use byteorder::{ByteOrder, NativeEndian};
|
|
|
|
use getrandom::getrandom;
|
|
|
|
|
|
|
|
let mut seed = [0_u8; 16];
|
|
|
|
|
|
|
|
getrandom(&mut seed).expect("failed to getrandom");
|
|
|
|
|
|
|
|
(
|
|
|
|
NativeEndian::read_u64(&seed[0..8]),
|
|
|
|
NativeEndian::read_u64(&seed[8..16]),
|
|
|
|
)
|
|
|
|
}
|