/**********************************************************
 *
 *   NAME
 *      Display -- display animation frames 
 *
 *   SYNOPSIS
 *      Display();
 *
 *   DESCRIPTION
 *      This function displays the specified HAM animation frames in
 *      order.
 *
 *      copyright (c) 1987 Martin D. Hash
 * 
 *   LAST EDITED
 *      Martin Hash                        22 Aug 1987
 *
 *   EDIT HISTORY
 *      19 Dec 1986  MH  Created.
 *      31 Dec           Added Dongel.
 *      21 Mar 1987	 Added real-time playback.
 *	 5 Jul		 More filename suffixes.
 *	11 Aug		 Standard filerequest.
 *	16 Aug		 ANIM standard.
 *
 **********************************************************/

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <graphics/sprite.h>
#include "df1:FileCons.h"
#include "df1:ANIMCons.h"

/* EXTERNAL FUNCTIONS */

void Display(),
     LoadIFF(),
     Display_Aud();
struct Window *OpenWindow();
struct ColorMap *GetColorMap();
BOOL Manual(),
     FileRequest();
char *strrchr();
FrameNode *Call_Pack();

/* LOCAL CONSTANTS */

#define RECORD 			1
#define VIEW			2
#define NOT_ENOUGH_MEMORY	-1000
#define WIDTH			352
#define HEIGHT			240

/* GLOBAL VARIABLES */

struct Window *window;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct ViewPort viewport1, viewport2;
struct BitMap bitmap1, bitmap2;
struct View view1, view2;
struct RastPort rastport1, rastport2;
FrameNode *patriarch;
int start;

char animpathname[STRINGSIZE],
     numbertext[MAXNUMBERTEXT];
struct IntuiText framenumbertext = {
   1, 0, JAM2,
   0, 0, NULL,
   numbertext,
   NULL
};

/* FILE VARIABLES */

static UBYTE *displaymem;
static struct View *oldview;
static struct RasInfo rastinfo1, rastinfo2;

static BOOL iff = FALSE;

static UWORD blank_pointer[] = { 0, 0, 0,0,0,0, 0, 0 };

static struct NewWindow newwindow = {
   0, 0, 640, 200,
   0, 1, 
   MOUSEBUTTONS | GADGETUP | GADGETDOWN | REQCLEAR | RAWKEY,
   ACTIVATE | SMART_REFRESH,
   NULL,
   NULL, NULL, NULL, NULL,
   0, 0, 0, 0,
   WBENCHSCREEN
};

/* This is the first function of the Audition program, hence it needs
 * to include main(). */

