Animation with MFC

I’ve got a question about how to implement animation with MFC in WinNT. Does any one has some sample code or can give me some suggestion?

Be more explicit. What do you mean by animatiom? You can implement animation using CDC’s or DirextX or OpenGl.

NewROmancer

Thanks for your reply. I am working on a robotics project with MFC and OpenGL. What I mean by animation is that I want to show the motion of the robot(3D) on the screen. I have the pose(position, joint angles) of the robot at each second. But I don’t know how to make the animation to show the movement of the robot from beginning to end.

Is your problem how to calculate the movement between the frames (hierarchical transformations and rotations) or is the problem how you can activate the animation in Windows to get automatically one frame rendered after another?

My question is the later one. What kind of function should I use? OnIdle, or something else. And do I need to fork a new thread to do that?

Originally posted by zhangdm:
My question is the later one. What kind of function should I use? OnIdle, or something else. And do I need to fork a new thread to do that?

I tend to set up a timer in MFC using SetTimer, and then use the WMTimer function to respond to it. if you set up the timer to run at x milliseconds you can then force an Invalidate(FALSE) call which in turn will call OnDraw, which in turn can have your call out to renderscene or what ever function you OGL with.

If you only want to run for a limited amount of time the you can obviously set up a flag to stop when a timer counter expires, or you can use a modulus operator to work out if its time to render or do something else like comms etc.

I have no idea if this is at all eleoquent but it works for me, and if it works…

Use SetTimer and the function to determine if the system is idle (the function is on an example from VC++6 about a kind of notepad that has a splash screen). Look how they check if the system is idle. The function you need to make the moves are glTranslate* and glRotate*

NewROmancer

I used the ol Peek-a-boo. This was my first attempt at a GL app. A few have told me this is just a bad way to animate, and they’re prolly right. But it does work. It leaves the UI live enough to use sliders for speed, single block & feed hold buttons etc.Ive used gluts IdleFunc callback and it works ok. Another possibility is to put your animation routine in a seperate thread. But Ive yet to code a thread so I cant comment on it. A few people suggested this to me when I was playing with my robot app.

//Peek-a-boo
int CRobot::Interpolate(CString block)
{

//…
//Interpolation code
//…

//Process window messages
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return 1;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

HTH
Sean

Does anybody have an code example
showing the technolgy ?
All methos described in this threed
are not optimal ( I was testing all of them) :

  1. The timer method does snot lead to smooth
    animation, the timer is not precise enought … Especialy horrible is this situation
    under Win9x

  2. The infinte loop eith the Message Pump
    does not work correct in MFC …
    It ofte leed to hanging some parts of the code in memory after exiting. It is impossible to exit the code proper in this way …

THe threed method will be probaby the best one. I was tring to do that, giving the new animation threed an pointer to the animation routine as an call parameter … But it have some great time-synchronization troubles again …

I’m sort of in a similar situation. What is the optimal way to get consistent screen updates in MFC?

Agreed, WM_TIMER messages are ok if you don’t need anything particularly precise. In fact, I read that TIMER messages are the lowest priority and they get processed after everything else is done. Also, I heard that the granularity is about 15 millisecs in a best case scenario, meaning that you just can’t go any lower than 15…seems like last time I tried this, I couldn’t go much below 40 or 50. sigh.

So, any better suggestions would be appreciated.

I do my animation by sending me a a windows message:

AnimFunc()
{
// Do animation stuff.

PostMessage(window, WM_USER, 0, 0);
}

The notification for this message calls the AnimFunc() again. So the you can interact with the user interface while the animation is running and you dont have to simulate the message loop (peek, translate and dispatch).