rust/lib-hedgewars-engine/src/render/camera.rs
author alfadur
Thu, 26 Sep 2019 22:31:06 +0300
changeset 15446 a6699f4a3d16
parent 15141 febccab419b1
permissions -rw-r--r--
fix typo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
     1
use integral_geometry::{Point, Rect, Size};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
     2
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
     3
#[derive(Debug)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
     4
pub struct Camera {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
     5
    pub position: Point,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
     6
    pub zoom: f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
     7
    size: Size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
     8
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
     9
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    10
impl Camera {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    11
    pub fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    12
        Self::with_size(Size::new(1024, 768))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    13
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    14
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    15
    pub fn with_size(size: Size) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    16
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    17
            position: Point::ZERO,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    18
            zoom: 1.0,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    19
            size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    20
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    21
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    22
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    23
    pub fn viewport(&self) -> Rect {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    24
        #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    25
        fn scale(value: usize, zoom: f32) -> i32 {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    26
            (value as f32 / zoom / 2.0) as i32
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    27
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    28
        let half_width = scale(self.size.width, self.zoom);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    29
        let half_height = scale(self.size.height, self.zoom);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    30
        Rect::from_box(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    31
            self.position.x - half_width,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    32
            self.position.x + half_width,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    33
            self.position.y - half_height,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    34
            self.position.y + half_height,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    35
        )
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    36
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    37
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    38
    pub fn projection(&self) -> [f32; 16] {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    39
        let viewport = self.viewport();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    40
        let left = viewport.left() as f32;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    41
        let width = viewport.width() as f32;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    42
        let height = viewport.height() as f32;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    43
        let top = viewport.top() as f32;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    44
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    45
        [
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    46
            2f32 / width,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    47
            0f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    48
            0f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    49
            0f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    50
            0f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    51
            2f32 / -height,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    52
            0f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    53
            0f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    54
            0f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    55
            0f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    56
            0.5f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    57
            0f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    58
            -(2.0 * left + width) / width,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    59
            (2.0 * top + height) / height,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    60
            0.5f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    61
            1f32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    62
        ]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    63
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14739
diff changeset
    64
}