merge.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import os
  2. from datetime import date
  3. cppFiles = ["Core", "ErrorReporting", "Animations", "Texture", "Shader", "Camera", "GraphicModel",\
  4. "gl3d"]
  5. headerFiles = ["Core", "ErrorReporting", "Animations", "json", "stb_image_write", "tiny_gltf", \
  6. "OBJ_Loader", "Texture", "Shader", "Camera", "GraphicModel",\
  7. "gl3d"]
  8. initialtext = \
  9. """////////////////////////////////////////////////
  10. //gl3D --Vlad Luta --
  11. //built on {date}
  12. ////////////////////////////////////////////////
  13. """
  14. perFileText = \
  15. """////////////////////////////////////////////////
  16. //{file}
  17. ////////////////////////////////////////////////
  18. """
  19. os.chdir("../")
  20. os.chdir("src/shaders")
  21. #shaders
  22. shaderDir = os.getcwd()
  23. os.chdir("../../")
  24. os.chdir("devUtils")
  25. devUtilsPath = os.getcwd()
  26. shadersNames = []
  27. for (root,dirs,f) in os.walk(shaderDir):
  28. #print (root)
  29. #print (dirs)
  30. #print (f)
  31. for file in f:
  32. fullPath = root + '/' + file
  33. #print(fullPath)
  34. os.system("glslOptimizer.exe " + fullPath)
  35. optimizedName = fullPath.split('.')
  36. optimizedName = optimizedName[0] + "_optimized." + optimizedName[1]
  37. optimizedFileName = file.split('.')
  38. optimizedFileName = optimizedFileName[0] + "_optimized." + optimizedFileName[1]
  39. os.replace(optimizedName, devUtilsPath + '/shaders/' + optimizedFileName)
  40. shadersNames.append(optimizedFileName)
  41. os.chdir("../")
  42. #print("working dir:")
  43. #print(os.getcwd())
  44. #.h
  45. finalHFile = open("headerOnly/gl3d.h", "w")
  46. finalHFile.write(initialtext.format(date = date.today()))
  47. finalHFile.write("\n\n")
  48. for i in headerFiles:
  49. f = open(os.path.join("src", "src", i + ".h"))
  50. content = f.read()
  51. for j in headerFiles:
  52. content = content.replace(f"#include <{j}.h>","")
  53. content = content.replace(f"#include<{j}.h>","")
  54. content = content.replace(f"#include \"{j}.h\"","")
  55. content = content.replace(f"#include\"{j}.h\"","")
  56. finalHFile.write(perFileText.format(file = i+".h"))
  57. finalHFile.write("#pragma region " + i + '\n')
  58. finalHFile.write(content)
  59. finalHFile.write("\n#pragma endregion" + '\n')
  60. finalHFile.write("\n\n")
  61. f.close()
  62. finalHFile.close()
  63. #.cpp
  64. finalCppFile = open("headerOnly/gl3d.cpp", "w")
  65. finalCppFile.write(initialtext.format(date = date.today()))
  66. finalCppFile.write("\n")
  67. finalCppFile.write("""#include \"gl3d.h\"""")
  68. finalCppFile.write("\n\n")
  69. finalCppFile.write("""
  70. #define TINYGLTF_IMPLEMENTATION
  71. #define STB_IMAGE_WRITE_IMPLEMENTATION
  72. #define TINYGLTF_NOEXCEPTION // optional. disable exception handling.
  73. #define TINYGLTF_NO_INCLUDE_JSON
  74. #define TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE
  75. """)
  76. headerOnlyFiles = ["json.h", "stb_image_write.h", "tiny_gltf.h"]
  77. for i in headerOnlyFiles:
  78. f = open(os.path.join("src", "src", i))
  79. content = f.read()
  80. for j in headerFiles:
  81. content = content.replace(f"#include <{j}.h>","")
  82. content = content.replace(f"#include<{j}.h>","")
  83. content = content.replace(f"#include \"{j}.h\"","")
  84. content = content.replace(f"#include\"{j}.h\"","")
  85. finalCppFile.write(content)
  86. finalCppFile.write("\n\n")
  87. f.close()
  88. for i in cppFiles:
  89. f = open(os.path.join("src", "src", i + ".cpp"))
  90. content = f.read()
  91. for j in headerFiles:
  92. content = content.replace(f"#include <{j}.h>","")
  93. content = content.replace(f"#include<{j}.h>","")
  94. content = content.replace(f"#include \"{j}.h\"","")
  95. content = content.replace(f"#include\"{j}.h\"","")
  96. if(i == "Shader"):
  97. newContent = content.split("\n")
  98. foundLineNr = 0
  99. for lineNr in range(len(newContent)):
  100. if(newContent[lineNr].find("//#pragma shaderSources") != -1):
  101. foundLineNr = lineNr
  102. break
  103. foundLineNr = foundLineNr+1
  104. newContent.insert(foundLineNr, " //std::pair test stuff...")
  105. #todo add at foundLine nr the std::pair stuff
  106. stringToInsert = """name}", "{value}"}"""
  107. for shader in shadersNames:
  108. noOptimizedName = shader.replace("_optimized", "")
  109. shaderFile = open("devutils/shaders/" + shader)
  110. shaderSource = shaderFile.read()
  111. shaderSource = shaderSource.split('\n')
  112. for index in range(len(shaderSource)):
  113. shaderSource[index] = shaderSource[index].lstrip()
  114. if(shaderSource[index].startswith("//")):
  115. shaderSource[index] = ""
  116. shaderSource[index] = shaderSource[index].replace("#pragma debug(on)", "")
  117. found = shaderSource[index].find("//")
  118. if found >= 0:
  119. shaderSource[index] = shaderSource[index][0:found]
  120. if(len(shaderSource[index]) != 0):
  121. shaderSource[index] = "\"" + shaderSource[index] + "\\n\""
  122. shaderSource = list(filter(None, shaderSource))
  123. shaderSource = '\n'.join(shaderSource)
  124. #newContent.insert(foundLineNr, " std::pair<std::string, const char*>{\""+noOptimizedName+"\", R\"(" + shaderSource + ")\"},\n")
  125. newContent.insert(foundLineNr, " std::pair<std::string, const char*>{\""+noOptimizedName+"\", " + shaderSource + "},\n")
  126. shaderFile.close()
  127. newContent.insert(0, "#define GL3D_LOAD_SHADERS_FROM_HEADER_ONLY")
  128. content = '\n'.join(newContent)
  129. finalCppFile.write(perFileText.format(file = i+".cpp"))
  130. finalCppFile.write("#pragma region " + i + '\n')
  131. finalCppFile.write(content)
  132. finalCppFile.write("\n#pragma endregion" + '\n')
  133. finalCppFile.write("\n\n")
  134. f.close()
  135. finalCppFile.close()