loop split when reaching border
This commit is contained in:
		
							parent
							
								
									2799341651
								
							
						
					
					
						commit
						22faceab57
					
				
					 1 changed files with 89 additions and 67 deletions
				
			
		
							
								
								
									
										86
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										86
									
								
								src/main.rs
									
										
									
									
									
								
							|  | @ -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,7 +139,10 @@ enum AppEvent { | ||||||
|     Decelerate, |     Decelerate, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn parse_event(event: Event) -> AppEvent { | impl TryFrom<Event> for AppEvent { | ||||||
|  |     type Error = (); | ||||||
|  | 
 | ||||||
|  |     fn try_from(event: Event) -> Result<Self, Self::Error> { | ||||||
|         match event { |         match event { | ||||||
|             Event::Key(key_event) if key_event.kind == KeyEventKind::Press => { |             Event::Key(key_event) if key_event.kind == KeyEventKind::Press => { | ||||||
|                 match key_event.code { |                 match key_event.code { | ||||||
|  | @ -136,45 +153,46 @@ fn parse_event(event: Event) -> AppEvent { | ||||||
|                         println_info("[e] randomize left luma"); |                         println_info("[e] randomize left luma"); | ||||||
|                         println_info("[r] randomize right pixels"); |                         println_info("[r] randomize right pixels"); | ||||||
|                         println_info("[f] randomize right luma"); |                         println_info("[f] randomize right luma"); | ||||||
|                     println_info("[→] move divider right"); |                         println_info("[→] accelerate divider right"); | ||||||
|                     println_info("[←] move divider left"); |                         println_info("[←] accelerate divider left"); | ||||||
|                     AppEvent::None |                         Err(()) | ||||||
|                     } |                     } | ||||||
|                     KeyCode::Char('q') => { |                     KeyCode::Char('q') => { | ||||||
|                         println_warning("terminating"); |                         println_warning("terminating"); | ||||||
|                     AppEvent::Close |                         Ok(AppEvent::Close) | ||||||
|                     } |                     } | ||||||
|                     KeyCode::Char('d') => { |                     KeyCode::Char('d') => { | ||||||
|                         println_debug("randomizing left pixels"); |                         println_debug("randomizing left pixels"); | ||||||
|                     AppEvent::RandomizeLeftPixels |                         Ok(AppEvent::RandomizeLeftPixels) | ||||||
|                     } |                     } | ||||||
|                     KeyCode::Char('e') => { |                     KeyCode::Char('e') => { | ||||||
|                         println_info("randomizing left luma"); |                         println_info("randomizing left luma"); | ||||||
|                     AppEvent::RandomizeLeftLuma |                         Ok(AppEvent::RandomizeLeftLuma) | ||||||
|                     } |                     } | ||||||
|                     KeyCode::Char('f') => { |                     KeyCode::Char('f') => { | ||||||
|                         println_info("randomizing right pixels"); |                         println_info("randomizing right pixels"); | ||||||
|                     AppEvent::RandomizeRightPixels |                         Ok(AppEvent::RandomizeRightPixels) | ||||||
|                     } |                     } | ||||||
|                     KeyCode::Char('r') => { |                     KeyCode::Char('r') => { | ||||||
|                         println_info("randomizing right luma"); |                         println_info("randomizing right luma"); | ||||||
|                     AppEvent::RandomizeRightLuma |                         Ok(AppEvent::RandomizeRightLuma) | ||||||
|                     } |                     } | ||||||
|                     KeyCode::Right => { |                     KeyCode::Right => { | ||||||
|                     AppEvent::Accelerate |                         Ok(AppEvent::Accelerate) | ||||||
|                     } |                     } | ||||||
|                     KeyCode::Left => { |                     KeyCode::Left => { | ||||||
|                     AppEvent::Decelerate |                         Ok(AppEvent::Decelerate) | ||||||
|                     } |                     } | ||||||
|                     key_code => { |                     key_code => { | ||||||
|                         println_debug(format!("unhandled KeyCode {key_code:?}")); |                         println_debug(format!("unhandled KeyCode {key_code:?}")); | ||||||
|                     AppEvent::None |                         Err(()) | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             event => { |             event => { | ||||||
|                 println_debug(format!("unhandled event {event:?}")); |                 println_debug(format!("unhandled event {event:?}")); | ||||||
|             AppEvent::None |                 Err(()) | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -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"); | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter