Cleaned up codebase, added cursor grab toggle

This commit is contained in:
Joshua Barretto 2019-01-23 22:21:47 +00:00
parent 248577bdef
commit 8a37662cf0
13 changed files with 54 additions and 47 deletions

View File

@ -13,14 +13,6 @@ use crate::{
// Local // Local
use self::cell::Cell; use self::cell::Cell;
/// A type representing a single figure bone (e.g: the limb of a character).
#[derive(Copy, Clone)]
pub struct Bone {
origin: Vec3<f32>,
offset: Vec3<f32>,
ori: Vec3<f32>,
}
/// A type representing a volume that may be part of an animated figure. /// A type representing a volume that may be part of an animated figure.
/// ///
/// Figures are used to represent things like characters, NPCs, mobs, etc. /// Figures are used to represent things like characters, NPCs, mobs, etc.

View File

@ -1 +1 @@
pub mod worker_pool;

View File

@ -1,10 +0,0 @@
// Library
use threadpool::ThreadPool;
pub struct WorkerPool {
thread_pool: ThreadPool,
}
impl WorkerPool {
}

View File

@ -14,6 +14,7 @@ use crate::vol::{
VolSize, VolSize,
}; };
#[derive(Debug)]
pub enum ChunkErr { pub enum ChunkErr {
OutOfBounds, OutOfBounds,
} }

View File

@ -10,6 +10,7 @@ use crate::vol::{
WriteVol, WriteVol,
}; };
#[derive(Debug)]
pub enum DynaErr { pub enum DynaErr {
OutOfBounds, OutOfBounds,
} }

View File

@ -21,6 +21,7 @@ use crate::{
}, },
}; };
#[derive(Debug)]
pub enum VolMapErr { pub enum VolMapErr {
NoSuchChunk, NoSuchChunk,
ChunkErr(ChunkErr), ChunkErr(ChunkErr),

View File

@ -34,7 +34,7 @@ impl GlobalState {
/// Called after a change in play state has occured (usually used to reverse any temporary /// Called after a change in play state has occured (usually used to reverse any temporary
/// effects a state may have made). /// effects a state may have made).
pub fn on_play_state_changed(&mut self) { pub fn on_play_state_changed(&mut self) {
self.window.untrap_cursor(); self.window.grab_cursor(false);
} }
} }

View File

@ -1,9 +1,6 @@
pub mod segment; pub mod segment;
pub mod terrain; pub mod terrain;
// Library
use vek::*;
// Crate // Crate
use crate::render::{ use crate::render::{
self, self,

View File

@ -9,7 +9,7 @@ use common::{
ReadVol, ReadVol,
}, },
volumes::dyna::Dyna, volumes::dyna::Dyna,
terrain::{Block, TerrainChunk}, terrain::Block,
}; };
// Crate // Crate

View File

