/*
	Ninja Library
	
	COPYRIGHT (C) SEGA ENTERPRISES,LTD.

*/
#ifndef _NJCSPRITE_H_
#define _NJCSPRITE_H_

/********************************************************************
 *
 *      Ninja Cell Sprite
 *
 ********************************************************************/
/*======================================*/
/*                                      */
/*      Cell Chunk Table                */
/*                                      */
/*======================================*/
/* table size = 4bits(16 entries) */
#define NJD_CC_SHIFT    28

#define NJD_CC_0        (0<<NJD_CC_SHIFT)
#define NJD_CC_1        (1<<NJD_CC_SHIFT)
#define NJD_CC_2        (2<<NJD_CC_SHIFT)
#define NJD_CC_3        (3<<NJD_CC_SHIFT)
#define NJD_CC_4        (4<<NJD_CC_SHIFT)
#define NJD_CC_5        (5<<NJD_CC_SHIFT)
#define NJD_CC_6        (6<<NJD_CC_SHIFT)
#define NJD_CC_7        (7<<NJD_CC_SHIFT)
#define NJD_CC_8        (8<<NJD_CC_SHIFT)
#define NJD_CC_9        (9<<NJD_CC_SHIFT)
#define NJD_CC_10      (10<<NJD_CC_SHIFT)
#define NJD_CC_11      (11<<NJD_CC_SHIFT)
#define NJD_CC_12      (12<<NJD_CC_SHIFT)
#define NJD_CC_13      (13<<NJD_CC_SHIFT)
#define NJD_CC_14      (14<<NJD_CC_SHIFT)
#define NJD_CC_15      (15<<NJD_CC_SHIFT)

#define NJD_CC_MASK    (0xf<<NJD_CC_SHIFT)


/*==================*/
/* Chunk (standard) */
/*==================*/
/*---------------------*/
/* Cell Chunk Time End */
/*---------------------*/
/* <Format>=[ChunkHead(4)|dammy(12)|timestamp(16)] */
#define NJD_CC_TE     (NJD_CC_0)

/*-----------------------*/
/* Cell Chunk Time Stamp */
/*-----------------------*/
/* <Format>=[ChunkHead(4)|longsize(12)|timestamp(16)] */
#define NJD_CC_TS     (NJD_CC_1)

/*-----------------*/
/* Cell Chunk Cell */
/*-----------------*/
/* <Format>=[ChunkHead(4)|longsize(4)|headbits(8)|entryId(16)]  */
/*          [Data ...]                                          */
/*      <headbits>                        */
/* FCC : Flag Cell Chunk                  */
/*      7 = texId and Attr (Block0)       */
/*           [texId(16)|attr(16)]         */
/*      6 = cell motion    (Block1)       */
/*      5 = cell priority  (Block2)       */
/*      4 = cell center    (Block3)       */
/*      3 = vertex color   (Block4)       */
/*      2 = uv             (Block5)       */
#define NJD_CC_CE     (NJD_CC_2)

/*------------------*/
/* Cell Chunk TexId */
/*------------------*/
/* <Format>=[ChunkHead(4)|entryId(12)|texId(16)] */
#define NJD_CC_TD     (NJD_CC_3)

/*-----------------*/
/* Cell Chunk Attr */
/*-----------------*/
/* <Format>=[ChunkHead(4)|entryId(12)|attr(16)]  */
#define NJD_CC_AT     (NJD_CC_4)

/*------------------*/
/* Cell Chunk czang */
/*------------------*/
/* <Format>=[ChunkHead(4)|entryId(12)|czang(16)] */
#define NJD_CC_CZ     (NJD_CC_5)

/*---------------*/
/* Cell Chunk cp */
/*---------------*/
/* <Format>=[ChunkHead(4)|entryId(12)|cp(16)]    */
#define NJD_CC_CP     (NJD_CC_6)

/*----------------------*/
/* Cell Chunk UserEvent */
/*----------------------*/
/* <Format>=[ChunkHead(4)|longsize(12)|eventId(16)] */
/*          [Data ...]                              */
#define NJD_CC_UE     (NJD_CC_7)


/*======================*/
/* Burst Chunk (sniper) */
/*======================*/
/*---------------------------------*/
/* Cell Burst Chunk (address type) */
/*---------------------------------*/
/* <Format>=[ChunkHead(4)|longsize(12)|longaddress(16)] */
/*          [Data ...]                                  */
#define NJD_CBC_AD    (NJD_CC_14)

