// ------------------------------------------------------------------------------------------------
// main.c
//
// This has the main function for the mw_snd64 testbed.
//
// COPYRIGHT (C) SEGA OF AMERICA, INC. 1998
// All rights reserved.
//
// Information and source code samples contained herein are 
// provided "as-is", without representations or warranties, and 
// are subject to change without notice.
//
// SEGA OF AMERICA, INC. cannot support modifications or 
// derivative works created from the sample source code provided. 
// Developers may use, reproduce, and modify portions or entire 
// sections of the sample source code for the purposes of creating
// Dreamcast applications.  
//
// Sample source code contained herein may not be appropriate 
// for use under all circumstances.  Please consult Developer 
// documentation provided along with your Dreamcast unit for
// additional information and suggestions.
//
//
// Author: Sean Hunt
//
// Rev:			06/08/99	File Originated (based on existing Strsfx and MW_ply01 samples)
//				06/08/99	Modified by Sean Hunt
//				06/09/99	Added cable check and VGA support
//							Removed unnecessary graphics init
// 				06/21/99	Removed Duck support (unnecessary to switch driver now)
// ------------------------------------------------------------------------------------------------

#include <shinobi.h>

/*	Display mode	*/
#define SYS_MODE		NJD_RESOLUTION_640x480_NTSCI
#define SYS_FRAME		NJD_FRAMEBUFFER_MODE_ARGB8888
#define SYS_COUNT		1

//	Vertex Buffer
#define SFD_VB_OP		-500000
#define SFD_VB_OM		0
#define SFD_VB_TP		20000
#define SFD_VB_TM		0
#define SFD_VB_PT		0

static void UsrGdErrFunc(void *obj, Sint32 errcode);
static void Exit( void );
void main(void);

void main(void)
{
	SYE_CBL_CABLE Cable;
	const NJS_PERIPHERAL	*per;

#ifdef __GNUC__
	shinobi_workaround();
#endif
	/*	Initialize system for middleware(call before sbInitSystem)	*/
	mwPlyPreInitSofdec();

	/*	Initialize SHINOBI		*/
	njSetBorderColor(0x00000000);

	// Check the cable for NTSC or VGA 
	Cable = syCblCheckCable();

	switch( Cable )
		{
		case SYE_CBL_CABLE_VGA:
			sbInitSystem(NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB565, 1);
			break;
	
		case SYE_CBL_CABLE_NTSC:
			sbInitSystem(NJD_RESOLUTION_640x480_NTSCNI, NJD_FRAMEBUFFER_MODE_RGB565, 1);
			break;
		}
	
	njInitVertexBuffer( 20000, 0, 20000, 0, 0 );
	njInitPrint( NULL, 0, NJD_TEXFMT_ARGB_1555 );
	njSetBackColor( 0x00000000, 0x00000000, 0x000000FF );
	njPrintSize(12);
	/*	Entry callback function of GD file system error	*/
	gdFsEntryErrFuncAll((void *)UsrGdErrFunc, NULL);

	for (;;) {
		per = njGetPeripheral(NJD_PORT_A0);
		njPrint(NJM_LOCATION(10, 3),
					"Middleware/Audio64 Switching Sample");
		njPrint(NJM_LOCATION(5, 6), "A     : Play MPEG Sofdec");
		njPrint(NJM_LOCATION(5,	8), "X     : Play MPEG/Audio");
		njPrint(NJM_LOCATION(5, 12), "START : Play Audio64 sounds");
		if (per->press & NJD_DGT_TA) {
			play_sfd("SAMPLE.SFD");
		}
		if (per->press & NJD_DGT_TX) {
			play_mpa("SAMPLE.MP2");
		}
		if (per->press & NJD_DGT_ST) {
			play_audio();
		}
		njWaitVSync();
	}

	MwExit();						/*	Finalize application		*/
	Exit();
}

void UsrGdErrFunc(void *obj, Sint32 errcode)
{
	if (errcode == GDD_ERR_TRAYOPEND || errcode == GDD_ERR_UNITATTENT) {
		MwExit();					/*	アプリケーション終了処理	*/
		Exit();							/*	Finalize application		*/
	}

	return;
}

void Exit( void )
{
	sbExitSystem();		/*	Finalize SHINOBI					*/
	syBtExit();				/*	Jump to Simple player				*/
}