/**************
 ******##****** Filename:                 >> AAP.H <<
 *****####***** Edit Date: 02.10.92
 ****######****
 ***##*##*##*** By: Wolfgang Hofer,
 **##**##**##**     Animation Player for IFF ANIM5 Files
 ******##******
 ******##****** Compiler: AztecC 5.0
 ******##****** Link:     see makefile
 ************** Computer: AMIGA 500
 **************
 ************** Include file
 **************
 **************
 */


/* System includes */

#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <graphics/gfxbase.h>
#include <libraries/dos.h>
#include <intuition/intuition.h>

#include <exec/interrupts.h>
#include <exec/nodes.h>
#include <hardware/intbits.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>

/* Privat includes */

#include <AAP_KEY.h>

#define MakeID(a,b,c,d)  ( (LONG)(a)<<24L | (LONG)(b)<<16L | (c)<<8 | (d) )

#define ID_FORM      MakeID('F', 'O', 'R', 'M')
#define ID_ANIM      MakeID('A', 'N', 'I', 'M')
#define ID_ILBM      MakeID('I', 'L', 'B', 'M')
#define ID_BODY      MakeID('B', 'O', 'D', 'Y')
#define ID_CMAP      MakeID('C', 'M', 'A', 'P')
#define ID_CAMG      MakeID('C', 'A', 'M', 'G')
#define ID_BMHD      MakeID('B', 'M', 'H', 'D')
#define ID_ANHD      MakeID('A', 'N', 'H', 'D')
#define ID_DLTA      MakeID('D', 'L', 'T', 'A')
#define ID_DPAN      MakeID('D', 'P', 'A', 'N')

#define MAX_NAME 50L


struct FrameInfo
{
  ULONG id;           /* Chunk id (BODY or DLTA) */
  ULONG reltime;      /* for timing w.r.t. last frame in jiffies */
  ULONG ViewModes;    /* AMIGA Viewmodes (calculated or from CAMG) */
  UWORD BytesPerRow;  /* raster width in Bytes (word boundaries) */
  UWORD Rows;         /* raster height in pixels */
  UBYTE Depth;        /* # source bitplanes */
  UBYTE compression;  /* compression type
                       *   if id == ID_BODY   0 .. no compression
                       *                      1 .. Byte RunLength coding
                       *   if id == ID_DLTA   0 .. Anim5 standard
                       *                      1 .. Anim7 short word
                       *                      2 .. Anim7 long word
                       */
  UBYTE masking;      /* masking (from bmhd Chunks)
                       * 0 .. no mask
                       * 1 .. a stencil is used (Body contains extra plane)
                       * 2 .. transparent color
                       * 3 .. lasso
                       */
};


/* A Frame struct contains offsets from file begin to the
 * 1. Databyte of the corresponding Chunk(s).
 * (points after ChunkID and ChunkLength)
 * Missing Chunk(s) are represented by Offset 0
 */

struct Frame
{
  struct Frame    *next;       /* NULL if last Frame */
  long             frame_ofs;  /* Fileoffset to BODY or DLTA chunk */
  long             frame_len;
  long             cmap_ofs;   /* Fileoffset to CMAP chunk (0 == no cmap) */
  long             cmap_len;
  struct FrameInfo info;       /* Data from BMHD, ANHD or CAMG chunks */
};


struct Anim
{
  struct Anim   *next;
  struct Frame  *frame;        /* Pointer to 1.st frame of the anim */
  char          *buff_ptr;     /* adress of loaded File in RAM */
			       /* or NULL if File is on Disk */
  long           buff_len;
  long           ct_frames;    /* number of frames in this anim */
  char           name[MAX_NAME];   /* Filename of Pic/Animation */
};


struct Script
{
  struct Script *next;
  long           ptr;        /* opcode dependent data or pointer */
  short          numb;
  char           opcode;
  char           flags;
};

/* redefinition of the Script for use with OP_PLAY */
struct Script_Redef
{
  struct Script *next;
  struct Anim   *ptr;        /* pointer to anim */
  short          numb;
  char           opcode;
  char           flags;
};

