1use std::io;
2use std::io::stdout;
3use std::time::Instant;
4use teng::components::Component;
5use teng::rendering::pixel::Pixel;
6use teng::rendering::renderer::Renderer;
7use teng::{
8 install_panic_handler, terminal_cleanup, terminal_setup, Game, SharedState, UpdateInfo,
9};
10
11fn main() -> io::Result<()> {
12 terminal_setup()?;
13 install_panic_handler();
14
15 let mut game = Game::new(stdout());
16 game.install_recommended_components();
17 game.add_component(Box::new(FpsCheckerFrameCountComponent::new()));
19 game.run()?;
20
21 terminal_cleanup()?;
22
23 Ok(())
24}
25
26pub struct FpsCheckerFrameTimeComponent {
28 start_time: Instant,
29}
30
31impl FpsCheckerFrameTimeComponent {
32 pub fn new() -> Self {
33 Self {
34 start_time: Instant::now(),
35 }
36 }
37}
38
39impl Component for FpsCheckerFrameTimeComponent {
40 fn render(&self, renderer: &mut dyn Renderer, shared_state: &SharedState, depth_base: i32) {
41 let elapsed = Instant::now() - self.start_time;
43 let time_per_frame = 1.0 / 144.0;
44 let frame = (elapsed.as_secs_f64() / time_per_frame) as usize;
45 let y = shared_state.display_info.height() / 2;
46 let x = frame % (shared_state.display_info.width() * 2);
48 let pixel = Pixel::new('█');
49 if x < shared_state.display_info.width() {
50 renderer.render_pixel(x, y, pixel, i32::MAX);
51 } else {
52 renderer.render_pixel(
53 shared_state.display_info.width() * 2 - x - 1,
54 y,
55 pixel,
56 i32::MAX,
57 );
58 }
59 }
60}
61
62pub struct FpsCheckerFrameCountComponent {
64 count: usize,
65}
66
67impl FpsCheckerFrameCountComponent {
68 pub fn new() -> Self {
69 Self { count: 0 }
70 }
71}
72
73impl Component for FpsCheckerFrameCountComponent {
74 fn update(&mut self, update_info: UpdateInfo, shared_state: &mut SharedState) {
75 self.count += 1;
76 }
77
78 fn render(&self, renderer: &mut dyn Renderer, shared_state: &SharedState, depth_base: i32) {
79 let y = shared_state.display_info.height() / 2;
80 let x = self.count % (shared_state.display_info.width() * 2);
82 let pixel = Pixel::new('█');
83 if x < shared_state.display_info.width() {
84 renderer.render_pixel(x, y, pixel, i32::MAX);
85 } else {
86 renderer.render_pixel(
87 shared_state.display_info.width() * 2 - x - 1,
88 y,
89 pixel,
90 i32::MAX,
91 );
92 }
93 }
94}