/*
	Ninja Sample

	COPYRIGHT (C) SEGA ENTERSRISES,LTD.

	Sample

	1998.2.3
*/

#include <shinobi.h>
#include <stdio.h>
#include <Nindows.h>

void main(void)
{
#ifdef __GNUC__
	shinobi_workaround();
#endif
    njSetBorderColor( 0x00000000 );
        njUserInit();
        while (njUserMain() == NJD_USER_CONTINUE) {
                njWaitVSync();
        }
        njUserExit();
}

#define MAX_MATRIX_DEPTH 128
#define MAX_VERTEXES 40000

#define	WATER_MOTION	motion_fish_man_model01
#define	WATER_SHAPE		shape_fish_man_model01
#define	WATER_OBJECT	object_fish_man_model01_model01
#define	WATER_TEXLIST	texlist_fish_man_model01

#define	SCENE_MOTION	motion_fish_man_world
#define	SCENE_SHAPE		shape_fish_man_world
#define	SCENE_OBJECT	object_fish_man_world_world
#define	SCENE_TEXLIST	texlist_fish_man_world

extern NJS_MOTION	WATER_MOTION[];
extern NJS_MOTION	WATER_SHAPE[];
extern NJS_OBJECT	WATER_OBJECT[];
extern NJS_TEXLIST	WATER_TEXLIST;

extern NJS_MOTION	SCENE_MOTION[];
extern NJS_MOTION	SCENE_SHAPE[];
extern NJS_OBJECT	SCENE_OBJECT[];
extern NJS_TEXLIST	SCENE_TEXLIST;

NJS_TEXMEMLIST	tex[100];

static NJS_MATRIX		*matrix;
static NJS_VERTEX_BUF	*vbuf;

NJS_VIEW		view;
NJS_LIGHT		light;

Float	buf[10000];
Float	water_cnt = 0.f, scene_cnt = 0.f;
Int		water_frames, scene_frames;
Float	step = 0.5f;

NJS_PERIPHERAL *per,*per1;

void njUserInit(void)
{
	sbInitSystem( NJD_RESOLUTION_640x480_NTSCNI_ANTI, NJD_FRAMEBUFFER_MODE_RGB565, 1 );

    /*matrixとvertexバッファの確保*/
    /* allocates the memory for matrix and vertex */
    matrix = (NJS_MATRIX *)syMalloc(sizeof (NJS_MATRIX) * MAX_MATRIX_DEPTH);
    vbuf = (NJS_VERTEX_BUF *)syMalloc(sizeof (NJS_VERTEX_BUF) * MAX_VERTEXES);

	njSetAspect(1.0F,0.45F);

    /* テクスチャバッファの確保 */
    /* sets necessary memory for work area of a texture */
    njInitTextureBuffer((Sint8 *)vbuf,0x800*10);

    /* 頂点データ登録用バッファをアロケート */
    /* allocates the buffers for registration of vertex data */
    njInitVertexBuffer(MAX_VERTEXES * 64 / 4, 0, MAX_VERTEXES * 64 / 4, 0, 0);
	njInitPrint( NULL, 0, NJD_TEXFMT_ARGB_1555 );
	njInitMatrix( matrix, MAX_MATRIX_DEPTH, 1 );
	njInitShape(buf);
	njInit3D( vbuf, MAX_VERTEXES );
	njInitView( &view );
	njLookAtView( &view, 0.f, -20.f, -120.f);
	njSetView( &view );
	njCreateLight( &light, NJD_SPEC_DIR );
	njSetLightIntensity( &light, 0.5f, 1.f, 0.f );

	njInitTexture( tex, 100 );
	nwInitSystem(10,NJD_PORT_A0);
	njLoadTexture( &WATER_TEXLIST );
	njLoadTexture( &SCENE_TEXLIST );
	njControl3D( NJD_CONTROL_3D_CONSTANT_TEXTURE_MATERIAL );
	njSetBackColor( 0xff000000,0xff000000,0xff0000ff );
	njClipZ( -1.0f, -500.f );

	water_frames = WATER_MOTION->nbFrame - 1;
	scene_frames = SCENE_MOTION->nbFrame - 1;
}

Sint32 njUserMain(void)
{
	njClearMatrix();
	njTranslate( NULL, 0.f, -10.f, -30.f );

	njSetTexture( &WATER_TEXLIST );
	njDrawShapeMotion( WATER_OBJECT, WATER_MOTION, WATER_SHAPE, water_cnt );
	water_cnt += step;
	if ( water_cnt >= water_frames ) water_cnt = 0.f;

	njSetTexture( &SCENE_TEXLIST );
	njDrawShapeMotion( SCENE_OBJECT, SCENE_MOTION, SCENE_SHAPE, scene_cnt );
	scene_cnt += step;
	if ( scene_cnt >= scene_frames ) scene_cnt = 0.f;

	return nwExecute();
}

void njUserExit(void)
{
	njExitPrint();
	njExitTexture();
	nwExitSystem();
	syFree(matrix);
	syFree(vbuf);
	sbExitSystem();
}