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

    Modifier Sample

    1999.01.20
*/

#include <Shinobi.h>


typedef struct {
    Float   x,y;
    Sint32  rx,ry,rz;
    Sint32  xx,yy,zz;
} TEAPOT;


#define ZZZZ    -10.f

#if 0


#define DRAW    njCnkEasyDrawObject /* */

#define LIGHT   njCnkSetEasyLight           /* */
#define COLOR   njCnkSetEasyLightColor      /* */

#else

#define DRAW    njCnkSimpleDrawObject /* */
#define LIGHT   njCnkSetSimpleLight         /* */
#define COLOR   njCnkSetSimpleLightColor        /* */

#endif

extern Uint16 T009[];

/*#define   OBJECT  object_tea_Teapot1_Teapot1 /* */
/*#define   OBJECT  object_tea_Teapot1T_Teapot1T /* */
#define OBJECT  object_tea_Teapot1E_Teapot1E /* */
extern NJS_CNK_OBJECT OBJECT[];

#define MODIFIER    object_drchunk_logo_logo
/*#define   MODIFIER    object_cube_cube3_cube3*/
/*#define   MODIFIER    object_trous_torus3_torus3*/
extern NJS_CNK_OBJECT MODIFIER[];


NJS_TEXNAME texname[1];
NJS_TEXLIST tlist1 = {texname,1};
NJS_TEXINFO info;

extern NJS_MATRIX   matrix[8];
extern NJS_VERTEX_BUF   vbuf[4096];

NJS_TEXMEMLIST  tex[10];

NJS_VIEW    view;
NJS_LIGHT   light;


static  Sint32  lxx,lyy,lzz = 0;
static  NJS_VECTOR  light1,light2;
static  Sint32  ld,la = 0;


static  TEAPOT  tea[16];


void njUserInit(void)
{
    Int i;
    
    /*
        njInitSystemの変わりにsbInitSystemを実行する。
    
    */
    /*
       Executing "sbInitSystem" in a change of "njInitSystem".
    */

    njSetBorderColor( 0 ) ;

/*  sbInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB565, 1 );*/
    sbInitSystem( NJD_RESOLUTION_640x480_NTSCI, NJD_FRAMEBUFFER_MODE_RGB565, 1 );

/*  njSetAspect( 1.0f, 0.5f );*/
    
    njInitVertexBuffer( 100000, 100000, 10000, 0 ,0);
    njInitPrint(NULL, 0, 0);
    njInitMatrix( matrix, 8 , 0);
    njInit3D( vbuf, 4096 );
    njInitView( &view );
    njSetView( &view );

    /*
        メモリからのテクスチャなのでnjInitTextureBufferは必要ない
    */
    /*
        Because of texture from memory, njInitTextureBuffer is needless.
    */
    njInitTexture( tex, 10 );
    njSetTextureInfo(&info,T009,NJD_TEXFMT_RGB_565|NJD_TEXFMT_TWIDDLED,128,128);
    njSetTextureName(&texname[0],&info,0,NJD_TEXATTR_TYPE_MEMORY|NJD_TEXATTR_GLOBALINDEX);
    njLoadTexture( &tlist1 );
    njSetTexture( &tlist1 );

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

    light1.x = 0.f;
    light1.y = 0.f;
    light1.z = 1.f;

    njCnkSetEasyLight( 0.f, 0.f, 1.0f );
    njCnkSetEasyLightIntensity( 1.0f, 0.0f );
    njCnkSetSimpleLight( 0.f, 0.f, 1.0f );
    njCnkSetSimpleLightIntensity( 1.05f, 0.0f );

    syTmrInit();

    tea[0].x = 0.f;
    tea[0].y = 0.f;
    
    tea[0].rx = 0;
    tea[0].ry = 0;
    tea[0].xx = njRandom()*300+100;
    tea[0].yy = njRandom()*300+100;
    tea[0].zz = njRandom()*300+100;

    tea[1].rx = 0;
    tea[1].ry = 0;
    tea[1].xx = njRandom()*300+100;
    tea[1].yy = njRandom()*300+100;
    tea[1].zz = njRandom()*300+100;
    
    njSetCheapShadowMode( 128 );
}


Sint32 njUserMain(void)
{
    Uint32  ta,tb;
    Int     i;
    
    TEAPOT  *ttt;
    
    ta = syTmrGetCount();


    for(i=0;i<1;i++) {
        ttt = &tea[i];
        COLOR( njSin(ttt->rx)*0.4f+0.6f, njSin(ttt->ry)*0.4f+0.6f, njSin(ttt->rz)*0.4f+0.6f );
        njClearMatrix();
        njTranslate( NULL, ttt->x, ttt->y, ZZZZ );
        njRotateXYZ( NULL, ttt->rx,ttt->ry,ttt->rz );
        DRAW( OBJECT );
        ttt->rx += ttt->xx;
        ttt->ry += ttt->yy;
        ttt->rz += ttt->zz;
    }

    njClearMatrix();
    njTranslate( NULL, 0.f, 0.f, -8.f );
    ttt = &tea[1];
    njRotateXYZ( NULL, ttt->rx,ttt->ry,ttt->rz );
    njScale( NULL, 0.5f, 0.5f, 15.f );
    ttt->rx += ttt->xx;
    ttt->ry += ttt->yy;
    ttt->rz += ttt->zz;
    njCnkModDrawObject( MODIFIER );

    
    tb = syTmrGetCount();

    return NJD_USER_CONTINUE;
}

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