Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [ Allegro 5.2.11 ] al_put_pixel and al_get_pixel very very slow compared to A4 !

This thread is locked; no one can reply to it. rss feed Print
[ Allegro 5.2.11 ] al_put_pixel and al_get_pixel very very slow compared to A4 !
SilverTES
Member #16,572
October 2016

Is there any other solution for that ?

{"name":"screenshot.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/f\/8ff2e540f5939052d380e14572e8a488.png","w":1267,"h":717,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/f\/8ff2e540f5939052d380e14572e8a488"}screenshot.png

Here my A5 function for determinant the sprite size by one-click on it (I know it's not the really best algo but in A4 it's really fast !):

#SelectExpand
1bool findMaxSize(ALLEGRO_BITMAP * bmp,int x, int y, ALLEGRO_COLOR transcolor, int &minX, int &minY, int &maxX, int &maxY) 2{ 3 ALLEGRO_COLOR current; 4 al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ANY_32_NO_ALPHA, ALLEGRO_LOCK_READONLY); 5 current=al_get_pixel(bmp,x,y); 6 al_unlock_bitmap(bmp); 7 //if(current!=transcolor) 8 if (memcmp(&current, &transcolor, sizeof(ALLEGRO_COLOR))) 9 { 10 if (x>0 && x<SCRW && y>0 && y<SCRH) 11 { 12 if (x<minX) minX = x; 13 if (x>maxX) maxX = x; 14 if (y<minY) minY = y; 15 if (y>maxY) maxY = y; 16 17 al_set_target_bitmap(bmp); 18 al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ANY_32_NO_ALPHA, ALLEGRO_LOCK_WRITEONLY); 19 al_put_pixel(x,y,transcolor); 20 al_unlock_bitmap(bmp); 21 findMaxSize(bmp,x+1,y,transcolor, minX, minY, maxX, maxY); 22 findMaxSize(bmp,x-1,y,transcolor, minX, minY, maxX, maxY); 23 findMaxSize(bmp,x,y+1,transcolor, minX, minY, maxX, maxY); 24 findMaxSize(bmp,x,y-1,transcolor, minX, minY, maxX, maxY); 25 26 findMaxSize(bmp,x-1,y-1,transcolor, minX, minY, maxX, maxY); 27 findMaxSize(bmp,x+1,y-1,transcolor, minX, minY, maxX, maxY); 28 findMaxSize(bmp,x-1,y+1,transcolor, minX, minY, maxX, maxY); 29 findMaxSize(bmp,x+1,y+1,transcolor, minX, minY, maxX, maxY); 30 } 31 return true; 32 } 33 else 34 { 35 return false; 36 } 37}

However, in allegro 5 with theses 'pixel' functions, the result is over extremely too slow ! O_o

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Some tips : Lock the bitmap once, and once only. Second, lock the bitmap in it's native format. You're using al_get_pixel, so it has to convert to an ALLEGRO_COLOR anyway. Third, compare a test pixel and the binary values directly. You can always leave a single pixel transparent in the upper left for testing purposes. Or in some other corner out of the way.

Rodolfo Lam
Member #16,045
August 2015

It might be worth to add that Allegro 5 does not really like accessing individual pixels at all, be it reading or writing them.

The problem is that Allegro no longer draws to system memory but to a GPU, and their memory cannot be directly accessed by the CPU. Allegro 4 was a software renderer so that kind of procedure you were previously doing were essentially the same as other memory operations.

I still don't understand shaders that well, but consider investigating them. They could possibly speed up your code, since now they are the ones with direct pixel access.

Failed that, you could try approaching the problem from another perspective, perhaps wrapping your individual bitmaps in some sort of structure with some metadata, that way you can store some pre-calculated values that can help on the calculation.

Go to: