Right way to "Take a picture" each frame

Hi all, I’ve added a Task to HelloPanda, like this:

taskMgr->add(new GenericAsyncTask("Takes a picture!", &TakePicture, window);

and the Task function is this one liner:

AsyncTask::DoneStatus TakePicture(GenericAsyncTask* task, void* data) {
    ((WindowFramework*)data)->get_graphics_output()->save_screenshot("file.png","comment");   
}

It works great right up until I close the window, and then it crashes.

Perhaps because the Window is invalid at this point?
One way to solve would be to check the window as valid before attempting to dereference it. (Only I don’t know how to do that)

But I can’t help thinking I’m going about it the wrong way.
Is there a better way of taking a snapshot every frame (in c++, obvs)?

I’m new to Panda3D and this is my first post. Looking forward to getting involved with the project.

Hi,

I assume, you want to quit your app by closing the window.
Instead of closing the window maybe you could define a key:

framework.define_key("q", "quit", &quit_app, nullptr);

which quits the app by calling:

void quit_app(const Event* ev, void* data) {
	PT(AsyncTaskManager) task_mgr = AsyncTaskManager::get_global_ptr();
	task_mgr->cleanup();  // remove all tasks
	framework.set_exit_flag();
}

Thanks for the reply. I think that’s exactly what I need to do, but for some reason it’s not working. I’m not receiving the callback when I press ‘q’. I have inserted some logging but never see the message.

In main(), I call define_key:

framework.define_key("q", "quit", &quit_app, &framework);

and my quit_app looks like this…

 void quit_app(const Event* ev, void* data) {
         std::cout << "QUIT REQUEST!" << std::endl;
         PT(AsyncTaskManager) task_mgr = AsyncTaskManager::get_global_ptr();
         task_mgr->cleanup();  // remove all tasks
         ((PandaFramework*)data)->set_exit_flag();
 }

Anything look strange?

I forgot to mention that you have to set

window->enable_keyboard();

This way if you press a button an event is generated.

1 Like

Woohoo! Thanks - works perfectly :slight_smile:

1 Like