2019-10-02 12:31:18 +10:00
|
|
|
use pixels::{Error, Pixels, SurfaceTexture};
|
2019-10-05 15:48:29 +10:00
|
|
|
use simple_invaders::{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))
|
|
|
|
.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-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 => {
|
|
|
|
invaders.update();
|
|
|
|
window.request_redraw();
|
|
|
|
}
|
2019-09-26 16:07:30 +10:00
|
|
|
_ => (),
|
|
|
|
});
|
|
|
|
}
|