/*
    Ninja Sample

    COPYRIGHT (C) SEGA ENTERSRISES,LTD.

    Read Binary for Official Format(BASIC) for SET5

    1999.01.20
*/

#include <Shinobi.h>
#include <Nindows.h>


NJS_OBJECT *BSK_OBJECT;
NJS_MOTION *BSK_OBJECT_MOTION;
NJS_TEXLIST *tlist1;
char *pFilename = "run_null3.nj";
char *pMFilename = "rocket03d_run.njm";


NJS_TEXMEMLIST  tex[100];
NJS_TEXINFO info;


extern NJS_MATRIX   matrix[128];
extern Uint8 vbuf[];
extern Sint8 work_buff[0x100000];

NJS_VIEW    view;
NJS_LIGHT   light;

Uint32 count = 0;

void Count(void)
{
    count++;

}

void njUserInit(void)
{
    Uint32 fpos = 0, rtype, fsize;
    GDFS fid;
    Sint32 sn;


    njSetBorderColor( 0 ) ;
    sbInitSystem( NJD_RESOLUTION_640x480_NTSCI, NJD_FRAMEBUFFER_MODE_RGB555, 1 );
    njInitVertexBuffer( 800000, 0, 300000, 0 ,0);
    njInitPrint(NULL,0,0);
    njInitMatrix( matrix, 128 , 1);
    njInit3D( vbuf, 4096*10 );

    njInitView( &view );
    njTranslateViewRelative(&view, 0.f, 0.f, 100.f);
    njSetView( &view );


    njCreateLight( &light, NJD_PHONG_DIR );
    njSetLightIntensity( &light, 1.5f, 1.0f, 0.7f );
    njSetLightAngle( &light, NJM_RAD_ANG(NJD_PI / 30.f), NJM_RAD_ANG(NJD_PI / 4.f) );
    njSetLightDirection( &light, 0, 0.f, -1.f );

    /*-- OBJECT-------------------------*/
    /*  njOpenBinary,njCloseBinaryを使用しない場合 
        work_buffとしてファイルがロードできるバッファを用意しておく
    */
    /*  In the case which njOpenBinary,njCloseBinary is not used for
        
        Preparing the buffer (work_buff) which a file can load.
    */
    fpos = 0;
    fid = gdFsOpen(pFilename, NULL);
    gdFsGetFileSctSize(fid, &sn);
    gdFsRead(fid, sn, work_buff);
    gdFsClose(fid);
    tlist1 = (NJS_TEXLIST *)njReadBinary(work_buff, &fpos, &rtype);
    BSK_OBJECT = (NJS_OBJECT *)njReadBinary(work_buff, &fpos, &rtype);

    /*---------------------------------*/

    /*-- OBJECT MOTION-----------------*/
    /* njOpenBinary,njCloseBinaryを使用しない場合 */
    /* In the case which njOpenBinary,njCloseBinary is not used for */
    fpos = 0;
    fid = gdFsOpen(pMFilename, NULL);
    gdFsGetFileSctSize(fid, &sn);
    gdFsRead(fid, sn, work_buff);
    gdFsClose(fid);
    BSK_OBJECT_MOTION = (NJS_MOTION *)njReadBinary(work_buff, &fpos, &rtype);
    /*---------------------------------*/

    njInitTextureBuffer(vbuf,4096*96);
    njInitTexture( tex, 100 );
    njLoadTexture( tlist1 );
    njSetTexture( tlist1 );

    njSetBackColor(0x00000000,0x00000000,0x000000FF);

    njSetVSyncFunction(Count);
    nwInitSystem(10,NJD_PORT_A0);
    njPrintSize(16);
}



Sint32 njUserMain(void)
{
    static float frame = 0.f;

    njDrawMotion(BSK_OBJECT, BSK_OBJECT_MOTION, frame);

    njPrintD(NJM_LOCATION(3,5),count,8);
    njPrintF(NJM_LOCATION(3,8),frame,8);

    frame++;
    if(frame > 17.f)
        frame = 0.f;

    return nwExecute();
}

void njUserExit(void)
{
    nwExitSystem();
    njExitTexture();
    njExitPrint() ;
    sbExitSystem();
}
