utility 2026.2.0
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
72 RessourceProvider(SystemIO &systemInterface,
73 const std::string &basePath = "");
74
78 ~RessourceProvider() = default;
79
90 [[nodiscard]] const std::map<uint32_t,
91 std::shared_ptr<graphic::Material>> &
92 getMaterials() const;
93
104 [[nodiscard]] const std::map<uint32_t,
105 std::shared_ptr<graphic::Texture>> &
106 getTextures() const;
107
118 [[nodiscard]] const std::map<uint32_t,
119 std::shared_ptr<graphic::Model>> &
120 getModels() const;
121
132 [[nodiscard]] const std::map<uint32_t,
133 std::shared_ptr<graphic::Shader>> &
134 getShaders() const;
135
146 [[nodiscard]] const std::map<uint32_t,
147 std::shared_ptr<graphic::CodePoints>> &
149
162 [[nodiscard]] uint64_t version() const noexcept;
163
179 [[nodiscard]] uint32_t getShaderID(const std::string &shaderName) const;
180
197 [[nodiscard]] uint32_t getMaterialID(const std::string &materialName);
198
215 [[nodiscard]] uint32_t getDefaultMaterialID();
216
229 [[nodiscard]] uint32_t getMeshMaterialID();
230
238 std::shared_ptr<graphic::Font> loadFont(const std::string &path);
239
248 std::shared_ptr<graphic::Font>
249 loadFontFromAsset(std::shared_ptr<utility::File> fontAsset);
250
251 // TODO: add loadFontsFromDirectory method to load all fonts from a
252 // directory But needs to be implemented in the SystemIO first to
253 // load all assets from a directory
254
255 // TODO: add loadFontFamily method to load all fonts from a directory
256 // But needs to be implemented in the SystemIO first to load all
257 // assets from a directory
258
268 std::shared_ptr<graphic::Font> loadFontFamilyFromAssets(
269 const std::vector<std::shared_ptr<utility::File>> &fontAssets);
270
280 std::shared_ptr<graphic::Material> loadMaterial(const std::string &path,
281 ShaderType shaderType);
282
293 std::shared_ptr<graphic::Material>
295 std::shared_ptr<utility::File> materialAsset);
296
303 std::shared_ptr<graphic::Texture> loadTexture(const std::string &path);
304
313 std::shared_ptr<graphic::Texture>
314 loadTextureFromAsset(std::shared_ptr<utility::File> textureAsset);
315
316 // TODO: add loadTexturesFromDirectory method to load all textures from
317 // a directory But needs to be implemented in the SystemIO first to
318 // load all assets from a directory
319
335 std::shared_ptr<graphic::Material>
336 loadImageMaterial(const std::string &path);
337
348 std::shared_ptr<graphic::Model> loadModel(
349 const std::string &path,
350 const utility::graphic::PoseF &pose = utility::graphic::PoseF(),
351 const std::string &material = "default_material");
352
364 std::shared_ptr<graphic::Model> loadModelFromAsset(
365 std::shared_ptr<utility::File> modelAsset,
366 const utility::graphic::PoseF &pose = utility::graphic::PoseF(),
367 const std::string &material = "default_material");
368
383 std::shared_ptr<graphic::Model> loadModelFromAsset(
384 std::shared_ptr<utility::File> modelAsset,
385 graphic::Model::ModelType type,
386 const utility::graphic::PoseF &pose = utility::graphic::PoseF(),
387 const std::string &material = "default_material");
388
401 std::shared_ptr<graphic::Model> loadObj(
402 const std::string &path,
403 const utility::graphic::PoseF &pose = utility::graphic::PoseF(),
404 const std::string &material = "default_material");
405
419 std::shared_ptr<graphic::Model> loadObjFromAsset(
420 std::shared_ptr<utility::File> modelAsset,
421 const utility::graphic::PoseF &pose = utility::graphic::PoseF(),
422 const std::string &material = "default_material");
423
435 std::shared_ptr<graphic::Shader>
436 loadShader(const std::string &vertexPath,
437 const std::string &fragmentPath);
438
450 std::shared_ptr<graphic::Shader>
451 loadShaderFromAssets(std::shared_ptr<utility::File> vertexAsset,
452 std::shared_ptr<utility::File> fragmentAsset);
453
460 std::shared_ptr<graphic::CodePoints>
461 loadCodePoints(const std::string &path);
462
470 std::shared_ptr<graphic::CodePoints> loadCodePointsFromAsset(
471 std::shared_ptr<utility::File> codePointsAsset);
472
480 std::unique_ptr<sound::AudioSource>
481 loadAudioSource(const std::string &path);
482
491 std::unique_ptr<sound::AudioSource>
492 loadAudioSourceFromAsset(std::shared_ptr<utility::File> audioAsset);
493
494 protected:
512 std::string buildShaderPath(const std::string &vertexPath,
513 const std::string &fragmentPath) const;
514
528 void onFontAtlasCreated(const std::string &name,
529 std::shared_ptr<graphic::Texture> atlas);
530
546 void registerShader(uint32_t id, const std::string &path,
547 const std::string &vertexPath,
548 std::shared_ptr<graphic::Shader> shader);
549
567 uint32_t getNextID();
568
576 void touch() noexcept;
577
585 uint32_t _currentID = 1;
586
594 uint64_t _version = 0;
595
599 std::map<uint32_t, std::shared_ptr<graphic::Font>> _fonts;
600
605 std::map<uint32_t, std::shared_ptr<graphic::Material>> _materials;
606
610 std::map<uint32_t, std::shared_ptr<graphic::Texture>> _textures;
611
615 std::map<uint32_t, std::shared_ptr<graphic::Model>> _models;
616
620 std::map<uint32_t, std::shared_ptr<graphic::Shader>> _shaders;
621
630 std::unordered_map<std::string, uint32_t> _shaderIDs;
631
636 std::map<uint32_t, std::shared_ptr<graphic::CodePoints>> _codePoints;
637
642 std::map<uint32_t, std::shared_ptr<sound::AudioBuffer>> _audioSources;
643
648 std::unordered_map<std::string, uint32_t> _elementsIDs;
649
662
675 std::string _basePath;
676
685 utility::sound::AudioManager _audioManager;
686
687 private:
702 std::string resolvePath(const std::string &path) const;
703
717 std::shared_ptr<utility::File> loadDefaultTextureAsset();
718
719 using Loggable::getLogger;
720 };
721} // 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