rust/lib-hedgewars-engine/src/render/camera.rs
author alfadur
Fri, 22 Mar 2019 21:44:06 +0300
changeset 14730 65c971417780
parent 14729 19358c313ebb
child 14739 5915a199cb81
permissions -rw-r--r--
fix zoom
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14725
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
     1
use integral_geometry::{Point, Rect, Size};
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
     2
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
     3
#[derive(Debug)]
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
     4
pub struct Camera {
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
     5
    pub position: Point,
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
     6
    pub zoom: f32,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
     7
    size: Size,
14725
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
     8
}
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
     9
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
    10
impl Camera {
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
    11
    pub fn new() -> Self {
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    12
        Self::with_size(Size::new(1024, 768))
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    13
    }
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    14
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    15
    pub fn with_size(size: Size) -> Self {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    16
        Self {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    17
            position: Point::ZERO,
14729
19358c313ebb fix texture initialization
alfadur
parents: 14726
diff changeset
    18
            zoom: 1.0,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    19
            size,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    20
        }
14725
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
    21
    }
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
    22
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
    23
    pub fn viewport(&self) -> Rect {
14730
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    24
        #[inline]
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    25
        fn scale(value: usize, zoom: f32) -> i32 {
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    26
            (value as f32 / zoom / 2.0) as i32
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    27
        }
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    28
        let half_width = scale(self.size.width, self.zoom);
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    29
        let half_height = scale(self.size.height, self.zoom);
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    30
        Rect::from_box(
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    31
            self.position.x - half_width,
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    32
            self.position.x + half_width,
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    33
            self.position.y - half_height,
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    34
            self.position.y + half_height,
65c971417780 fix zoom
alfadur
parents: 14729
diff changeset
    35
        )
14725
12db7e435ea6 move camera into engine
alfadur
parents:
diff changeset
    36
    }
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    37
}