exponentialgrowingboundschecker/
exponentialgrowingboundschecker.rs1use std::io;
4use std::io::stdout;
5use std::time::Instant;
6use teng::components::Component;
7use teng::rendering::pixel::Pixel;
8use teng::rendering::renderer::Renderer;
9use teng::util::planarvec::Bounds;
10use teng::util::planarvec2_experimental::ExponentialGrowingBounds;
11use teng::{
12 install_panic_handler, terminal_cleanup, terminal_setup, Game, SetupInfo, SharedState,
13 UpdateInfo,
14};
15
16fn main() -> io::Result<()> {
17 terminal_setup()?;
18 install_panic_handler();
19
20 let mut game = Game::new(stdout());
21 game.install_recommended_components();
22 game.add_component(Box::new(ExponentialBoundsCheckerComponent::new()));
23 game.run()?;
24
25 terminal_cleanup()?;
26
27 Ok(())
28}
29
30pub struct ExponentialBoundsCheckerComponent {
31 exp_bounds: ExponentialGrowingBounds,
32 screen_width: usize,
33 screen_height: usize,
34}
35
36impl ExponentialBoundsCheckerComponent {
37 pub fn new() -> Self {
38 Self {
39 exp_bounds: ExponentialGrowingBounds::new(),
40 screen_width: 0,
41 screen_height: 0,
42 }
43 }
44
45 fn center_screen(&self) -> (usize, usize) {
46 (self.screen_width / 2, self.screen_height / 2)
47 }
48
49 fn screen_coords_to_bounds_coords(&self, x: usize, y: usize) -> (i64, i64) {
50 let (center_x, center_y) = self.center_screen();
51 let x = x as i64 - center_x as i64;
52 let y = y as i64 - center_y as i64;
53 (x, y)
54 }
55
56 fn bounds_coords_to_screen_coords(&self, x: i64, y: i64) -> (usize, usize) {
57 let (center_x, center_y) = self.center_screen();
58 let x = (x + center_x as i64) as usize;
59 let y = (y + center_y as i64) as usize;
60 (x, y)
61 }
62}
63
64impl Component for ExponentialBoundsCheckerComponent {
65 fn setup(&mut self, setup_info: &SetupInfo, shared_state: &mut SharedState<()>) {
66 self.on_resize(
67 setup_info.display_info.width(),
68 setup_info.display_info.height(),
69 shared_state,
70 );
71 }
72
73 fn on_resize(&mut self, width: usize, height: usize, shared_state: &mut SharedState<()>) {
74 self.screen_width = width;
75 self.screen_height = height;
76 }
77
78 fn update(&mut self, _update_info: UpdateInfo, shared_state: &mut SharedState) {
79 if shared_state.mouse_info.left_mouse_down {
80 let (x, y) = shared_state.mouse_info.last_mouse_pos;
81 let (x, y) = self.screen_coords_to_bounds_coords(x, y);
82 self.exp_bounds.grow_to_contain((x, y));
83 }
84
85 if shared_state.mouse_pressed.right {
86 self.exp_bounds = ExponentialGrowingBounds::new();
87 }
88 }
89
90 fn render(&self, renderer: &mut dyn Renderer, shared_state: &SharedState, depth_base: i32) {
91 for b_x in self.exp_bounds.min_x()..=self.exp_bounds.max_x() {
94 for b_y in self.exp_bounds.min_y()..=self.exp_bounds.max_y() {
95 let (x, y) = self.bounds_coords_to_screen_coords(b_x, b_y);
96 renderer.render_pixel(x, y, Pixel::new('█'), depth_base);
97 }
98 }
99 }
100}