#[non_exhaustive]pub struct Sprite {
pub pos: Vec2D,
pub texture: String,
pub modifier: Modifier,
pub align: TextAlign2D,
}
Expand description
The Sprite
takes a multi-line string as a parameter, and can be used to draw ASCII art to a Canvas
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.pos: Vec2D
The position from which the sprite will be drawn from
texture: String
The ACII texture (pun intended) displayed by the Sprite
modifier: Modifier
A raw Modifier
, determining the appearance of the Sprite
align: TextAlign2D
How the Sprite should align to the position
Implementations§
Source§impl Sprite
impl Sprite
Sourcepub fn new(pos: Vec2D, texture: &str, modifier: Modifier) -> Self
pub fn new(pos: Vec2D, texture: &str, modifier: Modifier) -> Self
Create a new Sprite
struct. All newlines at the beginning of the texture will be removed
Examples found in repository?
examples/self-resizing.rs (line 23)
16fn main() {
17 let mut scale_view = ScaleFitView::new(ColChar::BACKGROUND);
18
19 let mut text = Text::new(Vec2D::ZERO, "This is some centered text!", Modifier::None)
20 .with_align(TextAlign::Centered);
21
22 let mut sprite =
23 Sprite::new(Vec2D::ZERO, TEXTURE, Modifier::None).with_align(TextAlign2D::CENTERED);
24
25 loop {
26 text.pos = scale_view.intended_size() / 2;
27 sprite.pos = scale_view.intended_size() / 2;
28 sprite.pos.y -= 5;
29
30 scale_view.update();
31 scale_view.view.draw(&text);
32 scale_view.view.draw(&sprite);
33 let _ = scale_view.view.display_render();
34
35 thread::sleep(Duration::from_millis(10));
36 }
37}
More examples
examples/complex-scene.rs (lines 34-38)
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 const fn with_align(self, align: TextAlign2D) -> Self
pub const fn with_align(self, align: TextAlign2D) -> Self
Return the Sprite
with an updated align
property. Consumes the original Sprite
Examples found in repository?
examples/self-resizing.rs (line 23)
16fn main() {
17 let mut scale_view = ScaleFitView::new(ColChar::BACKGROUND);
18
19 let mut text = Text::new(Vec2D::ZERO, "This is some centered text!", Modifier::None)
20 .with_align(TextAlign::Centered);
21
22 let mut sprite =
23 Sprite::new(Vec2D::ZERO, TEXTURE, Modifier::None).with_align(TextAlign2D::CENTERED);
24
25 loop {
26 text.pos = scale_view.intended_size() / 2;
27 sprite.pos = scale_view.intended_size() / 2;
28 sprite.pos.y -= 5;
29
30 scale_view.update();
31 scale_view.view.draw(&text);
32 scale_view.view.draw(&sprite);
33 let _ = scale_view.view.display_render();
34
35 thread::sleep(Duration::from_millis(10));
36 }
37}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Sprite
impl RefUnwindSafe for Sprite
impl Send for Sprite
impl Sync for Sprite
impl Unpin for Sprite
impl UnwindSafe for Sprite
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