Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
texture_manager.hpp
Go to the documentation of this file.
1
3#pragma once
4
6#include <aegis2d/render/export.hpp>
10
11#include <cstddef>
12#include <filesystem>
13#include <functional>
14#include <map>
15#include <optional>
16#include <string>
17#include <string_view>
18
19namespace aegis2d::render {
20
33class AEGIS2D_RENDER_EXPORT TextureManager {
34public:
37 explicit TextureManager(IRenderer& renderer) noexcept;
38
47 TextureId load(std::string_view name, const std::filesystem::path& path);
48
54 TextureId add(std::string_view name, const Image& image);
55
65 TextureId load_sheet(std::string_view name, const std::filesystem::path& path,
66 TextureSize frame, std::size_t count = 0);
67
77 bool reload(const std::filesystem::path& path);
78
84
91 void set_sheet(TextureId texture, SpriteSheet sheet);
92
96 [[nodiscard]] const SpriteSheet* sheet(TextureId texture) const noexcept;
97
105 [[nodiscard]] Rect source(TextureId texture, std::size_t frame) const;
106
111 [[nodiscard]] TextureId get(std::string_view name) const;
112
116 [[nodiscard]] std::optional<TextureId> find(std::string_view name) const;
117
120 [[nodiscard]] std::size_t size() const noexcept;
121
124 [[nodiscard]] std::size_t textures() const noexcept;
125
126private:
127 std::reference_wrapper<IRenderer> renderer_;
128 std::map<std::string, TextureId, std::less<>> names_;
129 std::map<std::filesystem::path, TextureId> files_;
130 std::map<TextureId, SpriteSheet> sheets_;
131 std::size_t created_ = 0;
132};
133
134} // namespace aegis2d::render
Watches files and reloads each one with its own function when it changes.
Definition hot_reload.hpp:40
What the game draws with, whatever the graphics API behind it.
Definition renderer.hpp:81
How a texture is cut into frames, numbered from 0.
Definition sprite_sheet.hpp:37
The game's textures, each loaded once and found by a name such as "player".
Definition texture_manager.hpp:33
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.
TextureId get(std::string_view name) const
Texture of a name.
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.
Rect source(TextureId texture, std::size_t frame) const
Part of a texture to draw for a frame, as Sprite::source takes it.
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::optional< TextureId > find(std::string_view name) const
Looks a name up.
const SpriteSheet * sheet(TextureId texture) const noexcept
Sprite sheet of a texture.
TextureManager(IRenderer &renderer) noexcept
Creates an empty manager whose textures go to renderer.
void watch(core::HotReload &hot)
Reloads each file loaded so far whenever it changes on disk.
bool reload(const std::filesystem::path &path)
Reads a loaded file again and replaces its texture's image.
void set_sheet(TextureId texture, SpriteSheet sheet)
Cuts a texture into the frames of a sprite sheet, replacing its sheet if it had one.
Files reloaded while the game runs when they change on disk, in debug builds.
Images in memory: loaded from files, generated by the engine, saved as PNG.
OpenGL renderer: sprites, text and camera (client only).
Definition bitmap_font.hpp:16
std::uint32_t TextureId
Identifier of a texture loaded by the resource manager.
Definition renderer.hpp:32
Definition endpoint.hpp:85
Backend-independent renderer interface and the sprite it draws.
Frames of a sprite sheet: the rectangles of a texture that a sprite shows in turn.
RGBA image with 8 bits per channel, ready to become a texture.
Definition image.hpp:25
Axis-aligned rectangle.
Definition renderer.hpp:20
Width and height of a texture, in pixels.
Definition renderer.hpp:38