/*---------------------------------*/
/* Cell Burst Chunk (entryId type) */
/*---------------------------------*/
/* <Format>=[ChunkHead(4)|longsize(4)|offset(8)|entryId(16)] */
/*          [Data ...]                                       */
#define NJD_CBC_ED    (NJD_CC_15)


/*======================================*/
/*                                      */
/*      Cell Chunk Flags                */
/*                                      */
/*======================================*/
/*-----------------*/
/* Cell Stream Max */
/*-----------------*/
#define NJD_CELL_PATTERN_MAX     256

/*-----------------*/
/* Cell Chunk Cell */
/*-----------------*/
/**** Flag Cell Block (for headbits) ****/
#define NJS_FCB_SHIFT     16

#define NJS_FCB_IA0       (BIT_7<<NJS_FCB_SHIFT)  /* texId and attr   */
#define NJS_FCB_MO1       (BIT_6<<NJS_FCB_SHIFT)  /* cell motion      */
#define NJS_FCB_PR2       (BIT_5<<NJS_FCB_SHIFT)  /* cell proirity    */
#define NJS_FCB_CE3       (BIT_4<<NJS_FCB_SHIFT)  /* texture center   */
#define NJS_FCB_VC4       (BIT_3<<NJS_FCB_SHIFT)  /* vertex color     */
#define NJS_FCB_UV5       (BIT_2<<NJS_FCB_SHIFT)  /* uv               */

#define NJS_FCB_MASK      (0xff<<NJS_FCB_SHIFT)

/*-----------------*/
/* Flag Cell Attr  */
/*-----------------*/
/*=======================================================*/
#ifndef NJD_FBS_ZER                    /* see NjChunk.h  */
/**** Flag Blending Src and Dst ****/
/* ZER : Zero                                            */
/* ONE : One                                             */
/* OC  : `Other' Color                                   */
/* IOC : Inverse `Other' Color                           */
/* SA  : Src Alpha                                       */
/* ISA : Inverse SRC Alpha                               */
/* DA  : DST Alpha                                       */
/* IDA : Inverse DST Alpha                               */

/* Flag Blending Src */

#define NJD_FBS_SHIFT     11
#define NJD_FBS_ZER      (0<<NJD_FBS_SHIFT)
#define NJD_FBS_ONE      (1<<NJD_FBS_SHIFT)
#define NJD_FBS_OC       (2<<NJD_FBS_SHIFT)
#define NJD_FBS_IOC      (3<<NJD_FBS_SHIFT)
#define NJD_FBS_SA       (4<<NJD_FBS_SHIFT)
#define NJD_FBS_ISA      (5<<NJD_FBS_SHIFT)
#define NJD_FBS_DA       (6<<NJD_FBS_SHIFT)
#define NJD_FBS_IDA      (7<<NJD_FBS_SHIFT)

#define NJD_FBS_MASK     (0x7<<NJD_FBS_SHIFT)

/* Flag Blending Dst */

#define NJD_FBD_SHIFT     8
#define NJD_FBD_ZER      (0<<NJD_FBD_SHIFT)
#define NJD_FBD_ONE      (1<<NJD_FBD_SHIFT)
#define NJD_FBD_OC       (2<<NJD_FBD_SHIFT)
#define NJD_FBD_IOC      (3<<NJD_FBD_SHIFT)
#define NJD_FBD_SA       (4<<NJD_FBD_SHIFT)
#define NJD_FBD_ISA      (5<<NJD_FBD_SHIFT)
#define NJD_FBD_DA       (6<<NJD_FBD_SHIFT)
#define NJD_FBD_IDA      (7<<NJD_FBD_SHIFT)

#define NJD_FBD_MASK     (0x7<<NJD_FBD_SHIFT)
#endif
/*=======================================================*/

/**** Flag Cell Attr (for attr member) ****/
/* Flag Blending Src(13-11) */
#define NJD_FCS_ZER      NJD_FBS_ZER
#define NJD_FCS_ONE      NJD_FBS_ONE
#define NJD_FCS_OC       NJD_FBS_OC
#define NJD_FCS_IOC      NJD_FBS_IOC
#define NJD_FCS_SA       NJD_FBS_SA
#define NJD_FCS_ISA      NJD_FBS_ISA
#define NJD_FCS_DA       NJD_FBS_DA
#define NJD_FCS_IDA      NJD_FBS_IDA

