diff --git a/src/console_render/c_math.rs b/src/console_render/c_math.rs index 12d5f07..3065b98 100644 --- a/src/console_render/c_math.rs +++ b/src/console_render/c_math.rs @@ -1,23 +1,19 @@ -pub fn mid(a: f32, b: f32) -> f32 { - (a + b) / 2.0 -} - #[derive(Clone)] pub struct f2d { - x: f32, - y: f32, + x: f64, + y: f64, } impl f2d { - pub fn new(a: f32, b: f32) -> Self { + pub fn new(a: f64, b: f64) -> Self { f2d { x: a, y: b } } pub fn def() -> Self { f2d { x: 0.0, y: 0.0 } } // - pub fn len(&self) -> f32 { - (self.x * self.x + self.y * self.y).sqrt() + pub fn len(&self) -> f64 { + self.x * self.x + self.y * self.y } pub fn add(&mut self, b: f2d) { @@ -26,8 +22,8 @@ impl f2d { } pub fn mul(&mut self, b: f2d) { - let x: f32 = self.x * b.x - self.y * b.y; - let y: f32 = self.x * b.y + self.y * b.x; + let x: f64 = self.x * b.x - self.y * b.y; + let y: f64 = self.x * b.y + self.y * b.x; self.x = x; self.y = y; diff --git a/src/console_render/mod.rs b/src/console_render/mod.rs index 05ef826..aa6caad 100644 --- a/src/console_render/mod.rs +++ b/src/console_render/mod.rs @@ -1,118 +1,72 @@ use crossterm::{ //cursor::MoveTo, + cursor::MoveTo, execute, + queue, style::{Color, Print, ResetColor, SetBackgroundColor}, + terminal::{Clear, ClearType}, }; -use std::io::stdout; +use std::io::{Write, stdout}; mod c_math; use c_math::*; -const X_SCAILE: f32 = 2.7; +const X_SCAILE: f64 = 2.7; pub fn m_render( size: (usize, usize), - scale: f32, - pos: (f32, f32), + scale: f64, + pos: (f64, f64), ) -> Result<(), Box> { - /* - let mut colors = [ - Color::Rgb { r: 0, g: 0, b: 0 }, - Color::Rgb { - r: 150, - g: 90, - b: 150, - }, - ]; - */ - let mut grad = Color::Rgb { r: 0, g: 0, b: 0 }; let mut out = stdout(); + queue!(out, MoveTo(0, 0))?; + //out.flush()?; let mut z: f2d; let mut c: f2d; - let xs: f32 = size.0 as f32 / 2.; - let ys: f32 = size.1 as f32 / 2.; - let deep: f32 = 0.2 / scale; + let xs: f64 = size.0 as f64 / 2.; + let ys: f64 = size.1 as f64 / 2.; + //let deep: f32 = 1.0 / scale; + let deep = (100.0 + scale.log10().abs() * 80.0) as usize; for i in 0..size.0 { for j in 0..size.1 { - let x: f32 = i as f32; - let y: f32 = j as f32; + let x: f64 = i as f64; + let y: f64 = j as f64; c = f2d::new( - (x - xs) * scale * X_SCAILE + pos.0, - (y - ys) * scale + pos.1, + (x - xs) * scale as f64 * X_SCAILE + pos.0, + (y - ys) * scale as f64 + pos.1, ); z = f2d::def(); - let mut m: f32 = 0.0; + let mut m: usize = 0; let mut k = true; while k { z.mul(z.clone()); z.add(c.clone()); - m += 1.0; - k = (z.len() < 2.0) && (m < deep); - //if m > deep { - // println!("Stop me!!!"); - //} + m += 1; + k = (z.len() < 4.0) && (m < deep); } if z.len() > 2.0 { - //m /= deep; - let smooth = m + 1.0 - z.len().ln().ln() / 2.0_f32.ln(); - /* - let t = (smooth / deep).clamp(0.0, 1.0); - grad = Color::Rgb { - r: (t * 230.0 + 10.0) as u8, - g: (t * 100.0 + 10.0) as u8, - b: (t * 240.0 + 10.0) as u8, - }; - */ + let smooth = (m as f64 + 1.0 - z.len().ln().ln() / 2.0_f64.ln()) as f32; grad = Color::Rgb { r: (((smooth * 0.1).sin() + 1.0) * 230.0) as u8, g: (((smooth * 0.1).sin() + 1.0) * 100.0) as u8, b: (((smooth * 0.1 + 0.1).sin() + 1.0) * 220.0) as u8, }; - - /* - grad = Color::Rgb { - r: (m * 230.0 + 10.0) as u8, - g: (m * 100.0 + 10.0) as u8, - b: (m * 230.0 + 10.0) as u8, - }; - */ - execute!(out, SetBackgroundColor(grad), Print(" "), ResetColor)?; + queue!(out, SetBackgroundColor(grad), Print(" "), ResetColor)?; + out.flush()?; grad = Color::Rgb { r: 0, g: 0, b: 0 }; } else { - execute!(out, SetBackgroundColor(grad), Print(" "), ResetColor,)?; + queue!(out, SetBackgroundColor(grad), Print(" "), ResetColor,)?; + out.flush()?; } - /* - z = f2d::def(); - for k in 0..deep { - z.mul(z.clone()); - z.add(c.clone()); - if z.len() > 2. { - let mut m = (k as f32) / (deep as f32); - //m = (m / 100.0).round() * 100.0; - m *= 255.0; - colors[1] = Color::Rgb { - r: m as u8, - g: m as u8, - b: m as u8, - }; - execute!(out, SetBackgroundColor(colors[1]), Print(" "), ResetColor,)?; - //print!("#"); - break; - } - } - - if z.len() < 2. { - execute!(out, SetBackgroundColor(colors[0]), Print(" "), ResetColor,)?; - } - */ } - println!(); + queue!(out, Print("\r\n"))?; } + out.flush()?; Ok(()) } diff --git a/src/input/mod.rs b/src/input/mod.rs new file mode 100644 index 0000000..94ac770 --- /dev/null +++ b/src/input/mod.rs @@ -0,0 +1,111 @@ +use crossterm::{ + event::{self, Event, KeyCode, KeyEventKind}, + terminal::{disable_raw_mode, enable_raw_mode}, +}; +use std::io::{self, Write}; + +pub enum InputEvent { + None, + Pos(f64, f64), + Scale(f64), +} + +pub fn in_pars() -> Result { + let mut input = String::new(); + + print!("In: "); + io::stdout().flush().unwrap(); + io::stdin() + .read_line(&mut input) + .expect("error input scan\n"); + + let worlds: Vec<&str> = input.trim().split(" ").collect(); + if worlds.len() == 0 { + return Err(String::from("zero lenth")); + } + + match worlds[0] { + "pos" => { + //println!("pos!"); + if worlds.len() < 3 { + return Err(String::from("not enough arguments")); + } + let r0 = worlds[1].parse::(); + let r1 = worlds[2].parse::(); + if r0.is_err() || r1.is_err() { + return Err(String::from("not enough arguments")); + } + return Ok(InputEvent::Pos(r0.unwrap(), r1.unwrap())); + } + "scl" => { + if worlds.len() < 2 { + return Err(String::from("not enough scale")); + } + let scl = worlds[1].parse::(); + if scl.is_err() { + return Err(String::from("scale is not value")); + } + return Ok(InputEvent::Scale(scl.unwrap())); + } + _ => {} + } + + Ok(InputEvent::None) +} + +pub enum KeyEvent { + None, + ESC, + R, + A, + W, + S, + D, + Q, + E, +} + +pub fn key_pars() -> Result, std::io::Error> { + _ = enable_raw_mode(); + if let Event::Key(key) = event::read()? { + match key.kind { + KeyEventKind::Repeat => { + //println!("Key is Repeat! {}", key.code); + } + KeyEventKind::Press => { + //println!("Key is press! {}", key.code); + match key.code { + KeyCode::Esc => { + _ = disable_raw_mode(); + return Ok(Some(KeyEvent::ESC)); + } + KeyCode::Char('a') => { + return Ok(Some(KeyEvent::A)); + } + KeyCode::Char('w') => { + return Ok(Some(KeyEvent::W)); + } + KeyCode::Char('s') => { + return Ok(Some(KeyEvent::S)); + } + KeyCode::Char('d') => { + return Ok(Some(KeyEvent::D)); + } + KeyCode::Char('q') => { + return Ok(Some(KeyEvent::Q)); + } + KeyCode::Char('e') => { + return Ok(Some(KeyEvent::E)); + } + _ => {} + } + } + KeyEventKind::Release => { + //println!("Key is replease! {}", key.code); + } + _ => {} + } + } + //_ = disable_raw_mode(); + Ok(None) +} diff --git a/src/main.rs b/src/main.rs index 11c710e..af7eaf3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,92 +1,87 @@ mod console_render; -use std::io::{self, Write}; + +use std::arch::x86_64::_SIDD_MASKED_NEGATIVE_POLARITY; use console_render::*; -enum InputEvent { - None, - Pos(f32, f32), - Scale(f32), +mod input; +use input::*; + +enum Input_type { + console, + keyborde, } - -fn in_pars() -> Result { - let mut input = String::new(); - - print!("In: "); - io::stdout().flush().unwrap(); - io::stdin() - .read_line(&mut input) - .expect("error input scan\n"); - - let worlds: Vec<&str> = input.trim().split(" ").collect(); - if worlds.len() == 0 { - return Err(String::from("zero lenth")); - } - - match worlds[0] { - "pos" => { - //println!("pos!"); - if worlds.len() < 3 { - return Err(String::from("not enough arguments")); - } - let r0 = worlds[1].parse::(); - let r1 = worlds[2].parse::(); - if r0.is_err() || r1.is_err() { - return Err(String::from("not enough arguments")); - } - return Ok(InputEvent::Pos(r0.unwrap(), r1.unwrap())); - } - "scl" => { - if worlds.len() < 2 { - return Err(String::from("not enough scale")); - } - let scl = worlds[1].parse::(); - if scl.is_err() { - return Err(String::from("scale is not value")); - } - return Ok(InputEvent::Scale(scl.unwrap())); - } - _ => {} - } - - Ok(InputEvent::None) -} - fn main() { - let size: (usize, usize) = (200, 450); - let mut pos: (f32, f32) = (0.0, 0.0); - let mut scale: f32 = 0.01; + let size: (usize, usize) = (100, 400); + let mut pos: (f64, f64) = (0.0, 0.0); + let mut scale: f64 = 0.01; let mut render = true; - //m_render(size, 0.007, (-0.4, 0.)); - - //_ = in_pars(); + let mut it = Input_type::keyborde; while true { if render { let _ = m_render(size, scale, pos); } - let input = in_pars(); - if input.is_err() { - render = false; - println!("ERROR: {}", input.err().unwrap()); - continue; - } else { - match input.unwrap() { - InputEvent::Pos(a, b) => { - pos.0 += a * scale * 10.0; - pos.1 += b * scale * 10.0; - } - InputEvent::Scale(s) => { - if s == 0.0 { - println!("Scale is 0 !!!"); - } else { - scale /= s; + match it { + Input_type::console => { + let input = in_pars(); + if input.is_err() { + render = false; + println!("ERROR: {}", input.err().unwrap()); + continue; + } else { + match input.unwrap() { + InputEvent::Pos(a, b) => { + pos.0 += a * scale * 10.0; + pos.1 += b * scale * 10.0; + } + InputEvent::Scale(s) => { + if s == 0.0 { + println!("Scale is 0 !!!"); + } else { + scale /= s; + } + } + _ => {} } } - _ => {} } + Input_type::keyborde => { + let input = key_pars(); + if input.is_err() { + render = false; + println!("ERROR: {}", input.err().unwrap()); + continue; + } else { + if let Some(k) = input.unwrap() { + match k { + KeyEvent::ESC => { + it = Input_type::console; + } + KeyEvent::A => { + pos.1 -= scale as f64 * 10.0; + } + KeyEvent::W => { + pos.0 -= scale as f64 * 10.0; + } + KeyEvent::S => { + pos.0 += scale as f64 * 10.0; + } + KeyEvent::D => { + pos.1 += scale as f64 * 10.0; + } + KeyEvent::Q => { + scale /= 1.1; + } + KeyEvent::E => { + scale *= 1.1; + } + _ => {} + } + } + } + } + _ => {} } - - render = true; } }