best practice for Panda run()

Hi guys,

I’m wondering what’s the best way to implement the equivalent panda3D for python ‘run()’
in a C++ code.

Especially in term of keeping alive the main code while the async tasks are running.

Is a ‘Sleep(time)’ recommended to avoid unecessary CPU load or does it slow Panda execution ??

Is there a smart way to cope with this ?

// simple C++ panda bone
	
#includes ...

PandaFramework	framework;

// ... various functions ...
	
int main(int argc, char *argv[]) { 

	framework.open_framework(argc, argv);	// open a new window framework

	// ...
	// ... whatever application code ...
	// ...
	
// various asynchronous tasks
	taskMgr->add(new GenericAsyncTask("task1", &task1, (void*) NULL));
	// ...
	taskMgr->add(new GenericAsyncTask("taskn", &taskn, (void*) NULL));
	
	// *** execution loop ***
	Thread *current_thread = Thread::get_current_thread();
	while(framework.do_frame(current_thread)) {
		Sleep(20);						// sleep for 20 milliseconds *** ?
	}

	framework.close_framework();			// close the windows framework
	return (0);
}

Is the ‘do_frame()’ blocking ?

Hi

Did you know about framework.main_loop(); ?
That’s all you need to do, unless you were planning on doing something fancy and need more control.

example main

int main(int argc, char *argv[]) {

PandaFramework framework;
framework.open_framework(argc, argv);

// stuff

framework.main_loop();

framework.close_framework();

return (0);

}

Hi Tah,
Thank you again.

The point is that I’m trying to interleave Panda execution within a Directshow filter, and I’m encountering timing/synchronization issues.

So, I guess my question actually should have been: how to control simulation steps in Panda c++ ?

Sorry, I should have known it was something fancy… ha

By any chance did you change your sync_video in the config file to 0 or #f ? By default sync_video is set true so frame rate can’t exceed 60 fps.

Not sure if that’s the problem…

Tried it… seems to eat more CPU power for nothing.

I’ll try and dig into how to control a Panda simulation step from an external program.
All this should be tied one way or another to managing a round robin real time event wheel or some sort of stuff.

Well, Panda’s task system isn’t meant to be a real-time scheduler. But very few task systems are, really. If you really need real-time scheduling guarantees, you’ll have to provide that scheduler yourself, and call frame->do_frame() once per epoch within that system.

But for most needs, it’s usually sufficient to let Panda run the main loop, and simply add a task to call your DirectShow filter once per frame. Of course, your task will only get called once per frame, which will be every 1/60 sec if you are generating 60fps. You have to be sure your task can handle that kind of timing.

David