Struct mooeye::sprite::SpritePool
source · pub struct SpritePool { /* private fields */ }
Expand description
A pool that contains a number of initialized Sprites at once and can be passed around and allows initialization of sprites using the prototype pattern and without having to re-access the file system or pass around a loading context. Provides functions for quickly initalizing folders of sprites and access methods similar to those of graphics::Image and Sprite.
File format and access
File names must be formatted as NAME_WIDTH_HEIGHT.EXTENSION
.
Access keys into the pool are the full path to the image file (relative to the resource directory), followed by only NAME, width, height and extension are stripped.
This allows easy replacement of sprites with different formats.
Example
A file named mage_16_16.png
in a subfolder /sprites/player
of the resource folder will be accessible with the key /sprites/player/mage
.
Implementations§
source§impl SpritePool
impl SpritePool
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new (empty) SpritePool instance.
Examples found in repository?
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
pub fn new(ctx: &Context) -> Result<Self, GameError> {
// Reusing the visuals from E.
let vis = ui::Visuals::new(
Color::from_rgb(180, 120, 60),
Color::from_rgb(18, 12, 6),
1.,
0.,
);
let hover_vis = ui::Visuals::new(
Color::from_rgb(160, 100, 40),
Color::from_rgb(18, 12, 6),
3.,
0.,
);
let cont_vis = ui::Visuals::new(
Color::from_rgb(60, 120, 180),
Color::from_rgb(180, 180, 190),
1.,
0.,
);
// A sprite can be loaded by specifying a path, just like an Image.
// Additionaly, you need to inform the sprite of the grid size of its sheet and the duration each frame is displayed.
let ui_sprite = sprite::Sprite::from_path(
"/moo-sheet_16_16.png",
ctx,
16,
24,
Duration::from_secs_f32(0.25),
)?
// Just like any UI element, a sprite can have visuals, tooltip, ect.
.to_element_builder(1, ctx)
.scaled(5., 5.)
.with_visuals(vis)
.with_hover_visuals(hover_vis)
.with_tooltip(
graphics::Text::new("This is a sprite! Click it to end the scene.")
.set_scale(28.)
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(0, ctx)
.with_visuals(cont_vis)
.build(),
)
.as_shrink()
.build();
// Sprites can also be initiated from a sprite pool, to make repeated file system access unneccessary
// and streamline loading of multiple sprites. This requires sprites in the folder to be formatted appropriately.
let sprite_pool = sprite::SpritePool::new()
// with_folder loads all .png/.bmp/.jpg/.jpeg files from the passed folder and optionally its subfolders
.with_folder(ctx, "/", true);
// We can now init a sprite from the pool. Sprites are saved in the pool with a key corresponding to their relative path
// (from the resource folder) with the format information and file ending removed.
let non_ui_sprite = sprite_pool.init_sprite("/mage-sheet", Duration::from_secs_f32(0.2))?;
// you can also initialize a sprite pool without any folder at all
let mut sprite_pool2 = sprite::SpritePool::new();
// in this case, you can use lazy initialisation of sprites to fill the sprite pool only with those sprites currently needed.
// Lazy initilisation draws from the pool if possible, from the file system if needed (and loads into the pool in this case) and panics if it can't find anything in the fs.
// Requires a mutable sprite pool!
// For testing purposes, we are loading a sprite we have already loaded - this should be drawn from the pool.
let lazy_sprite =
sprite_pool2.init_sprite_lazy(ctx, "/mage-sheet", Duration::from_secs_f32(0.5))?;
// now load the correct sprite
let lazy_sprite =
sprite_pool2.init_sprite_lazy(ctx, "/moo-sheet", lazy_sprite.get_frame_time())?;
Ok(Self {
gui: ui_sprite,
sprite: non_ui_sprite,
sprite2: lazy_sprite,
pos: Vec2::new(50., 200.),
v: Vec2::new(4., 4.),
})
}
sourcepub fn with_default_duration(self, default_duration: Duration) -> Self
pub fn with_default_duration(self, default_duration: Duration) -> Self
Sets a default duration this sprite pool will asign to every loaded sprite. This is especially useful when using a lot of Sprites only as references.
sourcepub fn with_folder(
self,
ctx: &Context,
path: impl AsRef<Path>,
search_subfolders: bool
) -> Self
pub fn with_folder( self, ctx: &Context, path: impl AsRef<Path>, search_subfolders: bool ) -> Self
Loads all sprites within the given folder (relative to the ggez resource directory, see ggez::context::ContextBuilder) into the sprite pool. Can also search all subfolders. See SpritePool for required name formatting in order to load sprites correctly.
Examples found in repository?
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
pub fn new(ctx: &Context) -> Result<Self, GameError> {
// Reusing the visuals from E.
let vis = ui::Visuals::new(
Color::from_rgb(180, 120, 60),
Color::from_rgb(18, 12, 6),
1.,
0.,
);
let hover_vis = ui::Visuals::new(
Color::from_rgb(160, 100, 40),
Color::from_rgb(18, 12, 6),
3.,
0.,
);
let cont_vis = ui::Visuals::new(
Color::from_rgb(60, 120, 180),
Color::from_rgb(180, 180, 190),
1.,
0.,
);
// A sprite can be loaded by specifying a path, just like an Image.
// Additionaly, you need to inform the sprite of the grid size of its sheet and the duration each frame is displayed.
let ui_sprite = sprite::Sprite::from_path(
"/moo-sheet_16_16.png",
ctx,
16,
24,
Duration::from_secs_f32(0.25),
)?
// Just like any UI element, a sprite can have visuals, tooltip, ect.
.to_element_builder(1, ctx)
.scaled(5., 5.)
.with_visuals(vis)
.with_hover_visuals(hover_vis)
.with_tooltip(
graphics::Text::new("This is a sprite! Click it to end the scene.")
.set_scale(28.)
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(0, ctx)
.with_visuals(cont_vis)
.build(),
)
.as_shrink()
.build();
// Sprites can also be initiated from a sprite pool, to make repeated file system access unneccessary
// and streamline loading of multiple sprites. This requires sprites in the folder to be formatted appropriately.
let sprite_pool = sprite::SpritePool::new()
// with_folder loads all .png/.bmp/.jpg/.jpeg files from the passed folder and optionally its subfolders
.with_folder(ctx, "/", true);
// We can now init a sprite from the pool. Sprites are saved in the pool with a key corresponding to their relative path
// (from the resource folder) with the format information and file ending removed.
let non_ui_sprite = sprite_pool.init_sprite("/mage-sheet", Duration::from_secs_f32(0.2))?;
// you can also initialize a sprite pool without any folder at all
let mut sprite_pool2 = sprite::SpritePool::new();
// in this case, you can use lazy initialisation of sprites to fill the sprite pool only with those sprites currently needed.
// Lazy initilisation draws from the pool if possible, from the file system if needed (and loads into the pool in this case) and panics if it can't find anything in the fs.
// Requires a mutable sprite pool!
// For testing purposes, we are loading a sprite we have already loaded - this should be drawn from the pool.
let lazy_sprite =
sprite_pool2.init_sprite_lazy(ctx, "/mage-sheet", Duration::from_secs_f32(0.5))?;
// now load the correct sprite
let lazy_sprite =
sprite_pool2.init_sprite_lazy(ctx, "/moo-sheet", lazy_sprite.get_frame_time())?;
Ok(Self {
gui: ui_sprite,
sprite: non_ui_sprite,
sprite2: lazy_sprite,
pos: Vec2::new(50., 200.),
v: Vec2::new(4., 4.),
})
}
sourcepub fn init_sprite(
&self,
path: impl AsRef<Path>,
frame_time: Duration
) -> Result<Sprite, GameError>
pub fn init_sprite( &self, path: impl AsRef<Path>, frame_time: Duration ) -> Result<Sprite, GameError>
Initialies a sprite from the sprite pool. The path syntax is exactly the same as for initalizing images or sprites, relative to the ggez resource folder. See graphics::Image and Sprite. If the sprite (path) is not yet contained in the pool, an error is returned. For lazy initalization, use SpritePool::init_sprite_lazy instead. See SpritePool for rules related to key assignment.
Examples found in repository?
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
pub fn new(ctx: &Context) -> Result<Self, GameError> {
// Reusing the visuals from E.
let vis = ui::Visuals::new(
Color::from_rgb(180, 120, 60),
Color::from_rgb(18, 12, 6),
1.,
0.,
);
let hover_vis = ui::Visuals::new(
Color::from_rgb(160, 100, 40),
Color::from_rgb(18, 12, 6),
3.,
0.,
);
let cont_vis = ui::Visuals::new(
Color::from_rgb(60, 120, 180),
Color::from_rgb(180, 180, 190),
1.,
0.,
);
// A sprite can be loaded by specifying a path, just like an Image.
// Additionaly, you need to inform the sprite of the grid size of its sheet and the duration each frame is displayed.
let ui_sprite = sprite::Sprite::from_path(
"/moo-sheet_16_16.png",
ctx,
16,
24,
Duration::from_secs_f32(0.25),
)?
// Just like any UI element, a sprite can have visuals, tooltip, ect.
.to_element_builder(1, ctx)
.scaled(5., 5.)
.with_visuals(vis)
.with_hover_visuals(hover_vis)
.with_tooltip(
graphics::Text::new("This is a sprite! Click it to end the scene.")
.set_scale(28.)
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(0, ctx)
.with_visuals(cont_vis)
.build(),
)
.as_shrink()
.build();
// Sprites can also be initiated from a sprite pool, to make repeated file system access unneccessary
// and streamline loading of multiple sprites. This requires sprites in the folder to be formatted appropriately.
let sprite_pool = sprite::SpritePool::new()
// with_folder loads all .png/.bmp/.jpg/.jpeg files from the passed folder and optionally its subfolders
.with_folder(ctx, "/", true);
// We can now init a sprite from the pool. Sprites are saved in the pool with a key corresponding to their relative path
// (from the resource folder) with the format information and file ending removed.
let non_ui_sprite = sprite_pool.init_sprite("/mage-sheet", Duration::from_secs_f32(0.2))?;
// you can also initialize a sprite pool without any folder at all
let mut sprite_pool2 = sprite::SpritePool::new();
// in this case, you can use lazy initialisation of sprites to fill the sprite pool only with those sprites currently needed.
// Lazy initilisation draws from the pool if possible, from the file system if needed (and loads into the pool in this case) and panics if it can't find anything in the fs.
// Requires a mutable sprite pool!
// For testing purposes, we are loading a sprite we have already loaded - this should be drawn from the pool.
let lazy_sprite =
sprite_pool2.init_sprite_lazy(ctx, "/mage-sheet", Duration::from_secs_f32(0.5))?;
// now load the correct sprite
let lazy_sprite =
sprite_pool2.init_sprite_lazy(ctx, "/moo-sheet", lazy_sprite.get_frame_time())?;
Ok(Self {
gui: ui_sprite,
sprite: non_ui_sprite,
sprite2: lazy_sprite,
pos: Vec2::new(50., 200.),
v: Vec2::new(4., 4.),
})
}
sourcepub fn init_sprite_unchecked(
&self,
path: impl AsRef<Path>,
frame_time: Duration
) -> Sprite
pub fn init_sprite_unchecked( &self, path: impl AsRef<Path>, frame_time: Duration ) -> Sprite
Initialies a sprite from the sprite pool. The path syntax is exactly the same as for initalizing images or sprites, relative to the ggez resource folder. See graphics::Image and Sprite. If the sprite (path) is not yet contained in the pool, this panics. If you want to return an error, use SpritePool::init_sprite instead. For lazy initalization, use SpritePool::init_sprite_lazy instead. See SpritePool for rules related to key assignment.
sourcepub fn init_sprite_lazy(
&mut self,
ctx: &Context,
path: impl AsRef<Path>,
frame_time: Duration
) -> Result<Sprite, GameError>
pub fn init_sprite_lazy( &mut self, ctx: &Context, path: impl AsRef<Path>, frame_time: Duration ) -> Result<Sprite, GameError>
Initialies a sprite from the sprite pool. The path syntax is exactly the same as for initalizing images or sprites, relative to the ggez resource folder. See graphics::Image and Sprite. If the sprite (path) is not yet contained in the pool, the system will attempt to load it from the file system and return it. If this also fails, an error is returned. See SpritePool for rules related to key assignment.
Examples found in repository?
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
pub fn new(ctx: &Context) -> Result<Self, GameError> {
// Reusing the visuals from E.
let vis = ui::Visuals::new(
Color::from_rgb(180, 120, 60),
Color::from_rgb(18, 12, 6),
1.,
0.,
);
let hover_vis = ui::Visuals::new(
Color::from_rgb(160, 100, 40),
Color::from_rgb(18, 12, 6),
3.,
0.,
);
let cont_vis = ui::Visuals::new(
Color::from_rgb(60, 120, 180),
Color::from_rgb(180, 180, 190),
1.,
0.,
);
// A sprite can be loaded by specifying a path, just like an Image.
// Additionaly, you need to inform the sprite of the grid size of its sheet and the duration each frame is displayed.
let ui_sprite = sprite::Sprite::from_path(
"/moo-sheet_16_16.png",
ctx,
16,
24,
Duration::from_secs_f32(0.25),
)?
// Just like any UI element, a sprite can have visuals, tooltip, ect.
.to_element_builder(1, ctx)
.scaled(5., 5.)
.with_visuals(vis)
.with_hover_visuals(hover_vis)
.with_tooltip(
graphics::Text::new("This is a sprite! Click it to end the scene.")
.set_scale(28.)
.set_font("Bahnschrift")
.to_owned()
.to_element_builder(0, ctx)
.with_visuals(cont_vis)
.build(),
)
.as_shrink()
.build();
// Sprites can also be initiated from a sprite pool, to make repeated file system access unneccessary
// and streamline loading of multiple sprites. This requires sprites in the folder to be formatted appropriately.
let sprite_pool = sprite::SpritePool::new()
// with_folder loads all .png/.bmp/.jpg/.jpeg files from the passed folder and optionally its subfolders
.with_folder(ctx, "/", true);
// We can now init a sprite from the pool. Sprites are saved in the pool with a key corresponding to their relative path
// (from the resource folder) with the format information and file ending removed.
let non_ui_sprite = sprite_pool.init_sprite("/mage-sheet", Duration::from_secs_f32(0.2))?;
// you can also initialize a sprite pool without any folder at all
let mut sprite_pool2 = sprite::SpritePool::new();
// in this case, you can use lazy initialisation of sprites to fill the sprite pool only with those sprites currently needed.
// Lazy initilisation draws from the pool if possible, from the file system if needed (and loads into the pool in this case) and panics if it can't find anything in the fs.
// Requires a mutable sprite pool!
// For testing purposes, we are loading a sprite we have already loaded - this should be drawn from the pool.
let lazy_sprite =
sprite_pool2.init_sprite_lazy(ctx, "/mage-sheet", Duration::from_secs_f32(0.5))?;
// now load the correct sprite
let lazy_sprite =
sprite_pool2.init_sprite_lazy(ctx, "/moo-sheet", lazy_sprite.get_frame_time())?;
Ok(Self {
gui: ui_sprite,
sprite: non_ui_sprite,
sprite2: lazy_sprite,
pos: Vec2::new(50., 200.),
v: Vec2::new(4., 4.),
})
}
sourcepub fn sprite_ref(
&mut self,
path: impl AsRef<Path>
) -> Result<&mut Sprite, GameError>
pub fn sprite_ref( &mut self, path: impl AsRef<Path> ) -> Result<&mut Sprite, GameError>
Returns a mutable reference to a sprite from the sprite pool.
Use
This is useful if you do not want to have each entity with the same sprite to hold a copy of the sprite.
Instead, you can just store keys to this sprite pool.
Note that the actual sprite_sheet
in the Sprite struct is just a handle to data on the GPU, so the storage
savings are not monumental.
However, you still save some space as the state of the sprite does not need to be stored.
Not storing the sprite has a distince disadvantage: Different entities with the same reference cannot have different states,
so they always need to display the same frame and the same sprite-state.
The performance losses are dependent on the size of the sprite pool. Smaller sprite pools work better.
Of course, this function is supremely useful if you only need to draw a sprite once.
Syntax
The path syntax is exactly the same as for initalizing images or sprites, relative to the ggez resource folder. See graphics::Image and Sprite. If the sprite (path) is not yet contained in the pool, an error is returned. For lazy initalization, use SpritePool::sprite_ref_lazy instead. See SpritePool for rules related to key assignment.
sourcepub fn sprite_ref_lazy(
&mut self,
ctx: &Context,
path: impl AsRef<Path>
) -> Result<&mut Sprite, GameError>
pub fn sprite_ref_lazy( &mut self, ctx: &Context, path: impl AsRef<Path> ) -> Result<&mut Sprite, GameError>
Returns a mutable reference to a sprite from the sprite pool. For syntax and use see SpritePool::sprite_ref, this is a lazy version of that function. If the requested key is not yet in the sprite pool, this function will attempt to load it and return the correct reference. If the requested file cannot be found, an error is returned.
sourcepub fn print_keys(&self)
pub fn print_keys(&self)
Prints all currently registered keys of this sprite pool. Useful if you are debugging key-issues.