fixed absolute paths

This commit is contained in:
Anselme 2016-04-24 15:01:29 +02:00
parent 31cac13971
commit 5185581f35
9 changed files with 27 additions and 32 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
Modules/* Modules/*
test/CMakeFiles/* test/CMakeFiles/*
test/res.cpp
cmake_install.cmake cmake_install.cmake
Makefile Makefile
CMakeCache.txt CMakeCache.txt

View File

@ -16,7 +16,7 @@ add_executable(SparrowResource ${CMAKE_TEMPLATE_PATH}/resource/main.cpp)
add_custom_command( add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE_DST_FILE} OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE_DST_FILE}
COMMAND SparrowResource COMMAND SparrowResource
ARGS ${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE_DST_FILE} ${RES_SRC_FILE} ARGS ${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE_DST_FILE} ${PROJECT_SOURCE_DIR} ${RES_SRC_FILE}
DEPENDS ${RES_SRC_FILE} DEPENDS ${RES_SRC_FILE}
COMMENT "Generating resource file" COMMENT "Generating resource file"
) )

View File

@ -1,10 +0,0 @@
project(SparrowResource)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_TEMPLATE_PATH "..")
# choose source file
set(EXEC_SRC_LIST main.cpp)
include(${CMAKE_TEMPLATE_PATH}/template.cmake)

View File

@ -6,17 +6,12 @@ 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\
typedef std::unordered_map<std::string, const char*> ResourceMap;\n" typedef std::unordered_map<std::string, const char*> ResourceMap;\n"
#define FILE_MID ""
#define FILE_END "\n\ #define FILE_END "\n\
}\n\ }\n\
}\n" }\n"
@ -42,7 +37,7 @@ bool addFile(const string &inFile, FILE *out)
fprintf(out, "\n"); fprintf(out, "\n");
} }
while(nbRead == NB_VAL_PER_LINE); while(nbRead == NB_VAL_PER_LINE);
fprintf(out, "};\n"); fprintf(out, ", 0x00}; // additionnal byte used to null-terminate ascii data\n");
++n; ++n;
fclose(in); fclose(in);
return true; return true;
@ -50,9 +45,13 @@ bool addFile(const string &inFile, FILE *out)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if(argc < 3) if(argc < 4)
{ {
printf("usage : %s RESOURCE_PACK_NAME [LIST_OF_FILES_TO_BAKE]\n", argv[0]); printf("usage : %s RESOURCE_PACK_FILE_TO_CREATE RESOURCE_PACK_ROOT [LIST_OF_FILES_TO_BAKE]\n\n", argv[0]);
printf("RESOURCE_PACK_FILE_TO_CREATE is the c++ source file that will be created,\n");
printf("the resource pack will be named from the file name\n");
printf("example, if the filename provided is : /directory/sources/myResourcePack.cpp , the resource pack will be named myResourcePack\n\n");
printf("RESOURCE_PACK_ROOT is the root directory or the resources, they will be accessible from their relative path to this directory\n");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -72,19 +71,28 @@ int main(int argc, char *argv[])
} }
fprintf(out, FILE_BEGIN); fprintf(out, FILE_BEGIN);
for(int i=2; i<argc; ++i) vector<int> offsets;
for(int i=3; i<argc; ++i)
{ {
int offset = 0;
while(argv[2][offset] == argv[i][offset])
++offset;
if(argv[i][offset] == '/')
++offset;
if(!addFile(argv[i], out)) if(!addFile(argv[i], out))
{ {
fclose(out); fclose(out);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
else else
printf("successfully added \"%s\" to \"%s\".\n", argv[i], packName.c_str()); {
printf("successfully added \"%s\" as \"%s\" to resource pack \"%s\".\n", argv[i], argv[i]+offset, packName.c_str());
offsets.push_back(offset);
}
} }
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<n; ++i) for(int i=0; i<n; ++i)
fprintf(out, "\tresourceFilesData[\"%s\"] = data%d;\n", argv[2+i], i); fprintf(out, "\tresourceFilesData[\"%s\"] = data%d;\n", argv[3+i]+offsets[i], i);
fprintf(out, FILE_END); fprintf(out, FILE_END);
fclose(out); fclose(out);
printf("successfully created resource file \"%s\".\n", outFilename.c_str()); printf("successfully created resource file \"%s\".\n", outFilename.c_str());

View File

@ -6,11 +6,9 @@ set(CMAKE_TEMPLATE_PATH "..")
#SET(RESOURCE_DIRS /shaders) #SET(RESOURCE_DIRS /shaders)
#SET(RES_SRC_FILE ${CMAKE_CURRENT_SOURCE_DIR}/plop.txt ${CMAKE_CURRENT_SOURCE_DIR}/plop2.txt) #SET(RES_SRC_FILE ${CMAKE_CURRENT_SOURCE_DIR}/plop.txt ${CMAKE_CURRENT_SOURCE_DIR}/plop2.txt)
FILE(GLOB RES_SRC_FILE plop.txt plop2.txt) FILE(GLOB RES_SRC_FILE hello.txt test.txt)
SET(RESOURCE_DST_FILE res.cpp) SET(RESOURCE_DST_FILE res.cpp)
MESSAGE(${RES_SRC_FILE})
# choose source file # choose source file
set(EXEC_SRC_LIST test.cpp ${RESOURCE_DST_FILE}) set(EXEC_SRC_LIST test.cpp ${RESOURCE_DST_FILE})

View File

@ -7,12 +7,10 @@ RESOURCE_PACK(res)
int main() int main()
{ {
cout << "Resource test" << endl;
Resource::ResourceMap resMap; Resource::ResourceMap resMap;
Resource::getResourcePack_res(resMap); Resource::getResourcePack_res(resMap);
const char *str = resMap["/data/Qt_workspace/cmaketemplate/test/plop.txt"];
if(str != NULL) cout << "resources available : " << endl << endl;
cout << str << endl; for(auto it : resMap)
else cout << it.first << " -> " << it.second << endl;
cout << "nope" << endl;
} }

Binary file not shown.