Reputation: 515
I'm doing dialog based, MFC game. During the game there is a battle phase , and I want to display each text on the screen with a small delay, I tried using sleep and also tried CTime, but both display only the last text at each battle... here is an example of the code...
void CDNDPprojectDlg::OnBnClickedAction()
{
CString Damaged;
damage=me->Attack(enemy,WeID);
SDialog.Format(_T("You done %d damage, using %s,\r\n %s has %d HP left"), damage,Damaged,monName,enemy->getHP());
Output.SetWindowTextW(SDialog);
if(enemy->getHP() <1)
{
Won();
return;
}
NextAttack();
if(me->getHP() <1)
{
Died();
return;
}
}
at the beginning of all 3 functions there is Sleep(700). it does a delay but display only the last
Output.SetWindowTextW(SDialog);
that I put...I guess its something of threading, please help me.
thanks
Upvotes: 0
Views: 456
Reputation: 156
There is a hard way, an easy way and a shameful way.
The hard way is to do multi threading - check AfxBeginThread, you pass a pointer to your CDNDPprojectDlg, and do the job there after each of your sleep. It's hard because at one point you will need to synchronise your main thread and your new thread when they access common resource... a lot to learn there but before that a lot of pain
The other way is to either use a timer SetTimer and override OnTimer
The third way is to use something like the DoEvents in Visual Basic (ha the shame!). Just call it after you have done something and your screen will refresh. I will deny any knowledge of having passed that code to you.
BOOL DoEvents()
{
MSG msg;
while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
return FALSE;
}
if (!AfxGetApp()->PreTranslateMessage(&msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
return TRUE;
}
Good luck!
Upvotes: 3