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>
impl<'s> ConvexShape<'s>
Sourcepub fn new(points_count: usize) -> ConvexShape<'s>
pub fn new(points_count: usize) -> ConvexShape<'s>
Sourcepub fn with_texture(
points_count: usize,
texture: &'s Texture,
) -> ConvexShape<'s>
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}
Sourcepub fn set_point<P: Into<Vector2f>>(&mut self, index: usize, point: P)
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}
Sourcepub fn set_point_count(&mut self, count: usize)
pub fn set_point_count(&mut self, count: usize)
Trait Implementations§
Source§impl<'s> Clone for ConvexShape<'s>
impl<'s> Clone for ConvexShape<'s>
Source§fn clone(&self) -> ConvexShape<'s>
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<'s> Debug for ConvexShape<'s>
impl<'s> Debug for ConvexShape<'s>
Source§impl Drawable for ConvexShape<'_>
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>,
)
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>( &'a self, target: &mut dyn RenderTarget, states: &RenderStates<'texture, 'shader, 'shader_texture>, )
Source§impl Drop for ConvexShape<'_>
impl Drop for ConvexShape<'_>
Source§impl<'s> Shape<'s> for ConvexShape<'s>
impl<'s> Shape<'s> for ConvexShape<'s>
Source§fn set_texture(&mut self, texture: &'s Texture, reset_rect: bool)
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)
fn disable_texture(&mut self)
Disables texturing for this shape.
Source§fn set_texture_rect(&mut self, rect: IntRect)
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)
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)
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)
fn set_outline_thickness(&mut self, thickness: f32)
Sets the thickness of the shape’s outline. Read more
Source§fn texture_rect(&self) -> IntRect
fn texture_rect(&self) -> IntRect
Gets the sub-rectangle of the texture displayed by the shape.
Source§fn fill_color(&self) -> Color
fn fill_color(&self) -> Color
Gets the fill color of this shape.
Source§fn outline_color(&self) -> Color
fn outline_color(&self) -> Color
Gets the outline color of this shape.
Source§fn outline_thickness(&self) -> f32
fn outline_thickness(&self) -> f32
Gets the outline thickness of this shape.
Source§fn point_count(&self) -> usize
fn point_count(&self) -> usize
Gets the total number of points of the shape.
Source§fn local_bounds(&self) -> FloatRect
fn local_bounds(&self) -> FloatRect
Gets the local bounding rectangle of the entity. Read more
Source§fn global_bounds(&self) -> FloatRect
fn global_bounds(&self) -> FloatRect
Gets the global (non-minimal) bounding rectangle of the entity. Read more
Source§impl Transformable for ConvexShape<'_>
impl Transformable for ConvexShape<'_>
Source§fn set_position<P: Into<Vector2f>>(&mut self, position: P)
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)
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)
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)
fn set_origin<O: Into<Vector2f>>(&mut self, origin: O)
Sets the local origin of the object. Read more
Source§fn move_<O: Into<Vector2f>>(&mut self, offset: O)
fn move_<O: Into<Vector2f>>(&mut self, offset: O)
Moves the object by a given offset. Read more
Source§fn inverse_transform(&self) -> &Transform
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> 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