Added window resize handling

This commit is contained in:
Joshua Barretto 2019-01-23 22:39:31 +00:00
parent 8a37662cf0
commit a1617e7b5c
5 changed files with 41 additions and 3 deletions

View File

@ -94,6 +94,16 @@ impl Renderer {
})
}
/// Get references to the internal render target views that get displayed directly by the window.
pub fn target_views(&self) -> (&TgtColorView, &TgtDepthView) {
(&self.tgt_color_view, &self.tgt_depth_view)
}
/// Get mutable references to the internal render target views that get displayed directly by the window.
pub fn target_views_mut(&mut self) -> (&mut TgtColorView, &mut TgtDepthView) {
(&mut self.tgt_color_view, &mut self.tgt_depth_view)
}
/// Queue the clearing of the color and depth targets ready for a new frame to be rendered.
/// TODO: Make a version of this that doesn't clear the colour target for speed
pub fn clear(&mut self, col: Rgba<f32>) {

View File

@ -21,7 +21,7 @@ impl Camera {
Self {
focus: Vec3::unit_z() * 10.0,
ori: Vec3::zero(),
dist: 100.0,
dist: 150.0,
fov: 1.3,
aspect: 1.618,
}
@ -64,4 +64,9 @@ impl Camera {
pub fn get_focus_pos(&self) -> Vec3<f32> { self.focus }
/// Set the focus position of the camera.
pub fn set_focus_pos(&mut self, focus: Vec3<f32>) { self.focus = focus; }
/// Get the aspect ratio of the camera.
pub fn get_aspect_ratio(&self) -> f32 { self.aspect }
/// Set the aspect ratio of the camera.
pub fn set_aspect_ratio(&mut self, aspect: f32) { self.aspect = aspect; }
}

View File

@ -105,6 +105,12 @@ impl Scene {
}
}
/// Get a reference to the scene's camera.
pub fn camera(&self) -> &Camera { &self.camera }
/// Get a mutable reference to the scene's camera.
pub fn camera_mut(&mut self) -> &mut Camera { &mut self.camera }
/// Handle an incoming user input event (i.e: cursor moved, key pressed, window closed, etc.).
pub fn handle_input_event(&mut self, event: Event) -> bool {
match event {

View File

@ -81,8 +81,8 @@ impl PlayState for SessionState {
let mut clock = Clock::new();
// Load a few chunks TODO: Remove this
for x in -4..5 {
for y in -4..5 {
for x in -6..7 {
for y in -6..7 {
for z in -1..2 {
self.client.load_chunk(Vec3::new(x, y, z));
}
@ -95,6 +95,10 @@ impl PlayState for SessionState {
for event in global_state.window.fetch_events() {
let _handled = match event {
Event::Close => return PlayStateResult::Shutdown,
// When the window is resized, change the camera's aspect ratio
Event::Resize(dims) => {
self.scene.camera_mut().set_aspect_ratio(dims.x as f32 / dims.y as f32);
},
// When 'q' is pressed, exit the session
Event::Char('q') => return PlayStateResult::Pop,
// Toggle cursor grabbing

View File

@ -67,11 +67,22 @@ impl Window {
// Copy data that is needed by the events closure to avoid lifetime errors
// TODO: Remove this if/when the compiler permits it
let cursor_grabbed = self.cursor_grabbed;
let renderer = &mut self.renderer;
let window = &mut self.window;
let mut events = vec![];
self.events_loop.poll_events(|event| match event {
glutin::Event::WindowEvent { event, .. } => match event {
glutin::WindowEvent::CloseRequested => events.push(Event::Close),
glutin::WindowEvent::Resized(glutin::dpi::LogicalSize { width, height }) => {
let (mut color_view, mut depth_view) = renderer.target_views_mut();
gfx_window_glutin::update_views(
&window,
&mut color_view,
&mut depth_view,
);
events.push(Event::Resize(Vec2::new(width as u32, height as u32)));
},
glutin::WindowEvent::ReceivedCharacter(c) => events.push(Event::Char(c)),
glutin::WindowEvent::KeyboardInput { input, .. } => match input.virtual_keycode {
Some(glutin::VirtualKeyCode::Escape) => events.push(if input.state == glutin::ElementState::Pressed {
@ -119,6 +130,8 @@ pub enum Key {
pub enum Event {
/// The window has been requested to close.
Close,
/// The window has been resized
Resize(Vec2<u32>),
/// A key has been typed that corresponds to a specific character.
Char(char),
/// The cursor has been panned across the screen while grabbed.