/* Flag Blending Dst(10-8) */
#define NJD_FCD_ZER      NJD_FBD_ZER
#define NJD_FCD_ONE      NJD_FBD_ONE
#define NJD_FCD_OC       NJD_FBD_OC
#define NJD_FCD_IOC      NJD_FBD_IOC
#define NJD_FCD_SA       NJD_FBD_SA
#define NJD_FCD_ISA      NJD_FBD_ISA
#define NJD_FCD_DA       NJD_FBD_DA
#define NJD_FCD_IDA      NJD_FBD_IDA

#define NJD_FCA_VC       BIT_7           /* vertex color */
#define NJD_FCA_AL       BIT_6           /* use alpha    */
#define NJD_FCA_PT       BIT_5           /* punch throw  */
#define NJD_FCA_HF       BIT_4           /* h flip       */
#define NJD_FCA_VF       BIT_3           /* v flip       */
#define NJD_FCA_HI       BIT_2           /* hide         */
#define NJD_FCA_TR       BIT_1           /* time repeat  */


/* CELLSPRITE VAL */
#define NJD_CELLSPRITE_CONSTANT_XYZ			(BIT_31)
#define NJD_CELLSPRITE_OFFSET_XYZ			(BIT_30)

#define NJD_CELLSPRITE_CONSTANT_ZANG		(BIT_29)
#define NJD_CELLSPRITE_OFFSET_ZANG			(BIT_28)

#define NJD_CELLSPRITE_CONSTANT_SXSY		(BIT_27)
#define NJD_CELLSPRITE_OFFSET_SXSY			(BIT_26)

#define NJD_CELLSPRITE_CONSTANT_DIFF		(BIT_25)
#define NJD_CELLSPRITE_OFFSET_DIFF			(BIT_24)
#define NJD_CELLSPRITE_OFFSET_DIFF_MULTI	(BIT_23)

#define NJD_CELLSPRITE_CONSTANT_SPEC		(BIT_22)
#define NJD_CELLSPRITE_OFFSET_SPEC			(BIT_21)
#define NJD_CELLSPRITE_OFFSET_SPEC_MULTI	(BIT_20)

/*======================================*/
/*                                      */
/*      Cell Sprite Structures          */
/*                                      */
/*======================================*/
typedef struct {
        /* texture Id and attr (block0) */
        Sint16          texId;
        Sint16           attr;

        /* texture cell motion (block1) */
        Float        cox, coy;     /* cell offset      */
        Float        csx, csy;     /* cell size(scale) */
        Sint16          czang;

        /* texture cell priority (block2) */
        Sint16             cp;     /* cell priority    */

        /* texture cell center (block3) */
        Float  cent_x, cent_y;     /* size retio       */

        /* vertex color (block4) */
        NJS_COLOR     argb[4];

        /* texture uv (block5) */
        Float          u0, v0;
        Float          u2, v2;
    } NJS_CELL;

typedef struct {
        NJS_CELL       *cells;
        Int            nbCell;
        NJS_POINT3        pos; /* [x, y, z](3D) or [x, y, priority](2D) */
        Angle            zang; /* zrotation            */
        Float          sx, sy; /* scale                */
        NJS_ARGB      diffuse; /* material diffuse     */
        NJS_ARGB     specular; /* material specular    */
    } NJS_CELL_SPRITE;

typedef struct {
        Sint32    *cellstream;
        Sint32   *cur_stamp_p;
        Sint32     timeoffset;
        Sint32        timemax;
    } NJS_CELL_STREAM;

typedef struct {
        Sint32       nbStream;
        Sint32     streamtime;
        NJS_CELL_SPRITE  *csp;
        Sint32          *wbuf;
        NJS_CELL_STREAM *streamlist[NJD_CELL_PATTERN_MAX];
    } NJS_CELL_STREAM_LIST;

