#### Archived

This topic is now archived and is closed to further replies.

# Visual C++ Bitmap manipulating

This topic is 6146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know for those of you who already programs in VC++, this question will be really simple, maybe stupid. But I just wanna learn 1st Question... I''m doing my final project for my VC++ class. The project is a Duck Hunt game. I''m using bitmaps for it (for the background and the ducks). Does anyone know how to use the transparency for bitmaps? I found 2 function: TransparentBlt and TransparentBitmap. but MSDN sucks explaining it... does anyone could help me out? Here the code that could be interesting for the 2 question: __________________________________________________________ //Create Window CDuckWin::CDuckWin() { Create (NULL, "Duck Hunt!", /*WS_OVERLAPPEDWINDOW*/NULL, CRect(1, 1, 900, 580), NULL, "Duck" ); m_bmpBackground.LoadBitmap ("BACK_BMP"); m_bmpDuck1.LoadBitmap ("DUCK1_BMP"); CClientDC dc (this); m_memDC.CreateCompatibleDC(&dc); m_memDC1.CreateCompatibleDC(&dc); m_memDC.SelectObject (&m_bmpBackground); m_memDC1.SelectObject (&m_bmpDuck1); x = 0; y=90; count=0; Score = 0; // TransparentBlt (NULL, x, 100, 117, 76, m_memDC1, 10, 10, 10, 10, RGB(0,0,0)); } afx_msg void CDuckWin::OnPaint () { CPaintDC dc (this); CRect rect; GetClientRect (&rect); dc.BitBlt (1, 1, 900, 580, &m_memDC, 0, 0, SRCCOPY); dc.BitBlt (x, y, 117, 76, &m_memDC1, 0, 0, SRCCOPY); } afx_msg void CDuckWin::OnTimer() { if(x>870) flag = 1; else if (x<2) flag = 2; if(flag==2) x += 2; else if (flag ==1) x -=2; num = rand() %3; if(num==1 && y <500) y +=8; else if(num==2 && y > 10) y -=8; InvalidateRect(NULL,FALSE); } __________________________________________________________ 2nd Question... I wanna make a SMOOTH path for the duck to fly. Right now, the path sucks. The duck is going everywhere in the screen and it''s always refreshing as hell (this doing an effect that isn''t too much professional) I wanna make a cool smoothing path, does anyone could help me out for this one also? Thanx a lot for your help! X-Fe@R... is back! ------------------

##### Share on other sites
Check out the MSDN. Read the function definition carefully. You''ll notice that the function takes a HDC, not a CDC. That means that the way you''re calling the function will not work.

Now I don''t use MFC, so I''m not sure if this is valid, but what you need to do is to get the member of the CDC class called m_hDC.

Thus your code should look like

TransparentBlt(dc.m_hDC,x, 100, 117, 76, m_memDC1.m_hDC, 10, 10, 10, 10, RGB(0,0,0));

Now if m_hDC is a protected (or private) field, then disregard what I''ve said above. I''ve no idea how to do it under MFC. My suggestion is to take a look at the Win32 API.

As to your second problem, it flickers because you''re redrawing the whole scene every frame and GDI isn''t exactly known for its speed What I suggest you do is create another memory DC & Bitmap, and use it as a back buffer. Then you do all your drawing to the back buffer, and only draw the changes to the window. That''ll make it a heck of a lot faster and reduce the change of flicker.

  //in your constructor add the linesCBitmap m_BackBm;CDC m_backDC;RECT rc;GetClientRect(&rc);//create the back buffer that has the same dimensions as the//client aream_backDC.CreateCompatibleDC(&dc);m_BackBm.CreateCompatibleBitmap(&dc, rc.right, rc.bottom);m_backDC.SelectObject(m_BackBm);

Now do all your drawings to this back buffer and then blit it to the Screen on the WM_PAINT. That ought to be faster (slightly). To be even faster, take a look at m_ps member of CPaintDC. Blt only the parts of the image that has changed and the m_ps structure will hold the region that needs to be updated only, as opposed to redrawing the entire image. Go do this one yourself

Hope that helped.