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

#include	<shinobi.h>
#include	"Km.h"
#include	"Sd.h"

KMVERTEXBUFFDESC		VertexBufferDesc;
KMSYSTEMCONFIGSTRUCT	SystemConfig;
PKMSURFACEDESC			pSurfaceArray[2];
KMSURFACEDESC			PrimarySurfaceDesc;
KMSURFACEDESC			BackSurfaceDesc;

PKMDWORD				pTextureWork;
PKMDWORD				pVertexBufferWork;
const PDS_PERIPHERAL	*Per;
Bool					bExitFlag;
Bool					bExitEnable;
/*----------------------------------------------------------------------------
		エントリ
		entry
----------------------------------------------------------------------------*/
void	main(void)
{
	KMWORKAREASIZE TexTmpArea;
	
	sbInitSystem();
	
	pTextureWork      = NULL;
	pVertexBufferWork = NULL;
	
	/* Kamuiの初期化     */
	/* Initialize Kamui  */
	kmInitDevice( KM_DREAMCAST );
	
	switch (syVideoGetVideoMode()) {
	case SYD_VIDEO_MODE_VGA:
		kmSetDisplayMode( KM_DSPMODE_VGA640x480, KM_DSPBPP_RGB565, TRUE, FALSE );
		break;
	case SYD_VIDEO_MODE_NTSC:
		kmSetDisplayMode( KM_DSPMODE_NTSCNI640x480, KM_DSPBPP_RGB565, TRUE, FALSE );
		break;
	case SYD_VIDEO_MODE_PAL:
		kmSetDisplayMode( KM_DSPMODE_PALNI640x480, KM_DSPBPP_RGB565, TRUE, FALSE );
		break;
	}
	kmSetWaitVsyncCount( 1 );
	
	pSurfaceArray[0] = &PrimarySurfaceDesc;
	pSurfaceArray[1] = &BackSurfaceDesc;
	
	SystemConfig.dwSize					= sizeof( KMSYSTEMCONFIGSTRUCT );
	SystemConfig.nVertexBufferSize		= VERTEXBUFFER_SIZE;
	SystemConfig.nTextureMemorySize		= TEXTURE_MEMORY_SIZE;
	SystemConfig.flags					= KM_CONFIGFLAG_ENABLE_CLEAR_FRAMEBUFFER
										| KM_CONFIGFLAG_ENABLE_2V_LATENCY
										| KM_CONFIGFLAG_NOWAIT_FINISH_TEXTUREDMA;
	
	SystemConfig.pBufferDesc 			= &VertexBufferDesc;
	SystemConfig.ppSurfaceDescArray		= pSurfaceArray;
	SystemConfig.fb.nNumOfFrameBuffer	= 2;
	SystemConfig.nNumOfVertexBank		= 2;
	SystemConfig.nPassDepth				= 1;
	
	SystemConfig.Pass[0].dwRegionArrayFlag		= KM_PASSINFO_AUTOSORT;
	SystemConfig.Pass[0].nDirectTransferList	= KM_OPAQUE_POLYGON;
	SystemConfig.Pass[0].fBufferSize[0]			=   0.0f;
	SystemConfig.Pass[0].fBufferSize[1]			=  20.0f;
	SystemConfig.Pass[0].fBufferSize[2]			=  40.0f;
	SystemConfig.Pass[0].fBufferSize[3]			=  20.0f;
	SystemConfig.Pass[0].fBufferSize[4]			=  20.0f;
	
	TexTmpArea.dwNumberOfFameBuffes     = 2;
	TexTmpArea.dwNumberOfNativeBuffers  = 2;
	TexTmpArea.dwNumberOfTextures       = 256;
	TexTmpArea.dwNumberOf8x8SmallVQ     = 0;
	TexTmpArea.dwNumberOf8x8SmallVQmm   = 0;
	TexTmpArea.dwNumberOf16x16SmallVQ   = 0;
	TexTmpArea.dwNumberOf16x16SmallVQmm = 0;
	TexTmpArea.dwNumberOf32x32SmallVQ   = 64;
	TexTmpArea.dwNumberOf32x32SmallVQmm = 0;
	TexTmpArea.dwNumberOf64x64SmallVQ   = 0;
	TexTmpArea.dwNumberOf64x64SmallVQmm = 0;
	kmuCalculateKamuiWorkareaSize( &TexTmpArea );
	
	pTextureWork      = syMalloc( TexTmpArea.dwKamuiWorkareaSize );
	pVertexBufferWork = syMalloc( SystemConfig.nVertexBufferSize );
	
	SystemConfig.nNumOfTextureStruct 	= TexTmpArea.dwTextureStructNum;
	SystemConfig.nNumOfSmallVQStruct 	= TexTmpArea.dwSmallVQStructNum;
	SystemConfig.pTextureWork			= pTextureWork;
	SystemConfig.pVertexBuffer 			= pVertexBufferWork;
	
	kmSetSystemConfiguration( &SystemConfig );
	
#if NOWLOADING
	InitBackGround( 0x000000, 0x000000, 0x000000 );
	DisplayNowLoading();
#endif
	
	/* GDドライブのマウント */
	if (gdFsMount()!=GDD_ERR_OK) goto exit;
	
	/*	マウスとキーボードの初期化 */
	pdMouseInit();
	pdKbdInit();
	
	/* ミドルウェア等で使用するファイバーマネージャの初期化 */
	/* Initialize fiber manager for middleware              */
	syMngInit();
	
#if DEBUG
	/*	デバッグ文字の初期化 */
	kmuInitPrint( &VertexBufferDesc );
	kmuPrintSize( 16.f );
	InitBackGround( 0x000000, 0xFF0000, 0x000000 );
#endif
	
	/*	サウンド初期化 */
	SoundInit();
	
	Per	= pdGetPeripheral( PDD_PORT_A0 );
	bExitFlag   = FALSE;
	bExitEnable = TRUE;
	
	/* UserInitでエラーの場合はExitしてください */
	if( UserInit()==USER_CONTINUE ) {
		while ( !bExitFlag || !bExitEnable ) {
			kmBeginScene( &SystemConfig );
			kmBeginPass( &VertexBufferDesc );
			
			pdExecPeripheralServer();
			pdMouseExecServer();
			
			if ( UserMain()!=USER_CONTINUE )
				bExitFlag = TRUE;
			
			kmEndPass( &VertexBufferDesc );
			kmRender( KM_RENDER_FLIP );
			kmEndScene( &SystemConfig );
			
			if ( pdIsPadReset( PDD_PORT_A0 )==TRUE )
				bExitFlag = TRUE;
			
			if ( gdFsIsTrayOpen()==TRUE )
				bExitFlag = TRUE;
		}
	}
	/* UserExitでエラーの場合は強制的にBOOTROMへ戻ります */
	if( UserExit()!=USER_CONTINUE )
		syBtExit();
	
	/*	サウンド終了 */
	SoundExit();
	
#if DEBUG
	/*	デバッグ文字開放 */
	kmuExitPrint();
#endif
	
	/* ミドルウェア等で使用するファイバーマネージャの終了処理 */
	/* Finalize filesystem for middleware                     */
	syMngFinish();
	
	/*	マウスとキーボードの終了 */
	pdKbdExit();
	pdMouseExit();
	
exit:
	/*	メモリの開放 */
	/*	frees the memory area */
	if ( pTextureWork      ) syFree( pTextureWork      );
	if ( pVertexBufferWork ) syFree( pVertexBufferWork );
	
	/* Kamuiの終了処理 */
	/* Finalize Kamui. */
	kmUnloadDevice();
	
	/*	システムの終了 */
	/*	exits the system	*/
	sbExitSystem();
	syBtExit();
}
/*-------------------------------------------------------------------------*/
void InitBackGround( Uint32 c0, Uint32 c1, Uint32 c2 )
{
	KMSTRIPCONTEXT	StripContext;
	KMSTRIPHEAD		StripHead;
	KMVERTEX_00		V[3];
	
	StripContext.nSize = sizeof(KMSTRIPCONTEXT);
	kmInitStripContext(
			KM_STRIPCONTEXT_SYS_GOURAUD | KM_OPAQUE_POLYGON, &StripContext );
	kmGenerateStripHead00( &StripHead, &StripContext );
	
	V[0].ParamControlWord  = KM_VERTEXPARAM_NORMAL;
	V[0].fX                =   0.0f;
	V[0].fY                =   0.0f;
	V[0].u.fZ              =   0.0001f;
	V[0].uBaseRGB.dwPacked = c0;
	
	V[1].ParamControlWord  = KM_VERTEXPARAM_NORMAL;
	V[1].fX                = 640.0f;
	V[1].fY                =   0.0f;
	V[1].u.fZ              =   0.0001f;
	V[1].uBaseRGB.dwPacked = c1;
	
	V[2].ParamControlWord  = KM_VERTEXPARAM_ENDOFSTRIP;
	V[2].fX                =   0.0f;
	V[2].fY                = 480.0f;
	V[2].u.fZ              =   0.0001f;
	V[2].uBaseRGB.dwPacked = c2;
	
	kmSetBackGround(
		&StripHead,
		KM_VERTEXTYPE_00,
		(PKMVOID)&V[0],
		(PKMVOID)&V[1],
		(PKMVOID)&V[2]
	);
}
/*-------------------------------------------------------------------------*/
