Add a 2d color-based renderer
This commit is contained in:
@@ -1,19 +1,26 @@
|
||||
use coremem::SimState;
|
||||
use coremem::render::NumericTermRenderer;
|
||||
use coremem::render::ColorTermRenderer as Renderer;
|
||||
use coremem::consts;
|
||||
use std::{thread, time};
|
||||
|
||||
fn main() {
|
||||
let mut state = SimState::new(16, 5);
|
||||
state.impulse_bz(0, 0, 1.0/consts::C);
|
||||
state.impulse_ey(0, 0, 1.0);
|
||||
state.impulse_bz(7, 0, 1.0/consts::C);
|
||||
state.impulse_ey(7, 2, 1.0);
|
||||
state.impulse_bz(15, 0, 1.0/consts::C);
|
||||
state.impulse_ey(15, 0, 1.0);
|
||||
let mut state = SimState::new(64, 16);
|
||||
loop {
|
||||
NumericTermRenderer.render(&state);
|
||||
state.impulse_ey(7, 2, 1.0);
|
||||
Renderer.render(&state);
|
||||
state.step();
|
||||
thread::sleep(time::Duration::from_millis(100));
|
||||
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));
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@@ -28,17 +28,23 @@ fn clamp(v: f32, range: f32) -> f32 {
|
||||
v.min(range).max(-range)
|
||||
}
|
||||
|
||||
fn norm_color(v: f32) -> u8 {
|
||||
(v * 64.0 + 128.0).max(0f32).min(255f32) as u8
|
||||
}
|
||||
|
||||
impl ColorTermRenderer {
|
||||
pub fn render(&self, state: &SimState) {
|
||||
unimplemented!()
|
||||
// let square = "█";
|
||||
// for cell in state.cells() {
|
||||
// let b_value = clamp(cell.by() * consts::C * 128.0, 255.0);
|
||||
// let b_color = RGB(0, b_value.max(0.0) as _, b_value.abs() as _);
|
||||
// let e_value = clamp(cell.ez() * 128.0, 255.0);
|
||||
// let e_color = RGB(e_value.abs() as _, e_value.max(0.0) as _, 0);
|
||||
// print!("{}{}", e_color.paint(square), b_color.paint(square));
|
||||
// }
|
||||
// print!("\n");
|
||||
let square = "█";
|
||||
for y in 0..state.height() {
|
||||
for x in 0..state.width() {
|
||||
let cell = state.get(x, y);
|
||||
let r = norm_color(cell.bz() * consts::C);
|
||||
let g = norm_color(cell.ex());
|
||||
let b = norm_color(cell.ey());
|
||||
print!("{}", RGB(r, g, b).paint(square));
|
||||
}
|
||||
print!("\n");
|
||||
}
|
||||
print!("\n");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user