pub struct SpriteBatch { /* private fields */ }
Expand description
SpriteBatch is a datastructure that handle all sprites that have the same texture. And make only 1 drawCall to draw them all. this way you can highly optimise data sended to GPU. You should use it in system where there is a lot’s of sprite that should be drawn with the same shaders and the same texture. SpriteBatch is a kind of collection that implement some iterator traits.
let texture = Rc::new(Texture::from_path("path_to_texture"));
let mut batch = SpriteBatch::from(&texture);
SpriteData
The idea behind SpriteBatch is to limit draw calls. Even if your sprites havn’t the same texture can pack textures. And give your Vertex text_coord the actual texture coordinate that you want to be drawn.
Implementations§
Source§impl SpriteBatch
impl SpriteBatch
Sourcepub fn new() -> SpriteBatch
pub fn new() -> SpriteBatch
Create a new empty spriteBatch
pub fn extend_from_slice(&mut self, slice: &mut [SpriteData])
Sourcepub fn sprites(&self) -> &[SpriteData]
pub fn sprites(&self) -> &[SpriteData]
Return spritedata slice
Sourcepub fn sprites_mut(&mut self) -> &mut [SpriteData]
pub fn sprites_mut(&mut self) -> &mut [SpriteData]
Return sprite mutable slice
Sourcepub fn get_sprite_mut(&mut self, idx: usize) -> Option<&mut SpriteData>
pub fn get_sprite_mut(&mut self, idx: usize) -> Option<&mut SpriteData>
Return maybe a mutable Slice.
Examples found in repository?
44fn event_process(event: Event, window: &mut Window, batch: &mut SpriteBatch) {
45 match event.1 {
46 Events::Key(Key::Escape, _, _, _) => {
47 window.close();
48 }
49 Events::Key(Key::W, _, Action::Press, _) => {
50 batch.translate(Vector::new(10.0, 10.0));
51 }
52 Events::Key(Key::D, _, Action::Press, _) => {
53 batch
54 .get_sprite_mut(0)
55 .unwrap()
56 .translate(Vector::new(10.0, 0.0));
57 }
58 Events::MouseButton(_, _, _) => {
59 println!("Mouse button !");
60 }
61 Events::CursorPos(x, y) => {
62 println!("x: {}, y: {}", x, y);
63 }
64 _ => println!("Another event !"),
65 }
66 drop(event);
67}
Sourcepub fn get_sprite(&self, idx: usize) -> Option<&SpriteData>
pub fn get_sprite(&self, idx: usize) -> Option<&SpriteData>
Return maybe a SpriteData.
Sourcepub fn push_sprite(&mut self, sprites: SpriteData)
pub fn push_sprite(&mut self, sprites: SpriteData)
Examples found in repository?
15fn main() -> Result<(), Box<Error>> {
16 let mut window = Window::new(gust::WIDTH, gust::HEIGHT, "Hello");
17
18 let texture = Rc::new(Texture::from_path("examples/texture/Dirt.png").unwrap());
19 let mut batch = SpriteBatch::from(&texture);
20 for i in 0..1_000_000 {
21 let mut data = SpriteData::new(Vector::new(i as f32 * 1.0, i as f32 * 10.0));
22 data.set_texture_raw([Vector::new(0.0, 0.0), Vector::new(1.0, 1.0)]);
23 batch.push_sprite(data);
24 }
25
26 let event_handler = EventHandler::new(&window);
27
28 window.set_clear_color(Color::new(0.45, 0.0, 1.0));
29 window.enable_cursor();
30 window.poll(None);
31 while window.is_open() {
32 window.poll_events();
33 for event in event_handler.fetch() {
34 event_process(event, &mut window, &mut batch);
35 }
36 window.clear();
37 window.draw_mut(&mut batch);
38 window.display();
39 }
40
41 Ok(())
42}
Sourcepub fn pop_sprite(&mut self) -> Option<SpriteData>
pub fn pop_sprite(&mut self) -> Option<SpriteData>
Pop a sprite and return it’s data.
Trait Implementations§
Source§impl Clone for SpriteBatch
impl Clone for SpriteBatch
Source§fn clone(&self) -> SpriteBatch
fn clone(&self) -> SpriteBatch
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for SpriteBatch
impl Debug for SpriteBatch
Source§impl Default for SpriteBatch
impl Default for SpriteBatch
Source§impl Drawable for SpriteBatch
impl Drawable for SpriteBatch
Source§fn draw<T: Drawer>(&self, target: &mut T)
fn draw<T: Drawer>(&self, target: &mut T)
Source§fn draw_with_context(&self, _context: &mut Context<'_>)
fn draw_with_context(&self, _context: &mut Context<'_>)
Source§fn update(&mut self)
fn update(&mut self)
Source§fn setup_draw(&self, context: &mut Context<'_>)
fn setup_draw(&self, context: &mut Context<'_>)
Source§impl DrawableMut for SpriteBatch
impl DrawableMut for SpriteBatch
Source§impl Movable for SpriteBatch
impl Movable for SpriteBatch
Source§impl Rotable for SpriteBatch
impl Rotable for SpriteBatch
Source§fn rotate<T>(&mut self, angle: T)
fn rotate<T>(&mut self, angle: T)
Source§fn set_rotation<T>(&mut self, angle: T)
fn set_rotation<T>(&mut self, angle: T)
Source§fn get_rotation(&self) -> f32
fn get_rotation(&self) -> f32
Source§impl Scalable for SpriteBatch
impl Scalable for SpriteBatch
Source§impl Transformable for SpriteBatch
impl Transformable for SpriteBatch
Auto Trait Implementations§
impl Freeze for SpriteBatch
impl RefUnwindSafe for SpriteBatch
impl !Send for SpriteBatch
impl !Sync for SpriteBatch
impl Unpin for SpriteBatch
impl UnwindSafe for SpriteBatch
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> SetParameter for T
impl<T> SetParameter for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.