pub struct Rect {
pub pos: Vec2D,
pub size: Vec2D,
pub fill_char: ColChar,
}
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
impl Rect
Sourcepub const fn new(pos: Vec2D, size: Vec2D, fill_char: ColChar) -> Self
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 18)
14fn main() {
15 let mut view = View::new(50, 12, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
16
17 let mut blocks: Vec<Rect> = (0..=5)
18 .map(|offset| Rect::new(Vec2D::new(0, offset * 2), BLOCK_SIZE, FILL_CHAR))
19 .collect();
20
21 let mut i = 0;
22 loop {
23 if blocks.iter().all(|b| b.pos.x % view.width as i64 == 0) {
24 thread::sleep(Duration::from_secs(2));
25 };
26
27 i += 1;
28 for (j, block) in (0u32..).zip(blocks.iter_mut()) {
29 if i % 2_u32.pow(j) == 0 {
30 block.pos.x += 1;
31 }
32 }
33
34 view.clear();
35 for block in &blocks {
36 view.draw(block);
37 }
38 let _ = view.display_render();
39
40 thread::sleep(Duration::from_secs_f32(1.0 / 60.0));
41 }
42}
More examples
examples/complex-scene.rs (lines 23-27)
15fn main() {
16 let mut view = View::new(60, 10, BACKGROUND_CHAR);
17
18 let mut pixel = Pixel::new(Vec2D::new(5, 9), FILL_CHAR);
19
20 let mut line = Line::new(Vec2D::new(2, 8), Vec2D::new(28, 7), FILL_CHAR);
21 let mut line1_direction = -1;
22
23 let rect = Rect::new(
24 Vec2D { x: 11, y: 1 },
25 Vec2D { x: 9, y: 3 },
26 ColChar::SOLID.with_rgb(200, 30, 0),
27 );
28
29 let test_image = r"
30 ______
31 /|_||_\`.__
32( _ _ _\
33=`-(_)--(_)-' ";
34 let mut sprite = Sprite::new(
35 Vec2D::new(30, 1),
36 test_image,
37 Modifier::from_rgb(20, 200, 0),
38 );
39
40 let mut draw_elapsed = Duration::default();
41 let mut render_elapsed = Duration::default();
42 fps_gameloop!(
43 {
44 pixel.pos.x += 2;
45 // loop the position back to the other side. This can be done with `WrappingMode::Wrap` but it won't change the element's actual position, so the pixel position being printed would continue to increase without looping
46 pixel.pos %= view.size();
47
48 line.pos1.y += line1_direction;
49 line.pos0.y = 10 - line.pos1.y;
50 if line.pos1.y > 7 {
51 line1_direction = -1;
52 } else if line.pos1.y < 3 {
53 line1_direction = 1;
54 }
55
56 sprite.pos.x += 1;
57 },
58 {
59 view.clear();
60
61 let now = Instant::now();
62 view.wrapping_mode = WrappingMode::Panic;
63 view.draw(&pixel);
64 view.draw(&line);
65 view.draw(&rect);
66 view.wrapping_mode = WrappingMode::Wrap;
67 view.draw(&sprite);
68 draw_elapsed = now.elapsed();
69
70 let now = Instant::now();
71 let _ = view.display_render();
72 render_elapsed = now.elapsed();
73 },
74 FPS,
75 |total_elapsed: Duration, _frame_skip| {
76 println!(
77 "Drawing: {:.2?} microseconds | Rendering: {:.2?} microseconds| Total: {:.2?}",
78 draw_elapsed.as_micros(),
79 render_elapsed.as_micros(),
80 total_elapsed.as_micros()
81 );
82 println!("Pixel position: {}", pixel.pos);
83 }
84 );
85}
Sourcepub fn new_from_to(
top_left: Vec2D,
bottom_right: Vec2D,
fill_char: ColChar,
) -> Self
pub fn new_from_to( top_left: Vec2D, bottom_right: Vec2D, fill_char: ColChar, ) -> Self
Create a new Rect
using two positions
Sourcepub fn bottom_right(&self) -> Vec2D
pub fn bottom_right(&self) -> Vec2D
Return the coordinates of the bottom right point
Trait Implementations§
Source§impl CanCollide for Rect
impl CanCollide for Rect
Source§fn collides_with_pos(&self, pos: Vec2D) -> bool
fn collides_with_pos(&self, pos: Vec2D) -> bool
Returns
true
if the collider intersects the passed positionAuto 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more