main()
{
   /* LOCAL VARIABLES */

   UWORD i;
   char *dummy,
        *suffix;
   UWORD depth = 0;

   /* FUNCTION */

   /* open libraries */   
   if ((IntuitionBase = (struct IntuitionBase *)
    OpenLibrary( "intuition.library", INTUITION_REV )) == NULL)
      exit( 0 );
   if ((GfxBase = (struct GfxBase *)
    OpenLibrary( "graphics.library", GRAPHICS_REV )) == NULL) {
      CloseLibrary( IntuitionBase );
      exit( 0 );
   }
   if ((window = OpenWindow( &newwindow )) == NULL) {
      CloseLibrary( IntuitionBase );
      CloseLibrary( GfxBase );
      exit( 0 );
   }

   strcpy( animpathname, "frames:anim/" );
   if (FileRequest( "Load Vignette", "  Load", animpathname, window, NULL )) {
      if (!stcpm( animpathname, ".", &suffix )) {
         CloseLibrary( IntuitionBase );
         CloseLibrary( GfxBase );
	 CloseWindow( window );
         exit( 0 );
      }
            
      if (stcpm( suffix, "4096", &dummy )) 
         depth = 6;
      else if (stcpm( suffix, "32", &dummy ))
	 depth = 5;
      else if (stcpm( suffix, "4", &dummy ))
   	 depth = 2;
      else if (stcpm( suffix, "8", &dummy ))
	 depth = 3;
      else if (stcpm( suffix, "16", &dummy ))
	 depth = 4;
      else if (stcpm( suffix, "2", &dummy ))
	 depth = 1;

      if (stcpm( suffix, "PAC", &dummy ))
         iff = FALSE;
      else if (stcpm( suffix, "IFF", &dummy )) 
         iff = TRUE;

      if (depth == 0) {
         CloseLibrary( IntuitionBase );
         CloseLibrary( GfxBase );
	 CloseWindow( window );
         exit( 0 );
      }

      oldview = GfxBase->ActiView;
   
      InitView( &view1 );
      InitView( &view2 );
      view1.Modes |= LACE;
      view2.Modes |= LACE;
      InitVPort( &viewport1 );
      InitVPort( &viewport2 );
      view1.ViewPort = &viewport1;
      view2.ViewPort = &viewport2;

      InitBitMap( &bitmap1, depth, WIDTH, HEIGHT );
      InitBitMap( &bitmap2, depth, WIDTH, HEIGHT );

      rastinfo1.RxOffset = 0;
      rastinfo2.RxOffset = 0;
      rastinfo1.RyOffset = 0;
      rastinfo2.RyOffset = 0;
      rastinfo1.Next = NULL;
      rastinfo2.Next = NULL;
      rastinfo1.BitMap = &bitmap1;
      rastinfo2.BitMap = &bitmap2;

      viewport1.DWidth = WIDTH;
      viewport2.DWidth = WIDTH;
      viewport1.DHeight = HEIGHT;
      viewport2.DHeight = HEIGHT;
      viewport1.DxOffset = -16;
      viewport2.DxOffset = -16;
      viewport1.DyOffset = -20;
      viewport2.DyOffset = -20;
      viewport1.RasInfo = &rastinfo1;
      viewport2.RasInfo = &rastinfo2;
      if (depth == 6) {
         viewport1.Modes |= HAM;
         viewport2.Modes |= HAM;
      }
      for (i = 0; i < depth; ++i) {
         bitmap1.Planes[i] = (PLANEPTR)AllocRaster( WIDTH, HEIGHT );
         bitmap2.Planes[i] = (PLANEPTR)AllocRaster( WIDTH, HEIGHT );
      }

      viewport1.ColorMap = GetColorMap(COLORS);
      viewport2.ColorMap = GetColorMap(COLORS);
      MakeVPort( &view1, &viewport1 );
      MakeVPort( &view2, &viewport2 );
      MrgCop( &view1 );
      MrgCop( &view2 );

      InitRastPort( &rastport1 );
      rastport1.BitMap = &bitmap1;
      InitRastPort( &rastport2 );
      rastport2.BitMap = &bitmap2;

      SetAPen( &rastport1, 0 );
      RectFill( &rastport1, 0, 0, WIDTH-1, HEIGHT-1 );
      SetAPen( &rastport2, 0 );
      RectFill( &rastport2, 0, 0, WIDTH-1, HEIGHT-1 );

      Display();

      FreeColorMap( viewport1.ColorMap );
      FreeColorMap( viewport2.ColorMap );
      FreeVPortCopLists( &viewport1 );
      FreeVPortCopLists( &viewport2 );
      FreeCprList( view1.LOFCprList );
      FreeCprList( view2.LOFCprList );
      FreeCprList( view1.SHFCprList );
      FreeCprList( view2.SHFCprList );
      for (i = 0; i < depth; ++i) {
         FreeRaster( bitmap1.Planes[i], WIDTH, HEIGHT );
         FreeRaster( bitmap2.Planes[i], WIDTH, HEIGHT );
      }
   }
   
   CloseWindow( window );
   CloseLibrary( IntuitionBase );
   CloseLibrary( GfxBase );
   OpenWorkBench();
}

/* FUNCTION */

void Display()
{
   /* LOCAL VARIABLES */

   int frame;
   char *framenumber;

   /* CODE */

   LoadView( &view1 );

   if (iff) {
      strcat( animpathname, "/" );
      while (FileRequest( "Load Frame", "  Load", animpathname,
       window, NULL )) {
         SetPointer( window, &blank_pointer, 2, 8, 0, 0 );
         if (framenumber = strrchr( animpathname, '/' ));
	 else
            framenumber = strrchr( animpathname, ':' );

         stcd_i( framenumber+1, &start ); 
         *(framenumber+1) = '\0';	/* leave only path */
         for (frame = start; TRUE; ++frame) {
            LoadIFF( frame, &bitmap1, &viewport1 );
            if (Manual( &rastport1, &viewport1 ))
               break;
	 }
         LoadView( oldview );  
         ClearPointer( window );
      }
   }
   else {
      SetPointer( window, &blank_pointer, 2, 8, 0, 0 );
      patriarch = Call_Pack( animpathname, &bitmap1, &bitmap2, 
       &viewport1, &viewport2 );
      Display_Aud();
      ClearPointer( window );
      LoadView( oldview );  
   }
}



