// CharacterDisplay.cpp : 实现文件
//

#include "stdafx.h"
#include "CgDemoBy5000asi.h"
#include "CharacterDisplay.h"
#include "By5000asiplay.h"
#include "MyDemo.h"

// CCharacterDisplay 对话框

IMPLEMENT_DYNAMIC(CCharacterDisplay, CDialog)
CCharacterDisplay::CCharacterDisplay(CWnd* pParent /*=NULL*/)
	: CDialog(CCharacterDisplay::IDD, pParent)
{
}

CCharacterDisplay::~CCharacterDisplay()
{
}

void CCharacterDisplay::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CCharacterDisplay, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_DISP_CG, OnBnClickedButtonDispCg)
END_MESSAGE_MAP()


// CCharacterDisplay 消息处理程序

void CCharacterDisplay::OnBnClickedButtonDispCg()
{
	// TODO: 在此添加控件通知处理程序代码
	CBitmap * pBitmap ;
	pBitmap = new CBitmap;
	CClientDC dc(this);
	CDC memDC;
	ULONG *pPicture,*ptemp,ulLeftDistance,ulUpDistance;
	CString bless ;
	LOGFONT lf;
	CFont font1;
	COLORREF colorbk,colortext;
	unsigned int i,j,k,bmw,bmh,sizebl;
	ULONG pa,pr,pb,pg,pels,pelsf,pelsb;

	bless="字幕卡字幕显示演示";
	colorbk=RGB(0,0,0);
	colortext=RGB(255,255,255);

	memset(&lf,0,sizeof(LOGFONT));
	lf.lfWidth=30;                  //以后用字体对话框代替
	lf.lfHeight=120;
	lf.lfWeight=1000;
	strcpy((LPSTR)&(lf.lfFaceName),"方正宋三简体");

	font1.CreateFontIndirect(&lf); //创建字体
	memDC.CreateCompatibleDC(&dc); //创建DC
	memDC.SelectObject(&font1);

	sizebl=bless.GetLength()+1;   //1:修正量
	bmw=lf.lfWidth*sizebl+memDC.GetTextCharacterExtra()*sizebl;
	bmh=lf.lfHeight+2;


	pBitmap->CreateCompatibleBitmap(&dc,bmw,bmh);
	CBitmap *pOld=memDC.SelectObject(pBitmap); 
	//	memDC.FillSolidRect(0,0,bmw,bmh,RGB(10,10,10)); 

	memDC.SetTextColor(colortext);
	memDC.SetBkColor(colorbk);
	//	memDC.SetBkMode(TRANSPARENT);
	memDC.TextOut(0,1,bless); 

	i = 0;
	ULONG * wrLong = new ULONG[ BURST_LENGTH*BURST_NO ];

	pPicture = new ULONG[ bmw*bmh ];
	ptemp=new ULONG[ bmw*bmh ];
	pBitmap ->GetBitmapBits(bmw*bmh*4,pPicture);
	for( i=0;i<bmw*bmh ;i++)
	{
		if (pPicture[i]!=0x00000000)            //如果不是黑色.颜色的16进制，可为任意颜色
			pPicture[i]=pPicture[i]|0xff000000;  //按位作或操作，使alpha通道为255
	}
	memcpy(ptemp,pPicture,bmw*bmh*4);
	for ( i=1;i<bmh-1;i++)  //消除闪烁用
	{
		for (j=0;j<bmw;j++)
		{
			pels=pPicture[j+i*bmw];
			pelsf=ptemp[j+(i-1)*bmw];
			pelsb=ptemp[j+(i+1)*bmw];
			pr=((pels&0x00ff0000)+((pelsf&0x00ff0000)+(pelsb&0x00ff0000))/2)/2;
			pb=((pels&0x0000ff00)+((pelsf&0x0000ff00)+(pelsb&0x0000ff00))/2)/2;
			pg=((pels&0x000000ff)+((pelsf&0x000000ff)+(pelsb&0x000000ff))/2)/2;
			pa=((pels>>8&0x00ff0000)+((pelsf>>8&0x00ff0000)+(pelsb>>8&0x00ff0000))/2)/2;
			pa=pa<<8;
			pels=(pa&0xff000000)+(pr&0x00ff0000)+(pb&0x0000ff00)+(pg&0x000000ff);
			pPicture[j+i*bmw]=pels;
		}
	}  

	if( BURST_LENGTH > bmw)
		ulLeftDistance = ( BURST_LENGTH - bmw ) / 2;
	else
		ulLeftDistance = 0;
	ulUpDistance = 200;

	//开始写数据到字幕卡缓存

	PVOID pBuffer;
	Seekfit_By5000asiplay_CgGetBufferAddress(pBuffer, m_uCardNum);

	for( j=0;j<BURST_NO;j++ )
	{
		if( ( j < ( bmh + ulUpDistance ) ) && ( j >= ulUpDistance ))
		{
			for( k=0; k<BURST_LENGTH; k++ )
				if( ( k < ( bmw + ulLeftDistance ) ) && ( k >= ulLeftDistance ) )
					wrLong[ j*BURST_LENGTH + k ] = ( pPicture[ ( j - ulUpDistance )*bmw + k - ulLeftDistance ] );
				else
					wrLong[ j*BURST_LENGTH + k ] = 0;
		}
		else
			for( k=0; k<BURST_LENGTH; k++ )
				wrLong[ j*BURST_LENGTH + k ] = 0;

	}
	Sleep(100) ;

	for( j=0;j<BURST_NO;j++ )
	{
		memcpy(PVOID((PULONG)pBuffer + 720*j), &wrLong[ j*BURST_LENGTH ], 720*4);
		//Seekfit_By5000asiplay_CgSetRectangle(0, j, 720, 1, &wrLong[ j*BURST_LENGTH ], m_uCardNum);
	}
	while (!Seekfit_By5000asiplay_CgUpdateOneFrame(m_uCardNum))
		Sleep(5);
	delete ptemp;
	delete pPicture;
	//		} 

	//	}
	memDC.SelectObject(pOld);
	delete pBitmap;
}
