diff --git a/resource/main.cpp b/resource/main.cpp index 6b40f74..ad12c2e 100644 --- a/resource/main.cpp +++ b/resource/main.cpp @@ -1,30 +1,24 @@ #include #include +#include using namespace std; #define NB_VAL_PER_LINE 256 + #define FILE_BEGIN "#include \n\ #include \n\ \n\ namespace Resource {\n\ -std::unordered_map *resourceFilesData;\n" +typedef std::unordered_map ResourceMap;\n" -#define FILE_MID "\nvoid initResourceData()\n\ -{\n\ - resourceFilesData = new std::unordered_map();\n" +#define FILE_MID "" + + #define FILE_END "\n\ }\n\ -\n\ -const char* get(const std::string &fileName)\n\ -{\n\ - if(resourceFilesData->count(fileName) > 0)\n\ - return (*resourceFilesData)[fileName];\n\ - else\n\ - return NULL;\n\ -}\n\ }\n" unsigned int n = 0; @@ -34,7 +28,7 @@ bool addFile(const string &inFile, FILE *out) FILE *in = fopen(inFile.c_str(), "r"); if(in == NULL) { - fprintf(stderr, "can't open \"%s\" for readingn", inFile.c_str()); + fprintf(stderr, "can't open \"%s\" for reading\n", inFile.c_str()); return false; } fprintf(out, "\nconst char data%d[] = {\n", n); @@ -58,11 +52,19 @@ int main(int argc, char *argv[]) { if(argc < 3) { - printf("usage : %s RESOURCE_FILE.cpp [LIST_OF_FILES_TO_BAKE]\n", argv[0]); + printf("usage : %s RESOURCE_PACK_NAME [LIST_OF_FILES_TO_BAKE]\n", argv[0]); return EXIT_SUCCESS; } - FILE *out = fopen(argv[1], "w"); + string outFilename(argv[1]); + string packName = outFilename.substr(outFilename.find_last_of('/')+1); + packName = packName.substr(0, packName.find_first_of('.')); + if(!regex_match(packName, regex("[a-zA-Z0-9]+"))) + { + printf("error : \"%s\" is an invalid resource pack name, it must match this regex : [a-zA-Z0-9]+\n", packName.c_str()); + return EXIT_SUCCESS; + } + FILE *out = fopen(outFilename.c_str(), "w"); if(out == NULL) { fprintf(stderr, "can't open \"%s\" for writing\n", argv[1]); @@ -78,13 +80,13 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } else - printf("successfully added \"%s\" to \"%s\".\n", argv[i], argv[1]); + printf("successfully added \"%s\" to \"%s\".\n", argv[i], packName.c_str()); } - fprintf(out, FILE_MID); + fprintf(out, "\nvoid getResourcePack_%s(ResourceMap &resourceFilesData)\n{\n", packName.c_str()); for(int i=0; i #include -namespace Resource { - extern void initResourceData(); - extern const char *get(const std::string&); +#define RESOURCE_PACK(pack) namespace Resource {\ +void getResourcePack_ ## pack(ResourceMap &resourceFilesData);\ } +/** + * To load an attached resource pack file, you must declare the pack with : + * RESOURCE_PACK(pack_name) + * it will allow you to use the function : + * getResourcePack_pack_name(ResourceMap &map) + * This function will give access the the embedded data through its original file name, + * using the provided map. + * + * example : + * RESOURCE_PACK(shaders) // declaring the resource pack function + * Resource::ResourceMap myShaders; // defining a resource map + * Resource::getResourcePack(myShaders); // putting the resources of the resource pack in the map + * string *shader = new string(myShaders["shaders/shaderFile.glsl"]); // getting a resource file + */ +namespace Resource { + typedef std::unordered_map ResourceMap; +} #endif // RES_H diff --git a/test/res.cpp b/test/res.cpp deleted file mode 100644 index 2dab7e3..0000000 --- a/test/res.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include - -namespace Resource { -std::unordered_map *resourceFilesData; - -const char data0[] = { - 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64,0x20,0x21,0xa -}; - -const char data1[] = { - 0x54,0x68,0x69,0x73,0x20,0x69,0x73,0x20,0x61,0x20,0x74,0x65,0x73,0x73,0x73,0x74,0xa,0x66,0x65,0x66,0x65,0x61,0xa -}; - -void initResourceData() -{ - resourceFilesData = new std::unordered_map(); - (*resourceFilesData)["/data/Qt_workspace/cmaketemplate/test/plop.txt"] = data0; - (*resourceFilesData)["/data/Qt_workspace/cmaketemplate/test/plop2.txt"] = data1; - -} - -const char* get(const std::string &fileName) -{ - if(resourceFilesData->count(fileName) > 0) - return (*resourceFilesData)[fileName]; - else - return NULL; -} -} diff --git a/test/test.cpp b/test/test.cpp index 95b6f97..81b8bd2 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -3,11 +3,14 @@ using namespace std; +RESOURCE_PACK(res) + int main() { - cout << "Resource test" << endl; - Resource::initResourceData(); - const char *str = Resource::get("/data/Qt_workspace/cmaketemplate/test/plop.txt"); + cout << "Resource test" << endl; + Resource::ResourceMap resMap; + Resource::getResourcePack_res(resMap); + const char *str = resMap["/data/Qt_workspace/cmaketemplate/test/plop.txt"]; if(str != NULL) cout << str << endl; else