/*===========================================================================
 *
 * File:	Df3dexpo.CPP
 * Author:	Dave Humphrey (uesp@m0use.net)
 * Created On:	July 5, 2001
 *
 * Implements export related routines for Daggerfall 3D objects.
 *
 *=========================================================================*/

	/* Include Files */
#include "df3dexpo.h"
#include "bsa/df3dutil.h"


/*===========================================================================
 *
 * Begin Local Variable Definitions
 *
 *=========================================================================*/
  DEFINE_FILE();
/*===========================================================================
 *		End of Local Variable Definitions
 *=========================================================================*/


/*===========================================================================
 *
 * Function - boolean ExportDF3dObjectTo3DS (pFilename, DF3dObject);
 *
 * Export the given Daggerfall 3D object to a complete 3DS file with the 
 * given path and filename.  Returns FALSE on any error.  Also exports any
 * texture files used by the new 3DS file which must accompany the file 
 * if textures are to be displayed correctly.  Texture files are output to
 * the same path as the 3DS file.
 * 
 *=========================================================================*/
boolean ExportDF3dObjectTo3DS (const char* pFilename, const CDF3dObject& DF3dObject) {
  DEFINE_FUNCTION("ExportDF3dObjectTo3DS()")
  C3dsFile File3DS;
  char     PathBuffer[_MAX_PATH+1];
  boolean  Result;

	/* Ensure valid input */
  ASSERT(pFilename != NULL);

	/* Attempt to open file for output */
  Result = File3DS.Open(pFilename, "wb");
  if (!Result) return (FALSE);

	/* Attempt to output 3DS chunk headers */
  Result  = File3DS.StartMainChunk();
  Result &= File3DS.StartEditChunk();

	/* Attempt to output the required materials */
  Result &= ExportDFMaterialsTo3DS(File3DS, DF3dObject);

	/* Output a single 3DS object to a trimesh chunk */
  Result &= ExportDF3dObjectTo3DS(File3DS, DF3dObject);

  if (!Result) {
    File3DS.Close();
    return (FALSE);
   }

	/* End the main chunk sections */
  Result &= File3DS.EndEditChunk();
  Result &= File3DS.EndMainChunk();

  	/* Export any texture files used by the object.  Texture files will be
	 * output to the same path as the 3DS file. */
  ExtractPath(PathBuffer, pFilename, _MAX_PATH);
  Result &= ExportDF3dObjectTextures(DF3dObject, PathBuffer);

  return (Result);
 }
/*===========================================================================
 *		End of Function ExportDF3dObjectTo3DS()
 *=========================================================================*/


/*===========================================================================
 *
 * Function - boolean ExportDF3dObjectToDXF (pFilename, DF3dObject);
 *
 * Export the given Daggerfall 3D object to a complete DXF AutoCad file with
 * the given path and filename.  Returns FALSE on any error.
 *
 *=========================================================================*/
boolean ExportDF3dObjectToDXF (const char* pFilename, const CDF3dObject& DF3dObject) {
  DEFINE_FUNCTION("ExportDF3dObjectToDXF()")
  CDxfFile FileDXF;
  boolean  Result;

	/* Ensure valid input */
  ASSERT(pFilename != NULL);

	/* Attempt to open file for output */
  Result = FileDXF.Open(pFilename, "wb");
  if (!Result) return (FALSE);

	/* Output DXF file header */
  Result = FileDXF.StartEntities();

	/* Output a single 3DS object to the DXF file */
  Result &= ExportDF3dObjectToDXF(FileDXF, DF3dObject);

	/* Output end of DXF file */
  Result &= FileDXF.EndEntities();
  Result &= FileDXF.EndFile();
  return (Result);
 }
/*===========================================================================
 *		End of Function ExportDF3dObjectToDXF()
 *=========================================================================*/