VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
visutwin::canvas::ResourceLoader Class Reference

Async resource loader with background I/O thread, pixel decoding, and main-thread callbacks. More...

#include <framework/handlers/resourceLoader.h>

Public Member Functions

void addHandler (const std::string &type, std::unique_ptr< ResourceHandler > handler)
bool hasPending () const
void load (const std::string &url, const std::string &type, LoadSuccessCallback onSuccess, LoadErrorCallback onError=nullptr)
ResourceLoaderoperator= (const ResourceLoader &)=delete
void processCompletions (int maxCompletions=0)
void removeHandler (const std::string &type)
 ResourceLoader (const ResourceLoader &)=delete
 ResourceLoader (const std::shared_ptr< Engine > &engine)
void shutdown ()
 ~ResourceLoader ()

Detailed Description

Async resource loader with background I/O thread, pixel decoding, and main-thread callbacks.

Asynchronous resource loader with a single background I/O thread.

Thread model

┌──────────────┐ load() ┌────────────────┐ │ Main thread │ ────────▶ │ Worker thread │ │ │ │ (I/O + decode) │ │ process │ ◀──────── │ │ │ Completions()│ callback └────────────────┘ └──────────────┘

  1. Call load() from the main thread to queue a request.
  2. The worker thread picks up the request, calls the registered ResourceHandler::load(), and pushes the result to a completion queue.
  3. Call processCompletions() each frame (from Engine::update) to dispatch success / error callbacks on the main thread.

Usage:

loader->addHandler("texture", std::make_unique<TextureResourceHandler>());
loader->addHandler("container", std::make_unique<ContainerResourceHandler>());
loader->load("path/to/img.png", "texture",
[](auto data) { ... GPU upload ... },
[](auto& err) { ... handle error ... });
// each frame:
loader->processCompletions();

Definition at line 132 of file resourceLoader.h.

Constructor & Destructor Documentation

◆ ResourceLoader() [1/2]

visutwin::canvas::ResourceLoader::ResourceLoader ( const std::shared_ptr< Engine > & engine)
explicit

Definition at line 24 of file resourceLoader.cpp.

Referenced by operator=(), and ResourceLoader().

◆ ~ResourceLoader()

visutwin::canvas::ResourceLoader::~ResourceLoader ( )

Definition at line 32 of file resourceLoader.cpp.

References shutdown().

◆ ResourceLoader() [2/2]

visutwin::canvas::ResourceLoader::ResourceLoader ( const ResourceLoader & )
delete

References ResourceLoader().

Member Function Documentation

◆ addHandler()

void visutwin::canvas::ResourceLoader::addHandler ( const std::string & type,
std::unique_ptr< ResourceHandler > handler )

Register a handler for a given asset type (e.g. "texture", "container").

Definition at line 37 of file resourceLoader.cpp.

◆ hasPending()

bool visutwin::canvas::ResourceLoader::hasPending ( ) const

Returns true if there are pending or in-flight requests.

Definition at line 106 of file resourceLoader.cpp.

◆ load()

void visutwin::canvas::ResourceLoader::load ( const std::string & url,
const std::string & type,
LoadSuccessCallback onSuccess,
LoadErrorCallback onError = nullptr )

Queue an asynchronous load request.

The background thread calls the appropriate ResourceHandler::load(). On the next processCompletions() call the onSuccess or onError callback is invoked on the main thread.

Parameters
urlLocal file-system path.
typeAsset type string used to look up the handler.
onSuccessCalled on the main thread with loaded data.
onErrorCalled on the main thread with an error message.

Definition at line 47 of file resourceLoader.cpp.

Referenced by visutwin::canvas::Asset::loadAsync().

◆ operator=()

ResourceLoader & visutwin::canvas::ResourceLoader::operator= ( const ResourceLoader & )
delete

References ResourceLoader().

◆ processCompletions()

void visutwin::canvas::ResourceLoader::processCompletions ( int maxCompletions = 0)

Must be called from the main thread each frame (from Engine::update). Dispatches completed load callbacks.

Parameters
maxCompletionsMaximum number of callbacks to dispatch per call. 0 means unlimited (drain all). Use 1 to spread heavy main-thread work (e.g. GLB parsing) across frames and avoid stalling the event loop.

Definition at line 58 of file resourceLoader.cpp.

◆ removeHandler()

void visutwin::canvas::ResourceLoader::removeHandler ( const std::string & type)

Remove a handler for a given asset type.

Definition at line 42 of file resourceLoader.cpp.

◆ shutdown()

void visutwin::canvas::ResourceLoader::shutdown ( )

Shut down the worker thread. Safe to call multiple times.

Definition at line 93 of file resourceLoader.cpp.

Referenced by ~ResourceLoader().


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