mirror of https://github.com/Leinnan/doppler.git
Handle keyboard
This commit is contained in:
parent
baf60f1523
commit
637ffd06d6
|
|
@ -11,6 +11,7 @@ use cgmath::prelude::*;
|
|||
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Vector3};
|
||||
#[cfg(feature = "glfw_obsolete")]
|
||||
use glfw;
|
||||
use glutin::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
|
||||
#[cfg(feature = "glfw_obsolete")]
|
||||
use imgui_glfw_rs::glfw;
|
||||
|
||||
|
|
@ -24,12 +25,14 @@ pub struct ExampleClient {
|
|||
object_info_id: i32,
|
||||
show_light_info: bool,
|
||||
light_info_id: i32,
|
||||
delta: f32,
|
||||
}
|
||||
|
||||
impl ExampleClient {
|
||||
pub fn create() -> ExampleClient {
|
||||
let sky = unsafe { Sky::new() };
|
||||
ExampleClient {
|
||||
delta: 0.0,
|
||||
models: vec![],
|
||||
camera: Camera {
|
||||
position: Point3::new(0.0, 8.0, 13.0),
|
||||
|
|
@ -52,6 +55,27 @@ impl ExampleClient {
|
|||
}
|
||||
|
||||
impl Client for ExampleClient {
|
||||
fn on_keyboard(&mut self, code: &VirtualKeyCode, state: &ElementState) {
|
||||
match (code, state) {
|
||||
(VirtualKeyCode::W, ElementState::Pressed) => {
|
||||
self.camera.process_keyboard(CameraMovement::FORWARD, self.delta)
|
||||
}
|
||||
(VirtualKeyCode::S, ElementState::Pressed) => {
|
||||
self.camera.process_keyboard(CameraMovement::BACKWARD, self.delta)
|
||||
}
|
||||
(VirtualKeyCode::A, ElementState::Pressed) => {
|
||||
self.camera.process_keyboard(CameraMovement::LEFT, self.delta)
|
||||
}
|
||||
(VirtualKeyCode::D, ElementState::Pressed) => {
|
||||
self.camera.process_keyboard(CameraMovement::RIGHT, self.delta)
|
||||
}
|
||||
(VirtualKeyCode::LControl, _) => self
|
||||
.camera
|
||||
.enable_mouse_movement(state == &ElementState::Released),
|
||||
(_, _) => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn load_assets(&mut self, cache: &mut AssetsCache) {
|
||||
let ground = ModelComponent {
|
||||
transform: Transform {
|
||||
|
|
@ -126,29 +150,9 @@ impl Client for ExampleClient {
|
|||
}
|
||||
self.sky.draw(view, projection);
|
||||
}
|
||||
fn update(&mut self, _engine: &mut Engine) {}
|
||||
|
||||
#[cfg(feature = "glfw_obsolete")]
|
||||
fn process_input(&mut self, window: &glfw::Window, delta: f32) {
|
||||
use glfw::{Action, Key};
|
||||
if window.get_key(Key::W) == Action::Press {
|
||||
self.camera.process_keyboard(CameraMovement::FORWARD, delta);
|
||||
}
|
||||
if window.get_key(Key::S) == Action::Press {
|
||||
self.camera
|
||||
.process_keyboard(CameraMovement::BACKWARD, delta);
|
||||
}
|
||||
if window.get_key(Key::A) == Action::Press {
|
||||
self.camera.process_keyboard(CameraMovement::LEFT, delta);
|
||||
}
|
||||
if window.get_key(Key::D) == Action::Press {
|
||||
self.camera.process_keyboard(CameraMovement::RIGHT, delta);
|
||||
}
|
||||
self.camera
|
||||
.enable_mouse_movement(window.get_key(Key::LeftControl) != Action::Press);
|
||||
if window.get_key(Key::O) == Action::Press {
|
||||
println!("{:?}", self.camera);
|
||||
}
|
||||
fn update(&mut self, _engine: &Engine, delta: f32) {
|
||||
self.delta = delta;
|
||||
println!("{}",delta);
|
||||
}
|
||||
|
||||
#[cfg(feature = "imgui_inspect")]
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ use self::CameraMovement::*;
|
|||
// Default camera values
|
||||
const YAW: f32 = -90.0;
|
||||
const PITCH: f32 = 0.0;
|
||||
const SPEED: f32 = 4.5;
|
||||
const SPEED: f32 = 0.5;
|
||||
const SENSITIVTY: f32 = 0.1;
|
||||
const ZOOM: f32 = 45.0;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,11 @@
|
|||
use crate::gaia::assets_cache::AssetsCache;
|
||||
use crate::gaia::engine::Engine;
|
||||
#[cfg(feature = "glfw_obsolete")]
|
||||
use glfw;
|
||||
#[cfg(feature = "glfw_obsolete")]
|
||||
use imgui_glfw_rs::glfw;
|
||||
use glutin::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
|
||||
|
||||
pub trait Client {
|
||||
fn load_assets(&mut self, cache: &mut AssetsCache);
|
||||
fn update(&mut self, engine: &mut Engine);
|
||||
#[cfg(feature = "glfw_obsolete")]
|
||||
fn process_input(&mut self, window: &glfw::Window, delta: f32);
|
||||
fn update(&mut self, engine: &Engine, delta: f32);
|
||||
fn on_keyboard(&mut self, code: &VirtualKeyCode, state: &ElementState);
|
||||
fn on_mouse_scroll(&mut self, yoffset: f32);
|
||||
fn on_mouse_move(&mut self, x: f32, y: f32);
|
||||
// fn draw<T>(&mut self, engine: &mut Engine<T>) where T: Client;
|
||||
|
|
|
|||
|
|
@ -5,13 +5,60 @@ use crate::gaia::client::Client;
|
|||
use crate::gaia::consts;
|
||||
use crate::gaia::framebuffer::FramebufferSystem;
|
||||
use cgmath::Point3;
|
||||
use glutin::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
|
||||
#[cfg(feature = "imgui_inspect")]
|
||||
use imgui::Context;
|
||||
#[cfg(feature = "imgui_inspect")]
|
||||
use imgui_winit_support::{HiDpiMode, WinitPlatform};
|
||||
use std::time::Instant;
|
||||
|
||||
use log::{info, trace, warn};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TimeStep {
|
||||
last_time: Instant,
|
||||
delta_time: f32,
|
||||
frame_count: u32,
|
||||
frame_time: f32,
|
||||
}
|
||||
|
||||
impl TimeStep {
|
||||
pub fn new() -> TimeStep {
|
||||
TimeStep {
|
||||
last_time: Instant::now(),
|
||||
delta_time: 0.0,
|
||||
frame_count: 0,
|
||||
frame_time: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delta(&mut self) -> f32 {
|
||||
let current_time = Instant::now();
|
||||
let delta = current_time.duration_since(self.last_time).as_micros()
|
||||
as f32
|
||||
* 0.001;
|
||||
self.last_time = current_time;
|
||||
self.delta_time = delta;
|
||||
delta
|
||||
}
|
||||
|
||||
// provides the framerate in FPS
|
||||
pub fn frame_rate(&mut self) -> Option<u32> {
|
||||
self.frame_count += 1;
|
||||
self.frame_time += self.delta_time;
|
||||
let tmp;
|
||||
// per second
|
||||
if self.frame_time >= 1000.0 {
|
||||
tmp = self.frame_count;
|
||||
self.frame_count = 0;
|
||||
self.frame_time = 0.0;
|
||||
return Some(tmp);
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(not(feature = "glfw_obsolete"))]
|
||||
pub struct Engine {
|
||||
title: String,
|
||||
|
|
@ -25,7 +72,7 @@ impl Default for Engine {
|
|||
Engine {
|
||||
title: String::from("Doppler demo"),
|
||||
size: (1280, 720),
|
||||
debug_layer: true,
|
||||
debug_layer: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -55,11 +102,7 @@ impl Engine {
|
|||
// configure global opengl state
|
||||
// -----------------------------
|
||||
unsafe {
|
||||
gl::Enable(gl::BLEND);
|
||||
gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
|
||||
gl::Enable(gl::DEPTH_TEST);
|
||||
gl::DepthFunc(gl::LESS);
|
||||
gl::ClearColor(0.1, 0.1, 0.1, 1.0);
|
||||
}
|
||||
info!("DPI: {}", gl_window.window().scale_factor());
|
||||
let mut client = ExampleClient::create();
|
||||
|
|
@ -141,8 +184,7 @@ impl Engine {
|
|||
let mut last_y: f32 = consts::SCR_HEIGHT as f32 / 2.0;
|
||||
|
||||
// timing
|
||||
let mut delta_time: f32 = 0.0; // time between current frame and last frame
|
||||
let mut last_frame: f32 = 0.0;
|
||||
let mut timestep = TimeStep::new();
|
||||
let mut last_frame = std::time::Instant::now();
|
||||
|
||||
let mut screensize = self.size;
|
||||
|
|
@ -156,8 +198,11 @@ impl Engine {
|
|||
Event::NewEvents(_) => {
|
||||
// other application-specific logic
|
||||
last_frame = imgui.io_mut().update_delta_time(last_frame);
|
||||
|
||||
}
|
||||
Event::MainEventsCleared => {
|
||||
let delta = timestep.delta();
|
||||
client.update(&self,delta);
|
||||
// other application-specific logic
|
||||
platform
|
||||
.prepare_frame(imgui.io_mut(), &gl_window.window()) // step 4
|
||||
|
|
@ -166,6 +211,18 @@ impl Engine {
|
|||
}
|
||||
Event::LoopDestroyed => return,
|
||||
Event::WindowEvent { event, .. } => match event {
|
||||
WindowEvent::KeyboardInput {
|
||||
input:
|
||||
KeyboardInput {
|
||||
virtual_keycode: Some(virtual_code),
|
||||
state,
|
||||
..
|
||||
},
|
||||
..
|
||||
} => match (virtual_code, state) {
|
||||
(VirtualKeyCode::Escape, _) => *control_flow = ControlFlow::Exit,
|
||||
_ => client.on_keyboard(&virtual_code, &state),
|
||||
},
|
||||
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
||||
WindowEvent::Resized(size) => {
|
||||
info!("Resizing to {:?}", size);
|
||||
|
|
@ -195,7 +252,7 @@ impl Engine {
|
|||
let ui = imgui.frame();
|
||||
client.debug_draw(&ui);
|
||||
use imgui::*;
|
||||
let fps = 1.0 / delta_time;
|
||||
let fps = timestep.frame_rate().unwrap_or(0u32);
|
||||
let size = [250.0, 110.0];
|
||||
let offset = 20.0;
|
||||
Window::new(im_str!("EngineInfo"))
|
||||
|
|
|
|||
|
|
@ -53,12 +53,12 @@ impl FramebufferSystem {
|
|||
"Generating new framebuffer with dimensions {}x{}",
|
||||
scr_width, scr_height
|
||||
);
|
||||
let screenShader = Shader::from_file(
|
||||
let shader = Shader::from_file(
|
||||
"resources/shaders/framebuffers_screen.vs",
|
||||
"resources/shaders/framebuffers_screen.fs",
|
||||
);
|
||||
|
||||
let quadVertices: [f32; 24] = [
|
||||
let quad_vert: [f32; 24] = [
|
||||
// vertex attributes for a quad that fills the entire screen in Normalized Device Coordinates.
|
||||
// positions // texCoords
|
||||
-1.0, 1.0, 0.0, 1.0, -1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0,
|
||||
|
|
@ -73,8 +73,8 @@ impl FramebufferSystem {
|
|||
gl::BindBuffer(gl::ARRAY_BUFFER, quad_vbo);
|
||||
gl::BufferData(
|
||||
gl::ARRAY_BUFFER,
|
||||
(quadVertices.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
|
||||
&quadVertices[0] as *const f32 as *const c_void,
|
||||
(quad_vert.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
|
||||
&quad_vert[0] as *const f32 as *const c_void,
|
||||
gl::STATIC_DRAW,
|
||||
);
|
||||
gl::EnableVertexAttribArray(0);
|
||||
|
|
@ -89,10 +89,10 @@ impl FramebufferSystem {
|
|||
stride,
|
||||
(2 * mem::size_of::<GLfloat>()) as *const c_void,
|
||||
);
|
||||
screenShader.use_program();
|
||||
screenShader.setInt(c_str!("screenTexture"), 0);
|
||||
screenShader.setFloat(c_str!("screen_width"), scr_width as f32);
|
||||
screenShader.setFloat(c_str!("screen_height"), scr_height as f32);
|
||||
shader.use_program();
|
||||
shader.setInt(c_str!("screenTexture"), 0);
|
||||
shader.setFloat(c_str!("screen_width"), scr_width as f32);
|
||||
shader.setFloat(c_str!("screen_height"), scr_height as f32);
|
||||
|
||||
// framebuffer configuration
|
||||
// -------------------------
|
||||
|
|
@ -149,7 +149,7 @@ impl FramebufferSystem {
|
|||
|
||||
FramebufferSystem {
|
||||
texture_color_buffer: textureColorbuffer,
|
||||
shader: screenShader,
|
||||
shader: shader,
|
||||
vao: quad_vao,
|
||||
vbo: quad_vbo,
|
||||
framebuffer: framebuffer,
|
||||
|
|
|
|||
Loading…
Reference in New Issue