www.pudn.com > loasource.rar > CMessageBox.cpp


//-------------------------------------------------------------------------------------------------------- 
//                        游戏消息框模块 
//CMessageBox.cpp 
//游戏引擎中的消息框部分 
//作者:吴振华(kylinx)(中国科大01级11系) 
//E-mail:game-diy@163.com 
//创建于:2003/7/20 by Kylinx 
//-------------------------------------------------------------------------------------------------------- 
#include"CMessageBox.h" 
#include"CGame.h" 
#include"CRender.h" 
#include"CText.h" 
#include"CCursor.h" 
#include"CFont.h" 
#include"CMacro.h" 
void CMessageBox::ShowMsgBoxOK(CGame*pGame,char*str) 
{ 
	LOA_ASSERT(pGame);  
 
	MSG msg; 
	CText text; 
	CFont font; 
	font.CreateFont();															//创建字体 
 
	CDisplay*pDisplay=pGame->GetDisplay(); 
	while(pGame->IsPlayGame()) 
	{ 
		if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) 
		{ 
			if(!GetMessage(&msg, NULL,0,0))  
				return; 
			TranslateMessage(&msg);  
			DispatchMessage(&msg); 
		} 
		else if(pGame->IsActive()) 
		{ 
			pGame->LockFPS(); 
			BYTE key=pGame->GetKeyBoard()->ReadLastBufferKey(50); 
			if(key==DIK_SPACE) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_SPACE,100)) 
				{ 
					return; 
				} 
			} 
			else if(key==DIK_RETURN) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_RETURN,100)) 
				{ 
					return; 
				} 
			} 
			else if(key==DIK_ESCAPE) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_ESCAPE,100)) 
				{ 
					return; 
				} 
			} 
			pDisplay->Clear(); 
			pDisplay->Blt(0,0,m_pSurfBack); 
			pDisplay->AlphaBlt(192,144,m_pSurfDialog->GetDDrawSurface(),NULL,true,4); 
			 
			HDC hdc; 
			pDisplay->GetBackBuffer()->GetDC(&hdc); 
			::SelectObject(hdc,font.GetFont()); 
			::SetBkMode(hdc,TRANSPARENT); 
			::SetTextColor(hdc,RGB(255,255,255)); 
			text.ShowMultiLineText(240,180,hdc,20,str); 
			pDisplay->GetBackBuffer()->ReleaseDC(hdc); 
			pGame->ShowFPS(); 
			pDisplay->Present(); 
		} 
		else 
			WaitMessage(); 
	} 
} 
int CMessageBox::ShowMsgBoxYesNo(CGame*pGame,char*szTitle) 
{ 
	LOA_ASSERT(pGame);  
 
	CIcon icon; 
	if(!icon.InitIcon(pGame->GetDisplay(),LOA_ICON_ARROW)) 
		return LOA_MSG_BOX_IDNO; 
	MSG msg; 
	CText text; 
	CFont font; 
	font.CreateFont();															//创建字体 
 
	CDisplay*pDisplay=pGame->GetDisplay(); 
 
	int nCurrentSelect=LOA_MSG_BOX_IDYES; 
 
	while(pGame->IsPlayGame()) 
	{ 
		if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) 
		{ 
			if(!GetMessage(&msg, NULL,0,0))  
				return msg.message; 
			TranslateMessage(&msg);  
			DispatchMessage(&msg); 
		} 
		else if(pGame->IsActive()) 
		{ 
			pGame->LockFPS(); 
			BYTE key=pGame->GetKeyBoard()->ReadLastBufferKey(50); 
			if(key==DIK_SPACE) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_SPACE,100)) 
				{ 
					return nCurrentSelect; 
				} 
			} 
			else if(key==DIK_RETURN) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_RETURN,100)) 
				{ 
					return nCurrentSelect; 
				} 
			} 
			else if(key==DIK_ESCAPE) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_ESCAPE,150)) 
				{ 
					return LOA_MSG_BOX_IDNO; 
				} 
			} 
			else if(key==DIK_LEFT)  
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_LEFT,250)) 
				{ 
					if(nCurrentSelect==LOA_MSG_BOX_IDYES) 
						nCurrentSelect=LOA_MSG_BOX_IDNO; 
					else 
						nCurrentSelect=LOA_MSG_BOX_IDYES; 
				} 
			} 
			else if(key==DIK_RIGHT) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_RIGHT,250)) 
				{ 
					if(nCurrentSelect==LOA_MSG_BOX_IDYES) 
						nCurrentSelect=LOA_MSG_BOX_IDNO; 
					else 
						nCurrentSelect=LOA_MSG_BOX_IDYES; 
				} 
			} 
			pDisplay->Clear(); 
			pDisplay->Blt(0,0,m_pSurfBack); 
			pDisplay->AlphaBlt(192,144,m_pSurfDialog->GetDDrawSurface(),NULL,true,4); 
			 
			HDC hdc; 
			pDisplay->GetBackBuffer()->GetDC(&hdc); 
			::SelectObject(hdc,font.GetFont()); 
			::SetBkMode(hdc,TRANSPARENT); 
			::SetTextColor(hdc,RGB(255,255,255)); 
			text.ShowMultiLineText(240,180,hdc,20,szTitle); 
			if(nCurrentSelect==LOA_MSG_BOX_IDYES) 
			{ 
				::TextOut(hdc,380,280,"否",strlen("否")); 
				::SetTextColor(hdc,RGB(0,255,0)); 
				::TextOut(hdc,240,280,"是",strlen("是")); 
			} 
			else 
			{ 
				::TextOut(hdc,240,280,"是",strlen("是")); 
				::SetTextColor(hdc,RGB(0,255,0)); 
				::TextOut(hdc,380,280,"否",strlen("否")); 
			} 
 
			pDisplay->GetBackBuffer()->ReleaseDC(hdc); 
			 
			if(nCurrentSelect==LOA_MSG_BOX_IDYES) 
			{ 
				icon.Show(220,280); 
			} 
			else 
			{	 
				icon.Show(360,280); 
			} 
			pGame->ShowFPS(); 
			pDisplay->Present(); 
		} 
		else 
			WaitMessage(); 
	} 
	return LOA_MSG_BOX_IDNO; 
} 
int CMessageBox::ShowMsgBoxBuySell(CGame*pGame,char*szTitle) 
{ 
	LOA_ASSERT(pGame);  
 
	CIcon icon; 
	if(!icon.InitIcon(pGame->GetDisplay(),LOA_ICON_ARROW)) 
		return LOA_MSG_BOX_IDLEAVE; 
	MSG msg; 
	CText text; 
	CFont font; 
	font.CreateFont();															//创建字体 
 
	CDisplay*pDisplay=pGame->GetDisplay(); 
 
	int nCurrentSelect=LOA_MSG_BOX_IDBUY; 
 
	while(pGame->IsPlayGame()) 
	{ 
		if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) 
		{ 
			if(!GetMessage(&msg, NULL,0,0))  
				return msg.message; 
			TranslateMessage(&msg);  
			DispatchMessage(&msg); 
		} 
		else if(pGame->IsActive()) 
		{ 
			pGame->LockFPS(); 
			BYTE key=pGame->GetKeyBoard()->ReadLastBufferKey(50); 
			if(key==DIK_SPACE) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_SPACE,100)) 
				{ 
					return nCurrentSelect; 
				} 
			} 
			else if(key==DIK_RETURN) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_RETURN,100)) 
				{ 
					return nCurrentSelect; 
				} 
			} 
			else if(key==DIK_ESCAPE) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_ESCAPE,150)) 
				{ 
					return LOA_MSG_BOX_IDLEAVE; 
				} 
			} 
			else if(key==DIK_LEFT)  
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_LEFT,250)) 
				{ 
					if(nCurrentSelect==LOA_MSG_BOX_IDBUY) 
						nCurrentSelect=LOA_MSG_BOX_IDLEAVE; 
					else if(nCurrentSelect==LOA_MSG_BOX_IDLEAVE) 
						nCurrentSelect=LOA_MSG_BOX_IDSELL; 
					else 
						nCurrentSelect=LOA_MSG_BOX_IDBUY; 
				} 
			} 
			else if(key==DIK_RIGHT) 
			{ 
				if(pGame->GetKeyBoard()->WaitKey(DIK_RIGHT,250)) 
				{ 
					if(nCurrentSelect==LOA_MSG_BOX_IDBUY) 
						nCurrentSelect=LOA_MSG_BOX_IDSELL; 
					else if(nCurrentSelect==LOA_MSG_BOX_IDSELL) 
						nCurrentSelect=LOA_MSG_BOX_IDLEAVE; 
					else 
						nCurrentSelect=LOA_MSG_BOX_IDBUY; 
				} 
			} 
			pDisplay->Clear(); 
			pDisplay->Blt(0,0,m_pSurfBack); 
			pDisplay->AlphaBlt(192,144,m_pSurfDialog->GetDDrawSurface(),NULL,true,4); 
			 
			HDC hdc; 
			pDisplay->GetBackBuffer()->GetDC(&hdc); 
			::SelectObject(hdc,font.GetFont()); 
			::SetBkMode(hdc,TRANSPARENT); 
			::SetTextColor(hdc,RGB(255,255,255)); 
			text.ShowMultiLineText(240,180,hdc,20,szTitle); 
			if(nCurrentSelect==LOA_MSG_BOX_IDBUY) 
			{ 
				::TextOut(hdc,380,280,"离开",strlen("离开")); 
				::TextOut(hdc,310,280,"卖",strlen("卖")); 
 
				::SetTextColor(hdc,RGB(0,255,0)); 
				::TextOut(hdc,240,280,"买",strlen("买")); 
			} 
			else if(nCurrentSelect==LOA_MSG_BOX_IDSELL) 
			{ 
				::TextOut(hdc,380,280,"离开",strlen("离开"));	 
				::TextOut(hdc,240,280,"买",strlen("买")); 
				::SetTextColor(hdc,RGB(0,255,0)); 
				::TextOut(hdc,310,280,"卖",strlen("卖")); 
			} 
			else 
			{ 
				::TextOut(hdc,310,280,"卖",strlen("卖"));	 
				::TextOut(hdc,240,280,"买",strlen("买")); 
				::SetTextColor(hdc,RGB(0,255,0)); 
				 
				::TextOut(hdc,380,280,"离开",strlen("离开")); 
			} 
			pDisplay->GetBackBuffer()->ReleaseDC(hdc); 
			 
			if(nCurrentSelect==LOA_MSG_BOX_IDBUY) 
			{ 
				icon.Show(220,280); 
			} 
			else if(nCurrentSelect==LOA_MSG_BOX_IDSELL) 
			{ 
				icon.Show(290,280); 
			} 
			else 
			{	 
				icon.Show(360,280); 
			} 
			pGame->ShowFPS(); 
			pDisplay->Present(); 
		} 
		else 
			WaitMessage(); 
	} 
	return LOA_MSG_BOX_IDLEAVE; 
} 
 
