/*
 *  Middleware Library
 *  Copyright (c) 1998 SEGA
 *  
 *		Middleware Play Sample
 *
 *  Module  : Sample Program
 *  File    : test.c
 *  Create  : 1998-09-16
 *  Modify  : 
 *  Version : 0.57
 *  Note    : 
 */

#include	<machine.h>
#include	<shinobi.h>
#include	<sg_mwply.h>

void	initSnd(void *ptr);
void	*load_file(char *fname, long *len);
void	getVer(Sint32 *drvver, Sint32 *libver);
void	showSmpInfo(Uint32 cnt, Sint32 drvver, Sint32 libver);
void	sfcnt2time(long sf, long ncnt, long *hh, long *mm, long *ss, long *ff);
void	showStat(MWPLY ply);
void	errorFunc(void *errobj, Sint32 ecode);

void	initSnd(void *ptr)
{
	SDMEMBLK	memblk = NULL;

	/*	サウンドライブラリ初期化				*/
	/*	実引数はダミー							*/
	sdLibInit(NULL, 0, 0);
	/*	メモリブロックハンドル生成				*/
	sdMemBlkCreate(&memblk);
	/*	メモリブロックハンドル設定				*/
	sdMemBlkSetPrm(memblk, ptr, 0, SDD_MEMBLK_SYNC_FUNC, NULL);
	/*	サウンドドライバ初期化					*/
	sdDrvInit(memblk);
	/*	メモリブロック解放						*/
	sdMemBlkDestroy(memblk);
}

void	*load_file(char *fname, long *len)
{
	GDFS gdfs;
	long fsize, nsct;
	void *dat=NULL;

	if ( (gdfs=gdFsOpen(fname, NULL)) == NULL )
		for (;;);
	gdFsGetFileSize(gdfs, &fsize);
	nsct = (fsize+2047)/2048;
	if ( (dat=syMalloc(nsct*2048)) != NULL ) {
		gdFsRead(gdfs, nsct, dat);
	}
	gdFsClose(gdfs);
	if ( len != NULL )
		*len = fsize;

	return dat;
}

void getVer(Sint32 *drvver, Sint32 *libver)
{
	Sint32	tmp;
	SDS_VER drv_ver, lib_ver;

	sdDrvGetVer(&drv_ver);
	sdLibGetVer(&lib_ver);

	tmp = (Sint32)(drv_ver.m_MajorVer);
	*drvver = tmp << 16;
	tmp = (Sint32)drv_ver.m_MinorVer;
	*drvver |= tmp << 8;
	*drvver |= (Sint32)drv_ver.m_DebugVer;

	tmp = (Sint32)(lib_ver.m_MajorVer);
	*libver = tmp << 16;
	tmp = (Sint32)lib_ver.m_MinorVer;
	*libver |= tmp << 8;
	*libver |= (Sint32)lib_ver.m_DebugVer;
	return;
}

void showSmpInfo(Uint32 cnt, Sint32 drvver, Sint32 libver)
{
	njPrintC(NJM_LOCATION( 5,  2), "[Wave Decoder]");
	njPrintC(NJM_LOCATION( 5,  4), "Snd Drv   : ");
	njPrintH(NJM_LOCATION(17,  4), drvver, 6);
	njPrintC(NJM_LOCATION( 5,  5), "Snd Lib   : ");
	njPrintH(NJM_LOCATION(17,  5), libver, 6);
	njPrintC(NJM_LOCATION( 5,  6), "Prg Cnt   : ");
	njPrintD(NJM_LOCATION(17,  6), cnt, 8);
	njPrintC(NJM_LOCATION( 5,  8), "File Name : SAMPLE.WAV");
	return;
}

void sfcnt2time(long sf, long ncnt, long *hh, long *mm, long *ss, long *ff)
{
	register long nf = (long)((double)ncnt/(double)sf*100.0);

	*hh = nf/(60*60*100);
	*mm = nf/(60*100) - *hh*60;
	*ss = nf/100 - *hh*60*60 - *mm*60;
	*ff = nf%100;
}

