#include "stdafx.h"
#include "ActPlayBGA.h"
#include "DTX.h"
#include "DTXViewer.h"
#include "CTimer.h"

namespace DTXViewer {
	namespace Stage {
		namespace Play {

static const int s_nChannel[8] = {
	0x04, 0x07, 0x55, 0x56, 0x57, 0x58, 0x59, 0x60,
};

ActPlayBGA::ActPlayBGA()
{
	this->bFirstWork = true;
	this->bActivate = false;
}

bool ActPlayBGA::MyOneTimeSceneInit()
{
	// 偙偙偵僐乕僪傪婰弎偡傞丅AddChild() 側偳丅

	return Activity::MyOneTimeSceneInit();
}
bool ActPlayBGA::MyFinalCleanup()
{
	// 偙偙偵僐乕僪傪婰弎偡傞丅

	return Activity::MyFinalCleanup();
}
bool ActPlayBGA::MyInitDeviceObjects()
{
	// 偙偙偵僐乕僪傪婰弎偡傞丅CTexture/CSurface::InitDeviceObjects() 側偳丅

	return Activity::MyInitDeviceObjects();
}
bool ActPlayBGA::MyDeleteDeviceObjects()
{
	// 偙偙偵僐乕僪傪婰弎偡傞丅

	// 師偺 Activity::MyDeleteDeviceObjects() 偱偼丄巕僥僋僗僠儍丒巕僒乕僼僃僀僗偺 Delete (CTexture/CSurface::Delete) 偑峴傢傟傞丅
	return Activity::MyDeleteDeviceObjects();
}
bool ActPlayBGA::MyActivate()
{
	this->bFirstWork = true;
	// 偙偙偵僐乕僪傪婰弎偡傞丅
	for( int i = 0; i < 8; i++ ) {
		this->structLayer[i].pBMP = NULL;
		this->structLayer[i].pBMPTEX = NULL;
		this->structLayer[i].db堏摦奐巒帪崗ms = INIT_TIME;
	}

	// 師偺 Activity::MyActivate() 偱偼丄this->bActivity 偺僙僢僩側傜傃偵巕僒乕僼僃僀僗丒巕僥僋僗僠儍偺 Restore (MyRestoreDeviceTexture/Surface) 偑峴傢傟傞丅
	// 側偍丄巕 Activity 偺妶惈壔偼峴傢傟側偄丅乮偙偺儊僜僢僪傪敳偗偰偐傜峴傢傟傞丅乯
	return Activity::MyActivate();
}
bool ActPlayBGA::MyUnActivate()
{
	// 偙偙偵僐乕僪傪婰弎偡傞丅

	// 師偺 Activity::MyUnActivate() 偱偼丄this->bActivity 偺儕僙僢僩側傜傃偵巕僒乕僼僃僀僗丒巕僥僋僗僠儍偺 Invalidate (MyInvalidateDeviceObjects) 偑峴傢傟傞丅
	// 側偍丄巕Activity 偺旕妶惈壔偼峴傢傟側偄丅乮偙偺儊僜僢僪傪敳偗偰偐傜峴傢傟傞丅乯
	return Activity::MyUnActivate();
}
bool ActPlayBGA::MyRestoreDeviceTextures()
{
	// 偙偙偵僐乕僪傪婰弎偡傞丅

	// 師偺 Activity::MyRestoreDeviceTextures() 偱偼丄巕僥僋僗僠儍偺 Restore (CTexture::Restore) 偑峴傢傟傞丅
	return Activity::MyRestoreDeviceTextures();
}
bool ActPlayBGA::MyRestoreDeviceSurfaces()
{
	// 偙偙偵僐乕僪傪婰弎偡傞丅

	// 師偺 Activity::MyRestoreDeviceSurfaces() 偱偼丄巕僒乕僼僃僀僗偺 Restore (CSurface::Restore) 偑峴傢傟傞丅
	return Activity::MyRestoreDeviceSurfaces();
}
bool ActPlayBGA::MyInvalidateDeviceObjects()
{
	// 偙偙偵僐乕僪傪婰弎偡傞丅

	// 師偺 Activity::MyInvalidateDeviceObjects() 偱偼丄巕僥僋僗僠儍丒巕僒乕僼僃僀僗偺 Invalidate (CTexture/CSurface::Invalidate) 偑峴傢傟傞丅
	return Activity::MyRestoreDeviceSurfaces();
}
//
void ActPlayBGA::Start( int n僠儍儞僱儖, DTX::BMP *bmp, DTX::BMPTEX* bmptex, int n奐巒僒僀僘W, int n奐巒僒僀僘H, int n廔椆僒僀僘W, int n廔椆僒僀僘H, int n夋憸懁奐巒埵抲X, int n夋憸懁奐巒埵抲Y, int n夋憸懁廔椆埵抲X, int n夋憸懁廔椆埵抲Y, int n昞帵懁奐巒埵抲X, int n昞帵懁奐巒埵抲Y, int n昞帵懁廔椆埵抲X, int n昞帵懁廔椆埵抲Y, double db憤堏摦帪娫ms, double db堏摦奐巒帪崗ms )
{
	// 僠儍儞僱儖僠僃僢僋
	for( int i = 0; i < 8; i++ )
	{
		if( n僠儍儞僱儖 == s_nChannel[i] )
		{
			// 僙儖偵抣傪愝掕												BMP/BMPTEX	BGA		BGAPAN
			this->structLayer[i].pBMP				= bmp;				//		仜		仜		仜
			this->structLayer[i].pBMPTEX			= bmptex;			//		仜		仜		仜
			this->structLayer[i].sz奐巒僒僀僘.cx	= n奐巒僒僀僘W;		//		仜		仜		仜
			this->structLayer[i].sz奐巒僒僀僘.cy	= n奐巒僒僀僘H;		//		仜		仜		仜
			this->structLayer[i].sz廔椆僒僀僘.cx	= n廔椆僒僀僘W;		//		--		--		仜
			this->structLayer[i].sz廔椆僒僀僘.cy	= n廔椆僒僀僘H;		//		--		--		仜
			this->structLayer[i].pt夋憸懁奐巒埵抲.x	= n夋憸懁奐巒埵抲X;	//		--		仜		仜
			this->structLayer[i].pt夋憸懁奐巒埵抲.y	= n夋憸懁奐巒埵抲Y;	//		--		仜		仜
			this->structLayer[i].pt夋憸懁廔椆埵抲.x	= n夋憸懁廔椆埵抲X;	//		--		--		仜
			this->structLayer[i].pt夋憸懁廔椆埵抲.y	= n夋憸懁廔椆埵抲Y;	//		--		--		仜
			this->structLayer[i].pt昞帵懁奐巒埵抲.x	= n昞帵懁奐巒埵抲X;	//		--		仜		仜
			this->structLayer[i].pt昞帵懁奐巒埵抲.y	= n昞帵懁奐巒埵抲Y;	//		--		仜		仜
			this->structLayer[i].pt昞帵懁廔椆埵抲.x	= n昞帵懁廔椆埵抲X;	//		--		--		仜
			this->structLayer[i].pt昞帵懁廔椆埵抲.y	= n昞帵懁廔椆埵抲Y;	//		--		--		仜
			this->structLayer[i].db憤堏摦帪娫ms		= db憤堏摦帪娫ms;	//		--		--		仜
			this->structLayer[i].db堏摦奐巒帪崗ms	= (db堏摦奐巒帪崗ms != INIT_TIME) ? db堏摦奐巒帪崗ms : g_Timer.Get();
		}
	}
}
void ActPlayBGA::SkipStart( double db堏摦奐巒帪崗ms )
{
	for( DTX::Chip* cell = g_DTX.pChip; cell != NULL; cell = cell->next )
	{
		// 奐巒帪崗傛傝屻傠側傜丄偦偺僠僢僾偱挷嵏懪偪愗傝丅
		if( cell->dbTime > db堏摦奐巒帪崗ms ) 
			break;

		// BMP, BMPTEX, BGA, BGAPAN 側傜嵞惗巜帵
		switch( cell->BGAtype )
		{

		case BGATYPE_BMP:
			if( cell->pBMP )
				Start( cell->nChannel, cell->pBMP, NULL,
					   cell->pBMP->pSurface->dwWidth, cell->pBMP->pSurface->dwHeight, 0, 0,
					   0, 0, 0, 0,
					   0, 0, 0, 0, 0, cell->dbTime );
			break;

		case BGATYPE_BMPTEX:
			if( cell->pBMPTEX )
				Start( cell->nChannel, NULL, cell->pBMPTEX,
					   cell->pBMPTEX->pTexture->dwWidth, cell->pBMPTEX->pTexture->dwHeight, 0, 0,
					   0, 0, 0, 0,
					   0, 0, 0, 0, 0, cell->dbTime );
			break;

		case BGATYPE_BGA:
			if( cell->pBGA )
				Start( cell->nChannel, cell->pBMP, cell->pBMPTEX,
					   cell->pBGA->x2 - cell->pBGA->x1, cell->pBGA->y2 - cell->pBGA->y1, 0, 0, 
					   cell->pBGA->x1, cell->pBGA->y1, 0, 0,
					   cell->pBGA->ox, cell->pBGA->oy, 0, 0, 0, cell->dbTime );
			break;

		case BGATYPE_BGAPAN:
			if( cell->pBGAPan )
				Start( cell->nChannel, cell->pBMP, cell->pBMPTEX,
						cell->pBGAPan->sw, cell->pBGAPan->sh, cell->pBGAPan->ew, cell->pBGAPan->eh,
						cell->pBGAPan->ssx, cell->pBGAPan->ssy, cell->pBGAPan->sex, cell->pBGAPan->sey,
						cell->pBGAPan->dsx, cell->pBGAPan->dsy, cell->pBGAPan->dex, cell->pBGAPan->dey,
						cell->dbLong, cell->dbTime );
			break;
		}
	}
}
void ActPlayBGA::Stop()
{
	for( int i = 0; i < 8; i++ )
		this->structLayer[i].db堏摦奐巒帪崗ms = INIT_TIME;
}
void ActPlayBGA::Cont( double db嵞奐帪崗ms )
{
	for( int i = 0; i < 8; i++ )
	{
		if(  ( this->structLayer[i].pBMP    == NULL && this->structLayer[i].pBMPTEX == NULL ) 
		  || ( this->structLayer[i].pBMP    != NULL && ( ! this->structLayer[i].pBMP->bUse    || this->structLayer[i].pBMP->pSurface    == NULL ) )
		  || ( this->structLayer[i].pBMPTEX != NULL && ( ! this->structLayer[i].pBMPTEX->bUse || this->structLayer[i].pBMPTEX->pTexture == NULL ) ) )
			continue;

		this->structLayer[i].db堏摦奐巒帪崗ms = db嵞奐帪崗ms;
	}
}
void ActPlayBGA::ChangeScope( int n僠儍儞僱儖, DTX::BMP* bmp, DTX::BMPTEX* bmptex )
{
	// 僠儍儞僱儖僠僃僢僋
	for( int i = 0; i < 8; i++ )
	{
		if( n僠儍儞僱儖 == s_nChannel[i] )
		{
			// 僙儖偵抣傪愝掕(bmp, bmptex 偺傒)
			this->structLayer[i].pBMP		= bmp;
			this->structLayer[i].pBMPTEX	= bmptex;
		}
	}
}
int  ActPlayBGA::MainWork( int x, int y )
{
	for( int i = 0; i < 8; i++ )
	{
		// 枹巊梡側傜僗僉僢僾
		if( this->structLayer[i].db堏摦奐巒帪崗ms == INIT_TIME
			|| ( this->structLayer[i].pBMP    == NULL && this->structLayer[i].pBMPTEX == NULL ) 
			|| ( this->structLayer[i].pBMP    != NULL && ( ! this->structLayer[i].pBMP->bUse    || this->structLayer[i].pBMP->pSurface    == NULL ) )
			|| ( this->structLayer[i].pBMPTEX != NULL && ( ! this->structLayer[i].pBMPTEX->bUse || this->structLayer[i].pBMPTEX->pTexture == NULL ) ) )
			continue;

		// 僷儔儊乕僞拪弌丒嶼弌
		SIZE	sz奐巒僒僀僘		= this->structLayer[i].sz奐巒僒僀僘;
		SIZE	sz廔椆僒僀僘		= this->structLayer[i].sz廔椆僒僀僘;
		POINT	pt夋憸懁奐巒埵抲	= this->structLayer[i].pt夋憸懁奐巒埵抲;
		POINT	pt夋憸懁廔椆埵抲	= this->structLayer[i].pt夋憸懁廔椆埵抲;
		POINT	pt昞帵懁奐巒埵抲	= this->structLayer[i].pt昞帵懁奐巒埵抲;
		POINT	pt昞帵懁廔椆埵抲	= this->structLayer[i].pt昞帵懁廔椆埵抲;
		double	db憤堏摦帪娫ms		= this->structLayer[i].db憤堏摦帪娫ms;
		double	db堏摦奐巒帪崗ms	= this->structLayer[i].db堏摦奐巒帪崗ms; if( g_Timer.Get() < db堏摦奐巒帪崗ms ) db堏摦奐巒帪崗ms = g_Timer.Get();
		SIZE	szBGA僒僀僘			= { BGA_W, BGA_H };
		SIZE	szBMP僒僀僘			= { (this->structLayer[i].pBMP) ? this->structLayer[i].pBMP->pSurface->dwWidth  : this->structLayer[i].pBMPTEX->pTexture->dwImageWidth,
										(this->structLayer[i].pBMP) ? this->structLayer[i].pBMP->pSurface->dwHeight : this->structLayer[i].pBMPTEX->pTexture->dwImageHeight };

		double	db尰嵼帪崗ms		= ( g_Timer.Get() - db堏摦奐巒帪崗ms ) * g_DTX.fSpeed;	// 奐巒偐傜偺宱夁帪娫[ms]

		// 廔椆敾掕 (db憤堏摦帪崗ms==0 偺偲偒偼廔傢傝側偟乯
		if( db憤堏摦帪娫ms != 0.0 && db憤堏摦帪娫ms < db尰嵼帪崗ms )
		{
			this->structLayer[i].pt夋憸懁奐巒埵抲 = pt夋憸懁奐巒埵抲 = pt夋憸懁廔椆埵抲;
			this->structLayer[i].pt昞帵懁奐巒埵抲 = pt昞帵懁奐巒埵抲 = pt昞帵懁廔椆埵抲;
			this->structLayer[i].sz奐巒僒僀僘     = sz奐巒僒僀僘     = sz廔椆僒僀僘;
			this->structLayer[i].db憤堏摦帪娫ms	  = db憤堏摦帪娫ms   = 0.0;
			//db堏摦奐巒帪崗nms	= this->structLayer[i].db堏摦奐巒帪崗ms = INIT_TIME;		僞僀儅傪弶婜壔偟偨傜儗僀儎帺懱僗僉僢僾偝傟傞偐傜僟儊
		}

		/////////////
		// 尰嵼帪崗偐傜夋憸椞堟偲昞帵椞堟傪寁嶼偡傞

		RECT rc夋憸懁椞堟;		// 夋憸偺嵍忋偑 0, 0
		RECT rc昞帵懁椞堟;		// BGA椞堟偺嵍忋偑 0, 0

		if( db憤堏摦帪娫ms == 0.0 )
		{
			// 奐巒埵抲丒奐巒僒僀僘傪偢偭偲曐帩
			rc夋憸懁椞堟.left   = pt夋憸懁奐巒埵抲.x;
			rc夋憸懁椞堟.top    = pt夋憸懁奐巒埵抲.y;
			rc夋憸懁椞堟.right  = rc夋憸懁椞堟.left + sz奐巒僒僀僘.cx;
			rc夋憸懁椞堟.bottom = rc夋憸懁椞堟.top  + sz奐巒僒僀僘.cy;
			
			rc昞帵懁椞堟.left   = pt昞帵懁奐巒埵抲.x;
			rc昞帵懁椞堟.top    = pt昞帵懁奐巒埵抲.y;
			rc昞帵懁椞堟.right  = rc昞帵懁椞堟.left + sz奐巒僒僀僘.cx;
			rc昞帵懁椞堟.bottom = rc昞帵懁椞堟.top  + sz奐巒僒僀僘.cy;
		}
		else
		{
			// db妱崌; 奐巒仺廔椆 偺偲偒 0.0 仺 1.0
			double db妱崌 = db尰嵼帪崗ms / db憤堏摦帪娫ms;
		
			// 尰嵼偺僒僀僘乮夋憸丒昞帵嫟捠乯
			SIZE sz尰嵼偺僒僀僘 = {
				sz奐巒僒僀僘.cx + (LONG)( ( sz廔椆僒僀僘.cx - sz奐巒僒僀僘.cx ) * db妱崌 ),
				sz奐巒僒僀僘.cy + (LONG)( ( sz廔椆僒僀僘.cy - sz奐巒僒僀僘.cy ) * db妱崌 )
			};

			// 揮憲尦椞堟
			rc夋憸懁椞堟.left   = pt夋憸懁奐巒埵抲.x + (LONG)( ( pt夋憸懁廔椆埵抲.x - pt夋憸懁奐巒埵抲.x ) * db妱崌 );
			rc夋憸懁椞堟.top    = pt夋憸懁奐巒埵抲.y + (LONG)( ( pt夋憸懁廔椆埵抲.y - pt夋憸懁奐巒埵抲.y ) * db妱崌 );
			rc夋憸懁椞堟.right  = rc夋憸懁椞堟.left + sz尰嵼偺僒僀僘.cx;
			rc夋憸懁椞堟.bottom = rc夋憸懁椞堟.top  + sz尰嵼偺僒僀僘.cy;

			// 揮憲愭椞堟
			rc昞帵懁椞堟.left   = pt昞帵懁奐巒埵抲.x + (LONG)( ( pt昞帵懁廔椆埵抲.x - pt昞帵懁奐巒埵抲.x ) * db妱崌 );
			rc昞帵懁椞堟.top    = pt昞帵懁奐巒埵抲.y + (LONG)( ( pt昞帵懁廔椆埵抲.y - pt昞帵懁奐巒埵抲.y ) * db妱崌 );
			rc昞帵懁椞堟.right  = rc昞帵懁椞堟.left + sz尰嵼偺僒僀僘.cx;
			rc昞帵懁椞堟.bottom = rc昞帵懁椞堟.top  + sz尰嵼偺僒僀僘.cy;
		}

		// 揮憲晹暘偑側偄側傜僗僉僢僾嘆 僋儕僢僺儞僌慜
		if( rc夋憸懁椞堟.right <= 0 || rc夋憸懁椞堟.bottom <= 0 || rc夋憸懁椞堟.left >= szBMP僒僀僘.cx || rc夋憸懁椞堟.top >= szBMP僒僀僘.cy ) continue;
		if( rc昞帵懁椞堟.right <= 0 || rc昞帵懁椞堟.bottom <= 0 || rc昞帵懁椞堟.left >= szBGA僒僀僘.cx || rc昞帵懁椞堟.top >= szBGA僒僀僘.cy ) continue;

		// 僋儕僢僺儞僌嘆 揮憲尦
		if( rc夋憸懁椞堟.left < 0 )	{ rc昞帵懁椞堟.left += -rc夋憸懁椞堟.left; rc夋憸懁椞堟.left = 0; }
		if( rc夋憸懁椞堟.top  < 0 )	{ rc昞帵懁椞堟.top  += -rc夋憸懁椞堟.top;  rc夋憸懁椞堟.top  = 0; }
		if( rc夋憸懁椞堟.right  > szBMP僒僀僘.cx ) { rc昞帵懁椞堟.right  -= rc夋憸懁椞堟.right  - szBMP僒僀僘.cx; rc夋憸懁椞堟.right  = szBMP僒僀僘.cx; }
		if( rc夋憸懁椞堟.bottom > szBMP僒僀僘.cy ) { rc昞帵懁椞堟.bottom -= rc夋憸懁椞堟.bottom - szBMP僒僀僘.cy; rc夋憸懁椞堟.bottom = szBMP僒僀僘.cy; }

		// 僋儕僢僺儞僌嘇 揮憲愭
		if( rc昞帵懁椞堟.left < 0 ) { rc夋憸懁椞堟.left += -rc昞帵懁椞堟.left; rc昞帵懁椞堟.left = 0; }
		if( rc昞帵懁椞堟.top  < 0 ) { rc夋憸懁椞堟.top  += -rc昞帵懁椞堟.top;  rc昞帵懁椞堟.top  = 0; }
		if( rc昞帵懁椞堟.right  > szBGA僒僀僘.cx ) { rc夋憸懁椞堟.right  -= rc昞帵懁椞堟.right  - szBGA僒僀僘.cx; rc昞帵懁椞堟.right  = szBGA僒僀僘.cx; }
		if( rc昞帵懁椞堟.bottom > szBGA僒僀僘.cy ) { rc夋憸懁椞堟.bottom -= rc昞帵懁椞堟.bottom - szBGA僒僀僘.cy; rc昞帵懁椞堟.bottom = szBGA僒僀僘.cy; }

		// 揮憲晹暘偑側偄側傜僗僉僢僾嘇 僋儕僢僺儞僌屻
		if( rc夋憸懁椞堟.left >= rc夋憸懁椞堟.right || rc夋憸懁椞堟.top >= rc夋憸懁椞堟.bottom ) continue;
		if( rc昞帵懁椞堟.left >= rc昞帵懁椞堟.right || rc昞帵懁椞堟.top >= rc昞帵懁椞堟.bottom ) continue;
		if( rc夋憸懁椞堟.right < 0 || rc夋憸懁椞堟.bottom < 0 || rc夋憸懁椞堟.left > szBMP僒僀僘.cx || rc夋憸懁椞堟.top > szBMP僒僀僘.cy ) continue;
		if( rc昞帵懁椞堟.right < 0 || rc昞帵懁椞堟.bottom < 0 || rc昞帵懁椞堟.left > szBGA僒僀僘.cx || rc昞帵懁椞堟.top > szBGA僒僀僘.cy ) continue;

		// 仸僙儞僞儕儞僌偼丄BGAPAN 偱偼堄枴側偄乮偢傟傞乯偺偱攑巭(2003/03/02)

		// 昤夋
		if( this->structLayer[i].pBMP )
		{
			this->structLayer[i].pBMP->pSurface->ColorKeyEnable( (i == 0) ? false : (g_DTX.bBlackColorKey ? true : false) );	// 嵟壓憌儗僀儎偼摟夁偟側偄
			this->structLayer[i].pBMP->pSurface->Draw( x + rc昞帵懁椞堟.left, y + rc昞帵懁椞堟.top, &rc夋憸懁椞堟 );
		}
		else if( this->structLayer[i].pBMPTEX )
		{
			this->structLayer[i].pBMPTEX->pTexture->Draw( x + rc昞帵懁椞堟.left, y + rc昞帵懁椞堟.top, &rc夋憸懁椞堟 );
		}
	}
	return 0;
}

		}//Play
	}//Stage
}//DTXViewer
