Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » al_draw_bitmap fails to run on threads other than the parent thread

Credits go to Matthew Leverton for helping out!
This thread is locked; no one can reply to it. rss feed Print
al_draw_bitmap fails to run on threads other than the parent thread
iam_donald
Member #14,318
May 2012
avatar

I'm thought it'd be wise to have the drawing code of my game run on a separate thread for obvious performance reasons. After several attempts at this i decided to experiment and just run calls of al_draw_bitmap on a separate thread. Here is the code I used for this:

#SelectExpand
1void draw_video_frame(ALLEGRO_BITMAP * frame) { al_draw_bitmap(frame, 0, 0, 0); } 2 3void * t_draw_video_frame(ALLEGRO_THREAD * thread, void * arg) 4 { 5 ALLEGRO_BITMAP * frame = (ALLEGRO_BITMAP *) arg; 6 draw_video_frame(frame); 7 return NULL; 8 } 9 10void stream_video(Video * video) 11 { 12 load_frame(video); 13 video_frame_step(video); 14 ALLEGRO_THREAD * thread = al_create_thread(t_draw_video_frame, (void *) video->last_stepped_frame); 15 al_start_thread(thread); 16 al_join_thread(thread, NULL); 17 destroy_video_frame(video->prev_stepped_frame); 18 al_destroy_thread(thread); 19 }

This fails with the error: Assertion failed: (src), function al_copy_transform, file /Users/iam_donald/Downloads/allegro/src/transformations.c, line 32.

Calling draw_video_frame() works. My question is: Is this behavior by design or I am I doing something silly here? Currently this behavior doesn't allow running any chunk of code with calls to al_draw_bitmap on a thread other than parent thread.

Matthew Leverton
Supreme Loser
January 1999
avatar

See al_set_target_bitmap().

Quote:

A single display cannot be current for multiple threads simultaneously. If you need to release a display, so it is not current for the calling thread, call al_set_target_bitmap(NULL);

So set the parent's target bitmap to NULL and set the child's to the display's back buffer.

iam_donald
Member #14,318
May 2012
avatar

That explains it. Thanks.

Go to: