The game's textures, each loaded once and found by a name such as "player".
More...
#include <aegis2d/render/texture_manager.hpp>
|
| | TextureManager (IRenderer &renderer) noexcept |
| | Creates an empty manager whose textures go to renderer.
|
| |
| TextureId | load (std::string_view name, const std::filesystem::path &path) |
| | Loads an image file as a texture under a name, unless it is loaded already.
|
| |
| TextureId | add (std::string_view name, const Image &image) |
| | Adds an image made in memory, such as a marker or a font, under a name.
|
| |
| TextureId | load_sheet (std::string_view name, const std::filesystem::path &path, TextureSize frame, std::size_t count=0) |
| | Loads an image file as a sprite sheet of frames of the same size in a grid.
|
| |
| bool | reload (const std::filesystem::path &path) |
| | Reads a loaded file again and replaces its texture's image.
|
| |
| void | watch (core::HotReload &hot) |
| | Reloads each file loaded so far whenever it changes on disk.
|
| |
| void | set_sheet (TextureId texture, SpriteSheet sheet) |
| | Cuts a texture into the frames of a sprite sheet, replacing its sheet if it had one.
|
| |
| const SpriteSheet * | sheet (TextureId texture) const noexcept |
| | Sprite sheet of a texture.
|
| |
| Rect | source (TextureId texture, std::size_t frame) const |
| | Part of a texture to draw for a frame, as Sprite::source takes it.
|
| |
| TextureId | get (std::string_view name) const |
| | Texture of a name.
|
| |
| std::optional< TextureId > | find (std::string_view name) const |
| | Looks a name up.
|
| |
| std::size_t | size () const noexcept |
| | Number of names.
|
| |
| std::size_t | textures () const noexcept |
| | Number of textures created, one per file or added image.
|
| |
The game's textures, each loaded once and found by a name such as "player".
The game loads its images at start, then draws with get():
...
renderer.draw({.texture =
textures.get(
"player"), .position = ..., .
size = ...});
The game's textures, each loaded once and found by a name such as "player".
Definition texture_manager.hpp:33
TextureId load(std::string_view name, const std::filesystem::path &path)
Loads an image file as a texture under a name, unless it is loaded already.
std::size_t size() const noexcept
Number of names.
std::size_t textures() const noexcept
Number of textures created, one per file or added image.
std::filesystem::path asset_path(const std::filesystem::path &relative)
Path of one asset.
A file is read and sent to the GPU once, even when it is loaded again or under another name. A texture can be a sprite sheet, cut into frames (load_sheet(), set_sheet()): source() then gives the part of the texture a frame draws. The textures belong to the renderer, which must outlive the manager.
◆ TextureManager()
| aegis2d::render::TextureManager::TextureManager |
( |
IRenderer & |
renderer | ) |
|
|
explicitnoexcept |
Creates an empty manager whose textures go to renderer.
- Parameters
-
| renderer | Renderer that creates the textures. |
◆ add()
| TextureId aegis2d::render::TextureManager::add |
( |
std::string_view |
name, |
|
|
const Image & |
image |
|
) |
| |
Adds an image made in memory, such as a marker or a font, under a name.
- Parameters
-
| name | Name to find the texture with, not empty and not used yet. |
| image | RGBA image, not empty. |
- Returns
- The new texture.
- Exceptions
-
| std::invalid_argument | if name is empty or used, or image is empty. |
◆ find()
| std::optional< TextureId > aegis2d::render::TextureManager::find |
( |
std::string_view |
name | ) |
const |
Looks a name up.
- Parameters
-
- Returns
- Its texture, or nothing if no texture has this name.
◆ get()
| TextureId aegis2d::render::TextureManager::get |
( |
std::string_view |
name | ) |
const |
Texture of a name.
- Parameters
-
- Returns
- Its texture.
- Exceptions
-
| std::out_of_range | if no texture has this name. |
◆ load()
| TextureId aegis2d::render::TextureManager::load |
( |
std::string_view |
name, |
|
|
const std::filesystem::path & |
path |
|
) |
| |
Loads an image file as a texture under a name, unless it is loaded already.
- Parameters
-
- Returns
- The texture: the one already loaded when
name or the same file was loaded before.
- Exceptions
-
| std::invalid_argument | if name is empty, or already names another file or an added image. |
| ImageError | if the file cannot be read or decoded. |
◆ load_sheet()
| TextureId aegis2d::render::TextureManager::load_sheet |
( |
std::string_view |
name, |
|
|
const std::filesystem::path & |
path, |
|
|
TextureSize |
frame, |
|
|
std::size_t |
count = 0 |
|
) |
| |
Loads an image file as a sprite sheet of frames of the same size in a grid.
load(), then set_sheet() with SpriteSheet::grid().
- Parameters
-
| name | Name to find the texture with, not empty. |
| path | Image file. |
| frame | Size of one frame, in pixels. |
| count | Number of frames; 0 for every whole frame of the grid. |
- Returns
- The texture.
- Exceptions
-
◆ reload()
| bool aegis2d::render::TextureManager::reload |
( |
const std::filesystem::path & |
path | ) |
|
Reads a loaded file again and replaces its texture's image.
The texture keeps its identifier and its size, so every sprite drawn with it shows the new image from the next frame, and what was worked out from the size (a sprite sheet's frames, a tile map's tiles, sprite sizes) stays right.
- Parameters
-
- Returns
false if no texture was loaded from this file.
- Exceptions
-
| ImageError | if the file cannot be read or decoded; the texture is kept. |
| std::invalid_argument | if the new image has another size; the texture is kept. |
◆ set_sheet()
Cuts a texture into the frames of a sprite sheet, replacing its sheet if it had one.
- Parameters
-
| texture | Texture returned by load() or add(). |
| sheet | Frames of the texture. |
- Exceptions
-
| std::out_of_range | if texture does not come from this manager. |
| std::invalid_argument | if the sheet was made for a texture of another size. |
◆ sheet()
Sprite sheet of a texture.
- Parameters
-
| texture | Texture to look up. |
- Returns
- Its sheet, or nothing if it has none; valid until the next set_sheet().
◆ size()
| std::size_t aegis2d::render::TextureManager::size |
( |
| ) |
const |
|
noexcept |
Number of names.
- Returns
- Names given to load() and add().
◆ source()
| Rect aegis2d::render::TextureManager::source |
( |
TextureId |
texture, |
|
|
std::size_t |
frame |
|
) |
| const |
Part of a texture to draw for a frame, as Sprite::source takes it.
Turns ecs::Sprite::frame into the rectangle to draw.
- Parameters
-
| texture | Texture of the sprite, or no_texture. |
| frame | Frame number, from 0. |
- Returns
- The frame's rectangle for a sprite sheet; the whole texture for frame 0 of a texture without sheet, or for no_texture.
- Exceptions
-
| std::out_of_range | if the texture has no such frame. |
◆ textures()
| std::size_t aegis2d::render::TextureManager::textures |
( |
| ) |
const |
|
noexcept |
Number of textures created, one per file or added image.
- Returns
- At most size().
◆ watch()
Reloads each file loaded so far whenever it changes on disk.
Registers reload() of every loaded file with hot, whose update() then calls it; files loaded later are not watched until watch() is called again.
- Parameters
-
| hot | Watcher; it must not outlive this manager. |
The documentation for this class was generated from the following file: