loop split when reaching border

This commit is contained in:
Vinzenz Schroeter 2024-05-19 19:57:07 +02:00
parent 2799341651
commit 22faceab57

View file

@ -12,7 +12,7 @@ use crossterm::terminal::{
use log::LevelFilter; use log::LevelFilter;
use rand::distributions::{Distribution, Standard}; use rand::distributions::{Distribution, Standard};
use rand::Rng; use rand::Rng;
use servicepoint2::{ByteGrid, CompressionCode, Connection, FRAME_PACING, Grid, Origin, PIXEL_WIDTH, PixelGrid, TILE_HEIGHT, TILE_WIDTH}; use servicepoint2::{ByteGrid, CompressionCode, Connection, FRAME_PACING, Grid, Origin, PixelGrid, TILE_HEIGHT, TILE_WIDTH};
use servicepoint2::Command::{BitmapLinearWin, CharBrightness}; use servicepoint2::Command::{BitmapLinearWin, CharBrightness};
use crate::game::Game; use crate::game::Game;
@ -57,8 +57,8 @@ fn main() {
let mut pixels = PixelGrid::max_sized(); let mut pixels = PixelGrid::max_sized();
let mut luma = ByteGrid::new(TILE_WIDTH, TILE_HEIGHT); let mut luma = ByteGrid::new(TILE_WIDTH, TILE_HEIGHT);
let mut split_pixel = PIXEL_WIDTH / 2; let mut split_pixel = 0;
let mut split_speed = 1; let mut split_speed: i32 = 1;
let mut iteration = Wrapping(0u8); let mut iteration = Wrapping(0u8);
@ -72,34 +72,49 @@ fn main() {
right_luma.step(); right_luma.step();
iteration += Wrapping(1u8); iteration += Wrapping(1u8);
split_pixel = usize::clamp(split_pixel + split_speed, 0, pixels.width() - 1);
if split_speed > 0 && split_pixel == pixels.width() {
split_pixel = 0;
(left_luma, right_luma) = (right_luma, left_luma);
(left_pixels, right_pixels) = (right_pixels, left_pixels);
randomize(&mut left_pixels.field);
randomize(&mut left_luma.field);
} else if split_speed < 0 && split_pixel == 0 {
split_pixel = pixels.width();
(left_luma, right_luma) = (right_luma, left_luma);
(left_pixels, right_pixels) = (right_pixels, left_pixels);
randomize(&mut right_pixels.field);
randomize(&mut right_luma.field);
}
split_pixel = i32::clamp(split_pixel as i32 + split_speed, 0, pixels.width() as i32) as usize;
draw_pixels(&mut pixels, &left_pixels.field, &right_pixels.field, split_pixel); draw_pixels(&mut pixels, &left_pixels.field, &right_pixels.field, split_pixel);
draw_luma(&mut luma, &left_luma.field, &right_luma.field, split_pixel / 8); draw_luma(&mut luma, &left_luma.field, &right_luma.field, split_pixel / 8);
send_to_screen(&connection, &pixels, &luma); send_to_screen(&connection, &pixels, &luma);
while event::poll(Duration::from_secs(0)).expect("could not poll") { while event::poll(Duration::from_secs(0)).expect("could not poll") {
match parse_event(event::read().expect("could not read event")) { match event::read().expect("could not read event").try_into() {
AppEvent::None => {} Err(_) => {}
AppEvent::RandomizeLeftPixels => { Ok(AppEvent::RandomizeLeftPixels) => {
randomize(&mut left_pixels.field); randomize(&mut left_pixels.field);
} }
AppEvent::RandomizeRightPixels => { Ok(AppEvent::RandomizeRightPixels) => {
randomize(&mut right_pixels.field); randomize(&mut right_pixels.field);
} }
AppEvent::RandomizeLeftLuma => { Ok(AppEvent::RandomizeLeftLuma) => {
randomize(&mut left_luma.field); randomize(&mut left_luma.field);
} }
AppEvent::RandomizeRightLuma => { Ok(AppEvent::RandomizeRightLuma) => {
randomize(&mut right_luma.field); randomize(&mut right_luma.field);
} }
AppEvent::Accelerate => { Ok(AppEvent::Accelerate) => {
split_speed += 1; split_speed += 1;
} }
AppEvent::Decelerate => { Ok(AppEvent::Decelerate) => {
split_speed -= 1; split_speed -= 1;
} }
AppEvent::Close => { Ok(AppEvent::Close) => {
de_init(); de_init();
return; return;
} }
@ -115,7 +130,6 @@ fn main() {
} }
enum AppEvent { enum AppEvent {
None,
Close, Close,
RandomizeLeftPixels, RandomizeLeftPixels,
RandomizeRightPixels, RandomizeRightPixels,
@ -125,56 +139,60 @@ enum AppEvent {
Decelerate, Decelerate,
} }
fn parse_event(event: Event) -> AppEvent { impl TryFrom<Event> for AppEvent {
match event { type Error = ();
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
match key_event.code { fn try_from(event: Event) -> Result<Self, Self::Error> {
KeyCode::Char('h') => { match event {
println_info("[h] help"); Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
println_info("[q] quit"); match key_event.code {
println_info("[d] randomize left pixels"); KeyCode::Char('h') => {
println_info("[e] randomize left luma"); println_info("[h] help");
println_info("[r] randomize right pixels"); println_info("[q] quit");
println_info("[f] randomize right luma"); println_info("[d] randomize left pixels");
println_info("[→] move divider right"); println_info("[e] randomize left luma");
println_info("[←] move divider left"); println_info("[r] randomize right pixels");
AppEvent::None println_info("[f] randomize right luma");
} println_info("[→] accelerate divider right");
KeyCode::Char('q') => { println_info("[←] accelerate divider left");
println_warning("terminating"); Err(())
AppEvent::Close }
} KeyCode::Char('q') => {
KeyCode::Char('d') => { println_warning("terminating");
println_debug("randomizing left pixels"); Ok(AppEvent::Close)
AppEvent::RandomizeLeftPixels }
} KeyCode::Char('d') => {
KeyCode::Char('e') => { println_debug("randomizing left pixels");
println_info("randomizing left luma"); Ok(AppEvent::RandomizeLeftPixels)
AppEvent::RandomizeLeftLuma }
} KeyCode::Char('e') => {
KeyCode::Char('f') => { println_info("randomizing left luma");
println_info("randomizing right pixels"); Ok(AppEvent::RandomizeLeftLuma)
AppEvent::RandomizeRightPixels }
} KeyCode::Char('f') => {
KeyCode::Char('r') => { println_info("randomizing right pixels");
println_info("randomizing right luma"); Ok(AppEvent::RandomizeRightPixels)
AppEvent::RandomizeRightLuma }
} KeyCode::Char('r') => {
KeyCode::Right => { println_info("randomizing right luma");
AppEvent::Accelerate Ok(AppEvent::RandomizeRightLuma)
} }
KeyCode::Left => { KeyCode::Right => {
AppEvent::Decelerate Ok(AppEvent::Accelerate)
} }
key_code => { KeyCode::Left => {
println_debug(format!("unhandled KeyCode {key_code:?}")); Ok(AppEvent::Decelerate)
AppEvent::None }
key_code => {
println_debug(format!("unhandled KeyCode {key_code:?}"));
Err(())
}
} }
} }
} event => {
event => { println_debug(format!("unhandled event {event:?}"));
println_debug(format!("unhandled event {event:?}")); Err(())
AppEvent::None }
} }
} }
} }
@ -196,7 +214,7 @@ fn draw_luma(luma: &mut ByteGrid, left: &ByteGrid, right: &ByteGrid, split_tile:
let set = if x == split_tile { let set = if x == split_tile {
255 255
} else { } else {
left_or_right.get(x, y) u8::max(48, left_or_right.get(x, y))
}; };
luma.set(x, y, set); luma.set(x, y, set);
} }
@ -233,14 +251,18 @@ fn init() -> Connection {
.parse_default_env() .parse_default_env()
.init(); .init();
execute!(stdout(), EnterAlternateScreen, EnableLineWrap).expect("could not enter alternate screen"); execute!(stdout(), EnterAlternateScreen, EnableLineWrap)
enable_raw_mode().expect("could not enable raw terminal mode"); .expect("could not enter alternate screen");
enable_raw_mode()
.expect("could not enable raw terminal mode");
Connection::open(Cli::parse().destination) Connection::open(Cli::parse().destination)
.expect("Could not connect. Did you forget `--destination`?") .expect("Could not connect. Did you forget `--destination`?")
} }
fn de_init() { fn de_init() {
disable_raw_mode().expect("could not disable raw terminal mode"); disable_raw_mode()
execute!(stdout(), LeaveAlternateScreen).expect("could not leave alternate screen"); .expect("could not disable raw terminal mode");
execute!(stdout(), LeaveAlternateScreen)
.expect("could not leave alternate screen");
} }