export separate types.h/level.c/level.h files

This commit is contained in:
ABelliqueux 2021-04-12 14:50:44 +02:00
parent 9bd6a525a3
commit 223c9201c8

View File

@ -716,26 +716,47 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
filepath = filepath.replace(self.filename_ext, "") # Quick fix to get around the aforementioned 'bugfix' filepath = filepath.replace(self.filename_ext, "") # Quick fix to get around the aforementioned 'bugfix'
# ~ h_filepath = bpy.path.ensure_ext(filepath, '.h') # We're writing a few files:
# - custom_types.h contains the 'engine' 's specific struct definitions
# - level.h contains the forward declaration of the level's variables
# - level.c contains the initialization and data of those variables
h_filepath = folder + os.sep + 'custom_types.h' # 'custom_types.h' goes in project's root
# ~ c_filepath = bpy.path.ensure_ext(filepath, self.filename_ext) custom_types_h = folder + os.sep + 'custom_types.h'
c_filepath = folder + os.sep + 'level.c' # Levels files go in ./levels/
### Header (.h) # If ./levels does not exist, create it
if not os.path.exists(folder + os.sep + 'levels'):
os.mkdir(folder + os.sep + 'levels')
levels_folder = folder + os.sep + 'levels' + os.sep
# TODO : dynamic filenaming
level_h = levels_folder + 'level.h'
level_c = levels_folder + 'level.c'
### Custom types Header (custom_types.h)
# Open file # Open file
h = open(os.path.normpath(h_filepath),"w+") h = open(os.path.normpath(custom_types_h),"w+")
## Add C structures definitions ## Add C structures definitions
h.write( h.write(
"#pragma once\n" +
"#include <sys/types.h>\n" + "#include <sys/types.h>\n" +
"#include <libgte.h>\n" + "#include <libgte.h>\n" +
"#include <libgpu.h>\n\n" "#include <libgpu.h>\n\n"
) )
@ -877,17 +898,17 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
h.close() h.close()
## Data (.C) ## Level Data (level.c)
f = open(os.path.normpath(c_filepath),"w+") # Store every variable name in a list so that we can populate the level.h file later
level_variables = []
f = open(os.path.normpath(level_c),"w+")
f.write( f.write(
'#ifndef TYPES\n' + '#include "level.h"\n\n'
'\t#include "custom_types.h"\n' +
'#endif\n\n'
) )
@ -949,6 +970,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
"};\n\n") "};\n\n")
level_variables.append( "CAMPOS camPos_" + CleanName( bpy.data.objects[ o ].name ) )
# Find camera path points and append them to camPathPoints[] # Find camera path points and append them to camPathPoints[]
if bpy.data.objects[o].type == 'CAMERA' : if bpy.data.objects[o].type == 'CAMERA' :
@ -983,6 +1006,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
"\t{\n") "\t{\n")
level_variables.append( "CAMPATH camPath" )
f.write( "\t\t{ " + str( round( -bpy.data.objects[ camPathPoints[ point ] ].location.x * scale ) ) + f.write( "\t\t{ " + str( round( -bpy.data.objects[ camPathPoints[ point ] ].location.x * scale ) ) +
"," + str( round( bpy.data.objects[ camPathPoints[ point ] ].location.z * scale ) ) + "," + str( round( bpy.data.objects[ camPathPoints[ point ] ].location.z * scale ) ) +
@ -1011,6 +1036,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
"};\n\n" ) "};\n\n" )
level_variables.append( "CAMPATH camPath" )
## Lighting setup ## Lighting setup
# Light sources will be similar to Blender's sunlamp # Light sources will be similar to Blender's sunlamp
@ -1030,7 +1057,7 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
pad = 3 - len( bpy.data.lamps ) pad = 3 - len( bpy.data.lamps )
f.write( "static MATRIX lgtmat = {\n") f.write( "MATRIX lgtmat = {\n")
for l in range(len(bpy.data.lamps)): for l in range(len(bpy.data.lamps)):
@ -1074,9 +1101,11 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("\n\t};\n\n") f.write("\n\t};\n\n")
level_variables.append( "MATRIX lgtmat" )
# LCM : Local Color Matrix # LCM : Local Color Matrix
f.write( "static MATRIX cmat = {\n") f.write( "MATRIX cmat = {\n")
LCM = [] LCM = []
@ -1106,6 +1135,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("\t};\n\n") f.write("\t};\n\n")
level_variables.append( "MATRIX cmat" )
## Meshes ## Meshes
actorPtr = first_mesh actorPtr = first_mesh
@ -1136,6 +1167,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write( "SVECTOR " + "model" + cleanName + "_mesh[] = {\n" ) f.write( "SVECTOR " + "model" + cleanName + "_mesh[] = {\n" )
level_variables.append( "SVECTOR " + "model" + cleanName + "_mesh[]" )
for i in range( len( m.vertices ) ): for i in range( len( m.vertices ) ):
v = m.vertices[ i ].co v = m.vertices[ i ].co
@ -1166,6 +1199,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("SVECTOR "+"model"+cleanName+"_normal[] = {\n") f.write("SVECTOR "+"model"+cleanName+"_normal[] = {\n")
level_variables.append( "SVECTOR "+"model"+cleanName+"_normal[]" )
for i in range(len(m.vertices)): for i in range(len(m.vertices)):
poly = m.vertices[i] poly = m.vertices[i]
@ -1194,6 +1229,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("SVECTOR "+"model"+cleanName+"_uv[] = {\n") f.write("SVECTOR "+"model"+cleanName+"_uv[] = {\n")
level_variables.append( "SVECTOR "+"model"+cleanName+"_uv[]")
texture_image = m.uv_textures[t].data[0].image texture_image = m.uv_textures[t].data[0].image
tex_width = texture_image.size[0] tex_width = texture_image.size[0]
@ -1243,6 +1280,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("CVECTOR "+"model"+cleanName+"_color[] = {\n") f.write("CVECTOR "+"model"+cleanName+"_color[] = {\n")
level_variables.append( "CVECTOR "+"model"+cleanName+"_color[]" )
# If vertex colors exist, use them # If vertex colors exist, use them
if len(m.vertex_colors) != 0: if len(m.vertex_colors) != 0:
@ -1289,6 +1328,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write( "PRIM "+"model"+cleanName+"_index[] = {\n" ) f.write( "PRIM "+"model"+cleanName+"_index[] = {\n" )
level_variables.append( "PRIM "+"model"+cleanName+"_index[]" )
for i in range(len(m.polygons)): for i in range(len(m.polygons)):
poly = m.polygons[i] poly = m.polygons[i]
@ -1424,6 +1465,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
) )
level_variables.append( "VANIM model"+cleanName+"_anim" )
for v in range(len(nm.vertices)): for v in range(len(nm.vertices)):
if v == 0: if v == 0:
@ -1508,6 +1551,34 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
"\t};\n\n") "\t};\n\n")
level_variables.append( "MATRIX model"+cleanName+"_matrix" )
level_variables.append( "VECTOR model"+cleanName+"_pos" )
level_variables.append( "SVECTOR model"+cleanName+"_rot" )
level_variables.append( "short model"+cleanName+"_isRigidBody" )
level_variables.append( "short model"+cleanName+"_isStaticBody" )
level_variables.append( "short model"+cleanName+"_isPrism" )
level_variables.append( "short model"+cleanName+"_isAnim" )
level_variables.append( "short model"+cleanName+"_isActor" )
level_variables.append( "short model"+cleanName+"_isLevel" )
level_variables.append( "short model"+cleanName+"_isBG" )
level_variables.append( "short model"+cleanName+"_isSprite" )
level_variables.append( "long model"+cleanName+"_p" )
level_variables.append( "long model"+cleanName+"_OTz" )
level_variables.append( "BODY model"+cleanName+"_body" )
# Write TMESH struct # Write TMESH struct
f.write( "TMESH " + "model" + cleanName + " = {\n" ) f.write( "TMESH " + "model" + cleanName + " = {\n" )
@ -1516,6 +1587,12 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write( "\t" + "model" + cleanName + "_normal,\n" ) f.write( "\t" + "model" + cleanName + "_normal,\n" )
level_variables.append( "TMESH " + "model" + cleanName )
# ~ level_variables.append( "model" + cleanName + "_mesh" )
# ~ level_variables.append( "model" + cleanName + "_normal" )
if len(m.uv_textures) != None: if len(m.uv_textures) != None:
for t in range(len(m.uv_textures)): for t in range(len(m.uv_textures)):
@ -1524,6 +1601,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("\t"+"model"+cleanName+"_uv,\n") f.write("\t"+"model"+cleanName+"_uv,\n")
# ~ level_variables.append( "model" + cleanName + "_uv" )
else: else:
f.write("\t0,\n") f.write("\t0,\n")
@ -1533,6 +1612,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write( "\t"+"model" + cleanName + "_color, \n" ) f.write( "\t"+"model" + cleanName + "_color, \n" )
# ~ level_variables.append( "model" + cleanName + "_color" )
# According to libgte.h, TMESH.len should be # of vertices. Meh... # According to libgte.h, TMESH.len should be # of vertices. Meh...
f.write( "\t" + str( len ( m.polygons ) ) + "\n" ) f.write( "\t" + str( len ( m.polygons ) ) + "\n" )
@ -1612,6 +1693,14 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("TIM_IMAGE tim_" + prefix + ";\n\n") f.write("TIM_IMAGE tim_" + prefix + ";\n\n")
level_variables.append( "unsigned long "+"_binary_TIM_" + prefix + "_tim_start[]" )
level_variables.append( "unsigned long "+"_binary_TIM_" + prefix + "_tim_end[]" )
level_variables.append( "unsigned long "+"_binary_TIM_" + prefix + "_tim_length" )
level_variables.append( "TIM_IMAGE tim_" + prefix )
timList.append(prefix) timList.append(prefix)
f.write("NODE_DECLARATION\n") f.write("NODE_DECLARATION\n")
@ -1694,6 +1783,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
"\n};\n\n" "\n};\n\n"
) )
level_variables.append( "MESH mesh" + cleanName )
# Remove portals from mesh list as we don't want them to be exported # Remove portals from mesh list as we don't want them to be exported
meshList = list(bpy.data.meshes) meshList = list(bpy.data.meshes)
@ -1729,6 +1820,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("\n}; \n") f.write("\n}; \n")
level_variables.append( "MESH * meshes[" + str(len(meshList)) + "]")
# If camAngles is empty, use default camera, and do not include pre-calculated backgrounds # If camAngles is empty, use default camera, and do not include pre-calculated backgrounds
if not camAngles: if not camAngles:
@ -1741,6 +1834,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
"};\n\n") "};\n\n")
level_variables.append( "CAMANGLE camAngle_" + CleanName(defaultCam) )
# If camAngles is populated, use backgrounds and camera angles # If camAngles is populated, use backgrounds and camera angles
for camera in camAngles: for camera in camAngles:
@ -1968,6 +2063,18 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write( before ) f.write( before )
# Feed to level_variables
level_variables.append( "unsigned long "+"_binary_TIM_bg_" + prefix + "_tim_start[]")
level_variables.append( "unsigned long "+"_binary_TIM_bg_" + prefix + "_tim_end[]")
level_variables.append( "unsigned long "+"_binary_TIM_bg_" + prefix + "_tim_length")
level_variables.append( "TIM_IMAGE tim_bg_" + prefix )
level_variables.append( "CAMANGLE camAngle_" + prefix )
for portal in visiblePortal: for portal in visiblePortal:
w = objVertLtoW(portal) w = objVertLtoW(portal)
@ -2034,6 +2141,10 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("};\n\n") f.write("};\n\n")
# Feed to level_variables
level_variables.append( "CAMANGLE * camAngles[" + str(len(camAngles)) + "]" )
## Spatial Partitioning ## Spatial Partitioning
# Planes in the level - dict of strings # Planes in the level - dict of strings
@ -2366,6 +2477,10 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
"};\n\n") "};\n\n")
# Feed to level_variables
level_variables.append( "SIBLINGS node" + pName + "_siblings" )
# Write CHILDREN static objects structure # Write CHILDREN static objects structure
f.write("CHILDREN node" + pName + "_objects = {\n") f.write("CHILDREN node" + pName + "_objects = {\n")
@ -2397,6 +2512,10 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("\t}\n" + f.write("\t}\n" +
"};\n\n") "};\n\n")
# Feed to level_variables
level_variables.append( "CHILDREN node" + pName + "_objects" )
# Write CHILDREN rigidbodies structure # Write CHILDREN rigidbodies structure
f.write("CHILDREN node" + pName + "_rigidbodies = {\n") f.write("CHILDREN node" + pName + "_rigidbodies = {\n")
@ -2428,6 +2547,10 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("\t}\n" + f.write("\t}\n" +
"};\n\n") "};\n\n")
# Feed to level_variables
level_variables.append( "CHILDREN node" + pName + "_rigidbodies" )
# Write NODE structure # Write NODE structure
f.write( "NODE node" + pName + " = {\n" + f.write( "NODE node" + pName + " = {\n" +
@ -2442,6 +2565,10 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
"};\n\n" ) "};\n\n" )
# Feed to level_variables
level_variables.append( "NODE node" + pName )
f.write("MESH * actorPtr = &mesh" + CleanName(actorPtr) + ";\n") f.write("MESH * actorPtr = &mesh" + CleanName(actorPtr) + ";\n")
f.write("MESH * levelPtr = &mesh" + CleanName(levelPtr) + ";\n") f.write("MESH * levelPtr = &mesh" + CleanName(levelPtr) + ";\n")
@ -2452,6 +2579,18 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
f.write("NODE * curNode = &node" + CleanName(nodePtr) + ";\n\n") f.write("NODE * curNode = &node" + CleanName(nodePtr) + ";\n\n")
# Feed to level_variables
level_variables.append( "MESH * actorPtr" )
level_variables.append( "MESH * levelPtr" )
level_variables.append( "MESH * propPtr" )
level_variables.append( "CAMANGLE * camPtr" )
level_variables.append( "NODE * curNode" )
# Set default camera back in Blender # Set default camera back in Blender
if defaultCam != 'NULL': if defaultCam != 'NULL':
@ -2467,7 +2606,7 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
# Get the file content # Get the file content
f = open(os.path.normpath(c_filepath),"r") f = open(os.path.normpath(level_c),"r")
filedata = f.read() filedata = f.read()
@ -2483,6 +2622,8 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
Node_declaration += "NODE node" + CleanName(k) + ";\n\n" Node_declaration += "NODE node" + CleanName(k) + ";\n\n"
level_variables.append( "NODE node" + CleanName(k) )
# Do the substitution # Do the substitution
newdata = filedata.replace("NODE_DECLARATION\n", Node_declaration) newdata = filedata.replace("NODE_DECLARATION\n", Node_declaration)
@ -2499,12 +2640,31 @@ class ExportMyFormat(bpy.types.Operator, ExportHelper):
# Open and write file # Open and write file
f = open(os.path.normpath(c_filepath),"w") f = open(os.path.normpath(level_c),"w")
f.write( newdata ) f.write( newdata )
f.close() f.close()
## Level forward declarations (level.h)
h = open(os.path.normpath(level_h),"w+")
h.write(
'#pragma once\n\n' +
'#include "../custom_types.h"\n\n'
)
for var in level_variables:
h.write( "extern " + var + ";\n\n")
h.close()
return {'FINISHED'}; return {'FINISHED'};
def menu_func(self, context): def menu_func(self, context):