#ifndef POLYVOX_FILAMENT_LINUX_RESOURCE_LOADER_H #define POLYVOX_FILAMENT_LINUX_RESOURCE_LOADER_H #include #include #include #include #include #include #include #include #include "ResourceBuffer.hpp" using namespace std; static map _file_assets; static uint32_t _i = 0; ResourceBuffer loadResource(const char* name) { char cwd[PATH_MAX]; if (getcwd(cwd, sizeof(cwd)) != NULL) { std::cout << "Current working dir: " << cwd << std::endl; } string name_str(name); auto id = _i++; if (name_str.rfind("file://", 0) == 0) { name_str = name_str.substr(7); } name_str = string(cwd) + string("/") + name_str; std::cout << "Loading resource at " << name_str.c_str() << std::endl; streampos length; ifstream is(name_str, ios::binary); if(!is) { std::cout << "Failed to find resource at file path " << name_str.c_str() << std::endl; return ResourceBuffer(nullptr, 0, -1); } is.seekg (0, ios::end); length = is.tellg(); char * buffer; buffer = new char [length]; is.seekg (0, ios::beg); is.read (buffer, length); is.close(); _file_assets[id] = buffer; return ResourceBuffer(buffer, length, id); } void freeResource(uint32_t id) { std::cout << "Freeing resource " << id << std::endl; auto it = _file_assets.find(id); if (it != _file_assets.end()) { free(it->second); } } #endif