utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
ressource_provider.hpp
1#pragma once
2
3#include <cstdint>
4
5#include <utility/system_io/file.hpp>
6#include <utility/system_io/system_io.hpp>
7
8#include <utility/graphic/pose.hpp>
9#include <utility/graphic/text/font.hpp>
10#include <utility/graphic/text/code_points.hpp>
11#include <utility/graphic/text/text_material.hpp>
12#include <utility/graphic/material.hpp>
13#include <utility/graphic/texture.hpp>
14#include <utility/graphic/model.hpp>
15#include <utility/graphic/shader.hpp>
16
17#include <utility/sound/audio_source.hpp>
18#include <utility/sound/decoder/decoder_registry.hpp>
19#include <utility/sound/audio_manager.hpp>
20
23
24namespace utility
25{
37 protected utility::logging::Loggable<RessourceProvider,
38 utility::logging::DefaultLogger>
39 {
40 public:
50 enum class ShaderType { TEXT_SHADER, MESH_SHADER };
51
65 RessourceProvider(SystemIO &systemInterface,
66 const std::string &basePath = "");
67
71 ~RessourceProvider() = default;
72
83 [[nodiscard]] const std::map<uint32_t,
84 std::shared_ptr<graphic::Material>> &
85 getMaterials() const;
86
97 [[nodiscard]] const std::map<uint32_t,
98 std::shared_ptr<graphic::Texture>> &
99 getTextures() const;
100
111 [[nodiscard]] const std::map<uint32_t,
112 std::shared_ptr<graphic::Model>> &
113 getModels() const;
114
125 [[nodiscard]] const std::map<uint32_t,
126 std::shared_ptr<graphic::Shader>> &
127 getShaders() const;
128
139 [[nodiscard]] const std::map<uint32_t,
140 std::shared_ptr<graphic::CodePoints>> &
142
155 [[nodiscard]] uint64_t version() const noexcept;
156
172 [[nodiscard]] uint32_t getShaderID(const std::string &shaderName) const;
173
184 [[nodiscard]] uint32_t getMaterialID(const std::string &materialName);
185
198 [[nodiscard]] uint32_t getDefaultMaterialID();
199
205 [[nodiscard]] uint32_t getMeshMaterialID();
206
214 std::shared_ptr<graphic::Font> loadFont(const std::string &path);
215
224 std::shared_ptr<graphic::Font>
225 loadFontFromAsset(std::shared_ptr<utility::File> fontAsset);
226
227 // TODO: add loadFontsFromDirectory method to load all fonts from a
228 // directory But needs to be implemented in the SystemIO first to
229 // load all assets from a directory
230
231 // TODO: add loadFontFamily method to load all fonts from a directory
232 // But needs to be implemented in the SystemIO first to load all
233 // assets from a directory
234
244 std::shared_ptr<graphic::Font> loadFontFamilyFromAssets(
245 const std::vector<std::shared_ptr<utility::File>> &fontAssets);
246
256 std::shared_ptr<graphic::Material> loadMaterial(const std::string &path,
257 ShaderType shaderType);
258
269 std::shared_ptr<graphic::Material>
271 std::shared_ptr<utility::File> materialAsset);
272
279 std::shared_ptr<graphic::Texture> loadTexture(const std::string &path);
280
289 std::shared_ptr<graphic::Texture>
290 loadTextureFromAsset(std::shared_ptr<utility::File> textureAsset);
291
292 // TODO: add loadTexturesFromDirectory method to load all textures from
293 // a directory But needs to be implemented in the SystemIO first to
294 // load all assets from a directory
295
311 std::shared_ptr<graphic::Material>
312 loadImageMaterial(const std::string &path);
313
324 std::shared_ptr<graphic::Model> loadModel(
325 const std::string &path,
326 const utility::graphic::PoseF &pose = utility::graphic::PoseF(),
327 const std::string &material = "default_material");
328
340 std::shared_ptr<graphic::Model> loadModelFromAsset(
341 std::shared_ptr<utility::File> modelAsset,
342 const utility::graphic::PoseF &pose = utility::graphic::PoseF(),
343 const std::string &material = "default_material");
344
359 std::shared_ptr<graphic::Model> loadModelFromAsset(
360 std::shared_ptr<utility::File> modelAsset,
361 graphic::Model::ModelType type,
362 const utility::graphic::PoseF &pose = utility::graphic::PoseF(),
363 const std::string &material = "default_material");
364
377 std::shared_ptr<graphic::Model> loadObj(
378 const std::string &path,
379 const utility::graphic::PoseF &pose = utility::graphic::PoseF(),
380 const std::string &material = "default_material");
381
395 std::shared_ptr<graphic::Model> loadObjFromAsset(
396 std::shared_ptr<utility::File> modelAsset,
397 const utility::graphic::PoseF &pose = utility::graphic::PoseF(),
398 const std::string &material = "default_material");
399
411 std::shared_ptr<graphic::Shader>
412 loadShader(const std::string &vertexPath,
413 const std::string &fragmentPath);
414
426 std::shared_ptr<graphic::Shader>
427 loadShaderFromAssets(std::shared_ptr<utility::File> vertexAsset,
428 std::shared_ptr<utility::File> fragmentAsset);
429
436 std::shared_ptr<graphic::CodePoints>
437 loadCodePoints(const std::string &path);
438
446 std::shared_ptr<graphic::CodePoints> loadCodePointsFromAsset(
447 std::shared_ptr<utility::File> codePointsAsset);
448
456 std::unique_ptr<sound::AudioSource>
457 loadAudioSource(const std::string &path);
458
467 std::unique_ptr<sound::AudioSource>
468 loadAudioSourceFromAsset(std::shared_ptr<utility::File> audioAsset);
469
470 protected:
488 std::string buildShaderPath(const std::string &vertexPath,
489 const std::string &fragmentPath) const;
490
504 void onFontAtlasCreated(const std::string &name,
505 std::shared_ptr<graphic::Texture> atlas);
506
522 void registerShader(uint32_t id, const std::string &path,
523 const std::string &vertexPath,
524 std::shared_ptr<graphic::Shader> shader);
525
543 uint32_t getNextID();
544
552 void touch() noexcept;
553
561 uint32_t _currentID = 1;
562
570 uint64_t _version = 0;
571
575 std::map<uint32_t, std::shared_ptr<graphic::Font>> _fonts;
576
581 std::map<uint32_t, std::shared_ptr<graphic::Material>> _materials;
582
586 std::map<uint32_t, std::shared_ptr<graphic::Texture>> _textures;
587
591 std::map<uint32_t, std::shared_ptr<graphic::Model>> _models;
592
596 std::map<uint32_t, std::shared_ptr<graphic::Shader>> _shaders;
597
606 std::unordered_map<std::string, uint32_t> _shaderIDs;
607
612 std::map<uint32_t, std::shared_ptr<graphic::CodePoints>> _codePoints;
613
618 std::map<uint32_t, std::shared_ptr<sound::AudioBuffer>> _audioSources;
619
624 std::unordered_map<std::string, uint32_t> _elementsIDs;
625
638
651 std::string _basePath;
652
661 utility::sound::AudioManager _audioManager;
662
663 private:
678 std::string resolvePath(const std::string &path) const;
679
680 using Loggable::getLogger;
681 };
682} // namespace utility
The File class represents a I/O file asset.
Definition file.hpp:48
The RessourceProvider class is responsible for managing and loading various resources such as fonts,...
const std::map< uint32_t, std::shared_ptr< graphic::Shader > > & getShaders() const
Retrieves a map of loaded shaders.
std::map< uint32_t, std::shared_ptr< graphic::Material > > _materials
Internal map to store loaded materials for efficient retrieval.
std::string _basePath
An optional base path to prepend to resource paths when loading resources.
std::shared_ptr< graphic::Font > loadFontFromAsset(std::shared_ptr< utility::File > fontAsset)
Loads a font resource from a specified asset.
std::shared_ptr< graphic::Model > loadModelFromAsset(std::shared_ptr< utility::File > modelAsset, const utility::graphic::PoseF &pose=utility::graphic::PoseF(), const std::string &material="default_material")
Loads a model resource from a specified asset.
std::string buildShaderPath(const std::string &vertexPath, const std::string &fragmentPath) const
Builds a unique shader name based on the vertex and fragment shader file paths.
std::shared_ptr< graphic::Shader > loadShader(const std::string &vertexPath, const std::string &fragmentPath)
Loads a shader resource from specified vertex and fragment shader file paths.
const std::map< uint32_t, std::shared_ptr< graphic::CodePoints > > & getCodePoints() const
Retrieves a map of loaded code points resources.
const std::map< uint32_t, std::shared_ptr< graphic::Model > > & getModels() const
Retrieves a map of loaded models.
std::map< uint32_t, std::shared_ptr< graphic::Font > > _fonts
Internal maps to store loaded fonts for efficient retrieval.
uint32_t _currentID
Internal counter for generating unique IDs for resources.
uint64_t _version
Monotonic counter bumped on every mutation of the resource maps.
ShaderType
Enumeration for different shader types that can be associated with materials.
std::shared_ptr< graphic::Model > loadObj(const std::string &path, const utility::graphic::PoseF &pose=utility::graphic::PoseF(), const std::string &material="default_material")
Loads an OBJ model resource from a specified file path.
std::shared_ptr< graphic::Font > loadFont(const std::string &path)
Loads a font resource from a specified file path.
uint32_t getDefaultMaterialID()
Retrieves the unique ID of the default material.
std::shared_ptr< graphic::Texture > loadTexture(const std::string &path)
Loads a texture resource from a specified file path.
std::unordered_map< std::string, uint32_t > _elementsIDs
Internal map to store resource IDs for efficient lookup based on file paths.
std::shared_ptr< graphic::Font > loadFontFamilyFromAssets(const std::vector< std::shared_ptr< utility::File > > &fontAssets)
Loads a font family resource from a vector of font assets.
std::shared_ptr< graphic::Material > loadImageMaterial(const std::string &path)
Loads an image texture and wraps it in a material bound to the default shader.
void touch() noexcept
Records a mutation of the provider's resource maps.
utility::sound::AudioManager _audioManager
Internal audio manager instance for managing audio operations.
RessourceProvider(SystemIO &systemInterface, const std::string &basePath="")
Constructs a RessourceProvider object with a reference to a SystemIO instance.
std::unique_ptr< sound::AudioSource > loadAudioSourceFromAsset(std::shared_ptr< utility::File > audioAsset)
Loads an audio source resource from a specified asset.
void onFontAtlasCreated(const std::string &name, std::shared_ptr< graphic::Texture > atlas)
Registers a font texture atlas produced by a Font instance.
std::map< uint32_t, std::shared_ptr< graphic::Shader > > _shaders
Internal map to store loaded shaders for efficient retrieval.
std::shared_ptr< graphic::Model > loadModel(const std::string &path, const utility::graphic::PoseF &pose=utility::graphic::PoseF(), const std::string &material="default_material")
Loads a model resource from a specified file path.
const std::map< uint32_t, std::shared_ptr< graphic::Texture > > & getTextures() const
Retrieves a map of loaded textures.
const std::map< uint32_t, std::shared_ptr< graphic::Material > > & getMaterials() const
Retrieves a map of loaded materials.
std::shared_ptr< graphic::Shader > loadShaderFromAssets(std::shared_ptr< utility::File > vertexAsset, std::shared_ptr< utility::File > fragmentAsset)
Loads a shader resource from specified vertex and fragment shader assets.
std::shared_ptr< graphic::CodePoints > loadCodePointsFromAsset(std::shared_ptr< utility::File > codePointsAsset)
Loads a code points resource from a specified asset.
std::shared_ptr< graphic::Texture > loadTextureFromAsset(std::shared_ptr< utility::File > textureAsset)
Loads a texture resource from a specified asset.
std::map< uint32_t, std::shared_ptr< graphic::Model > > _models
Internal map to store loaded models for efficient retrieval.
uint32_t getMeshMaterialID()
Retrieves the unique ID of the mesh material.
std::unique_ptr< sound::AudioSource > loadAudioSource(const std::string &path)
Loads an audio source resource from a specified file path.
uint32_t getMaterialID(const std::string &materialName)
Retrieves the unique material ID associated with a given material name.
std::unordered_map< std::string, uint32_t > _shaderIDs
Shader-only index mapping a shader name to its id.
uint32_t getNextID()
Retrieves the next unique ID for a resource.
std::map< uint32_t, std::shared_ptr< graphic::CodePoints > > _codePoints
Internal map to store loaded code points resources for efficient retrieval.
uint64_t version() const noexcept
Retrieves a monotonic counter bumped on every mutation of the provider's resource maps.
std::map< uint32_t, std::shared_ptr< sound::AudioBuffer > > _audioSources
Internal map to store loaded audio buffers for efficient retrieval.
~RessourceProvider()=default
Destructs the RessourceProvider object.
uint32_t getShaderID(const std::string &shaderName) const
Retrieves the unique shader ID associated with a given shader name.
std::shared_ptr< graphic::CodePoints > loadCodePoints(const std::string &path)
Loads a code points resource from a specified file path.
std::shared_ptr< graphic::Material > loadMaterialFromAsset(ShaderType shaderType, std::shared_ptr< utility::File > materialAsset)
Loads a material resource from a specified asset.
void registerShader(uint32_t id, const std::string &path, const std::string &vertexPath, std::shared_ptr< graphic::Shader > shader)
Registers a loaded shader in the internal resource maps and the shader-only lookup index used by getS...
std::shared_ptr< graphic::Material > loadMaterial(const std::string &path, ShaderType shaderType)
Loads a material resource from a specified file path.
std::map< uint32_t, std::shared_ptr< graphic::Texture > > _textures
Internal map to store loaded textures for efficient retrieval.
std::shared_ptr< graphic::Model > loadObjFromAsset(std::shared_ptr< utility::File > modelAsset, const utility::graphic::PoseF &pose=utility::graphic::PoseF(), const std::string &material="default_material")
Loads an OBJ model resource from a specified asset.
SystemIO & _systemInterface
Reference to the SystemIO instance used for loading assets from file paths.
The SystemIO class is an abstract base class for managing file assets.
Definition system_io.hpp:49
Mixin base class providing logging capabilities to derived classes.
Definition loggable.hpp:55
Loggable(void)
Construct a Loggable with the specified LoggerType.
Definition loggable.hpp:64
Platform-specific default logger alias.
Mixin class to provide logging capabilities to derived classes.
The utility namespace contains classes and functions for the utility project.
Definition cache.hpp:34