[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-05-30
4
Medium Priority
?
206 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:iphone
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Mikeh926
ID: 16788354
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
 

Author Comment

by:iphone
ID: 16788423
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
 
LVL 3

Expert Comment

by:Mikeh926
ID: 16788456
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
 
LVL 48

Accepted Solution

by:
AlexFM earned 1000 total points
ID: 16788533
Add line
UpdateWindow(hwnd);
to the end of WM_MOUSEMOVE handler.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 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