开发者

Flickering while redrawing in MFC

I'm writing a tetris game using C++ and MF开发者_StackOverflow中文版C. I have a timer and OnTimer handler. Handler looks like this:

... do some game-only logic ...
this->RedrawWindow();

And in OnPaint handler I draw blocks, map(with bitmap background), score etc. For drawing i use bitmaps and BitBlt function. Everything is drawn from scratch, i redraw the whole window area. Overall performance is normal, flickering sometimes, but it's ok, but when i added bitmap background to the map, flickering while redrawing became insufferable. Do I have to use another algorithms to draw bitmap, or may be I'm wrong in doing all redrawings each time OnPaint is triggered?

How can i eliminate flickering? I can use only C++ and MFC, the latter one unfortunately i didnt know at all before starting this project.

So, again: how can I eliminate flickering and increase performance of redrawing?


Try overriding CWnd::OnEraseBkgnd to just return non-zero. This tells windows that you have handled erasing the window background yourself.


Try Double Buffering. “Double buffering” refers to the technique of writing into a memory DC and then BitBlt-ing the memory DC to the screen.

In connection with Windows, this technique can be used to handle WM_PAINT messages. Your OnDraw function calls BitBlt to copy the memory DC into the screen DC. The memory DC is associated with a member variable in the view class, and is written to during times when no other messages are being handled.

Here is a link for some code that can help.


One possible solution is to have an in-memory bitmap (DC) for all the drawing, and then to blit it into the screen once you're done with all the drawing. When you draw all the screen entities one by one, flickering may occur. If you use an in-memory bitmap, you won't have any flickering. This is the pseudo-code:

Clear Memory Buffer
Blit the background bitmap on the Memory Buffer
For each entity that needs to be drawn
    Draw entity on Memory Buffer
Blit the Memory Buffer to the screen DC


You should apply both previous anwsers.

If you are drawing the whole window area you should override OnEraseBkgnd (as sje397 suggested) to avoid Windows from painting the background in gray. That's whats causing the flickering.

But you also better apply the Double-Buffering technique. This way the drawing will be done in memory an copy to the screen at once. This will also improve your performance.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