I am trying to do a palette swap in Allegro 5. I have not found any way to do this from my research. So what I've thought of doing is to do it myself by setting individual pixels. I also haven't found a way to do this but Allegro 5 does allow for getting pixels. Is there some way to set a pixel in Allegro 5?
1 Answer
I'm not familiar with Allegro, but a scan through the documentation turns up the following:
al_draw_pixel
void al_draw_pixel(float x, float y, ALLEGRO_COLOR color)
Draws a single pixel at x, y.
al_put_pixel
void al_put_pixel(int x, int y, ALLEGRO_COLOR color)
Draw a single pixel on the target bitmap.
If you look at the source code linked from the documentation of either, you will notice that both make a call to al_get_target_bitmap
, which is used to get the target bitmap of the calling thread. That function has a setter companion: al_set_target_bitmap
which is used to select the bitmap to which all subsequent drawing operations in the calling thread will draw (docs for the latter).
For performance reasons, you might also want to look into locking the bitmap with al_lock_bitmap
when manipulating the pixels.
There also appears to be some example code with some manual palette manipulation.
-
\$\begingroup\$ Thanks for sharing. I've looked into these two and they don't actually put the pixel on the bitmap (from my understanding). For example, if I set a pixel at (0, 0), it will draw it at (0, 0) on the screen rather than on the bitmap. Also note that there is no bitmap pointer anywhere on these functions. \$\endgroup\$– WillCommented Jun 8, 2021 at 17:10
-
\$\begingroup\$ @Will There's no bitmap pointer because the target is acquired by
al_get_target_bitmap
. I've edited the answer to include this extra information; you can find the same info directly by checking the "source code" links in the documentation for either function & checking the functions used by their implementations. \$\endgroup\$– PikalekCommented Jun 8, 2021 at 18:11 -
\$\begingroup\$ Ah, thank you. This works. It's very slow because it does it pixel by pixel, but at least it works. Thank you. \$\endgroup\$– WillCommented Jun 9, 2021 at 20:17
-
\$\begingroup\$ Btw, just found out if you use al_lock_bitmap, this will drastically reduce the time it takes. \$\endgroup\$– WillCommented Jun 9, 2021 at 21:43
-
1\$\begingroup\$ @Will, you may also want to post your own answer demonstrating how you used
al_lock_bitmap
to achieve the speed you need. It's OK to post answers to your own question here, based on your experience of testing your solution. \$\endgroup\$– DMGregory ♦Commented Jun 10, 2021 at 1:54