#[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 put ASCII art, text and other such things on the View

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

source

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 24)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn main() {
    let mut scale_view = ScaleFitView::new(ColChar::BACKGROUND);

    let mut text = Text::new(Vec2D::ZERO, "This is some centered text!", Modifier::None)
        .with_align(TextAlign::Centered);

    let mut sprite =
        Sprite::new(Vec2D::ZERO, TEXTURE, Modifier::None).with_align(TextAlign2D::CENTERED);

    loop {
        text.pos = scale_view.intended_size() / 2;
        sprite.pos = scale_view.intended_size() / 2;
        sprite.pos.y -= 5;

        scale_view.update();
        scale_view.view.blit(&text, Wrapping::Wrap);
        scale_view.view.blit(&sprite, Wrapping::Wrap);
        let _ = scale_view.view.display_render();

        sleep(Duration::from_millis(10));
    }
}
More examples
Hide additional examples
examples/complex-scene.rs (lines 33-37)
13
14
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
fn main() {
    let mut view = View::new(60, 10, BACKGROUND_CHAR);
    view.coord_numbers_in_render = true;

    let mut pixel = Pixel::new(Vec2D::from((5u8, 9u8)), 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 blit_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.blit(&pixel, Wrapping::Panic);
            view.blit(&line, Wrapping::Panic);
            view.blit(&rect, Wrapping::Panic);
            view.blit(&sprite, Wrapping::Wrap);
            blit_elapsed = now.elapsed();

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

pub const fn with_align(self, align: TextAlign2D) -> Self

Return the Sprite with the modified align property

Examples found in repository?
examples/self-resizing.rs (line 24)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn main() {
    let mut scale_view = ScaleFitView::new(ColChar::BACKGROUND);

    let mut text = Text::new(Vec2D::ZERO, "This is some centered text!", Modifier::None)
        .with_align(TextAlign::Centered);

    let mut sprite =
        Sprite::new(Vec2D::ZERO, TEXTURE, Modifier::None).with_align(TextAlign2D::CENTERED);

    loop {
        text.pos = scale_view.intended_size() / 2;
        sprite.pos = scale_view.intended_size() / 2;
        sprite.pos.y -= 5;

        scale_view.update();
        scale_view.view.blit(&text, Wrapping::Wrap);
        scale_view.view.blit(&sprite, Wrapping::Wrap);
        let _ = scale_view.view.display_render();

        sleep(Duration::from_millis(10));
    }
}
source

pub fn draw(pos: Vec2D, texture: &str, modifier: Modifier) -> Vec<Pixel>

Render a string texture at a given position in a ViewElement::active_pixels()-readable format

source

pub fn draw_with_align( pos: Vec2D, texture: &str, align: TextAlign2D, modifier: Modifier ) -> Vec<Pixel>

Return a vector of Pixels to display the given content, aligning the content to the position as directed by the align attribute

Trait Implementations§

source§

impl Clone for Sprite

source§

fn clone(&self) -> Sprite

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Sprite

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl ViewElement for Sprite

source§

fn active_pixels(&self) -> Vec<Pixel>

Return a vector of the element’s Pixels - A ColChar. If your whole object is a solid colour, consider using utils::points_to_pixels() which will add the same ColChar to every point and can then be used as this function’s output
source§

fn active_points(&self) -> Vec<Vec2D>

Return the positions the ViewElement occupies, essentially active_pixels() without the ColChars. This has a default setting that extracts the Vec2Ds from active_pixels but you can set it to something else to make it faster

Auto Trait Implementations§

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.