typedef struct {
		Uint32           flag;
        NJS_POINT3    cst_pos; /* const [x, y, z](3D) or [x, y, priority](2D) */
        NJS_POINT3    off_pos; /* offset [x, y, z](3D) or [x, y, priority](2D) */
        Angle        cst_zang; /* const zrotation            */
        Angle        off_zang; /* offset zrotation           */
        Float   cst_sx,cst_sy; /* const scale                */
        Float   off_sx,off_sy; /* offset scale               */
        NJS_ARGB  cst_diffuse; /* const material diffuse     */
        NJS_ARGB  off_diffuse; /* offset material diffuse    */
        NJS_ARGB cst_specular; /* const material specular    */
        NJS_ARGB off_specular; /* offset material specular   */
}NJS_CELL_SPRITE_VAL;

/*-------------*/
/* cell stream */
/*-------------*/
Sint32 njCalcCellStreamWorkSize(NJS_CELL_SPRITE *csprite,Sint32 stream_max);
void njInitCellStreamList(NJS_CELL_STREAM_LIST *streamlist,NJS_CELL_SPRITE *csp,Sint32 *wbuf);
Sint32 njAddCellStream(NJS_CELL_STREAM_LIST *streamlist,NJS_CELL_STREAM *stream);
Sint32 njDeleteCellStream(NJS_CELL_STREAM_LIST *streamlist,NJS_CELL_STREAM *stream);
void njResetCellStreamTime(NJS_CELL_STREAM_LIST *streamlist);
void njStepCellStream(NJS_CELL_STREAM_LIST *streamlist, Sint32 addtime);

void njDrawCellStream2D(NJS_CELL_STREAM_LIST *streamlist, NJS_MOTION *motion, Sint32 addtime,NJS_CELL_SPRITE_VAL *cval);
void njDrawCellStream3D(NJS_CELL_STREAM_LIST *streamlist, NJS_MOTION *motion, Sint32 addtime,NJS_CELL_SPRITE_VAL *cval);

void njSetCellSpriteCallbackFunc(Int n,void (**func)(Int,void*));

/*----------------*/
/* cell sprite 2D */
/*----------------*/
void njDrawCellSprite2D(NJS_CELL_SPRITE *csprite,NJS_CELL_SPRITE_VAL *cval);

/*----------------*/
/* cell sprite 3D */
/*----------------*/
void njDrawCellSprite3D(NJS_CELL_SPRITE *csprite,NJS_CELL_SPRITE_VAL *cval);

void njSetCellSpriteMaterial( NJS_ARGB *cst_diff, NJS_ARGB *off_diff,NJS_ARGB *cst_spec,NJS_ARGB *off_spec,Int mode);
void njSetCellSpriteAttr( Sint16 and_attr, Sint16 or_attr );

#ifdef MOTIONSPEC
/*-------------------------*/
/* cell sprite motion spec */
/*-------------------------*/
    pos    : NJD_MTYPE_POS_0   (1<<0)  float  key[3]
    angle  : NJD_MTYPE_ANG_1   (1<<1)  Angle  angle 
    sx, sy : NJD_MTYPE_SCL 2   (1<<2)  float  key[2]
    argb   : NJD_MTYPE_RGB_8   (1<<9)  Sint32 key


<csprite_spec.nam>
---------
/* CSNAM 1.00alpha1 NinjaAsciiCellSpriteMotion */

/* MOTION : motion_csprite_spec */

MOTION_START

CSPOSITION pos_csprite_spec[]
START
    MKEYF( 0,   0.000000F, -0.978237F,  0.058765F ),
    ......
END

CSANGLE angle_csprite_spec[][]
START
    MKEYA( 0,   90.00000F ),
    ......
END

CSSCALE scl_csprite_spec[]
START
    MKEYF2( 0,   1.000000F, -1.000000F ),
    ......
END

CSCOLOR col_csprite_spec[]
START
    MKEYUI32( 0,  ARGB(255, 255, 255, 255) ),
    ......
END

MDATA4 mdata_csprite_spec01[]
START
    pos_csprite_spec, angle_csprite_spec, scl_csprite_spec, col_csprite_spec, 100, 100, 10, 10,
END

CSMOTION motion_csprite_spec[]
START
MdadaArray    mdata_csprite_spec01,
MFrameNum     100,
MoitonBit     0x0107,
InterpolFct   0x0000,
END

MOTION_END
---------
#endif /* MOTIONSPEC */


#endif /* _NJSCPRITE_H_ */

/*
 * End Of File
 */
