Thread: How can I stop the screen flicker?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    How can I stop the screen flicker?

    All right, I'm taking an MFC class at my college right now, and been tasked by the prof to find someway to fix a problem the entire class had last year:

    To simulate animation, the programs would periodically call the draw function, but this causes the whole screen to 'tear' and flicker. Since most of the programs turned in were games, this was a major draw back. We searched through all our text books, but couldn't find a solution to the problem.

    So I turn to you. How can we fix this problem? By any means at all, any way to keep this tearing and flickering from happening, even if we have to learn to draw all over again.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Search for 'double buffering' or 'back buffer' - essentially draw everything to an offscreen bitmap and, once all rendering is complete, draw that to the screen in a single step(see also CDC::BitBlt).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Double buffering is the best bet.....

    Also look at the OnEraseBkgnd() handler (and return 0)

    MFC is not that good for games due to the way it retrieves messages from the OS. Games update while there are no msg's.

    Look at 'Idle Loop Processing' in MSDN

    OnIdle() allows you to add functionality to the app when there are no msg's from the OS. But should not be used for lengthy operations.

    this is the MFC message loop
    Code:
    	for (;;)
    	{
    		// phase1: check to see if we can do idle work
    		while (bIdle &&
    			!::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE))
    		{
    			// call OnIdle while in bIdle state
    			if (!OnIdle(lIdleCount++))
    				bIdle = FALSE; // assume "no idle" state
    		}
    
    		// phase2: pump messages while available
    		do
    		{
    			// pump message, but quit on WM_QUIT
    			if (!PumpMessage())
    				return ExitInstance();
    
    			// reset "no idle" state after pumping "normal" message
    			if (IsIdleMessage(&m_msgCur))
    			{
    				bIdle = TRUE;
    				lIdleCount = 0;
    			}
    
    		} while (::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE));
    	}
    This is slow as PumpMessage() calls GetMessage() which is additional overhead as msg has already been 'found' with PeekMessage().

    Compare this to a game loop

    Code:
    MSG msg;
    
    
    // prime loop
    PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE);
    
    // run till user exits
    
    while (msg.message!=WM_QUIT) 
    {
    //use peek as it returns immediately
    if (PeekMessage( &msg, NULL, 0, 0, PM_REMOVE)) 
    {
            // dispatch the message
           TranslateMessage(&msg);
           DispatchMessage(&msg);
    
    } 
    else 
    {
               // update and redraw screen
    }
    Last edited by novacain; 08-23-2005 at 01:00 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does this user get screen to run under root?
    By Overworked_PhD in forum Tech Board
    Replies: 2
    Last Post: 06-28-2009, 09:31 AM
  2. Linux Problem Screen Resoultion
    By Matus in forum Tech Board
    Replies: 1
    Last Post: 01-29-2009, 04:39 PM
  3. [C] GDI: how to erase material drawn at an entire screen DC
    By pc2-brazil in forum Windows Programming
    Replies: 3
    Last Post: 01-24-2009, 07:24 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM