/***************************************************************************
 *
 *   NAME 
 *      SaveANIM -- save an IFF ANIM file to disk
 *
 *   SYNOPSIS
 *      if (SaveANIM( directory, frame, bitmapold, bitmapnew, viewport ))
 * 
 *      char directory[];
 *      UWORD frame;
 *      struct BitMap *bitmapold,
 *		      *bitmapnew;
 *      struct ViewPort *viewport;
 *
 *   DESCRIPTION
 *      As the animation continues, each frame is saved to disk
 *      under a subdirectory created with the name of the choreography,
 *      and each file name is the number of the frame.
 *
 *      copyright (c) 1987 Martin D. Hash
 *
 *   LAST EDITED
 *      Martin Hash			22 Aug 1987
 *
 *   EDIT HISTORY
 *       5 Aug  MH  Created.
 *
 **********************************************************************/

#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <df1:ilbm.h>
#include "df1:FileCons.h"
#include "df1:ANIMCons.h"

/* EXTERNAL FUNCTIONS */

struct FileLock *Lock(),
		*CreateDir();
BOOL UserRequest(),
     Pack_Frame();

/* FILE VARIABLES */

static char text[MAXNUMBERTEXT];

static struct IntuiText frametext = {
   2, 0, JAM1,
   0, 0, NULL,
   text,
   NULL
};

/* FUNCTION */

static BOOL ANHD_Chunk( file, anhd )

BPTR file;
ANHD *anhd;
{
   /* LOCAL VARIABLES */

   ULONG ANHDsize;

   /* CODE */

   if (Write( file, "ANHD", 4*sizeof(char)) == -1)
      return FALSE;
   ANHDsize = sizeof(ANHD);
   if (Write( file, &ANHDsize, sizeof(ULONG)) == -1)
      return FALSE;
   if (Write( file, anhd, sizeof(ANHD)) == -1)
      return FALSE;
   return TRUE;
}

/* FUNCTION */

BOOL SaveANIM( directory, frame, bitmapold, bitmapnew, viewport )

char directory[];
UWORD frame;
struct BitMap *bitmapold,
	      *bitmapnew;
struct ViewPort *viewport;
{
   /* LOCAL VARIABLES */

   BPTR file;
   char name[STRINGSIZE];
   struct FileLock *lock;
   ANHD anhd;
   ULONG zero = 0;

   /* FUNCTION */

   strcpy( name, "frames:anim" );
   if ((lock = Lock( name, ACCESS_READ )) == 0) 
      if ((lock = CreateDir( name )) == 0) {
         UserRequest( "          CANT CREATE ANIM" );
         return FALSE;
      }
   UnLock( lock );

   strcat( name, "/" );
   strcat( name, directory );

   if ((lock = Lock( name, ACCESS_READ )) == 0) 
      if ((lock = CreateDir( name )) == 0) {
         UserRequest( "         CANT CREATE FILE" );
         return FALSE;
      }
   UnLock( lock );

   stci_d( text, (int)frame, sizeof(text));
   strcat( name, "/" );
   strcat( name, text );

   if ((lock = Lock( name, ACCESS_READ )) != 0) {
      file = Open( name, MODE_OLDFILE );
      UnLock( lock );
   }
   else
      file = Open( name, MODE_NEWFILE );

   anhd.operation = XORVERT;
   if (frame == 1 || frame == 2) {
      Write( file, "FORM", 4*sizeof(char));
      Write( file, &zero, sizeof(ULONG));
      Write( file, "ANIM", 4*sizeof(char));
      PutPicture( file, bitmapnew, viewport->ColorMap->ColorTable );  
      Write( file, "FORM", 4*sizeof(char));
      Write( file, &zero, sizeof(ULONG));
      Write( file, "ILBM", 4*sizeof(char));
      ANHD_Chunk( &anhd );
   }
   else if (!Pack_Frame( file, bitmapold, bitmapnew, 
       viewport->ColorMap->ColorTable )) {
      Close( file );
      DeleteFile( name );
      return FALSE;
   }      

   Close( file );
   return TRUE;
}
