Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::render::TextureManager Class Reference

The game's textures, each loaded once and found by a name such as "player". More...

#include <aegis2d/render/texture_manager.hpp>

Public Member Functions

 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.
 

Detailed Description

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():

textures.load("player", core::asset_path("sprites/player.png"));
...
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.

Constructor & Destructor Documentation

◆ TextureManager()

aegis2d::render::TextureManager::TextureManager ( IRenderer &  renderer)
explicitnoexcept

Creates an empty manager whose textures go to renderer.

Parameters
rendererRenderer that creates the textures.

Member Function Documentation

◆ 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
nameName to find the texture with, not empty and not used yet.
imageRGBA image, not empty.
Returns
The new texture.
Exceptions
std::invalid_argumentif 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
nameName given to load() or add().
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
nameName given to load() or add().
Returns
Its texture.
Exceptions
std::out_of_rangeif 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
nameName to find the texture with, not empty.
pathImage file (see Image::load()).
Returns
The texture: the one already loaded when name or the same file was loaded before.
Exceptions
std::invalid_argumentif name is empty, or already names another file or an added image.
ImageErrorif 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
nameName to find the texture with, not empty.
pathImage file.
frameSize of one frame, in pixels.
countNumber of frames; 0 for every whole frame of the grid.
Returns
The texture.
Exceptions
std::invalid_argumentas load() and SpriteSheet::grid().
ImageErrorif the file cannot be read or decoded.

◆ 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
pathFile given to load() or load_sheet(), by any path that leads to it.
Returns
false if no texture was loaded from this file.
Exceptions
ImageErrorif the file cannot be read or decoded; the texture is kept.
std::invalid_argumentif the new image has another size; the texture is kept.

◆ set_sheet()

void aegis2d::render::TextureManager::set_sheet ( TextureId  texture,
SpriteSheet  sheet 
)

Cuts a texture into the frames of a sprite sheet, replacing its sheet if it had one.

Parameters
textureTexture returned by load() or add().
sheetFrames of the texture.
Exceptions
std::out_of_rangeif texture does not come from this manager.
std::invalid_argumentif the sheet was made for a texture of another size.

◆ sheet()

const SpriteSheet * aegis2d::render::TextureManager::sheet ( TextureId  texture) const
noexcept

Sprite sheet of a texture.

Parameters
textureTexture 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
textureTexture of the sprite, or no_texture.
frameFrame 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_rangeif 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()

void aegis2d::render::TextureManager::watch ( core::HotReload &  hot)

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
hotWatcher; it must not outlive this manager.

The documentation for this class was generated from the following file: