/*===========================================================================
 *
 * File:	Df3dutil.CPP
 * Author:	Dave Humphrey (uesp@m0use.net)
 * Created On:	Thursday, March 21, 2002
 *
 * Contains utility/common functions for handling 3D objects.
 *
 *=========================================================================*/

	/* Include Files */
#include "df3dutil.h"
#include "image/dftexdat.h"
#include "image/dfimgutl.h"


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


/*===========================================================================
 *
 * Function - int l_ExportDFFaceTexture (DFFace, pData);
 *
 * Local function used as a callback to the CDF3dObject::ForEachFace()
 * method.  Exports the face texture to a BMP file.  Does not export the
 * texture if the texture image has been tagged.
 *
 *=========================================================================*/
int l_ExportDFFaceTexture (const df3dface_t& DFFace, void* pData) {
  //DEFINE_FUNCTION("l_ExportDFFaceTexture()");
  dftexture_faceexport_data_t*	pCallbackData = (dftexture_faceexport_data_t *) pData;
  CDFTextureImage*		pImage;
  char				FileBuffer[512];
  boolean			Result;
  
	/* Special case for textures 0 and 1 (solid colors) */
  if (IsDFTextureSolidColor(DFFace.TextureIndex)) return (0);
    //long				SolidColor;
    //SolidColor = RGBRAWPAL2LONG(GetDFTextureSolidColor(DFFace.TextureIndex, DFFace.SubImageIndex));

	/* Attempt to retrieve the texture image */
  pImage = GetDFTextureImage(DFFace.TextureIndex, DFFace.SubImageIndex);
  if (pImage == NULL) return (0);

	/* Ignore any tagged texture images */
  if (pImage->GetTag()) return (0);

	/* Export the texture image and tag so it is not exported again */
  snprintf (FileBuffer, 500, "%sdf%03d%03d", pCallbackData->pExportPath, DFFace.TextureIndex, DFFace.SubImageIndex);
  Result = ExportDFTxtImgtoBMP(FileBuffer, *pImage);
  pImage->SetTag();

  return (0);
 }
/*===========================================================================
 *		End of Function l_ExportDFFaceTexture()
 *=========================================================================*/


/*===========================================================================
 *
 * Function - boolean ExportDF3dObjectTextures (DFObject, pPath);
 *
 * Attempts to export all the textures in the given 3D object to the given
 * path.  Returns FALSE on any error.  Textures are output as BMP files
 * with the filename format:
 *				DFaaabbbcc.BMP
 * Where:	aaa = Texture index (000-511)
 *		bbb = Texture image index (000-127)
 *		 cc = Image sub-index (only if present, 00-99)
 *
 * For regular textures, CC will not be present (only for animated
 * texture images).
 *
 *=========================================================================*/
boolean ExportDF3dObjectTextures (const CDF3dObject& DFObject, const char* pPath) {
  DEFINE_FUNCTION("ExportDF3dObjectTextures()");
  dftexture_faceexport_data_t CallbackData;
  char PathBuffer[_MAX_PATH+2];

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

	/* Ensure a valid output path */
  CreatePath(PathBuffer, pPath, _MAX_PATH+1);
  CallbackData.pExportPath = &PathBuffer[0];

	/* Export all the textures used in the object */
  ClearDFTextureTags();
  DFObject.ForEachFaceC(l_ExportDFFaceTexture, &CallbackData);

  return (TRUE);
 }
/*===========================================================================
 *		End of Function ExportDF3dObjectTextures()
 *=========================================================================*/