Struct ConvexShape

Source
pub struct ConvexShape<'s> { /* private fields */ }
Expand description

Specialized shape representing a convex polygon

It is important to keep in mind that a convex shape must always be… convex, otherwise it may not be drawn correctly. Moreover, the points must be defined in order; using a random order would result in an incorrect shape.

Implementations§

Source§

impl<'s> ConvexShape<'s>

Source

pub fn new(points_count: usize) -> ConvexShape<'s>

Create a new convex shape

§Arguments
  • points_count - The number of point for the convex shape
§Panics

Panics if for some reason a ConvexShape can’t be created.

Source

pub fn with_texture( points_count: usize, texture: &'s Texture, ) -> ConvexShape<'s>

Create a new convex shape with a texture

§Arguments
  • texture - The texture to apply to the convex shape
  • points_count - The number of point for the convex shape
Examples found in repository?
examples/borrowed-resources.rs (line 42)
12fn main() -> SfResult<()> {
13    example_ensure_right_working_dir();
14
15    let mut window = RenderWindow::new(
16        (800, 600),
17        "Borrowed resources",
18        Style::CLOSE,
19        &Default::default(),
20    )?;
21    window.set_vertical_sync_enabled(true);
22
23    // Create a new texture. (Hey Frank!)
24    let frank = Texture::from_file("frank.jpeg")?;
25
26    // Create a font.
27    let font = Font::from_file("sansation.ttf")?;
28
29    // Create a circle with the Texture.
30    let mut circle = CircleShape::with_texture(&frank);
31    circle.set_radius(70.0);
32    circle.set_position((100.0, 100.0));
33
34    // Create a Sprite.
35    let mut sprite = Sprite::new();
36    // Have it use the same texture as the circle.
37    sprite.set_texture(&frank, true);
38    sprite.set_position((400.0, 300.0));
39    sprite.set_scale(0.5);
40
41    // Create a ConvexShape using the same texture.
42    let mut convex_shape = ConvexShape::with_texture(6, &frank);
43    convex_shape.set_point(0, (400., 100.));
44    convex_shape.set_point(1, (500., 70.));
45    convex_shape.set_point(2, (450., 100.));
46    convex_shape.set_point(3, (580., 150.));
47    convex_shape.set_point(4, (420., 230.));
48    convex_shape.set_point(5, (420., 120.));
49
50    // Create an initialized text using the font.
51    let title = Text::new("Borrowed resources example!", &font, 50);
52
53    // Create a second text using the same font.
54    // This time, we create and initialize it separately.
55    let mut second_text = Text::default();
56    second_text.set_string("This text shares the same font with the title!");
57    second_text.set_font(&font);
58    second_text.set_fill_color(Color::GREEN);
59    second_text.set_position((10.0, 350.0));
60    second_text.set_character_size(20);
61
62    // Create a third text using the same font.
63    let mut third_text = Text::new("This one too!", &font, 20);
64    third_text.set_position((300.0, 100.0));
65    third_text.set_fill_color(Color::RED);
66
67    'mainloop: loop {
68        while let Some(event) = window.poll_event() {
69            match event {
70                Event::Closed
71                | Event::KeyPressed {
72                    code: Key::Escape, ..
73                } => break 'mainloop,
74                _ => {}
75            }
76        }
77
78        window.clear(Color::BLACK);
79        window.draw(&circle);
80        window.draw(&sprite);
81        window.draw(&convex_shape);
82        window.draw(&title);
83        window.draw(&second_text);
84        window.draw(&third_text);
85
86        // Little test here for `Shape::points`
87        let mut circ = CircleShape::new(4.0, 30);
88        circ.set_origin(2.0);
89        circ.set_fill_color(Color::YELLOW);
90
91        for p in convex_shape.points() {
92            circ.set_position(p);
93            window.draw(&circ);
94        }
95
96        window.display();
97    }
98    Ok(())
99}
Source

pub fn set_point<P: Into<Vector2f>>(&mut self, index: usize, point: P)

Set the position of a point.

Don’t forget that the polygon must remain convex, and the points need to stay ordered! set_point_count must be called first in order to set the total number of points. The result is undefined if index is out of the valid range.

§Arguments
  • index - Index of the point to change, in range [0 .. get_point_count() - 1]
  • point - New position of the point
§Panics

Panics on out of bounds access

