// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "CgDemoBy5000asi.h"
#include "ChildView.h"

#include "imagereader.h"
#include "pictureareamove.h"
#include "coloritem.h"
#include "winbase.h"
#include "boardspeed.h"

#include "Mmsystem.h"

#include "mydemo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChildView

CChildView::CChildView()
{
}

CChildView::~CChildView()
{
	if(m_hBitmap)
	{
		::DeleteObject(m_hBitmap);
		m_hBitmap = NULL;
	}
	if( wrLong )
	{
		delete wrLong;	
		wrLong = NULL;
	}
}


BEGIN_MESSAGE_MAP(CChildView,CWnd )
	//{{AFX_MSG_MAP(CChildView)
	ON_WM_PAINT()
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_COLOR_ITEM, OnColorItem)
	ON_COMMAND(ID_PICTURE_AREA_MOVE, OnPictureAreaMove)
	ON_COMMAND(ID_BOARD_CLEAR, OnBoardClear)
	ON_COMMAND(ID_BOARD_SPEED, OnBoardSpeed)
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_MENU_CHAR_DISPLAY, OnMenuCharDisplay)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);

	return TRUE;
}

void CChildView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CDC * pDC = &dc;
	
	pDC ->TextOut( 0,0,m_str );
	pDC ->TextOut( 0,30,m_StartEndTime_str );

	if(!m_hBitmap)return;
	CBitmap bmp;
	BITMAP bm;	
	bmp.Attach(m_hBitmap);
	bmp.GetBitmap(&bm);
	CDC mem;
	mem.CreateCompatibleDC(pDC);
	CBitmap* pOld = mem.SelectObject(&bmp);
	pDC ->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&mem,0,0,SRCCOPY);
	mem.SelectObject(pOld);
	mem.DeleteDC();	
	bmp.Detach();

	pDC ->TextOut( 0,0,m_str );
	pDC ->TextOut( 0,30,m_StartEndTime_str );
}

void CChildView::Initial(UINT uCardNum, CString strEventName)
{
	m_uCardNum = uCardNum;
	m_strEventName = strEventName;

	ULONG ulTmp;

	m_hBitmap = NULL;
	nInitialInt = 0;
	nCloseInt = 0;

	wrLong = new ULONG[ BURST_LENGTH*(BURST_NO+1) ];
	ulTmp = (ULONG)wrLong;
	while( ulTmp & 0x07 )
	{
		wrLong++;
		ulTmp = (ULONG)wrLong;
	}
}

void CChildView::OnFileOpen() 
{
	// TODO: Add your command handler code here

	CBitmap * pBitmap ;
	pBitmap = new CBitmap;
	CTgaReader tgaReader;
	BITMAP bm;

	CString strFileName;
	POSITION posFile;
	ULONG *pPicture,ulLeftDistance,ulUpDistance;
	ULONG *rdLong;
	unsigned int i,j,k;
	PVOID pCardAddr;

	BOOL dataErr;
	ULONG ulDataErrCnt;
	CString strErr;

	CFileDialog dlg(TRUE,
		NULL,
		_T("*.tga"),
        OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY|OFN_NOREADONLYRETURN|OFN_ALLOWMULTISELECT ,
		_T("Picture Files (*.tga) | *.tga | ")
        _T("All Files (*.*) | *.* ||"));

	rdLong = new ULONG[ BURST_LENGTH*BURST_NO ];
	i = 0;
	dataErr = TRUE;
	ulDataErrCnt = 0;
	if ( dlg.DoModal()==IDOK )
	{
		posFile = dlg.GetStartPosition();
		while( posFile && ( i < STORE_FRAME_NO ) )
		{
			strFileName = dlg.GetNextPathName( posFile );
			tgaReader.LoadImage(strFileName,pBitmap);
			pBitmap->GetBitmap(&bm);
			pPicture = new ULONG[ bm.bmWidth*bm.bmHeight ];
			pBitmap ->GetBitmapBits(bm.bmWidth*bm.bmHeight*4,pPicture);

			if( BURST_LENGTH > bm.bmWidth )
				ulLeftDistance = ( BURST_LENGTH - bm.bmWidth ) / 2;
			else
				ulLeftDistance = 0;
			
			if( BURST_NO > bm.bmHeight )
				ulUpDistance = ( BURST_NO - bm.bmHeight ) / 2;
			else
				ulUpDistance = 0;

			for( j=0;j<BURST_NO;j++ )
			{
				if( ( j < ( bm.bmHeight + ulUpDistance ) ) && ( j >= ulUpDistance ))
				{
					for( k=0; k<BURST_LENGTH; k++ )
					if( ( k < ( bm.bmWidth + ulLeftDistance ) ) && ( k >= ulLeftDistance ) )
						wrLong[ j*BURST_LENGTH + k ] = ( pPicture[ ( j - ulUpDistance )*bm.bmWidth + k - ulLeftDistance ] ) & 0xffffffff;
					else
						wrLong[ j*BURST_LENGTH + k ] = 0;
				}
				else
					for( k=0; k<BURST_LENGTH; k++ )
						wrLong[ j*BURST_LENGTH + k ] = 0;

			}
			Seekfit_By5000asiplay_CgGetBufferAddress(pCardAddr, m_uCardNum);
			pCardAddr = ( (char*)pCardAddr + i*4*720*576 );

			memcpy( (PVOID)((int)pCardAddr),wrLong,4*720*576);
			i++;

			delete pPicture;
		}
	}
	
	delete pBitmap;
	delete rdLong;
}

