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-09-26 16:07:30 +10:00
|
|
|
use winit::event;
|
|
|
|
use winit::event_loop::{ControlFlow, EventLoop};
|
|
|
|
|
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-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-02 12:31:18 +10:00
|
|
|
let (window, surface, width, height) = {
|
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-02 12:31:18 +10:00
|
|
|
let size = window.inner_size().to_physical(window.hidpi_factor());
|
2019-09-26 16:11:36 +10:00
|
|
|
|
2019-10-02 12:31:18 +10:00
|
|
|
(window, surface, size.width as u32, size.height as u32)
|
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-05 15:48:29 +10:00
|
|
|
let mut fb = Pixels::new(224, 256, surface_texture)?;
|
2019-10-14 13:48:20 +11:00
|
|
|
let mut invaders = World::new(debug);
|
2019-10-07 19:00:33 +11:00
|
|
|
let mut last = Instant::now();
|
2019-10-14 13:48:20 +11:00
|
|
|
|
2019-10-08 18:35:53 +11:00
|
|
|
let mut controls = Controls::default();
|
2019-10-14 13:48:20 +11:00
|
|
|
let mut last_state = false;
|
|
|
|
let mut button_state = false;
|
|
|
|
let mut rising_edge = false;
|
2019-10-03 13:22:20 +10:00
|
|
|
|
2019-09-26 16:07:30 +10:00
|
|
|
event_loop.run(move |event, _, control_flow| match event {
|
|
|
|
event::Event::WindowEvent { event, .. } => match event {
|
2019-10-08 18:35:53 +11:00
|
|
|
// Close events
|
2019-09-26 16:07:30 +10:00
|
|
|
event::WindowEvent::KeyboardInput {
|
|
|
|
input:
|
|
|
|
event::KeyboardInput {
|
|
|
|
virtual_keycode: Some(event::VirtualKeyCode::Escape),
|
|
|
|
state: event::ElementState::Pressed,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
}
|
2019-09-30 11:42:31 +10:00
|
|
|
| event::WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
2019-10-08 18:35:53 +11:00
|
|
|
|
|
|
|
// Keyboard controls
|
|
|
|
event::WindowEvent::KeyboardInput {
|
|
|
|
input:
|
|
|
|
event::KeyboardInput {
|
|
|
|
virtual_keycode: Some(virtual_code),
|
|
|
|
state: event::ElementState::Pressed,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
} => match virtual_code {
|
|
|
|
event::VirtualKeyCode::Left => controls.direction = Direction::Left,
|
|
|
|
event::VirtualKeyCode::Right => controls.direction = Direction::Right,
|
2019-10-14 13:48:20 +11:00
|
|
|
event::VirtualKeyCode::Space => button_state = true,
|
2019-10-08 18:35:53 +11:00
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
|
|
|
|
event::WindowEvent::KeyboardInput {
|
|
|
|
input:
|
|
|
|
event::KeyboardInput {
|
|
|
|
virtual_keycode: Some(virtual_code),
|
|
|
|
state: event::ElementState::Released,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
} => match virtual_code {
|
|
|
|
event::VirtualKeyCode::Left => controls.direction = Direction::Still,
|
|
|
|
event::VirtualKeyCode::Right => controls.direction = Direction::Still,
|
2019-10-14 13:48:20 +11:00
|
|
|
event::VirtualKeyCode::Space => button_state = false,
|
2019-10-08 18:35:53 +11:00
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
|
2019-10-15 16:17:42 +11:00
|
|
|
// Resize the window
|
|
|
|
event::WindowEvent::Resized(size) => {
|
|
|
|
let size = size.to_physical(window.hidpi_factor());
|
|
|
|
let width = size.width as u32;
|
|
|
|
let height = size.height as u32;
|
|
|
|
|
|
|
|
fb.resize(width, height);
|
|
|
|
}
|
|
|
|
|
2019-10-08 18:35:53 +11:00
|
|
|
// Redraw the screen
|
2019-10-05 15:48:29 +10:00
|
|
|
event::WindowEvent::RedrawRequested => fb.render(invaders.draw()),
|
2019-10-08 18:35:53 +11:00
|
|
|
|
2019-09-30 11:42:31 +10:00
|
|
|
_ => (),
|
2019-09-26 16:07:30 +10:00
|
|
|
},
|
2019-10-05 15:48:29 +10:00
|
|
|
event::Event::EventsCleared => {
|
2019-10-07 19:00:33 +11:00
|
|
|
// Get a new delta time.
|
|
|
|
let now = Instant::now();
|
|
|
|
let dt = now.duration_since(last);
|
|
|
|
last = now;
|
|
|
|
|
2019-10-14 13:48:20 +11:00
|
|
|
// Compute rising edge based on current and last button states
|
|
|
|
rising_edge = button_state && !last_state;
|
|
|
|
last_state = button_state;
|
|
|
|
|
|
|
|
// Fire button only uses rising edge
|
|
|
|
controls.fire = rising_edge;
|
|
|
|
|
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
|
|
|
_ => (),
|
|
|
|
});
|
|
|
|
}
|