Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » slow function which draws shadows

Credits go to Trent Gamblin for helping out!
This thread is locked; no one can reply to it. rss feed Print
slow function which draws shadows
William Labbett
Member #4,486
March 2004
avatar

hi guys,

got some code I need some advice about. The function here gets called every draw and it maked the display scrolling jerky - so obviously it's taking quite long to do.

Can anyone see any obvious bottlenecks ?#

Does it take time to allocate the array shadow_vals ?

#SelectExpand
1void make_shadow_bitmap( ALLEGRO_BITMAP *shadow_bitmap, unsigned char ***shadow_arrays, STD shadows[], int number_to_draw) 2{ 3 unsigned char shadow_vals[31 * 16][41 * 16] = { {0} }; 4 5 int sn; /* shadow number */ 6 7 int x, y; 8 9 int shadow_id; 10 11 int first_x, first_y; 12 13 14 for(sn = 0; sn < number_to_draw; ++sn) 15 { 16 //printf(" drawing shadow %d\n", sn); 17 18 shadow_id = shadows[sn].shadow.shadow_id; 19 20 first_x = shadows[sn].grid_x * 16 + shadows[sn].shadow.x_offset; 21 first_y = shadows[sn].grid_y * 16 + shadows[sn].shadow.y_offset; 22 23 for(y = 0; y < shadows[sn].shadow.h; ++y) 24 { 25 for(x = 0; x < shadows[sn].shadow.w; ++x) 26 { 27 if(first_x + x >= 0 && first_x + x < 41 * 16 && first_y + y >= 0 && first_y + y < 31 * 16) 28 { 29 //printf("inside bitmap.\n"); 30 if( shadow_arrays[shadow_id][y][x] > shadow_vals[first_y + y][first_x + x]) 31 { 32 shadow_vals[first_y + y][first_x + x] = shadow_arrays[shadow_id][y][x]; 33 } 34 } 35 } 36 } 37 38 } 39 40 ALLEGRO_LOCKED_REGION *shadow_bitmap_locked = al_lock_bitmap(shadow_bitmap, ALLEGRO_PIXEL_FORMAT_RGBA_8888, ALLEGRO_LOCK_WRITEONLY); 41 42 int col; 43 44 for(y = 0; y < 31 * 16; ++y) 45 { 46 for(x = 0; x < 41 * 16; ++x) 47 { 48 col = (int) shadow_vals[y][x]; 49 50 *(int *) ( (unsigned char *) shadow_bitmap_locked->data + y * shadow_bitmap_locked->pitch + x * 4) = col; 51 52 53 } 54 } 55 56 al_unlock_bitmap(shadow_bitmap); 57 58 59}

Trent Gamblin
Member #261
April 2000
avatar

Do you really mean to be generating the shadows every draw frame? Could you generate them and keep them at program start? Creating that array wont take much time on the stack, but locking and drawing the shadows over and over probably will. Another thing I'll mention, but I don't think will help here, is if you're not drawing to the whole image, lock only part of it.

William Labbett
Member #4,486
March 2004
avatar

Thanks Trent.

Could you generate them and keep them at program start?

The thing about this way of doing it is that I can layer shadows over eachother in a realistic way.

Eg if there happens to be two shadows in one place, this way means that the alpha value of the black pixel drawn wont be the sum of the two shadows, it'll be the darkest of the two.

Perhaps there's a blend mode for this ?

Trent Gamblin
Member #261
April 2000
avatar

Yes, the default blend mode should do it.

William Labbett
Member #4,486
March 2004
avatar

al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA); ?

What about drawing bitmaps over the top of the shadows ?

I'll need them to be opaque.

Trent Gamblin
Member #261
April 2000
avatar

That's the one. If the sprite is opaque, when you draw it (with that same blending mode), it will still be opaque.

William Labbett
Member #4,486
March 2004
avatar

Cool. Thanks Trent.

Appreciate the help :)

Go to: