we can now load multiple resource packs
This commit is contained in:
parent
4191691e84
commit
31cac13971
@ -1,30 +1,24 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
#define NB_VAL_PER_LINE 256
|
#define NB_VAL_PER_LINE 256
|
||||||
|
|
||||||
|
|
||||||
#define FILE_BEGIN "#include <string>\n\
|
#define FILE_BEGIN "#include <string>\n\
|
||||||
#include <unordered_map>\n\
|
#include <unordered_map>\n\
|
||||||
\n\
|
\n\
|
||||||
namespace Resource {\n\
|
namespace Resource {\n\
|
||||||
std::unordered_map<std::string, const char*> *resourceFilesData;\n"
|
typedef std::unordered_map<std::string, const char*> ResourceMap;\n"
|
||||||
|
|
||||||
|
|
||||||
|
#define FILE_MID ""
|
||||||
|
|
||||||
#define FILE_MID "\nvoid initResourceData()\n\
|
|
||||||
{\n\
|
|
||||||
resourceFilesData = new std::unordered_map<std::string, const char*>();\n"
|
|
||||||
|
|
||||||
#define FILE_END "\n\
|
#define FILE_END "\n\
|
||||||
}\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"
|
}\n"
|
||||||
|
|
||||||
unsigned int n = 0;
|
unsigned int n = 0;
|
||||||
@ -34,7 +28,7 @@ bool addFile(const string &inFile, FILE *out)
|
|||||||
FILE *in = fopen(inFile.c_str(), "r");
|
FILE *in = fopen(inFile.c_str(), "r");
|
||||||
if(in == NULL)
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
fprintf(out, "\nconst char data%d[] = {\n", n);
|
fprintf(out, "\nconst char data%d[] = {\n", n);
|
||||||
@ -58,11 +52,19 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
if(argc < 3)
|
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;
|
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)
|
if(out == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "can't open \"%s\" for writing\n", argv[1]);
|
fprintf(stderr, "can't open \"%s\" for writing\n", argv[1]);
|
||||||
@ -78,13 +80,13 @@ int main(int argc, char *argv[])
|
|||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
else
|
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<n; ++i)
|
for(int i=0; i<n; ++i)
|
||||||
fprintf(out, "\t(*resourceFilesData)[\"%s\"] = data%d;\n", argv[2+i], i);
|
fprintf(out, "\tresourceFilesData[\"%s\"] = data%d;\n", argv[2+i], i);
|
||||||
fprintf(out, FILE_END);
|
fprintf(out, FILE_END);
|
||||||
fclose(out);
|
fclose(out);
|
||||||
printf("successfully created resource file \"%s\".\n", argv[1]);
|
printf("successfully created resource file \"%s\".\n", outFilename.c_str());
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,26 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace Resource {
|
#define RESOURCE_PACK(pack) namespace Resource {\
|
||||||
extern void initResourceData();
|
void getResourcePack_ ## pack(ResourceMap &resourceFilesData);\
|
||||||
extern const char *get(const std::string&);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<std::string, const char*> ResourceMap;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // RES_H
|
#endif // RES_H
|
||||||
|
30
test/res.cpp
30
test/res.cpp
@ -1,30 +0,0 @@
|
|||||||
#include <string>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
namespace Resource {
|
|
||||||
std::unordered_map<std::string, const char*> *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<std::string, const char*>();
|
|
||||||
(*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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,11 +3,14 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
RESOURCE_PACK(res)
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
cout << "Resource test" << endl;
|
cout << "Resource test" << endl;
|
||||||
Resource::initResourceData();
|
Resource::ResourceMap resMap;
|
||||||
const char *str = Resource::get("/data/Qt_workspace/cmaketemplate/test/plop.txt");
|
Resource::getResourcePack_res(resMap);
|
||||||
|
const char *str = resMap["/data/Qt_workspace/cmaketemplate/test/plop.txt"];
|
||||||
if(str != NULL)
|
if(str != NULL)
|
||||||
cout << str << endl;
|
cout << str << endl;
|
||||||
else
|
else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user