Initial commit
This commit is contained in:
commit
aaa212b9a9
22 changed files with 4778 additions and 0 deletions
91
examples/old/live.rs
Normal file
91
examples/old/live.rs
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
use crate::{
|
||||
model::{Coloring, Model, Settings},
|
||||
solver::Solver,
|
||||
space::Space,
|
||||
};
|
||||
|
||||
use sdl2::{event::Event, keyboard::Keycode, pixels::PixelFormatEnum};
|
||||
use std::{
|
||||
sync::{Arc, RwLock, RwLockReadGuard},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
const FRAMEDUR: u64 = 30;
|
||||
|
||||
pub fn run<
|
||||
T: Copy,
|
||||
P,
|
||||
S: Settings,
|
||||
M: Model<T, S, D> + Coloring<T, P, [u8; 3], D>,
|
||||
V: Solver<T, S, M, D>,
|
||||
const D: usize,
|
||||
>(
|
||||
space: Arc<RwLock<Space<T, S, M, V, D>>>,
|
||||
) {
|
||||
let sdl_context = sdl2::init().unwrap();
|
||||
let video_subsystem = sdl_context.video().unwrap();
|
||||
|
||||
let size = space.read().unwrap().size;
|
||||
|
||||
let window = video_subsystem
|
||||
.window("Model", size.0 as u32, size.1 as u32)
|
||||
.resizable()
|
||||
.position_centered()
|
||||
.opengl()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let mut canvas = window.into_canvas().build().unwrap();
|
||||
let texture_creator = canvas.texture_creator();
|
||||
|
||||
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||
let interval = Duration::from_millis(FRAMEDUR);
|
||||
|
||||
let mut image = vec![0u8; size.0 * size.1 * 3];
|
||||
|
||||
'running: loop {
|
||||
for event in event_pump.poll_iter() {
|
||||
match event {
|
||||
Event::Quit { .. }
|
||||
| Event::KeyDown {
|
||||
keycode: Some(Keycode::Escape),
|
||||
..
|
||||
} => {
|
||||
break 'running;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
render(space.read().unwrap(), &mut image);
|
||||
|
||||
let mut texture = texture_creator
|
||||
.create_texture_streaming(PixelFormatEnum::RGB24, size.0 as u32, size.1 as u32)
|
||||
.unwrap();
|
||||
texture.update(None, &image, size.0 * 3).unwrap();
|
||||
canvas.copy(&texture, None, None).unwrap();
|
||||
|
||||
canvas.present();
|
||||
|
||||
std::thread::sleep(interval);
|
||||
}
|
||||
}
|
||||
|
||||
fn render<
|
||||
T: Copy,
|
||||
P,
|
||||
S: Settings,
|
||||
M: Model<T, S, D> + Coloring<T, P, [u8; 3], D>,
|
||||
V: Solver<T, S, M, D>,
|
||||
const D: usize,
|
||||
>(
|
||||
space: RwLockReadGuard<Space<T, S, M, V, D>>,
|
||||
image: &mut Vec<u8>,
|
||||
) {
|
||||
image.resize(space.points.len() * 3, 0);
|
||||
if let Some(pre) = M::prepare(space.points.iter().map(|point| point.pos)) {
|
||||
for (i, point) in space.points.iter().enumerate() {
|
||||
image[i * 3..i * 3 + 3].copy_from_slice(&M::color(&pre, point.pos));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue