aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2021-10-27 21:16:29 +0300
committereug-vs <eugene@eug-vs.xyz>2021-10-27 21:16:29 +0300
commitba78b90209c5fd3a183043ad3f5110c4a60bf335 (patch)
tree1f7b5708c3561988f0cb6a8f3fafca32580e622f /src
parent9d913bd2ca292874c211bd901e7ac4e0784a5c18 (diff)
downloadpistol-ba78b90209c5fd3a183043ad3f5110c4a60bf335.tar.gz
feat: control camera with VIM keys
Diffstat (limited to 'src')
-rw-r--r--src/camera.rs9
-rw-r--r--src/main.rs44
2 files changed, 36 insertions, 17 deletions
diff --git a/src/camera.rs b/src/camera.rs
index 9f22e99..4773a81 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -91,21 +91,14 @@ impl Camera {
return dist
}
- pub fn rorate_around_point(& mut self, point: Vector) {
- let rotations_per_round = 2.0;
- self.position = rotate_z(self.position - point, rotations_per_round * 2.0 * PI / 60.0) + point;
- self.direction = (point - self.position).normalize();
- }
-
pub fn screen(&self) -> (f32, f32) {
let width = self.distance * 2.0 * (self.angle / 2.0).tan();
let height = width * self.aspect_ratio;
// println!("Screen {}x{} units", width, height);
(width, height)
}
- pub fn render(& mut self) {
- self.rorate_around_point(Vector { x: 4.0, y: 0.0, z: 0.0 });
+ pub fn render(& mut self) {
let palette = "$@B%8&WM#oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
let (screen_width, screen_height) = self.screen();
diff --git a/src/main.rs b/src/main.rs
index 9fec82d..804ddfa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,11 +2,11 @@ extern crate ncurses;
mod camera;
mod canvas;
-use std::f32::consts::PI;
+use std::{f32::consts::PI, time::Instant};
use cgmath::Vector3;
use ncurses::*;
-use crate::camera::{Buffer, Camera, WIDTH, HEIGHT};
+use crate::camera::{Buffer, Camera, WIDTH, HEIGHT, rotate_z};
fn main() {
let mut cam = Camera {
@@ -22,14 +22,40 @@ fn main() {
initscr();
- for _round in 0..20 {
- for i in 0..60 {
- cam.time = (i as f32 / 60.0) * 2.0 * PI;
- cam.render();
+ let time = 0;
- clear();
- addstr(&cam.buffer.to_string());
- refresh();
+ while true {
+ clear();
+ flushinp();
+
+ // Render
+ cam.time = (time as f32 / 60.0) * 2.0 * PI;
+ let timestamp = Instant::now();
+ cam.render();
+ addstr(&cam.buffer.to_string());
+ addstr(&format!("\nRendered in {:?}\n", timestamp.elapsed()).to_string());
+ refresh();
+
+ // Handle input
+ let char = getch();
+
+ let cam_speed = 0.5;
+ let cam_turn_rate = 30.0;
+
+ if char == 106 || char == 74 {
+ cam.position -= cam.direction * cam_speed;
+ } else if char == 107 || char == 75 {
+ cam.position += cam.direction * cam_speed;
+ } else if char == 104 {
+ cam.direction = rotate_z(cam.direction, -2.0 * PI / cam_turn_rate);
+ } else if char == 108 {
+ cam.direction = rotate_z(cam.direction, 2.0 * PI / cam_turn_rate);
+ } else if char == 72 {
+ cam.position -= rotate_z(cam.direction, PI / 2.0) * cam_speed;
+ } else if char == 76 {
+ cam.position += rotate_z(cam.direction, PI / 2.0) * cam_speed;
+ } else if char == 70 { // F
+ cam.direction = -cam.direction;
}
}