Newer
Older
Import / applications / HighwayDash / ports / Framework / ResourceLoader.h
#ifndef RESOURCE_LOADER_H
#define RESOURCE_LOADER_H


#include <mutex>
#include <condition_variable>
#include <vector>
#include <atomic>
#include <functional>


using ByteArray = std::vector<uint8_t>;


struct Resource
{
    virtual ~Resource();
    virtual void Load() = 0;

    template <typename T>
    bool operator==(T& other) {
      return false;
    }

    virtual bool operator==(Resource&);
    bool IsLoaded();
    void Wait();

    enum State {
        Pending,
        Loading,
        CallbackPending,
        Complete
    };
    
    std::mutex                      mutex;
    std::condition_variable         cond;
    std::atomic<State>              state;
    std::function<void(Resource*)>  callback;
    ByteArray                       data;
};


// Async API
Resource* LoadFileAsync(const char* filename, bool onlineAsset, std::function<void(Resource*)> callback);


// Non-async API
Resource* LoadFile(const char* filename, bool onlineAsset = false);


// Call periodically to allow dispatch of pending callbacks on the main thread (so things like OpenGL are happy)
void ResourceLoaderUpdate(float dt);


#endif // RESOURCE_LOADER_H