Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have read about using buffers to move a ball on the screen.

I am using C++11 on Microsoft Windows 32 bit.

No visual C++. No Visual Basic. No .NET. No Active X. Please.

Thank you.

Would you please look at what I have and adjust it to correct logic? It works but I want it to be better.

Thank you.

C++
//  Each time that this proceedure is called four (4) buffers are created.
//  The hdcBacdkgroundBitmapBuffer will be stretch blitted to the
//  hdcWorkingBackBuffer. This will be behind the animation and is placed
//  there EVERY time that the animation changes in order to erase the
//  previous animation.
//
//  The hdcAnimationBuffer_001 will have the animation mask placed into
//  it with SRCAND.
//  The hdcAnimationBuffer_001 will have the animation bitmap placed into
//  it with SRCPAINT.
//  With the mask and bitmap combined the hdcAnimationBuffer_001 will have
//  a custom shaped region.
//            
//  I am not certain what the hbmOldBuffer does. I think that it is not
//  necessary but I do not know.
//
//  The hdcWorkingBackBuffer will be blitted to the screen which in this
//  case is hdc.
//
void DrawBall(HDC hdc, RECT* prc)
{
   //  This creates a back buffer to blt to.
   HDC hdcWorkingBackBuffer = CreateCompatibleDC(hdc);

   //  HBITMAP is An object handle that manages bitmap data.
   HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);

   //  Where is this hbmOldBuffer used? It looks like it is created, then
   //  a lot of stuff is done without it, then it is selected (not used) and
   //  then deleted. What is the use of the next line?
   HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hdcWorkingBackBuffer, hbmBuffer);

   // This puts the background in place. EACH TIME! BEFORE the animation is put
   // into place.
   // This creates another buffer for the background bitmap to be blitted from.
   HDC hdcBacdkgroundBitmapBuffer = CreateCompatibleDC(hdc);
   HBITMAP hbmOldRed =
      (HBITMAP)SelectObject(hdcBacdkgroundBitmapBuffer, g_RedGreenBlue);

   //  This blitts the background buffer onto a back buffer.
   StretchBlt(hdcWorkingBackBuffer, 0, 0, 320, 240,
      hdcBacdkgroundBitmapBuffer, 0, 0, 320, 240, SRCCOPY);

   //  This puts the animation in place. EACH TIME! AFTER the animation is put
   //  into place.
   //  This creates another buffer for the animation bitmap to be blitted from.
   HDC hdcAnimationBuffer_001 = CreateCompatibleDC(hdc);

   //  What is hbmOld and why is it not defined earlier like
   //     HBITMAP g_hbmMask = NULL; is?
   //  Why can't I say (HBITMAP hbmOld = NULL) and then say
   //     hbmOld = (HBITMAP)SelectObject(hdcAnimationBuffer_001, g_hbmMask); ?
   //  Doesn't that mean that hbmOld has to be defined over and over again as
   //  DrawBall is called or used?
   //  That seems kind of inefficient to me unless there is some reason for
   //  doing it this was being better.
   //  What is the next line doing and why is it necessary (in full) here?
   HBITMAP hbmOld = (HBITMAP)SelectObject(hdcAnimationBuffer_001, g_hbmMask);

   //  Does the next line put the ball image into a back buffer? I think that
   //  it does. Is this hdcWorkingBackBuffer what a pointer would point to if
   //  I had an animation? Please explain your answer.
   BitBlt(hdcWorkingBackBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, 
      g_ballInfo.height, hdcAnimationBuffer_001, 0, 0, SRCAND);

   //  What is the use of the next line?
   //  Would it be more efficient to use a pointer to the bitmap?
   SelectObject(hdcAnimationBuffer_001, g_hbmBall);
   BitBlt(hdcWorkingBackBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width,
      g_ballInfo.height, hdcAnimationBuffer_001, 0, 0, SRCPAINT);
   //  Like this
   //  BitBlt(hdcWorkingBackBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width,
   //     g_ballInfo.height, [pointer to the bitmap in memory], 0, 0, SRCPAINT);

   //  This blitts the working (composted) buffer to the front (screen) buffer.
   BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcWorkingBackBuffer,
       0, 0, SRCCOPY);

   //  Background stuff
   SelectObject(hdcAnimationBuffer_001, hbmOldRed);
   DeleteDC(hdcBacdkgroundBitmapBuffer);

   //  Animation stuff
   SelectObject(hdcAnimationBuffer_001, hbmOld);
   DeleteDC(hdcAnimationBuffer_001);

   //  Why are we selecting and then deleting the following when we never
   //  used it (as far as I know)?
   SelectObject(hdcWorkingBackBuffer, hbmOldBuffer);
   DeleteDC(hdcWorkingBackBuffer);
   DeleteObject(hbmBuffer);

   //  Where is the part that fills a back buffer or two (etc.) of them and
   //  then "points" to them? It looks like a buffer is filled, then copied
   //  instead of pointed to, then deleted. That looks not so efficient, or
   //  maybe I am wrong about this. I would like to have 5 buffers that I can
   //  fill once or re-fill as I like and then "point" to them or whatever
   //  that is. Help!
}


What I have tried:

I have studied this and I think that I am beginning to understand the creation of an animation via blitting, and more specifically how to do this with a bitmap background. But now that it works, I have been trying to further understand what it is doing.

I have added comments. I would like someone that is more advanced than me to look at it and correct where I am wrong.

It works but I want it to be better.

Thank you.
Posted
Updated 29-May-21 7:00am
v4
Comments
Greg Utas 29-May-21 13:02pm    
I have reformatted your code to improve the odds that someone will look at it.
Member 15078716 29-May-21 13:53pm    
Thank you Greg Utas.
It is now easer for even me to read.
I like this site. You guys are so helpful.

Now how about someone helping me with making the code better?

OK?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900