From 71400eb4ffe1e5e3a158a29df725f1d006468698 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Fri, 17 Jan 2020 23:18:08 -0600 Subject: [PATCH] Add arrow key spinnies --- src/main.rs | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9fa88d2..479cc53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,7 @@ use nom::{ }; use sdl2::event::Event; -use sdl2::keyboard::Keycode; - +use sdl2::keyboard::{Keycode, Scancode}; use std::collections::HashMap; use std::convert::TryInto; use std::ffi::{c_void, CStr, CString}; @@ -622,6 +621,10 @@ fn main () { let mut last_frame_time = Instant::now (); let mut frame = 0; let mut timestep_accum = Duration::from_millis (0); + let mut azimuth: f32 = 0.0; + let mut altitude: f32 = 0.0; + let mut spin_speed: i32 = 0; + const SPIN_RAMP_TIME: i32 = 30; //println! ("Entering main loop"); @@ -637,10 +640,34 @@ fn main () { timestep_accum += (frame_time - last_frame_time).mul_f32 (fps_num); + let keyboard_state = event_pump.keyboard_state (); + for _ in 0..4 { if timestep_accum > Duration::from_millis (fps_den) { frame += 1; timestep_accum -= Duration::from_millis (fps_den); + + let spin_f = 4.0 * spin_speed as f32 / SPIN_RAMP_TIME as f32; + + if keyboard_state.is_scancode_pressed (Scancode::Left) { + spin_speed = std::cmp::min (spin_speed + 1, SPIN_RAMP_TIME); + azimuth += spin_f; + } + else if keyboard_state.is_scancode_pressed (Scancode::Right) { + spin_speed = std::cmp::min (spin_speed + 1, SPIN_RAMP_TIME); + azimuth -= spin_f; + } + else if keyboard_state.is_scancode_pressed (Scancode::Up) { + spin_speed = std::cmp::min (spin_speed + 1, SPIN_RAMP_TIME); + altitude = f32::min (90.0, altitude + spin_f); + } + else if keyboard_state.is_scancode_pressed (Scancode::Down) { + spin_speed = std::cmp::min (spin_speed + 1, SPIN_RAMP_TIME); + altitude = f32::max (-90.0, altitude - spin_f); + } + else { + spin_speed = 0; + } } else { break; @@ -663,8 +690,10 @@ fn main () { window.gl_make_current (&gl_ctx).unwrap (); - let longitude = (frame as f32).to_radians (); - let latitude = (frame as f32 / 5.0).to_radians ().sin () * -40.0f32.to_radians () - 75.0f32.to_radians (); + //let longitude = (frame as f32).to_radians (); + let longitude = azimuth.to_radians (); + let latitude = (altitude - 90.0).to_radians (); + //let latitude = (frame as f32 / 5.0).to_radians ().sin () * -40.0f32.to_radians () - 75.0f32.to_radians (); let proj_mat = Mat4::perspective_rh_gl (30.0f32.to_radians (), 1280.0 / 720.0, 0.5, 500.0);