Double Buffering, Animation is lagging, need help from expert.

Posted on 2006-05-30
Last Modified: 2010-04-01

VOID DrawTest(HDC hdc,Image* ball){      
    Graphics graphics(hdc);      
    Bitmap bmp(p1->GetWidth()*2,p2->GetHeight());
    Graphics* memGraph = Graphics::FromImage(&bmp);         
    delete memGraph;

   hdc = BeginPaint(hwnd,&ps);

    hdc = GetDC(hwnd);    
    Mouse_x = GET_X_LPARAM(lParam);                  
    Mouse_y = GET_Y_LPARAM(lParam);                        
    InvalidateRect(hwnd, NULL, FALSE);                  
    RECT rcts = {Mouse_x,Mouse_y,Mouse_x+ball.GetWidth(),Mouse_y+ball.GetHeight()};

p1 and p2 is Image object declare in GLOBAL. Both of them will draw as background, and ball is the animation that will move according to cursor on top of p1 and p2.

The mouse is moving following cursor but it is lagging.

I wonder if im using the wrong method in double buffering for animation.

Question by:iphone
    LVL 3

    Expert Comment

    No matter how hard you try, it's virtually impossible to draw an object that is exactly synchronised with the position of the standard cursor from a normal user mode application, without getting some sort of flicker or lag.

    The "lag" you are observing is because by the time that you get the WM_MOUSEMOVE message, the cursor will have already been redrawn in it's new position by the Windows display driver. There is also going to be a delay before you get the message due to the overhead of windows. Cursor updates in the display driver happen asyncronously based on interupts from the mouse driver, so they will never be exactly synchronised with your drawing. Sometimes, by the time you get the WM_MOUSEMOVE message, the cursor can already have moved again (if you are using a high-refresh rate mouse device, like a graphics tablet). You might be able to improve matters by calling GetCursorPos() inside your WM_MOUSEMOVE which may give you more up to date coordinates.

    There is nothing really wrong with your code, it's just that you are not in control of the how the cursor is drawn. If you want to get both your ball and the cursor synchronised so they get drawn at the same instant to avoid the lag, you need to draw both the ball and the cursor yourself.

    So, the only practical way to solve this is draw the cursor yourself. This is what most Drawing applications do.

    Call SetCursor() to set a blank cursor image for your window. When you get the WM_MOUSEMOVE messages, simply draw your own version of the cursor at the same time as your ball (or any other image that represents the cursor location). Then the two objects will be synchronised at the same position and providing your drawing code runs quick enough, you won't perceive any "lag".


    Author Comment


    Thanx for your advise.

    Finally i understand. I guess i dun have to make the ball follow exactly my cursor speed, just make it follow the cursor in delay, but at least it move smooth following the cursor's path, can you give me an idea how can i do that? Something like tweening in macromedia flash, is it using timer?

    LVL 3

    Expert Comment

    If you want it to move smoothly, you need to synchronise your drawing with the frame rate of the monitor. You need to update the position of the ball with every frame. You can frame sync using the DirectDraw API which has some functions for getting the monitor frequency and checking for the vertical blanking period.

    The other problem is that mouse events will not be synchronised with the display. They will be happing at a different frequency (usually slower than the frame rate), so you may have to interpolate some of the cursor positions for each new frame.

    LVL 48

    Accepted Solution

    Add line
    to the end of WM_MOUSEMOVE handler.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One C++ Help from Top Experts

    Solve your toughest problems, fast.
    C++ experts are online now and ready to help you.

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now