Skip to main content

sim_logic/rendering/extraction/
images.rs

1//! CPU-only image records and application-provenance preflight.
2
3use sim_engine::{Color, Layer, LogicalScreenPosition, LogicalScreenVector};
4
5use crate::{
6    assets::{ImageAssetId, ImageAssetRegistry},
7    identity::{LogicEntity, WorldGeneration},
8    screen::{ImageFilter, ImageRegion, ScreenImageVisual},
9};
10
11use super::super::ExtractionError;
12
13/// One immutable screen-image description in a published CPU snapshot.
14///
15/// Positions are current logical pixels, independent of camera interpolation.
16/// The registry owns its source pixels; this record never holds a GPU handle.
17#[derive(Debug, Clone, Copy, PartialEq)]
18pub struct ResolvedScreenImage {
19    source: LogicEntity,
20    visual: ScreenImageVisual,
21}
22
23impl ResolvedScreenImage {
24    /// Returns clockwise center rotation in radians, independent of the World camera.
25    pub const fn rotation(self) -> f32 {
26        self.visual.rotation()
27    }
28    /// Returns the sampled fixed-screen clip, including an explicit empty scope.
29    pub const fn clip(self) -> crate::screen::ScreenClip {
30        self.visual.clip()
31    }
32    /// Returns the managed entity that produced this image.
33    pub const fn source(self) -> LogicEntity {
34        self.source
35    }
36    /// Returns the immutable application-registered image identity.
37    pub const fn image(self) -> ImageAssetId {
38        self.visual.image()
39    }
40    /// Returns logical screen top-left, with y increasing downward.
41    pub const fn position(self) -> LogicalScreenPosition {
42        self.visual.position()
43    }
44    /// Returns strictly positive logical width and height.
45    pub const fn size(self) -> LogicalScreenVector {
46        self.visual.size()
47    }
48    /// Returns normalized straight-linear RGBA multiplied with decoded pixels.
49    pub const fn tint(self) -> Color {
50        self.visual.tint()
51    }
52    /// Returns the optional source texel region; None means the complete image.
53    pub const fn source_region(self) -> Option<ImageRegion> {
54        self.visual.source_region()
55    }
56    /// Returns nearest or linear texture sampling.
57    pub const fn filter(self) -> ImageFilter {
58        self.visual.filter()
59    }
60    /// Returns the primary mixed-screen ordering layer.
61    pub const fn layer(self) -> Layer {
62        self.visual.layer()
63    }
64    /// Returns finite mixed-screen ordering depth, not geometric camera depth.
65    pub const fn draw_order_depth(self) -> f32 {
66        self.visual.draw_order_depth()
67    }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq)]
71pub(crate) struct ScreenImageSource {
72    entity: LogicEntity,
73    visual: ScreenImageVisual,
74    registered: bool,
75}
76
77impl ScreenImageSource {
78    pub(crate) fn new(
79        entity: LogicEntity,
80        visual: &ScreenImageVisual,
81        registry: &ImageAssetRegistry,
82    ) -> Self {
83        Self {
84            entity,
85            visual: *visual,
86            registered: registry.get(visual.image()).is_some(),
87        }
88    }
89
90    pub(super) fn resolve(
91        self,
92        generation: WorldGeneration,
93    ) -> Result<ResolvedScreenImage, ExtractionError> {
94        if self.entity.world_generation() != generation {
95            return Err(ExtractionError::ForeignEntity {
96                entity: self.entity,
97                expected: generation,
98            });
99        }
100        if !self.registered {
101            return Err(ExtractionError::UnregisteredImageAsset {
102                entity: self.entity,
103                image: self.visual.image(),
104            });
105        }
106        self.visual
107            .validate()
108            .map_err(|error| ExtractionError::InvalidScreenImage {
109                entity: self.entity,
110                error,
111            })?;
112        Ok(ResolvedScreenImage {
113            source: self.entity,
114            visual: self.visual,
115        })
116    }
117}