#define OP_PLAY       0
#define OP_PAUSE      1
#define OP_SPEED      2
#define OP_BEG_LOOP   3
#define OP_END_LOOP   4
#define OP_VIEW_DX    5
#define OP_VIEW_DY    6
#define OP_ADRESS     7
#define OP_MSG        8
#define OP_KEY_BREAK  9
#define OP_KEY_JUMP   10
#define OP_KEY_WAIT   11
#define OP_EXIT       12

/* directives */
#define OP_LOAD_ON    100
#define OP_LOAD_OFF   101
#define OP_PIC_TIME   102
#define OP_LABEL      103

/*
 * ---------------------------------------------------------
 * Usage of the Script struct (depends on the opcode):
 * ---------------------------------------------------------
 *
 * opcode .. OP_PLAY   Play Anim or show Picture
 * ptr ..... Pointer to requested Anim (struct Anim *)
 * numb .... # of loops
 * flags.... 1..continous_mode, 2..full_mode
 *
 * opcode .. OP_PAUSE (increase frame_delay of last displayed frame)
 * ptr ..... time in jiffies
 *
 * opcode .. OP_SPEED (set frame delay, 0 == delay of reltime in ANHD)
 * ptr ..... time in jiffies
 *
 * opcode .. OP_BEG_LOOP
 * ptr ..... current loopcount (down to 0)
 * numb .... # of loops
 *
 * opcode .. OP_END_LOOP
 * ptr ..... Pointer to Beg_Loop statement. (struct Script *)
 *           (NULL == End without Begin)
 *
 * opcode .. OP_KEY_JUMP
 * ptr ..... 1.pass Labelnumber,
 *           2.nd pass and exe: ptr to statement before jump-destination
 * numb .... raw_key code as jump-condition  (LSB set)
 *           RAW_ANY_KEY (LSB clear, not 0) means jump if any key is pressed
 *           value of 0 stands for UNCONDITIONAL Jump
 *
 * opcode .. OP_KEY_WAIT
 * ptr ..... unused
 * numb .... raw_key code (wait until this raw_key is pressed)
 *           RAW_ANY_KEY or 0 (wait until any key is pressed)
 *
 * opcode .. OP_EXIT
 * ptr ..... returncode
 *
 * -------------------------------------------------------------
 */

#define MAX_LABELNAME 32
struct Label
{
  struct Label  *next;
  long           adr;        /* Adress in Script (NULL == undefined Label) */
  short          number;     /* Label Number (define sequence number) */
  char           name[MAX_LABELNAME];   /* Symbolic Label name */
};


/* A BitMapHeader is stored in a BMHD chunk. */
struct BitMapHeader
{
    UWORD w, h;         /* raster width & height in pixels */
    WORD  x, y;         /* position for this image */
    UBYTE nPlanes;      /* # source bitplanes */
    UBYTE masking;      /* masking technique */
    UBYTE compression;  /* compression algoithm */
    UBYTE pad1;         /* UNUSED.  For consistency, put 0 here.      */
    UWORD transparentColor;   /* transparent "color number"           */
    UBYTE xAspect, yAspect;   /* aspect ratio, a rational number x/y  */
    WORD  pageWidth, pageHeight;  /* source "page" size in pixels     */
};

struct AnimationHeader
{
   UBYTE operation; /* == 5 for RIFF style, == 7 for LongSkip style */
   UBYTE mask;
   UWORD w,h;
   WORD  x,y;
   ULONG abstime; /* for timing start of anim file in jiffies (notused)*/
   ULONG reltime; /* for timing last frame in jiffies */
   UBYTE interleave; /* =0 for XORing to two frames back, =1 for last frame
		      *     (not used yet)
		      */
   UBYTE pad0;
   ULONG bits;
   UBYTE pad[16];
};

struct CAMG
{
   ULONG ViewModes;
};

struct DPAN
{
   /* I dont know all about this chunk */
   UBYTE pad1[2];
   UWORD ct_valid_frames;
   UBYTE frames_per_sec;
   UBYTE pad2[3];
};