void showStat(MWPLY ply)
{
	Sint32	stat, tscl, cnt;
	Sint32	hh, mm, ss, ff;
	Sint8	*strstat[5] = {"STOP","PREP","EXEC","END ","ERROR"};

	hh = mm = ss = ff = 0;
	stat = mwPlyGetStat(ply);
	mwPlyGetTime(ply, &cnt, &tscl);
	sfcnt2time(tscl, cnt, &hh, &mm, &ss, &ff);
	njPrintC(NJM_LOCATION( 5,  9), "Frequency :");
	njPrintD(NJM_LOCATION(17,  9), tscl, 5);
	njPrintC(NJM_LOCATION( 5, 10), "Status    :");
	njPrintC(NJM_LOCATION(17, 10), strstat[stat]);
	njPrintC(NJM_LOCATION( 5, 11), "Time      :");
	njPrintD(NJM_LOCATION(17, 11), hh, 2);
	njPrintC(NJM_LOCATION(19, 11), ":");
	njPrintD(NJM_LOCATION(20, 11), mm, 2);
	njPrintC(NJM_LOCATION(22, 11), ":");
	njPrintD(NJM_LOCATION(23, 11), ss, 2);
	njPrintC(NJM_LOCATION(25, 11), ".");
	njPrintD(NJM_LOCATION(26, 11), ff, 2);
	return;
}

void	errorFunc(void *errobj, Sint32 ecode)
{
	njPrintC(NJM_LOCATION(  5, 13), "Error!!");
	njPrintD(NJM_LOCATION( 14, 13), ecode, 8);
}


void	main(void)
{
	MWPLY			mwply;
	MWS_PLY_CPRM	cprm;
	void			*snddrv;
	Sint32			stat;
	Uint32			cnt;
	Sint32			drvver;
	Sint32			libver;

	/*	忍ライブラリの初期化				*/
    njSetBorderColor(0x00000000) ;
	sbInitSystem(NJD_RESOLUTION_320x240_NTSCNI,NJD_FRAMEBUFFER_MODE_RGB565,1);
	njInitVertexBuffer(10000, 0, 10000, 0, 0);
#if 0
	njInitPrint(NULL, 0, NJD_TEXFMT_ARGB_1555);
#else
    njInitPrint(NULL, 0, 0);
#endif
	njSetBackColor(0x00000000, 0x00000000, 0x000000FF);

	/*	サウンドドライバの読み込み			*/
	snddrv = load_file("MANATEE.DRV", NULL);
	/*	サウンドライブラリ初期化			*/
	initSnd(snddrv);
	getVer(&drvver, &libver);
	/*	サウンドのサーバ処理登録			*/
	njSetVSyncFunction((void *)sdSysServer);
	/*	ライブラリ初期化					*/
	mwPlyInitWav(MWD_PLY_MN_EXEC_VSYNC);
	/*	エラー関数登録						*/
	mwPlyEntryErrFunc(errorFunc, NULL);
	/*	ハンドル生成						*/
	mwply = mwPlyCreateWav(&cprm);

	/*	再生開始							*/
	mwPlyStartFname(mwply, "SAMPLE.WAV");
	for (cnt=0;;cnt++) {
		/*	サンプルプログラムの情報表示		*/
		showSmpInfo(cnt, drvver, libver);
		/*	再生状態の表示						*/
		showStat(mwply);
		/*	再生状態の確認						*/
		stat = mwPlyGetStat(mwply);
		if (stat == MWE_PLY_STAT_PLAYEND) {
			break;
		}
		mwPlyExecServer();
		njWaitVSync();
	}
	/*	再生停止							*/
	mwPlyStop(mwply);
	/*	ハンドル消去						*/
	mwPlyDestroy(mwply);
	/*	ライブラリ終了処理					*/
	mwPlyFinishWav();
	njWaitVSync();
	njWaitVSync();
	njWaitVSync();
	/*	忍ライブラリの終了処理				*/
	sbExitSystem();
	for (;;);
}