void CChildView::OnColorItem() 
{
	// TODO: Add your command handler code here
	CColorItem ColorItemDlg;
	ColorItemDlg.m_uCardNum = m_uCardNum;
	ColorItemDlg.DoModal();
}

void CChildView::OnPictureAreaMove() 
{
	// TODO: Add your command handler code here
	CPictureAreaMove PicAreaMoveDlg;
	PicAreaMoveDlg.m_uCardNum = m_uCardNum;
	PicAreaMoveDlg.m_strEventName = m_strEventName;
	PicAreaMoveDlg.DoModal();
}

/*
  清除板卡中的数据。
*/

void CChildView::OnBoardClear() 
{
	Seekfit_By5000asiplay_CgClearScreen(m_uCardNum);
}

void CChildView::OnBoardSpeed() 
{
	// TODO: Add your command handler code here
	DWORD timeBegin ;
	DWORD timeEnd ;
	CClientDC dc(this);
	int i,j;

	for( i=0;i<BURST_NO;i++)
		for( j=0;j<BURST_LENGTH;j++)
			wrLong[ i*BURST_LENGTH + j ] = 0;

	m_str.Format(_T(" Write 1000 frames data to board,please wait!") );
	dc.TextOut(0,0,m_str);

	m_StartEndTime_str.Format(_T("It is writing data to board.                                              "));
	dc.TextOut( 0,30,m_StartEndTime_str );

	CBoardSpeed boardSpeed;
	boardSpeed.m_uCardNum = m_uCardNum;
	if( boardSpeed.DoModal() == IDOK )
	{

		timeBegin = timeGetTime(); 
		PVOID address;
		Seekfit_By5000asiplay_CgGetBufferAddress(address, m_uCardNum);
		for( int kk = 0;kk<1000;kk++ )
			 memcpy(LPDWORD(address), wrLong, 720*576*4);

		timeEnd = timeGetTime(); 

		m_SpendTime_str.Format(_T("spend time：%d ms，%f frame/second"), timeEnd - timeBegin, 1000.0*1000/(timeEnd - timeBegin));
		dc.TextOut( 0,30,m_SpendTime_str );
	}
	else
	{
		m_str.Format(_T("Board speed test is canceled!                                         "));
		dc.TextOut( 0,0,m_str );

		m_StartEndTime_str.Format(_T("                                                              "));
		dc.TextOut( 0,30,m_StartEndTime_str );
	}
}

void CChildView::OnMenuCharDisplay()
{
	m_CharDisplay.m_uCardNum = m_uCardNum;
	m_CharDisplay.DoModal();
}
