added file size and Reader to ResourcePack system
This commit is contained in:
parent
6fa86842bd
commit
81b3a5ffef
@ -10,16 +10,14 @@ using namespace std;
|
|||||||
#include <unordered_map>\n\
|
#include <unordered_map>\n\
|
||||||
\n\
|
\n\
|
||||||
namespace Resource {\n\
|
namespace Resource {\n\
|
||||||
typedef std::unordered_map<std::string, const char*> ResourceMap;\n"
|
typedef std::unordered_map<std::string, const char*> ResourceMap;\n\
|
||||||
|
typedef std::unordered_map<std::string, size_t> ResourceSizeMap;\n"
|
||||||
#define FILE_END "\n\
|
|
||||||
}\n\
|
|
||||||
}\n"
|
|
||||||
|
|
||||||
unsigned int n = 0;
|
unsigned int n = 0;
|
||||||
|
|
||||||
bool addFile(const string &inFile, FILE *out)
|
bool addFile(const string &inFile, FILE *out)
|
||||||
{
|
{
|
||||||
|
unsigned int size = 0;
|
||||||
FILE *in = fopen(inFile.c_str(), "r");
|
FILE *in = fopen(inFile.c_str(), "r");
|
||||||
if(in == NULL)
|
if(in == NULL)
|
||||||
{
|
{
|
||||||
@ -32,12 +30,14 @@ bool addFile(const string &inFile, FILE *out)
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
nbRead = fread(ptr, sizeof(char), NB_VAL_PER_LINE, in);
|
nbRead = fread(ptr, sizeof(char), NB_VAL_PER_LINE, in);
|
||||||
|
size += nbRead;
|
||||||
for(unsigned int i=0; i<nbRead; ++i)
|
for(unsigned int i=0; i<nbRead; ++i)
|
||||||
fprintf(out, i ? ",0x%x" : "\t0x%x", ptr[i]);
|
fprintf(out, i ? ",0x%x" : "\t0x%x", ptr[i]);
|
||||||
fprintf(out, ",\n");
|
fprintf(out, ",\n");
|
||||||
}
|
}
|
||||||
while(nbRead == NB_VAL_PER_LINE);
|
while(nbRead == NB_VAL_PER_LINE);
|
||||||
fprintf(out, "\t0x00}; // additionnal byte used to null-terminate ascii data\n");
|
fprintf(out, "\t0x00}; // additionnal byte used to null-terminate ascii data\n");
|
||||||
|
fprintf(out, "size_t data%d_size = %d;\n", n, size);
|
||||||
++n;
|
++n;
|
||||||
fclose(in);
|
fclose(in);
|
||||||
return true;
|
return true;
|
||||||
@ -93,10 +93,19 @@ int main(int argc, char *argv[])
|
|||||||
offsets.push_back(offset);
|
offsets.push_back(offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// create the function initializing the data pointers map
|
||||||
fprintf(out, "\nvoid getResourcePack_%s(ResourceMap &resourceFilesData)\n{\n", packName.c_str());
|
fprintf(out, "\nvoid getResourcePack_%s(ResourceMap &resourceFilesData)\n{\n", packName.c_str());
|
||||||
for(int i=0; i<int(n); ++i)
|
for(int i=0; i<int(n); ++i)
|
||||||
fprintf(out, "\tresourceFilesData[\"%s\"] = data%d;\n", argv[3+i]+offsets[i], i);
|
fprintf(out, "\tresourceFilesData[\"%s\"] = data%d;\n", argv[3+i]+offsets[i], i);
|
||||||
fprintf(out, FILE_END);
|
fprintf(out, "}\n");
|
||||||
|
|
||||||
|
// create the function initializing the data size map
|
||||||
|
fprintf(out, "\nvoid getResourceSizes_%s(ResourceSizeMap &resourceFilesSizes)\n{\n", packName.c_str());
|
||||||
|
for(int i=0; i<int(n); ++i)
|
||||||
|
fprintf(out, "\tresourceFilesSizes[\"%s\"] = data%d_size;\n", argv[3+i]+offsets[i], i);
|
||||||
|
fprintf(out, "}\n");
|
||||||
|
|
||||||
|
fprintf(out, "}\n");
|
||||||
fclose(out);
|
fclose(out);
|
||||||
printf("successfully created resource file \"%s\".\n", outFilename.c_str());
|
printf("successfully created resource file \"%s\".\n", outFilename.c_str());
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -3,19 +3,26 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
#define RESOURCE_PACK(pack) namespace Resource {\
|
#define RESOURCE_PACK(pack) namespace Resource {\
|
||||||
void getResourcePack_ ## pack(ResourceMap &resourceFilesData);\
|
void getResourcePack_ ## pack(ResourceMap &resourceFilesData);\
|
||||||
|
void getResourceSizes_ ## pack(ResourceSizeMap &resourceFilesSizes);\
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To load an attached resource pack file, you must declare the pack with :
|
* To load an attached resource pack file, you must declare the pack with :
|
||||||
* RESOURCE_PACK(pack_name)
|
* RESOURCE_PACK(pack_name)
|
||||||
|
*
|
||||||
* it will allow you to use the function :
|
* it will allow you to use the function :
|
||||||
* getResourcePack_pack_name(ResourceMap &map)
|
* getResourcePack_pack_name(ResourceMap &map)
|
||||||
* This function will give access the the embedded data through its original file name,
|
* This function will give access the the embedded data through its original file name,
|
||||||
* using the provided map.
|
* using the provided map.
|
||||||
*
|
*
|
||||||
|
* it will also allow you to use the function :
|
||||||
|
* getResourceSizes_pack_name(ResourceSizeMap &map)
|
||||||
|
* This function will give access the the size of the embedded data.
|
||||||
|
*
|
||||||
* example :
|
* example :
|
||||||
* RESOURCE_PACK(shaders) // declaring the resource pack function
|
* RESOURCE_PACK(shaders) // declaring the resource pack function
|
||||||
* Resource::ResourceMap myShaders; // defining a resource map
|
* Resource::ResourceMap myShaders; // defining a resource map
|
||||||
@ -23,7 +30,59 @@ void getResourcePack_ ## pack(ResourceMap &resourceFilesData);\
|
|||||||
* string *shader = new string(myShaders["shaders/shaderFile.glsl"]); // getting a resource file
|
* string *shader = new string(myShaders["shaders/shaderFile.glsl"]); // getting a resource file
|
||||||
*/
|
*/
|
||||||
namespace Resource {
|
namespace Resource {
|
||||||
typedef std::unordered_map<std::string, const char*> ResourceMap;
|
|
||||||
}
|
typedef std::unordered_map<std::string, const char*> ResourceMap;
|
||||||
|
typedef std::unordered_map<std::string, size_t> ResourceSizeMap;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Reader is a wrapper that allows you to read files and resources exactly the same way
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Reader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* inspired from cstdio standard fread
|
||||||
|
* http://www.cplusplus.com/reference/cstdio/fread
|
||||||
|
*/
|
||||||
|
virtual size_t read (void * ptr, size_t size, size_t count) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class FileReader
|
||||||
|
{
|
||||||
|
FILE* m_stream;
|
||||||
|
|
||||||
|
public:
|
||||||
|
FileReader(FILE* stream) : m_stream(stream) {}
|
||||||
|
|
||||||
|
virtual size_t read (void * ptr, size_t size, size_t count)
|
||||||
|
{ return fread ( ptr, size, count, m_stream ); }
|
||||||
|
};
|
||||||
|
|
||||||
|
class ResourceReader
|
||||||
|
{
|
||||||
|
unsigned char* m_ptr;
|
||||||
|
size_t m_size;
|
||||||
|
size_t m_cursor;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ResourceReader(unsigned char* ptr, size_t size) : m_ptr(ptr), m_size(size), m_cursor(0) {}
|
||||||
|
|
||||||
|
virtual size_t read (void * ptr, size_t size, size_t count)
|
||||||
|
{
|
||||||
|
size_t nbRead = 0;
|
||||||
|
unsigned char* p = (unsigned char*)ptr;
|
||||||
|
while(m_cursor + size < m_size && nbRead < count)
|
||||||
|
{
|
||||||
|
for(size_t i=0; i<size; ++i)
|
||||||
|
p[nbRead*size + i] = m_ptr[m_cursor + i];
|
||||||
|
m_cursor += size;
|
||||||
|
++nbRead;
|
||||||
|
}
|
||||||
|
return nbRead;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Resource
|
||||||
|
|
||||||
#endif // RES_H
|
#endif // RES_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user