gemini_engine::primitives

Struct Rect

Source
pub struct Rect {
    pub pos: Vec2D,
    pub size: Vec2D,
    pub fill_char: ColChar,
}
Expand description

A rectangle primitive which implements CanDraw, and so can be drawn to Canvases

Fields§

§pos: Vec2D

The position of the top-left corner of the Rect

§size: Vec2D

The size of the Rect, extending from pos

§fill_char: ColChar

The ColChar used to fill the rectangle

Implementations§

Source§

impl Rect

Source

pub const fn new(pos: Vec2D, size: Vec2D, fill_char: ColChar) -> Self

Create a new Rect using a position and size

Examples found in repository?
examples/multi-movement.rs (line 19)
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
fn main() {
    let mut view = View::new(50, 12, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);

    let mut blocks = vec![
        Rect::new(Vec2D::new(0, 0), BLOCK_SIZE, FILL_CHAR),
        Rect::new(Vec2D::new(0, 2), BLOCK_SIZE, FILL_CHAR),
        Rect::new(Vec2D::new(0, 4), BLOCK_SIZE, FILL_CHAR),
        Rect::new(Vec2D::new(0, 6), BLOCK_SIZE, FILL_CHAR),
        Rect::new(Vec2D::new(0, 8), BLOCK_SIZE, FILL_CHAR),
        Rect::new(Vec2D::new(0, 10), BLOCK_SIZE, FILL_CHAR),
    ];

    let mut i = 0;
    fps_gameloop!(
        {
            i += 1;
            for (j, block) in (0u32..).zip(blocks.iter_mut()) {
                if i % 2_u32.pow(j) == 0 {
                    block.pos.x += 1;
                }
            }
        },
        {
            view.clear();
            for block in &blocks {
                view.draw(block);
            }
            let _ = view.display_render();

            if blocks.iter().all(|b| b.pos.x % view.width as i64 == 0) {
                thread::sleep(Duration::from_secs(2));
            };
        },
        60.0
    );
}
More examples
Hide additional examples
examples/complex-scene.rs (lines 23-27)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
fn main() {
    let mut view = View::new(60, 10, BACKGROUND_CHAR);

    let mut pixel = Pixel::new(Vec2D::new(5, 9), FILL_CHAR);

    let mut line = Line::new(Vec2D::new(2, 8), Vec2D::new(28, 7), FILL_CHAR);
    let mut line1_direction = -1;

    let rect = Rect::new(
        Vec2D { x: 11, y: 1 },
        Vec2D { x: 9, y: 3 },
        ColChar::SOLID.with_rgb(200, 30, 0),
    );

    let test_image = r"
  ______
 /|_||_\`.__
(   _    _ _\
=`-(_)--(_)-'   ";
    let mut sprite = Sprite::new(
        Vec2D::new(30, 1),
        test_image,
        Modifier::from_rgb(20, 200, 0),
    );

    let mut draw_elapsed = Duration::default();
    let mut render_elapsed = Duration::default();
    fps_gameloop!(
        {
            pixel.pos.x += 2;
            // loop the position back to the other side. This can be done with `Wrapping::Wrap` but it won't change the element's actual position, so the pixel position being printed would continue to increase without looping
            pixel.pos %= view.size();

            line.pos1.y += line1_direction;
            line.pos0.y = 10 - line.pos1.y;
            if line.pos1.y > 7 {
                line1_direction = -1;
            } else if line.pos1.y < 3 {
                line1_direction = 1;
            }

            sprite.pos.x += 1;
        },
        {
            view.clear();

            let now = Instant::now();
            view.wrapping_mode = WrappingMode::Panic;
            view.draw(&pixel);
            view.draw(&line);
            view.draw(&rect);
            view.wrapping_mode = WrappingMode::Wrap;
            view.draw(&sprite);
            draw_elapsed = now.elapsed();

            let now = Instant::now();
            let _ = view.display_render();
            render_elapsed = now.elapsed();
        },
        FPS,
        |total_elapsed: Duration, _frame_skip| {
            println!(
                "Drawing: {:.2?} microseconds | Rendering: {:.2?} microseconds| Total: {:.2?}",
                draw_elapsed.as_micros(),
                render_elapsed.as_micros(),
                total_elapsed.as_micros()
            );
            println!("Pixel position: {}", pixel.pos);
        }
    );
}
Source

pub fn new_from_to( top_left: Vec2D, bottom_right: Vec2D, fill_char: ColChar, ) -> Self

Create a new Rect using two positions

Source

pub fn bottom_right(&self) -> Vec2D

Return the coordinates of the bottom right point

Trait Implementations§

Source§

impl CanCollide for Rect

Source§

fn collides_with_pos(&self, pos: Vec2D) -> bool

Returns true if the collider intersects the passed position
Source§

impl CanDraw for Rect

Source§

fn draw_to(&self, canvas: &mut impl Canvas)

Draw the element to a struct that implements CanDraw

Auto Trait Implementations§

§

impl Freeze for Rect

§

impl RefUnwindSafe for Rect

§

impl Send for Rect

§

impl Sync for Rect

§

impl Unpin for Rect

§

impl UnwindSafe for Rect

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> 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>,

Source§

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>,

Source§

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.