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-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-09-26 16:07:30 +10:00
|
|
|
let surface = 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-02 12:31:18 +10: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)?;
|
|
|
|
let mut invaders = World::new();
|
2019-10-07 19:00:33 +11:00
|
|
|
let mut last = Instant::now();
|
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 {
|
|
|
|
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-05 15:48:29 +10:00
|
|
|
event::WindowEvent::RedrawRequested => fb.render(invaders.draw()),
|
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;
|
|
|
|
|
|
|
|
// TODO: Keyboard and controller input.
|
|
|
|
let controls = Controls {
|
|
|
|
direction: Direction::Still,
|
|
|
|
fire: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
invaders.update(dt, controls);
|
2019-10-05 15:48:29 +10:00
|
|
|
window.request_redraw();
|
|
|
|
}
|
2019-09-26 16:07:30 +10:00
|
|
|
_ => (),
|
|
|
|
});
|
|
|
|
}
|