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

Hi,

VOID DrawTest(HDC hdc,Image* ball){      
    Graphics graphics(hdc);      
    Bitmap bmp(p1->GetWidth()*2,p2->GetHeight());
    Graphics* memGraph = Graphics::FromImage(&bmp);         
    memGraph->DrawImage(p1,0,0,p1->GetWidth(),p1->GetHeight());
    memGraph->DrawImage(p2,p2->GetWidth(),0,p2->GetWidth(),p2->GetHeight());            
    graphics.DrawImage(&bmp,50,25,850,633);                  
    graphics.DrawImage(ball,Mouse_x,Mouse_y);
    delete memGraph;
}

WM_PAINT
{
   PAINTSTRUCT  ps;
   hdc = BeginPaint(hwnd,&ps);
   DrawTest(hdc,&ball);
   EndPaint(hwnd,&ps);      
}

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

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.



Regards
iphoneAsked:
Who is Participating?
 
AlexFMCommented:
Add line
UpdateWindow(hwnd);
to the end of WM_MOUSEMOVE handler.
0
 
Mikeh926Commented:
Hi,
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".

Regards,
Mike.
0
 
iphoneAuthor Commented:
Hi,

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?

Regards
0
 
Mikeh926Commented:
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.

Mike.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.