/************************************************************
  Ninja Sample
  COPYRIGHT (C) SEGA ENTERPRISES, LTD

  タイトル ： ポリゴンを回転させる
              rotate the polygon
  編集履歴 ： 98-04-30 作成開始
              98-06-19 SET5対応
              99-04-05 appends the English comments
************************************************************/

#include <shinobi.h>
#include <njdef.h>

void main(void)
{
	njSetBorderColor( 0x00000000 );
    njUserInit();
    while (njUserMain() == NJD_USER_CONTINUE) {
        njWaitVSync();
    }
    njUserExit();
}

#define MAX_MATRIX_DEPTH 10
#define MAX_VERTEXES 200

#define ROTATE_ANGLE_SPEED NJM_DEG_ANG(1.0F)
#define POINT_NUMBER 3/* 頂点の数 */
                      /* the number of vertex */
#define SCREEN_SAVE_TIME 30

static NJS_POINT3COL Point;
static NJS_POINT3 PointData[POINT_NUMBER];
static NJS_COLOR ColorData[POINT_NUMBER];

static NJS_MATRIX *TranslationMatrix;
static NJS_VERTEX_BUF *VertexBuffer;

static NJS_VIEW View;
static NJS_LIGHT DirLight;

/*--------------------------------------------
    初期化
    initialize
--------------------------------------------*/
void njUserInit(void)
{
    /* システムの初期化 */
    /* Initializes the Dreamcast H/W and readies the SEGA library */
    sbInitSystem(NJD_RESOLUTION_640x240_NTSCNI, NJD_FRAMEBUFFER_MODE_RGB565, 1);

    /* 画面のアスペクト比を補正 */
	/* corrects the screen aspect ratio */
    njSetAspect( 1.0F, 0.45F );

    /* メモリの割り当て */
    /* allocates the memory */
    TranslationMatrix = syMalloc(sizeof(NJS_MATRIX) * MAX_MATRIX_DEPTH);
    VertexBuffer   = syMalloc(sizeof(NJS_VERTEX_BUF) * MAX_VERTEXES);

    /* 背景色の設定 */
	/* sets background color */
    njSetBackColor(0x00000000, 0x00000000, 0x00000000);

    /* 頂点格納バッファの確保 */
    /* 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 );

    /* マトリックススタックの初期化 */
  	/* initializes the matrix stack */
    njInitMatrix(TranslationMatrix, MAX_MATRIX_DEPTH, 1);

    /* 3Dシステムの初期化 */
    /* initializes 3D system  */ 
    njInit3D(VertexBuffer, MAX_VERTEXES);

    /* 光源の初期化 */
    /* initializes the light source */
    njCreateLight(&DirLight, NJD_DIR_LIGHT);

    /* 視点の初期化 */
    /* initializes the user-defined view */
    njInitView(&View);

    /* 視点の設定 */
	/* specifies the current view */
    njSetView(&View);

    /* マトリックススタックのクリア */
	/* clears the matrix stack */
    njClearMatrix();    

    /* 頂点座標、色の設定 */
    /* sets the potision and color */
    PointData[0].x = 0.0F;
    PointData[0].y = 1.15F;
    PointData[0].z = 0.0F;
    ColorData[0].color = 0xFFC0C0C0;

    PointData[1].x = -1.0F;
    PointData[1].y = -0.58F;
    PointData[1].z = 0.0F;
    ColorData[1].color = 0xFFC0C0C0;

    PointData[2].x = 1.0F;
    PointData[2].y = -0.58F;
    PointData[2].z = 0.0F;
    ColorData[2].color = 0xFFC0C0C0;

    Point.p = PointData;
    Point.col = ColorData;
    Point.tex = NULL;
    Point.num = POINT_NUMBER;

	/* 文字色の設定 */
    /* sets the text color */
	njPrintColor(0xFFC0C0C0);
}

Sint32 njUserMain(void)
{
    static Angle rotateAng = NJM_DEG_ANG(0.0F);/* 回転角度 */
                                               /* rotation angle */
	static int counter = 0;
	static PDS_PERIPHERAL *key;/* ボタン入力 */
	                           /* peripheral information */
	static Bool pause = FALSE; /* ポーズがかかっているかどうか */
                               /* pause flag */
	/* ポーズの判定 */
	/* checks pause */
	key = pdGetPeripheral(PDD_PORT_A0);

	if (key->press & PDD_DGT_ST) {
	    if (pause == FALSE) {
		    pause = TRUE;
		} else {
		    pause = FALSE;
		}
	}

    /* タイトル表示 */
    /* prints the sample program name */
    njPrintC(NJM_LOCATION(23, 4), "Ninja Sample POLYGON ROTATION");

    /* マトリクスの待避 */
	/* pushes the matrix stack */
    njPushMatrix(NULL);
    
    /* 描画点への移動 */
    /* moves to the draw point */
    njTranslate(NULL, 0.0F, 0.0F, -10.0F);

    /* 回転 */
    /* rotates */
    njRotateXYZ(NULL, rotateAng, rotateAng, rotateAng);
    
    /* 三角形の描画 */
    /* draws triangle */
	if (pause == FALSE) {
	    njDrawTriangle3D(&Point, 1, NJD_DRAW_NORMAL|NJD_FILL);
	} else {
	    counter++;
	    if (counter > SCREEN_SAVE_TIME) {
		    njDrawTriangle3D(&Point, 1, NJD_DRAW_NORMAL|NJD_FILL);
			njPrintC(NJM_LOCATION(30, 5), "PAUSE");
		}
		if (counter == SCREEN_SAVE_TIME * 2) {
				counter = 0;
		}
	}
    /* 回転角度の設定 */
    /* sets the rotation angle */
	if (pause == FALSE) {
	    rotateAng += ROTATE_ANGLE_SPEED;
	}
    /* マトリクスの復帰 */
    /* pops the matrix stack */
    njPopMatrix(1);
    return NJD_USER_CONTINUE;
}
/*-----------------------------------
    終了
    exit
-----------------------------------*/
void njUserExit(void)
{
	njExitPrint();

    /* メモリの開放 */
	/* frees the memory area */
    syFree(TranslationMatrix);
    syFree(VertexBuffer);

    sbExitSystem();
}
