#include "stdafx.h"
#include "ActPlayAVI.h"
#include "CSurface.h"
#include "DTX.h"
#include "CStage.h"
#include "DTXViewer.h"

namespace DTXViewer {
	namespace Stage {
		namespace Play {

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

bool ActPlayAVI::MyOneTimeSceneInit()
{
	// 偙偙偵僐乕僪傪婰弎偡傞丅AddChild() 側偳丅
	AddChild( &this->sfAVI );

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

	return Activity::MyFinalCleanup();
}
bool ActPlayAVI::MyInitDeviceObjects()
{
	// 偙偙偵僐乕僪傪婰弎偡傞丅CTexture/CSurface::InitDeviceObjects() 側偳丅
	this->sfAVI.InitDeviceObjects( _T("AVI"), BGA_W, BGA_H );

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

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

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

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

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

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

	// 師偺 Activity::MyInvalidateDeviceObjects() 偱偼丄巕僥僋僗僠儍丒巕僒乕僼僃僀僗偺 Invalidate (CTexture/CSurface::Invalidate) 偑峴傢傟傞丅
	return Activity::MyRestoreDeviceSurfaces();
}
//
void ActPlayAVI::Start( int n僠儍儞僱儖斣崋, DTX::AVI* pAVI, 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 )
{
	// 僠儍儞僱儖僠僃僢僋
	if( n僠儍儞僱儖斣崋 != 0x54 )
		return;

	// 僙儖偵抣傪愝掕
	int i = 0;
	this->structLayer[i].pAVI	= pAVI;
	this->structLayer[i].n奐巒僒僀僘W	= n奐巒僒僀僘W;
	this->structLayer[i].n奐巒僒僀僘H	= n奐巒僒僀僘H;
	this->structLayer[i].n夋憸懁奐巒埵抲X	= n夋憸懁奐巒埵抲X;
	this->structLayer[i].n夋憸懁奐巒埵抲Y	= n夋憸懁奐巒埵抲Y;
	this->structLayer[i].n昞帵懁奐巒埵抲X	= n昞帵懁奐巒埵抲X;
	this->structLayer[i].n昞帵懁奐巒埵抲Y	= n昞帵懁奐巒埵抲Y;
	this->structLayer[i].db憤堏摦帪娫ms		= db憤堏摦帪娫ms;
	this->structLayer[i].db堏摦奐巒帪崗ms	= ( db堏摦奐巒帪崗ms != INIT_TIME ) ? db堏摦奐巒帪崗ms : g_Timer.Get();
	this->structLayer[i].dw慜夞昞帵偟偨僼儗乕儉斣崋 = 0;
}
void ActPlayAVI::SkipStart( double db堏摦奐巒帪崗ms )
{
	for( DTX::Chip* cell = g_DTX.pChip; cell != NULL; cell = cell->next )
	{
		// 奐巒帪崗傛傝屻傠側傜丄偦偺僠僢僾偱挷嵏懪偪愗傝丅
		if( cell->dbTime > db堏摦奐巒帪崗ms ) 
			break;

		// AVI, AVIPAN 側傜嵞惗巜帵
		switch( cell->AVItype )
		{

		case AVITYPE_AVI:
			if( cell->pAVI )
				this->Start(
					cell->nChannel, cell->pAVI,
					BGA_W, BGA_H, 0, 0,			// sw, sh, ew, eh		; 椞堟   弶婜僒僀僘丄廔椆僒僀僘
					0, 0, 0, 0,					// ssx, ssy, sex, sey	; 夋憸懁 弶婜埵抲丄嵟廔埵抲
					0, 0, 0, 0,					// dsx, dsy, dex, dey	; 昞帵懁 弶婜埵抲丄嵟廔埵抲
					0, cell->dbTime );			// len, start
			break;

		case AVITYPE_AVIPAN:
			if( cell->pAVIPan )
				this->Start(
					cell->nChannel, cell->pAVI,
					cell->pAVIPan->sw, cell->pAVIPan->sh, cell->pAVIPan->ew, cell->pAVIPan->eh,		// 椞堟   弶婜僒僀僘丄廔椆僒僀僘
					cell->pAVIPan->ssx, cell->pAVIPan->ssy, cell->pAVIPan->sex, cell->pAVIPan->sey,	// 夋憸懁 弶婜埵抲丄嵟廔埵抲
					cell->pAVIPan->dsx, cell->pAVIPan->dsy, cell->pAVIPan->dex, cell->pAVIPan->dey,	// 昞帵懁 弶婜埵抲丄嵟廔埵抲
					cell->dbLong, cell->dbTime );													// len, start
			break;
		}
	}
}
void ActPlayAVI::Stop()
{
	for( int i = 0; i < MAX_AVI_LAYER; i++ ) {
		if( this->structLayer[i].pAVI && this->structLayer[i].pAVI->bUse )
			this->structLayer[i].db堏摦奐巒帪崗ms = INIT_TIME;
	}
}
void ActPlayAVI::Cont( double db嵞奐帪崗 )
{
	for( int i = 0; i < MAX_AVI_LAYER; i++ ) {
		if( this->structLayer[i].pAVI && this->structLayer[i].pAVI->bUse )
			this->structLayer[i].db堏摦奐巒帪崗ms = db嵞奐帪崗;
	}
}
int  ActPlayAVI::MainWork( int x, int y )
{
	// AVI 巜掕側偟側傜壗傕偟側偄
	if( ! g_DTX.bUseAVI )
		return 0;

	// 恑峴仌昤夋
	for( int i = 0; i < MAX_AVI_LAYER; i++ )
	{
		// 枹巊梡側傜僗僉僢僾
		if( this->structLayer[i].db堏摦奐巒帪崗ms == INIT_TIME )
			continue;
		if( ! this->structLayer[i].pAVI || ! this->structLayer[i].pAVI->bUse )
			continue;

		// 僷儔儊乕僞拪弌丒嶼弌
		DTX::AVI*	 pAVI		= this->structLayer[i].pAVI;
		SIZE szAVI僒僀僘;		pAVI->avi._僼儗乕儉僒僀僘傪庢摼偡傞( &(szAVI僒僀僘.cx), &(szAVI僒僀僘.cy) );
		SIZE szBGA僒僀僘		= { BGA_W, BGA_H };
		SIZE sz奐巒僒僀僘		= { this->structLayer[i].n奐巒僒僀僘W, this->structLayer[i].n奐巒僒僀僘H };
		SIZE sz廔椆僒僀僘		= { this->structLayer[i].n廔椆僒僀僘W, this->structLayer[i].n廔椆僒僀僘H };
		POINT pt夋憸懁奐巒埵抲	= { this->structLayer[i].n夋憸懁奐巒埵抲X, this->structLayer[i].n夋憸懁廔椆埵抲Y };
		POINT pt夋憸懁廔椆埵抲	= { this->structLayer[i].n夋憸懁廔椆埵抲X, this->structLayer[i].n夋憸懁廔椆埵抲Y };
		POINT pt昞帵懁奐巒埵抲	= { this->structLayer[i].n昞帵懁奐巒埵抲X, this->structLayer[i].n昞帵懁奐巒埵抲Y };
		POINT pt昞帵懁廔椆埵抲	= { this->structLayer[i].n昞帵懁廔椆埵抲X, this->structLayer[i].n昞帵懁廔椆埵抲Y };
		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();

		double db尰嵼帪崗ms		= ( g_Timer.Get() - db堏摦奐巒帪崗ms ) * g_DTX.fSpeed;	// 奐巒偐傜偺宱夁帪娫[ms]
		DWORD  dw僼儗乕儉斣崋	= pAVI->avi._帪崗偐傜僼儗乕儉斣崋傪曉偡( db尰嵼帪崗ms );

		// 廔椆敾掕乮db憤堏摦帪娫ms==0 偺偲偒偼AVI嵞惗廔椆傑偱乯
		if( db憤堏摦帪娫ms != 0.0 && db憤堏摦帪娫ms < db尰嵼帪崗ms )
		{
			this->structLayer[i].db憤堏摦帪娫ms   = 0;
			this->structLayer[i].db堏摦奐巒帪崗ms = INIT_TIME;	// 嵞惗廔椆
			return 0;
		}

		// 尰嵼嵗昗偺寁嶼丟埲壓偺俀曄悢傪嶼弌偡傞丅
		RECT rc夋憸懁椞堟;		// 夋憸偺嵍忋偑 0, 0
		RECT rc昞帵懁椞堟;		// BGA椞堟偺嵍忋偑 0, 0

		if( db憤堏摦帪娫ms == 0.0 )
		{
			// 弶婜埵抲丒弶婜僒僀僘傪偢偭偲曐帩
			SetRect( &rc夋憸懁椞堟,
				pt夋憸懁奐巒埵抲.x,
				pt夋憸懁奐巒埵抲.y,
				pt夋憸懁奐巒埵抲.x + sz奐巒僒僀僘.cx,
				pt夋憸懁奐巒埵抲.y + sz奐巒僒僀僘.cy );
			
			SetRect( &rc昞帵懁椞堟,
				pt昞帵懁奐巒埵抲.x,
				pt昞帵懁奐巒埵抲.y,
				pt昞帵懁奐巒埵抲.x + sz奐巒僒僀僘.cx,
				pt昞帵懁奐巒埵抲.y + 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妱崌 )
			};

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

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

			// 揮憲晹暘偑側偄側傜僗僉僢僾嘆 僋儕僢僺儞僌慜
			if( rc夋憸懁椞堟.right <= 0 || rc夋憸懁椞堟.bottom <= 0 || rc夋憸懁椞堟.left >= szAVI僒僀僘.cx || rc夋憸懁椞堟.top >= szAVI僒僀僘.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  > szAVI僒僀僘.cx ) { rc昞帵懁椞堟.right  -= rc夋憸懁椞堟.right  - szAVI僒僀僘.cx; rc夋憸懁椞堟.right  = szAVI僒僀僘.cx; }
			if( rc夋憸懁椞堟.bottom > szAVI僒僀僘.cy ) { rc昞帵懁椞堟.bottom -= rc夋憸懁椞堟.bottom - szAVI僒僀僘.cy; rc夋憸懁椞堟.bottom = szAVI僒僀僘.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 > szAVI僒僀僘.cx || rc夋憸懁椞堟.top > szAVI僒僀僘.cy ) continue;
			if( rc昞帵懁椞堟.right < 0 || rc昞帵懁椞堟.bottom < 0 || rc昞帵懁椞堟.left > szBGA僒僀僘.cx || rc昞帵懁椞堟.top > szBGA僒僀僘.cy ) continue;
		}
	
		// 昤夋
		this->structLayer[i].dw慜夞昞帵偟偨僼儗乕儉斣崋 = dw僼儗乕儉斣崋;

		HDC hdc;
		int ret = 0;
		
		this->sfAVI.Clear();
		
		if( SUCCEEDED( this->sfAVI.GetDC( &hdc ) ) )
		{
			ret = pAVI->avi._巜掕帪崗偺僼儗乕儉傪DC傊昤夋偡傞( g_App.hWnd, hdc, db尰嵼帪崗ms, 
				rc夋憸懁椞堟.left, rc夋憸懁椞堟.top, rc夋憸懁椞堟.right - rc夋憸懁椞堟.left, rc夋憸懁椞堟.bottom - rc夋憸懁椞堟.top,
				rc昞帵懁椞堟.left, rc昞帵懁椞堟.top, rc昞帵懁椞堟.right - rc昞帵懁椞堟.left, rc昞帵懁椞堟.bottom - rc昞帵懁椞堟.top );
			this->sfAVI.ReleaseDC( hdc );
			this->sfAVI.Draw( x, y );
		}
		if( ret != 0 )
		{
			// 嵞惗姰椆
			this->structLayer[i].db堏摦奐巒帪崗ms = INIT_TIME;
		}
	}
	return 0;
}

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