15 #ifndef FPLBASE_ASYNC_LOADER_H
16 #define FPLBASE_ASYNC_LOADER_H
24 #include "fplbase/config.h"
25 #include "fplbase/asset.h"
27 #ifdef FPLBASE_BACKEND_STDLIB
30 #include <condition_variable>
41 typedef void *Semaphore;
72 virtual void Load() = 0;
91 bool ok =
data_ !=
nullptr;
115 finalize_callbacks_.push_back(callback);
123 for (
auto it = finalize_callbacks_.begin();
124 it != finalize_callbacks_.end(); ++it) {
127 finalize_callbacks_.clear();
135 std::vector<AssetFinalizedCallback> finalize_callbacks_;
183 #ifdef FPLBASE_BACKEND_SDL
184 void Lock(
const std::function<
void()> &body);
185 template <
typename T>
186 T LockReturn(
const std::function<T()> &body) {
188 Lock([&ret, &body]() { ret = body(); });
194 static int LoaderThread(
void *user_data);
196 std::deque<AsyncAsset *> queue_, done_;
197 #ifdef FPLBASE_BACKEND_SDL
200 Thread worker_thread_;
206 Semaphore job_semaphore_;
207 #elif defined(FPLBASE_BACKEND_STDLIB)
208 std::thread worker_thread_;
210 std::condition_variable job_cv_;
212 #error Need to define FPLBASE_BACKEND_XXX
219 #endif // FPLBASE_ASYNC_LOADER_H
void AddFinalizeCallback(AssetFinalizedCallback callback)
Adds a callback to be called when the asset is finalized.
Definition: async_loader.h:114
void Stop()
Shuts down the loader after completing all pending loads.
virtual bool Finalize()=0
Override with converting the data into the resource.
virtual bool IsValid()=0
Whether this object loaded and finalized correctly. Call after Finalize has been called (by AssetMana...
void StopLoadingWhenComplete()
Ends the loading thread when all jobs are done.
const std::string & filename() const
The name of the file associated with the resource.
Definition: async_loader.h:107
std::function< void()> AssetFinalizedCallback
A function pointer to an asset loaded callback function.
Definition: async_loader.h:51
Base class of all assets that may be managed by Assetmanager.
Definition: asset.h:26
AsyncAsset(const char *filename)
Construct an AsyncAsset with a given file name.
Definition: async_loader.h:59
void CallFinalizeCallback()
Calls app callbacks when an asset is ready to be used.
Definition: async_loader.h:122
void set_filename(const std::string &filename)
Sets the filename that should be loaded.
Definition: async_loader.h:102
const uint8_t * data_
The resource data.
Definition: async_loader.h:133
Handles loading AsyncAsset objects.
Definition: async_loader.h:142
std::string filename_
The resource file name.
Definition: async_loader.h:131
virtual ~AsyncAsset()
AsyncAsset destructor.
Definition: async_loader.h:63
virtual void Load()=0
Override with the actual loading behavior.
AsyncAsset()
Default constructor for an empty AsyncAsset.
Definition: async_loader.h:54
bool TryFinalize()
Call to Finalize any resources that have finished loading.
void QueueJob(AsyncAsset *res)
Queues AsyncResources to be loaded by StartLoading.
void StartLoading()
Launches the loading thread for the previously queued jobs.
bool LoadNow()
Performs a synchronous load by calling Load & Finalize.
Definition: async_loader.h:89
Definition: async_loader.h:48
void PauseLoading()
Pause the loading thread for previously queued jobs.