int CMessageBox::ShowMessageBox(CGame*pGame,int iStyle,char*str,...) 
{ 
	LOA_ASSERT(pGame); 
	LOA_ASSERT(str); 
 
	CDisplay*pDisplay=pGame->GetDisplay(); 
	CSurface *pSurf; 
	CSurface*pSurfDialog; 
	if(FAILED(pDisplay->CreateSurfaceFromBitmap(&pSurfDialog,"pic\\system\\dialog.bmp",96,96))) 
		return 0; 
	pSurfDialog->SetColorKey(RGB(255,0,255)); 
 
	if(FAILED(pDisplay->CreateSurface(&pSurf,640,480))) 
		return 0; 
	if(FAILED(pDisplay->CreateSurface(&m_pSurfDialog,256,192))) 
		return 0; 
	m_pSurfDialog->Clear(0); 
	m_pSurfDialog->SetColorKey(0); 
 
	RECT rect; 
	::SetRect(&rect,0,0,32,32);									//左上角 
	m_pSurfDialog->BltFast( 0,0,pSurfDialog,&rect,true); 
	::SetRect(&rect,64,0,96,32);								//右上角 
	m_pSurfDialog->BltFast(224,0,pSurfDialog,&rect,true); 
	::SetRect(&rect,0,64,32,96);								//左下角 
	m_pSurfDialog->BltFast(0,160,pSurfDialog,&rect,true); 
	::SetRect(&rect,64,64,96,96);								//右下角 
	m_pSurfDialog->BltFast(224,160,pSurfDialog,&rect,true); 
 
	::SetRect(&rect,32,32,64,64); 
	for(int i=1;i<7;i++) 
	{ 
		for(int j=1;j<5;j++)	 
			m_pSurfDialog->BltFast(32*i,32*j,pSurfDialog,&rect,true); 
	} 
	::SetRect(&rect,32,0,64,32); 
	for(i=1;i<7;i++) 
	{ 
		m_pSurfDialog->BltFast(32*i,0,pSurfDialog,&rect,true); 
	} 
 
	::SetRect(&rect,32,64,64,96); 
	for(i=1;i<7;i++) 
	{ 
		m_pSurfDialog->BltFast(32*i,160,pSurfDialog,&rect,true); 
	} 
 
	::SetRect(&rect,0,32,32,64); 
 
	for(i=1;i<5;i++) 
	{ 
		m_pSurfDialog->BltFast(0,i*32,pSurfDialog,&rect,true); 
	} 
 
	::SetRect(&rect,64,32,96,64); 
	for(i=1;i<5;i++) 
	{ 
		m_pSurfDialog->BltFast(224,32*i,pSurfDialog,&rect,true); 
	} 
	pSurf->BltFast(0,0,pDisplay->GetBackBuffer(),NULL,false); 
 
 
	m_pSurfBack=pSurf; 
 
	char txt[1024]; 
	memset(txt,0,1024); 
	va_list vl; 
	va_start(vl,str); 
	wvsprintf(txt,str,vl); 
	va_end(vl); 
 
	int iValue=0; 
	switch(iStyle) 
	{ 
	case LOA_MSG_BOX_OK: 
		{ 
			 
			ShowMsgBoxOK(pGame,txt); 
		} 
		break; 
	case LOA_MSG_BOX_YESNO: 
		{ 
			iValue=ShowMsgBoxYesNo(pGame,txt); 
		} 
		break; 
	case LOA_MSG_BOX_BUYSELL: 
		{ 
			iValue=ShowMsgBoxBuySell(pGame,txt); 
		} 
		break; 
	} 
	 
 
	delete pSurf; 
	delete pSurfDialog; 
	delete m_pSurfDialog; 
 
	return iValue; 
}