-- -----------------------------------
--
-- .x Exporter
-- Ben Kenwright
-- Email: bkenwright@xbdev.net
--
-- -----------------------------------


global xlastfile=""
global xExport


fn writeTOKEN_NAME f str=
(
	writeshort f 1		       -- // U16 - token ID  1
	writelong  f str.count     -- // U32 - Length of name field, in bytes 
						    
	for i = 1 to str.count do  -- // Array U8 - array count ASCII name
	(
	   	c = bit.charAsInt(str[i])
		writebyte f c	
	)
)

fn writeTOKEN_OBRACE f=
(
	writeshort f 10		       -- // U16 - token ID  1
)

fn writeTOKEN_CBRACE f=
(
	writeshort f 11		       -- // U16 - token ID  1
)

fn writeTOKEN_INTEGER_LIST f iList iSize=
(
	writeshort f 6		       -- // U16 - token ID  1
	writelong  f iSize         -- // U32 - Number of integers in list field
	
	for i = 1 to iSize do      -- // Array U32 - array integers
	(
		writelong f iList[i]
	) 
)

fn writeTOKEN_FLOAT_LIST f fList iSize=
(
	writeshort f 7		       -- // U16 - token ID  1
	writelong  f iSize         -- // U32 - Number of integers in list field
	
	for i = 1 to iSize do      -- // Array U32 - array integers
	(
		writefloat f fList[i]
	) 
)

-- --------------------------------------------------------------------------------------

fn writemesh f obj=
(
    writeTOKEN_NAME   		f "Mesh"
	writeTOKEN_NAME   		f "Box01"
	writeTOKEN_OBRACE 		f
	
	
	a = #()
	a[1] = obj.numverts
	writeTOKEN_INTEGER_LIST f a 1
	
	j = 1
	verts = #()
	for i=1 to obj.numverts do
	(
		local v=in coordsys world(getvert obj i)
		verts[j] = v.x
		j+=1
		verts[j] = v.y
		j+=1
		verts[j] = v.z
		j+=1
	)
	writeTOKEN_FLOAT_LIST f verts (j-1)
	
	
	j = 1
	indices = #()
	
	indices[j] = obj.numfaces
	j+=1
	
	for i=1 to obj.numfaces do
	(
		indices[j] = 3
		j+=1
		
		i1=((getFace obj i).z)-1
		i2=((getFace obj i).y)-1
		i3=((getFace obj i).x)-1
		
		indices[j] = i1
		j+=1
		indices[j] = i2
		j+=1
		indices[j] = i3
		j+=1
	)
	writeTOKEN_INTEGER_LIST f indices (j-1)
	
	
	writeTOKEN_CBRACE f
)

-- --------------------------------------------------------------------------------------

fn writex obj xfilename=
(
	if obj==undefined do return"No object"
	if (classof obj)!=editable_mesh do return "Object is not a mesh"
	
	f=fopen xfilename "wb"

	magic  = 0x20666F78 -- "xof "
	version= 0x33303330 -- "0303"
	type   = 0x206E6962 -- "bin "
	acc    = 0X32333030 -- "0032"

	writelong f magic
	writelong f version
	writelong f type
	writelong f acc    
	
	writemesh f obj
	
	fclose f
)

-- --------------------------------------------------------------------------------------

fn addcheck cb=
(
 	if classof cb==string do messagebox cb title:"DirectX .X Export Error"
)

-- --------------------------------------------------------------------------------------

utility XExport ".x Mesh Exporter"
(
	group "Export"
	(
		button bexport "     Export    "
	)
	group "About"
	(
		label titleLabel	".x Mesh Exporter v0.01"
		HyperLink addy "by Ben Kenwright" align:#center address:"mailto:bkenwright@xbdev.net" color:(color 0 100 0) hoverColor:(color 0 0 100)
		HyperLink me "HomePage" align:#center address:"www.xbdev.net" color:(color 0 100 0) hoverColor:(color 0 0 100)

	)
	
	on bexport pressed do
	( 
		xfilename=getsavefilename caption:"Save .x" \
			filename:xlastfile \
			types:"DirectX .X (*.x)|*.x|All Files (*.*)|*.*|"
		if xfilename!=undefined do 
		(				
			addcheck (writex $ xfilename)
			xlastfile=xfilename
		)
	)
)

