summaryrefslogtreecommitdiff
path: root/assets.cmake
blob: 11868f8f06d820e9bc2b859e57e586cad6881282 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function(serialize_folder name dir data header)
  file(APPEND ${header} "namespace ${name} {\n")
  file(APPEND ${data} "namespace ${name} {\n")
  file(GLOB files ${dir}/*)
  list(LENGTH files file_count)
  foreach(file ${files})
    string(REGEX MATCH "([^/]+)$" filename ${file})
    string(REGEX REPLACE "\\.| |-" "_" filename ${filename})
    file(READ ${file} filedata HEX)
    string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata})
    file(APPEND ${data}   "const unsigned char file_${filename}[] = {${filedata}};\n")
    file(APPEND ${data}   "const unsigned file_${filename}_size = sizeof(file_${filename});\n")
    file(APPEND ${header} "extern const unsigned char file_${filename}[];\n")
    file(APPEND ${header} "extern const unsigned file_${filename}_size;\n")
  endforeach()
  file(APPEND ${data} "}\n")
  file(APPEND ${header} "const File files[] {\n")
  foreach(file ${files})
    string(REGEX MATCH "([^/]+)$" filename ${file})
    string(REGEX REPLACE "\\.| |-" "_" flat_filename ${filename})
    file(APPEND ${header} "File{\"${filename}\", file_${flat_filename}, file_${flat_filename}_size},\n")
  endforeach()
  file(APPEND ${header} "};\n")
  file(APPEND ${header} "const unsigned file_count = ${file_count};\n")
  file(APPEND ${header} "}\n")
endfunction()

function(serialize_assets dir data header)
  file(WRITE ${data} "")
  file(APPEND ${data} "#include \"${header}\"\n")
  file(APPEND ${data} "namespace assets {\n")
  file(WRITE ${header} "")
  file(APPEND ${header} "#pragma once\n")
  file(APPEND ${header} "#include <string>\n")
  file(APPEND ${header} "namespace assets {\n")
  file(APPEND ${header} "struct File { const std::string name; const unsigned char* data; const unsigned size; };\n")
  serialize_folder("palette" ${dir}/palette ${data} ${header})
  serialize_folder("noise" ${dir}/noise ${data} ${header})
  serialize_folder("shader" ${dir}/shader ${data} ${header})
  file(APPEND ${data} "}\n")
  file(APPEND ${header} "}\n")
endfunction()