/*
	Ninja Sample
	
	COPYRIGHT (C) SEGA ENTERSRISES,LTD.

	Sample

	1998.2.3
*/

#include <Ninjawin.h>
#include <stdio.h>
#include <stdlib.h>

#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];
NJS_MATRIX		matrix[128];
NJS_VERTEX_BUF	vbuf[4096];
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)
{
	njInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB555, 1 );
	njInitVertexBuffer( 500000, 0, 500000, 0 );
	njInitMatrix( matrix, 128, 1 );
	njInitShape(buf);
	njInit3D( vbuf, 4096 );
	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 );
	njSetTexturePath( "SCENEDATA\\" );
	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)
{
	per1 = njGetPeripheral(NJD_PORT_JOYSTICK1);
	per = njGetPeripheral(NJD_PORT_SYSKEYBOARD);

	njClearMatrix();
	njTranslate( NULL, 0.f, -20.f, -120.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;

	if((per->on & NJD_DGT_ST)||(per1->on & NJD_DGT_ST)){
		return NJD_USER_EXIT;
	}
	return NJD_USER_CONTINUE;
}

void njUserExit(void)
{
	njExitTexture();
	njExitSystem();
}
