/*----------------------------------------------------------------------------
  Shinobi2 Sample
  COPYRIGHT (C) SEGA ENTERPRISES, LTD.
----------------------------------------------------------------------------*/

#include	<shinobi.h>
#include	"Nj.h"
#include	"Sd.h"
#include	"TexMalloc.h"

#if DEBUG
#include	<nindows2.h>
#endif

NJS_VERTEXBUFFER_INFO	gVbinfo;
NJS_MATRIX				*pMatrix;
NJS_VERTEX_BUF			*pVtxBuf;
NJS_TEXMANAGE			*pTexMng;
NJS_TEXSYSTEM			*pTexSys;
Sint8					*pTexBuf;
const PDS_PERIPHERAL	*Per;

Bool					bExitFlag;
Bool					bExitEnable;

/*----------------------------------------------------------------------------
		エントリ
		entry
----------------------------------------------------------------------------*/
void	main(void)
{
	sbInitSystem();
	
	pMatrix = NULL;
	pVtxBuf = NULL;
	pTexMng = NULL;
	pTexSys = NULL;
	pTexBuf = NULL;
	gVbinfo.pVertexBuffer = NULL;
	
	/* Ninjaの初期化     */
	/* Initialize Ninja  */
	njInitDevice();
	njInitSystem( NJD_RESOLUTION_640x480_AUTO_NI,
										NJD_FRAMEBUFFER_MODE_RGB565, 1 );
	
	/*	システム情報の設定 */
	gVbinfo.bNoWaitVsync          = FALSE;
	gVbinfo.nNumOfMaxPass         = 1;
	gVbinfo.sPassInfo[0].bPreSort = FALSE;
	gVbinfo.sPassInfo[0].nSize[0] = -1;
	gVbinfo.sPassInfo[0].nSize[1] = 0x100000/4;
	gVbinfo.sPassInfo[0].nSize[2] = 0x200000/4;
	gVbinfo.sPassInfo[0].nSize[3] = 0x100000/4;
	gVbinfo.sPassInfo[0].nSize[4] = 0x100000/4;
	gVbinfo.nNumOfTextures        = TEX_NORMAL_NUM;
	gVbinfo.nNumOfSmallVQ         = 0;
	gVbinfo.nTextureMemorySize    = 0x390000/4;
	
	gVbinfo.nVertexBufferSize     = njCalcVertexBufferSize( &gVbinfo );
	gVbinfo.pVertexBuffer         = syMalloc( gVbinfo.nVertexBufferSize );
	
	InitTexFixedArea(0x80000);
	njInitVertexBufferEx( &gVbinfo );
	SetTexFixedArea();

	/*	バッファの確保       */
	/*	allocates the memory */
	pMatrix	= (NJS_MATRIX*)syMalloc( sizeof( NJS_MATRIX )     * MATRIX_SIZE );
	if (pMatrix == NULL) goto exit;
	
	pVtxBuf	= (NJS_VERTEX_BUF*)syMalloc( sizeof( NJS_VERTEX_BUF ) * VTXBUF_SIZE );
	if (pVtxBuf == NULL) goto exit;
	
	pTexMng = (NJS_TEXMANAGE*)syMalloc( sizeof( NJS_TEXMANAGE ) * (TEX_NORMAL_NUM + TEX_SMALLVQ_NUM) );
	if (pTexMng == NULL) goto exit;
	
	pTexSys = (NJS_TEXSYSTEM*)syMalloc( sizeof( NJS_TEXSYSTEM ) * (TEX_NORMAL_NUM + TEX_SMALLVQ_NUM) );
	if (pTexSys == NULL) goto exit;
	
	pTexBuf = (Sint8*)syMalloc( TEXBUF_SIZE );
	if (pTexBuf == NULL) goto exit;
	
	/*	マトリクススタックの初期化   */
	/*	initializes the matrix stack */
	njInitMatrix( pMatrix, MATRIX_SIZE );
	
	/*	3Dシステムの初期化     */
	/*	initializes 3D system  */ 
	njInit3D( pVtxBuf, VTXBUF_SIZE );
	
	/*	テクスチャ読み込みバッファの設定 */
	nuInitTextureBuffer( pTexBuf, TEXBUF_SIZE );
	
	/*	テクスチャメモリリストの設定 */
	njInitTextureEx( pTexMng, TEX_NORMAL_NUM + TEX_SMALLVQ_NUM,
					 pTexSys, TEX_NORMAL_NUM + TEX_SMALLVQ_NUM );
	
#if NOWLOADING
	njSetBackColor( 0x000000, 0x000000, 0x000000 );
//	DisplayNowLoading();
#endif
	
	/* GDドライブのマウント */
	if (gdFsMount()!=GDD_ERR_OK) goto exit;
	
	/*	マウスとキーボードの初期化 */
	pdMouseInit();
	pdKbdInit();
	
	/* ミドルウェア等で使用するファイバーマネージャの初期化 */
	/* Initialize fiber manager for middleware              */
	syMngInit();
	
#if DEBUG
	/*	デバッグ文字の初期化 */
	njInitPrint( NULL, 0, 0 );
	njPrintSize(16);
	
	/*	Nindows2の初期化 */
	{
		NWINFORMATION Info;
		Info.nSize  = sizeof( NWINFORMATION );
		Info.nFlags = NWD_USE_KEYBOARD
					| NWD_USE_MOUSE
					| NWD_USE_SERIAL;
		Info.nPadPort  = PDD_PORT_B0;
		if (njGetDisplayMode()&NJD_DSP_VGA)
			Info.nFontSize = NWD_FONT_NORMAL;
		else
			Info.nFontSize = NWD_FONT_LARGE;
		Info.pWork     = NULL;
		nwInitialize( &Info );
	}
	
	njSetBackColor( 0x000000, 0x0000FF, 0x000000 );
#endif
	
	/*	サウンド初期化 */
	SoundInit();
	
	Per	= pdGetPeripheral( PDD_PORT_A0 );
	bExitFlag   = FALSE;
	bExitEnable = TRUE;
	
	/* UserInitでエラーの場合はExitしてください */
	if( UserInit()==USER_CONTINUE ) {
		while ( !bExitFlag || !bExitEnable ) {
			njStartDraw();
			pdExecPeripheralServer();
			pdMouseExecServer();
			
			if ( UserMain()!=USER_CONTINUE )
				bExitFlag = TRUE;
#if DEBUG
			if ( nwMainExecute()!=USER_CONTINUE )
				bExitFlag = TRUE;
#endif
			njRender();
			njWaitVBlank();
			
			if ( pdIsPadReset( PDD_PORT_A0 )==TRUE )
				bExitFlag = TRUE;
			
			if ( gdFsIsTrayOpen()==TRUE )
				bExitFlag = TRUE;
		}
	}
	/* UserExitでエラーの場合は強制的にBOOTROMへ戻ります */
	if( UserExit()!=USER_CONTINUE )
		syBtExit();
	
	/*	サウンド終了 */
	SoundExit();
	
#if DEBUG
	/*	Nindowsの終了 */
	nwFinalize();
	
	/*	デバッグ文字開放 */
	njExitPrint();
#endif
	
	/* ミドルウェア等で使用するファイバーマネージャの終了処理 */
	/* Finalize filesystem for middleware                     */
	syMngFinish();
	
	/*	マウスとキーボードの終了 */
	pdKbdExit();
	pdMouseExit();
	
exit:
	/* Ninjaの終了処理 */
	/* Finalize Ninja. */
	njExitTexture();
	njExitSystem();
	
	/*	メモリの開放          */
	/*	frees the memory area */
	if ( pTexBuf ) syFree( pTexBuf );
	if ( pTexMng ) syFree( pTexMng );
	if ( pTexSys ) syFree( pTexSys );
	if ( pVtxBuf ) syFree( pVtxBuf );
	if ( pMatrix ) syFree( pMatrix );
	if ( gVbinfo.pVertexBuffer ) syFree( gVbinfo.pVertexBuffer );
	
	/*	システムの終了      */
	/*	exits the system	*/
	sbExitSystem();
	syBtExit();
}
/*--------------------------------------------------------------------------*/
