Solved

Strange BitBlt behavior

Posted on 2002-04-14
5
319 Views
Last Modified: 2011-09-20
I have a simple board game I'm writing in C++.  I have an eight by eight grid of blocks.  When the user starts a new level rather than having the blocks just "appear" I wanted them to fall, a la bejeweled.

I have written a procedure that seems to work great when I step through it in the debugger.  The blocks fall just like they are supposed to.  But when I run it without being in single-step mode I get nothing until the normal WM_PAINT fires and redraws the whole board.

Any ideas what might cause a BitBlt to appear in single step mode but not in normal-speed execution?  Here is the code for what its worth.   Note that I am going to refine this to move the blocks based on how much time has elapsed to compensate for older machines.  But right now, I'm only concerned that it runs.  Not how fast/slowly it runs.

Thanks!

--------------------------

void CChildView::AnimateBlocks()
{
     DWORD StartTime;
     DWORD CurrentTime;
     int nRow, nCol;
     CDC pDC;
     CBitmap pBMP;
     int nSavedDC;
     int nWidth;
     int nHeight;

     nWidth = (BLOCK_WIDTH * m_nBlocksWide);
     nHeight = (BLOCK_HEIGHT * m_nBlocksHigh);
     
     pDC.CreateCompatibleDC(NULL);
     pBMP.CreateCompatibleBitmap(this->GetDC(), nWidth, nHeight);
     nSavedDC = pDC.SaveDC();
     pDC.SelectObject(&pBMP);

     CDC *pDestDC;

     StartTime = GetTickCount();

     //m_nBlocksFalling is the number of blocks that are being animated
     //At the beginning of a level, this wil be 64 (8x8 grid)
     while(m_nBlocksFalling)
     {
          //Fill my offscreen buffer with black
          pDC.FillSolidRect(0, 0, nWidth, nHeight, 0);

          CurrentTime = GetTickCount();
          for(nRow = 0; nRow < m_nBlocksHigh; nRow++)
          {
               for(nCol = 0; nCol < m_nBlocksWide; nCol++)
               {
                    //If this block isn't where it is supposed to be, and its delay
                    //has expired, move it
                    if(
                         (m_Blocks[nRow][nCol].nY != m_Blocks[nRow][nCol].nDesiredY) && 
                         (m_Blocks[nRow][nCol].nDepartureOffset + StartTime <= CurrentTime) )
                    {

                         //Calculate the new position...
                         m_Blocks[nRow][nCol].nY += m_Blocks[nRow][nCol].nVelocity;

                         //Did it move past the desired location?
                         if(m_Blocks[nRow][nCol].nY > m_Blocks[nRow][nCol].nDesiredY)
                         {
                              m_Blocks[nRow][nCol].nY = m_Blocks[nRow][nCol].nDesiredY;
                              m_nBlocksFalling--;     //This block has reached its destination
                         }
                         else
                              //increase velocity to simulate a gravitational effect
                              m_Blocks[nRow][nCol].nVelocity = (int)(m_Blocks[nRow][nCol].nVelocity*  1.5);

                         //Draw the block in its current location
                         DrawBlock(nRow, nCol, &pDC, 0, 0);
                    }
               }
          }

          //Once the entire grid has been redraw, blt the offscreen buffer to the window
          pDestDC = this->GetDC();
          pDestDC->BitBlt(m_nLeftMargin, m_nTopMargin, nWidth, nHeight, &pDC, 0, 0, SRCCOPY);
          ReleaseDC(pDestDC);          //DO I NEED THIS?

          //This delay was added to slow things down so I can see what it happening.
          while(GetTickCount() - StartTime < 5000);          
     }

     //Cleanup
     pDC.RestoreDC(nSavedDC);
     ReleaseDC(&pDC);
}

0
Comment
Question by:l99057j
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:Ber
ID: 6940297
Perhaps you could force a redraw event using the sendmessage API

CWnd* window;


pDestDC->BitBlt(m_nLeftMargin, m_nTopMargin, nWidth, nHeight, &pDC, 0, 0, SRCCOPY);


window = pDestDC->GetWindow();
SendMessage(window,WM_PAINT,NULL,NULL);

I think this should work give it a shot and see

Cheers...
Ber...
0
 
LVL 2

Accepted Solution

by:
Ber earned 200 total points
ID: 6940311
Alternatively...
CWnd* window

window = pDestDC->GetWindow(;

window->SendMessage(WM_PAINT, (WPARAM) pDestDC->GetSafeHdc(),0);

Cheers...
Ber...
0
 
LVL 2

Author Comment

by:l99057j
ID: 6940474
The real culprit was an error in the logic, but the paint suggestion helped nonetheless.  Thanks, Ber!
0
 
LVL 2

Expert Comment

by:Ber
ID: 6940659
glad I could help...

Cheers...
Ber...
0
 
LVL 2

Author Comment

by:l99057j
ID: 6940683
The real culprit was an error in the logic, but the paint suggestion helped nonetheless.  Thanks, Ber!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question