aboutsummaryrefslogtreecommitdiff
path: root/src/display/components.rs
blob: 1deb6f5117094ef5cad93e3ee07337e4ee400553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use bevy::{prelude::*, render::render_resource::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages}};
use bevy_dither_post_process::components::DitherPostProcessSettings;
use bevy_headless_render::{components::{HeadlessRenderBundle, HeadlessRenderDestination}, render_assets::HeadlessRenderSource};

/// Marker component for terminal display
#[derive(Component)]
pub struct TerminalDisplay;

/// Bundle for terminal display, contains a handle to an image to be used as a render target to
/// render to the terminal
#[derive(Bundle)]
pub struct TerminalDisplayBundle {
    _terminal_display: TerminalDisplay,
    _headless_render_bundle: HeadlessRenderBundle,
    _dither_post_process_settings: DitherPostProcessSettings,
    image_handle: Handle<Image>,
}

impl TerminalDisplayBundle {
    /// Create a new terminal display with the given dither level. A higher level exponentially
    /// increases the size of the bayer matrix used in the ordered dithering calculations. If in
    /// doubt, 3 is a good starting value to test with.
    pub fn new(dither_level: u32, asset_server: &AssetServer) -> Self {
        let terminal_size = crossterm::terminal::size().unwrap();
        let size = Extent3d {
            width: (terminal_size.0 as u32) * 2,
            height: (terminal_size.1 as u32) * 4,
            depth_or_array_layers: 1,
        };

        let mut image = Image {
            texture_descriptor: TextureDescriptor {
                label: None,
                size,
                dimension: TextureDimension::D2,
                format: TextureFormat::R8Unorm,
                mip_level_count: 1,
                sample_count: 1,
                usage: TextureUsages::TEXTURE_BINDING
                    | TextureUsages::COPY_SRC
                    | TextureUsages::RENDER_ATTACHMENT,
                view_formats: &[],
            },
            ..default()
        };

        image.resize(size);
        let image_handle = asset_server.add(image);

        let framebuffer_extract_source =
            asset_server.add(HeadlessRenderSource(image_handle.clone()));

        Self {
            _terminal_display: TerminalDisplay,
            _headless_render_bundle: HeadlessRenderBundle {
                source: framebuffer_extract_source,
                dest: HeadlessRenderDestination::default(),
            },
            image_handle,
            _dither_post_process_settings: DitherPostProcessSettings::new(
                dither_level,
                asset_server,
            ),
        }
    }

    /// Retrieves the handle to this display's target image. Anything written here will be
    /// displayed.
    pub fn image_handle(&self) -> Handle<Image> {
        self.image_handle.clone()
    }
}