Buffer the rendering for better framerates; create a more useful example

This commit is contained in:
2020-07-24 23:25:16 -07:00
parent 5360d2c6d3
commit 2e92980113
2 changed files with 12 additions and 19 deletions

View File

@@ -4,23 +4,13 @@ use coremem::consts;
use std::{thread, time}; use std::{thread, time};
fn main() { fn main() {
let mut state = SimState::new(64, 16); let mut state = SimState::new(101, 101);
let mut step = 0u64;
loop { loop {
state.impulse_ey(7, 2, 1.0); step += 1;
state.impulse_ey(50, 50, 50f32 * ((step as f64)*0.1f64).sin() as f32);
Renderer.render(&state); Renderer.render(&state);
state.step(); state.step();
thread::sleep(time::Duration::from_millis(50)); thread::sleep(time::Duration::from_millis(33));
state.impulse_ey(50, 10, 1.0);
Renderer.render(&state);
state.step();
thread::sleep(time::Duration::from_millis(50));
state.impulse_ey(7, 2, -1.0);
Renderer.render(&state);
state.step();
thread::sleep(time::Duration::from_millis(50));
state.impulse_ey(50, 10, -1.0);
Renderer.render(&state);
state.step();
thread::sleep(time::Duration::from_millis(50));
} }
} }

View File

@@ -1,6 +1,7 @@
use ansi_term::Color::RGB; use ansi_term::Color::RGB;
use crate::consts; use crate::consts;
use crate::SimState; use crate::SimState;
use std::fmt::Write as _;
pub struct NumericTermRenderer; pub struct NumericTermRenderer;
@@ -34,17 +35,19 @@ fn norm_color(v: f32) -> u8 {
impl ColorTermRenderer { impl ColorTermRenderer {
pub fn render(&self, state: &SimState) { pub fn render(&self, state: &SimState) {
let mut buf = String::new();
let square = ""; let square = "";
for y in 0..state.height() { for y in 0..state.height() {
for x in 0..state.width() { for x in 0..state.width() {
let cell = state.get(x, y); let cell = state.get(x, y);
let r = norm_color(cell.bz() * consts::C); //let r = norm_color(cell.bz() * consts::C);
let r = 0;
let g = norm_color(cell.ex()); let g = norm_color(cell.ex());
let b = norm_color(cell.ey()); let b = norm_color(cell.ey());
print!("{}", RGB(r, g, b).paint(square)); write!(&mut buf, "{}", RGB(r, g, b).paint(square));
} }
print!("\n"); write!(&mut buf, "\n");
} }
print!("\n"); println!("{}", buf);
} }
} }