@ -12,10 +12,7 @@ use vek::*;
use client::Client; use client::Client;
use common::{ use common::{
terrain::TerrainMap, terrain::TerrainMap,
volumes::vol_map::{ volumes::vol_map::VolMapErr,
VolMap,
VolMapErr,
},
vol::SampleVol, vol::SampleVol,
}; };
@ -179,7 +176,7 @@ impl Terrain {
} }
pub fn render(&self, renderer: &mut Renderer, globals: &Consts<Globals>) { pub fn render(&self, renderer: &mut Renderer, globals: &Consts<Globals>) {
for (pos, chunk) in &self.chunks { for (_, chunk) in &self.chunks {
renderer.render_terrain_chunk( renderer.render_terrain_chunk(
&chunk.model, &chunk.model,
globals, globals,

View File

@ -17,7 +17,7 @@ use crate::{
PlayState, PlayState,
PlayStateResult, PlayStateResult,
GlobalState, GlobalState,
window::Event, window::{Event, Key},
render::Renderer, render::Renderer,
scene::Scene, scene::Scene,
}; };
@ -75,14 +75,14 @@ impl SessionState {
impl PlayState for SessionState { impl PlayState for SessionState {
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult { fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult {
// Trap the cursor // Trap the cursor
global_state.window.trap_cursor(); global_state.window.grab_cursor(true);
// Set up an fps clock // Set up an fps clock
let mut clock = Clock::new(); let mut clock = Clock::new();
// Load a few chunks TODO: Remove this // Load a few chunks TODO: Remove this
for x in -2..3 { for x in -4..5 {
for y in -2..3 { for y in -4..5 {
for z in -1..2 { for z in -1..2 {
self.client.load_chunk(Vec3::new(x, y, z)); self.client.load_chunk(Vec3::new(x, y, z));
} }
@ -97,8 +97,12 @@ impl PlayState for SessionState {
Event::Close => return PlayStateResult::Shutdown, Event::Close => return PlayStateResult::Shutdown,
// When 'q' is pressed, exit the session // When 'q' is pressed, exit the session
Event::Char('q') => return PlayStateResult::Pop, Event::Char('q') => return PlayStateResult::Pop,
// Toggle cursor grabbing
Event::KeyDown(Key::ToggleCursor) => {
global_state.window.grab_cursor(!global_state.window.is_cursor_grabbed());
},
// Pass all other events to the scene // Pass all other events to the scene
event => self.scene.handle_input_event(event), event => { self.scene.handle_input_event(event); },
}; };
// TODO: Do something if the event wasn't handled? // TODO: Do something if the event wasn't handled?
} }

View File

@ -15,8 +15,9 @@ use crate::{
pub struct Window { pub struct Window {
events_loop: glutin::EventsLoop, events_loop: glutin::EventsLoop,
window: glutin::GlWindow,
renderer: Renderer, renderer: Renderer,
window: glutin::GlWindow,
cursor_grabbed: bool,
} }
@ -47,13 +48,14 @@ impl Window {
let tmp = Ok(Self { let tmp = Ok(Self {
events_loop, events_loop,
window,
renderer: Renderer::new( renderer: Renderer::new(
device, device,
factory, factory,
tgt_color_view, tgt_color_view,
tgt_depth_view, tgt_depth_view,
)?, )?,
window,
cursor_grabbed: false,
}); });
tmp tmp
} }
@ -62,15 +64,27 @@ impl Window {
pub fn renderer_mut(&mut self) -> &mut Renderer { &mut self.renderer } pub fn renderer_mut(&mut self) -> &mut Renderer { &mut self.renderer }
pub fn fetch_events(&mut self) -> Vec<Event> { pub fn fetch_events(&mut self) -> Vec<Event> {
// 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 mut events = vec![]; let mut events = vec![];
self.events_loop.poll_events(|event| match event { self.events_loop.poll_events(|event| match event {
glutin::Event::WindowEvent { event, .. } => match event { glutin::Event::WindowEvent { event, .. } => match event {
glutin::WindowEvent::CloseRequested => events.push(Event::Close), glutin::WindowEvent::CloseRequested => events.push(Event::Close),
glutin::WindowEvent::ReceivedCharacter(c) => events.push(Event::Char(c)), 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 {
Event::KeyDown(Key::ToggleCursor)
} else {
Event::KeyUp(Key::ToggleCursor)
}),
_ => {},
},
_ => {}, _ => {},
}, },
glutin::Event::DeviceEvent { event, .. } => match event { glutin::Event::DeviceEvent { event, .. } => match event {
glutin::DeviceEvent::MouseMotion { delta: (dx, dy), .. } => glutin::DeviceEvent::MouseMotion { delta: (dx, dy), .. } if cursor_grabbed =>
events.push(Event::CursorPan(Vec2::new(dx as f32, dy as f32))), events.push(Event::CursorPan(Vec2::new(dx as f32, dy as f32))),
_ => {}, _ => {},
}, },
@ -84,25 +98,33 @@ impl Window {
.map_err(|err| Error::BackendError(Box::new(err))) .map_err(|err| Error::BackendError(Box::new(err)))
} }
pub fn trap_cursor(&mut self) { pub fn is_cursor_grabbed(&self) -> bool {
self.window.hide_cursor(true); self.cursor_grabbed
self.window.grab_cursor(true)
.expect("Failed to grab cursor");
} }
pub fn untrap_cursor(&mut self) { pub fn grab_cursor(&mut self, grab: bool) {
self.window.hide_cursor(false); self.cursor_grabbed = grab;
self.window.grab_cursor(false) self.window.hide_cursor(grab);
.expect("Failed to ungrab cursor"); self.window.grab_cursor(grab)
.expect("Failed to grab/ungrab cursor");
} }
} }
/// Represents a key that the game recognises after keyboard mapping
pub enum Key {
ToggleCursor,
}
/// Represents an incoming event from the window /// Represents an incoming event from the window
pub enum Event { pub enum Event {
/// The window has been requested to close. /// The window has been requested to close.
Close, Close,
/// A key has been typed that corresponds to a specific character. /// A key has been typed that corresponds to a specific character.
Char(char), Char(char),
/// The cursor has been panned across the screen while trapped. /// The cursor has been panned across the screen while grabbed.
CursorPan(Vec2<f32>), CursorPan(Vec2<f32>),
/// A key that the game recognises has been pressed down
KeyDown(Key),
/// A key that the game recognises has been released down
KeyUp(Key),
} }

View File

@ -25,6 +25,8 @@ impl World {
} }
pub fn generate_chunk(&self, chunk_pos: Vec3<i32>) -> TerrainChunk { pub fn generate_chunk(&self, chunk_pos: Vec3<i32>) -> TerrainChunk {
// TODO: This is all test code, remove/improve this later
let mut chunk = TerrainChunk::filled(Block::empty(), TerrainChunkMeta::void()); let mut chunk = TerrainChunk::filled(Block::empty(), TerrainChunkMeta::void());
let air = Block::empty(); let air = Block::empty();
@ -50,7 +52,7 @@ impl World {
} }
} else { } else {
air air
}); }).unwrap();
} }
chunk chunk