pub struct Camera {
    pub center: Vec2<f32>,
    pub rotation: f32,
    pub scale: Vec2<f32>,
    /* private fields */
}
Expand description

A simple 2D camera that can translate, rotate, and scale everything on the screen.

Fields§

§center: Vec2<f32>

The center of the camera becomes the center of the screen

§rotation: f32

needs to be in degrees

§scale: Vec2<f32>

This controls the size of every object in view

Implementations§

source§

impl Camera

source

pub fn new(engine: &Engine) -> Self

creates a new camera with these values:

Camera {
    center: Vec2{x : 0.0, y: 0.0},
    rotation: 0.0,
    scale: Vec2{x: 1.0, y: 1.0},
}
Examples found in repository?
examples/camera.rs (line 21)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    let mut engine = EngineBuilder::new().build().unwrap();

    let texture = Texture::new(&mut engine, "examples/bplogo.png");

    let material = MaterialBuilder::new()
        .add_texture(texture)
        .build(&mut engine);
    
    let camera = Camera::new(&engine);

    let text = TextMaterial::new("Mouse pos: 0,0 \n Mouse pos: 0, 0", Colour::WHITE, 15.0, 20.0, &mut engine);

    let game = CameraExample {
        material,
        text,
        camera,
    };

    engine.run(game);
}
source

pub fn transform_point( &self, point: Vec2<f32>, screen_size: Vec2<u32> ) -> Vec2<f32>

This will transform a point in screen space to camera space. You can get the screen size from Engine::get_window_size

Examples found in repository?
examples/camera.rs (line 100)
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    fn update(&mut self, engine_handle: &mut Engine) {
        let dt = engine_handle.get_frame_delta_time();
        let mouse_pos = engine_handle.get_mouse_position();
        let size = engine_handle.get_window_size();

        let move_factor = 15.0;

        if engine_handle.is_key_down(Key::A) {
            self.camera.center.x -= move_factor * dt;
        }

        if engine_handle.is_key_down(Key::D) {
            self.camera.center.x += move_factor * dt;
        }

        if engine_handle.is_key_down(Key::W) {
            self.camera.center.y += move_factor * dt;
        }

        if engine_handle.is_key_down(Key::S) {
            self.camera.center.y -= move_factor * dt;
        }

        if engine_handle.is_key_down(Key::Left) {
            self.camera.rotation += move_factor * dt;
        }

        if engine_handle.is_key_down(Key::Right) {
            self.camera.rotation -= move_factor * dt;
        }

        if engine_handle.is_key_down(Key::L) {
            self.camera.scale += vec2!(2.0 * dt, 2.0 * dt);
        }

        if engine_handle.is_key_down(Key::K) {
            self.camera.scale -= vec2!(2.0 * dt, 2.0 * dt);
        }

        if engine_handle.is_key_pressed(Key::Enter) {
            self.camera.rotation += 45.0;
        }

        let trans_mouse = self.camera.transform_point(mouse_pos, size);

        self.text.set_text(
            &format!("Screen mouse pos: {:.3}, {:.3}\nWorld mouse pos: {:.3}, {:.3}", mouse_pos.x, mouse_pos.y, trans_mouse.x, trans_mouse.y),
            Colour::WHITE,
            engine_handle,
        );

        self.text.prepare(engine_handle);
    }
source

pub fn set_active<'others, 'pass>( &'others self, renderer: &mut RenderInformation<'pass, 'others> )
where 'others: 'pass,

Sets this camera to the active camera transforming all objects with this camera.

Examples found in repository?
examples/camera.rs (line 49)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    fn render<'pass, 'others>(
        &'others mut self,
        mut render_handle: RenderInformation<'pass, 'others>,
    ) where
        'others: 'pass,
    {
        self.material.add_rectangle(Vec2 { x: 0.0, y: 0.0 }, Vec2{x: 300.0, y: 300.0}, Colour::WHITE, &render_handle);

        self.camera.set_active(&mut render_handle);
        self.material.draw(&mut render_handle);

        render_handle.reset_camera();
        self.text.add_instance(vec2!(0.0), Colour::WHITE, &render_handle);
        self.text.draw(&mut render_handle);
    }

Auto Trait Implementations§

§

impl !RefUnwindSafe for Camera

§

impl Send for Camera

§

impl Sync for Camera

§

impl Unpin for Camera

§

impl !UnwindSafe for Camera

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast<T> for T

source§

fn downcast(&self) -> &T

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Upcast<T> for T

source§

fn upcast(&self) -> Option<&T>

source§

impl<T> WasmNotSend for T
where T: Send,

source§

impl<T> WasmNotSync for T
where T: Sync,