Created
June 25, 2018 08:54
-
-
Save veryjos/7e1f84cd3df75a668bd54502662fa7ad 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::unique_ptr<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); | |
auto v = new std::vector<char>(); | |
v->resize(length); | |
fin.read(&((*v)[0]), length); | |
promise.set_value(std::unique_ptr<std::vector<char>>(v)); | |
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