Created
June 25, 2018 16:43
-
-
Save veryjos/5caa9eefb3b8c8102d1b20493bd0b6b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "BabyDI.h" | |
#include "ResourceDaemon.h" | |
#include "resources/ImageResourceFactory.h" | |
#include <fstream> | |
using namespace tdrp; | |
void ConfigureDI() { | |
ResourceDataProvider* dataProvider = new ResourceDataProvider { | |
[](const IResourceRecord& record) { | |
std::promise<std::vector<char>> promise; | |
printf("loading %s\n", record.path.c_str()); | |
auto fin = std::ifstream(record.path, std::ifstream::in | std::ifstream::binary); | |
fin.seekg(0, fin.end); | |
int length = fin.tellg(); | |
fin.seekg(0, fin.beg); | |
std::vector<char> data; | |
data.resize(length); | |
fin.read(&data[0], length); | |
promise.set_value(data); | |
return promise.get_future(); | |
} | |
}; | |
auto daemon = new ResourceDaemon( | |
std::unique_ptr<ResourceDataProvider>(dataProvider), | |
{ | |
new ImageResourceFactory() | |
} | |
); | |
PROVIDE(ResourceDaemon, daemon); | |
} | |
int main(int argc, char* argv[]) { | |
ConfigureDI(); | |
// Get a reference to the resource daemon | |
auto resourceDaemon = BabyDI::Get<ResourceDaemon>(); | |
auto imageHandle = resourceDaemon->Load<Image>("shit.png"); | |
// Wait while the image is being loaded/deserialized | |
while (!imageHandle.IsValid()) {}; | |
auto img = imageHandle.Lock(); | |
printf("%d : %d\n", img->width, img->height); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment