/*
Test ReadBNC for Official Format (BASIC)
*/

#include <Shinobi.h>
#include <nindows.h>
#include <NinjaCnk.h>
#include <NinjaCnk2.h>
#include <nuBinary.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 NJS_VERTEX_BUF	vbuf[4096];*/

NJS_VIEW	view;
NJS_LIGHT	light;

Uint32 count = 0;



void Count(void)
{
	count++;

}



void njUserInit(void)
{
    Uint8 *work_buff;
    Uint32 fpos = 0, rtype, fsize;

	/*sbInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB555, 1 );*/
    sbInitSystem( NJD_RESOLUTION_640x480_NTSCI, NJD_FRAMEBUFFER_MODE_RGB555, 1 );
	njInitVertexBuffer( 800000, 0, 300000, 0 );
	njInitMatrix( matrix, 128 , 1);
	njInit3D( vbuf, 4096 );


	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.707f, -0.707f );*/
	njSetLightDirection( &light, 0, 0.f, -1.f );

    /*-- OBJECT-------------------------*/
    fpos = 0;
    work_buff = nuOpenBinary(pFilename);
    tlist1 = (NJS_TEXLIST *)nuReadBinaryChunk(work_buff, &fpos, &rtype);
	BSK_OBJECT = (NJS_OBJECT *)nuReadBinaryChunk(work_buff, &fpos, &rtype);
    nuCloseBinary(work_buff);
    /*---------------------------------*/

    /*-- OBJECT MOTION-----------------*/
    fpos = 0;
    work_buff = nuOpenBinary(pMFilename);
	BSK_OBJECT_MOTION = (NJS_MOTION *)nuReadBinaryChunk(work_buff, &fpos, &rtype);
    nuCloseBinary(work_buff);
    /*---------------------------------*/

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

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

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



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();
	sbExitSystem();
}

intcInit(){}