Examples found in repository?
examples/borrowed-resources.rs (line 43)
12fn main() -> SfResult<()> {
13    example_ensure_right_working_dir();
14
15    let mut window = RenderWindow::new(
16        (800, 600),
17        "Borrowed resources",
18        Style::CLOSE,
19        &Default::default(),
20    )?;
21    window.set_vertical_sync_enabled(true);
22
23    // Create a new texture. (Hey Frank!)
24    let frank = Texture::from_file("frank.jpeg")?;
25
26    // Create a font.
27    let font = Font::from_file("sansation.ttf")?;
28
29    // Create a circle with the Texture.
30    let mut circle = CircleShape::with_texture(&frank);
31    circle.set_radius(70.0);
32    circle.set_position((100.0, 100.0));
33
34    // Create a Sprite.
35    let mut sprite = Sprite::new();
36    // Have it use the same texture as the circle.
37    sprite.set_texture(&frank, true);
38    sprite.set_position((400.0, 300.0));
39    sprite.set_scale(0.5);
40
41    // Create a ConvexShape using the same texture.
42    let mut convex_shape = ConvexShape::with_texture(6, &frank);
43    convex_shape.set_point(0, (400., 100.));
44    convex_shape.set_point(1, (500., 70.));
45    convex_shape.set_point(2, (450., 100.));
46    convex_shape.set_point(3, (580., 150.));
47    convex_shape.set_point(4, (420., 230.));
48    convex_shape.set_point(5, (420., 120.));
49
50    // Create an initialized text using the font.
51    let title = Text::new("Borrowed resources example!", &font, 50);
52
53    // Create a second text using the same font.
54    // This time, we create and initialize it separately.
55    let mut second_text = Text::default();
56    second_text.set_string("This text shares the same font with the title!");
57    second_text.set_font(&font);
58    second_text.set_fill_color(Color::GREEN);
59    second_text.set_position((10.0, 350.0));
60    second_text.set_character_size(20);
61
62    // Create a third text using the same font.
63    let mut third_text = Text::new("This one too!", &font, 20);
64    third_text.set_position((300.0, 100.0));
65    third_text.set_fill_color(Color::RED);
66
67    'mainloop: loop {
68        while let Some(event) = window.poll_event() {
69            match event {
70                Event::Closed
71                | Event::KeyPressed {
72                    code: Key::Escape, ..
73                } => break 'mainloop,
74                _ => {}
75            }
76        }
77
78        window.clear(Color::BLACK);
79        window.draw(&circle);
80        window.draw(&sprite);
81        window.draw(&convex_shape);
82        window.draw(&title);
83        window.draw(&second_text);
84        window.draw(&third_text);
85
86        // Little test here for `Shape::points`
87        let mut circ = CircleShape::new(4.0, 30);
88        circ.set_origin(2.0);
89        circ.set_fill_color(Color::YELLOW);
90
91        for p in convex_shape.points() {
92            circ.set_position(p);
93            window.draw(&circ);
94        }
95
96        window.display();
97    }
98    Ok(())
99}
Source

pub fn set_point_count(&mut self, count: usize)

Set the number of points of a convex

§Arguments
  • count - New number of points of the convex

Trait Implementations§

Source§

impl<'s> Clone for ConvexShape<'s>

Source§

fn clone(&self) -> ConvexShape<'s>

Return a new ConvexShape or panic if there is not enough memory

1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'s> Debug for ConvexShape<'s>

Source§

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

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

impl Drawable for ConvexShape<'_>

Source§

fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>( &'a self, target: &mut dyn RenderTarget, states: &RenderStates<'texture, 'shader, 'shader_texture>, )

Draws into target with RenderStates states.
Source§

impl Drop for ConvexShape<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'s> Shape<'s> for ConvexShape<'s>

Source§

fn set_texture(&mut self, texture: &'s Texture, reset_rect: bool)

Changes the source texture of the shape. Read more
Source§

fn disable_texture(&mut self)

Disables texturing for this shape.
Source§

fn set_texture_rect(&mut self, rect: IntRect)

Sets the sub-rectangle of the texture that the shape will display. Read more
Source§

fn set_fill_color(&mut self, color: Color)

Sets the fill color of the shape. Read more
Source§

fn set_outline_color(&mut self, color: Color)

Sets the outline color of the shape. Read more
Source§

fn set_outline_thickness(&mut self, thickness: f32)

Sets the thickness of the shape’s outline. Read more
Source§

fn texture(&self) -> Option<&'s Texture>

Gets the source texture of the shape. Read more
Source§

fn texture_rect(&self) -> IntRect

Gets the sub-rectangle of the texture displayed by the shape.
Source§

fn fill_color(&self) -> Color

Gets the fill color of this shape.
Source§

fn outline_color(&self) -> Color

Gets the outline color of this shape.
Source§

fn outline_thickness(&self) -> f32

Gets the outline thickness of this shape.
Source§

fn point_count(&self) -> usize

Gets the total number of points of the shape.
Source§

fn point(&self, index: usize) -> Vector2f

Gets a point of the shape. Read more
Source§

fn local_bounds(&self) -> FloatRect

Gets the local bounding rectangle of the entity. Read more
Source§

fn global_bounds(&self) -> FloatRect

Gets the global (non-minimal) bounding rectangle of the entity. Read more
Source§

fn points<'a>(&'a self) -> impl Iterator<Item = Vector2f> + 'a

Returns an immutable iterator over all points
Source§

impl Transformable for ConvexShape<'_>

Source§

fn set_position<P: Into<Vector2f>>(&mut self, position: P)

Sets the position of the object. Read more
Source§

fn set_rotation(&mut self, angle: f32)

Set the orientation of the object in degrees. Read more
Source§

fn set_scale<S: Into<Vector2f>>(&mut self, scale: S)

Sets the scale factors of the object. Read more
Source§

fn set_origin<O: Into<Vector2f>>(&mut self, origin: O)

Sets the local origin of the object. Read more
Source§

fn position(&self) -> Vector2f

Gets the position of the object.
Source§

fn rotation(&self) -> f32

Gets the rotation of the object in degrees. Read more
Source§

fn get_scale(&self) -> Vector2f

Gets the current scale of the object.
Source§

fn origin(&self) -> Vector2f

Gets the local origin of the object.
Source§

fn move_<O: Into<Vector2f>>(&mut self, offset: O)

Moves the object by a given offset. Read more
Source§

fn rotate(&mut self, angle: f32)

Rotates the object. Read more
Source§

fn scale<F: Into<Vector2f>>(&mut self, factors: F)

Scales the object. Read more
Source§

fn transform(&self) -> &Transform

Gets the combined transform of the object.
Source§

fn inverse_transform(&self) -> &Transform

Gets the inverse combined transform of the object.

Auto Trait Implementations§

§

impl<'s> Freeze for ConvexShape<'s>

§

impl<'s> RefUnwindSafe for ConvexShape<'s>

§

impl<'s> !Send for ConvexShape<'s>

§

impl<'s> !Sync for ConvexShape<'s>

§

impl<'s> Unpin for ConvexShape<'s>

§

impl<'s> UnwindSafe for ConvexShape<'s>

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

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.