Are there any functions in Allegro 5 to do bitmasked drawing?
Ala the ground in this picture:
{"name":"WormsArmageddon1.PNG","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/d\/cd522b2ea198b49ac72533a93431b1c6.png","w":633,"h":252,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/d\/cd522b2ea198b49ac72533a93431b1c6"}
You'd use the alpha channel.
So you're saying use the alpha channel to denote whether or not there is land at that point?
That's neat and it might work, but that doesn't really allow things like rolling textures or multiple textures. I think I'm looking for more of a "draw X bitmap given Y mask against the screen."
I guess I could use some sort of Stencil Buffer... though I can't remember if that was depreciated.
Ah... I think I'd approach this problem using a shader. Allegro definitely doesn't expose the stenciling feature in any way I know of.
It's possible to do masked blends using the features of Allegro 5 only by using al_set_blender. I do this to generate automatic blends for my game's tile maps. Here's a snippet from my code that should be helpful:
Edit:
Note that this will likely be slower than using a shader. Maybe masked draws would be a nice feature for a later version of Allegro....
Edit2:
Looks like stencil buffers are not obsolete and available in opengl
https://www.khronos.org/opengles/sdk/docs/man3/html/glStencilOp.xhtml
And in opengl ES 2.0 and 3.0 (though not 1.0?)
https://www.khronos.org/opengles/sdk/docs/man3/html/glStencilOp.xhtml
So definitely something we may be able to put an Allegro wrapper around...
Edit 3:
Also in Direct3D 9
https://msdn.microsoft.com/en-us/library/windows/desktop/bb206123%28v=vs.85%29.aspx
And 10/11:
http://stackoverflow.com/questions/19320308/porting-opengl-stencil-functionality-to-directx-11
You can also look at the ex_depth_mask example, it does something similar without a shader, using the depth buffer as a mask. Stencil support definitely would be nice as well, especially in cases where you need both depth and stencil buffers at the same time.
I can't get it to work, these blender options and alpha has always been confusing to me.
Do I want my mask to be in the RGB, or in the Alpha channel? I tried converting my map to alpha values, but that doesn't seem to work.
I guess I'm having trouble understanding when alpha moves. If I draw something with alpha, does that set the alpha of the destination bitmap?
If I draw something with alpha, does that set the alpha of the destination bitmap?
Only if you use al_set_separate_blender. Otherwise the destination alpha is untouched.
Well you're drawing the bitmap over the mask which probably doesn't work. I'm drawing the mask over the bitmap to another bitmap, which does work if you re-draw this resulting bitmap to screen.
Thanks! It took me awhile to things to sink in (they still aren't great), but the biggest error between the sample code and mine was that I forgot to convert my "map" bitmap's black color to an alpha channel. Once I called that function, it works great!
However, there is one downside to the depth test method (other than losing normal use of the depth buffer while using it for masking). What about smooth transitions?
If you wanted something to be drawn using variable alpha levels, how would you frame it? Is it exactly like that "rounded rectangle" thread that's also going on?
Keep in mind, these alphas/blenders are still very confusing to me so please break it down and elaborate.
Thank you.
Hey what am I doing?! I totally have the function you're looking for!
but use this overload for simplicity:
ALLEGRO_BITMAP *create_masked_bitmap(ALLEGRO_BITMAP *top_image, ALLEGRO_BITMAP *bottom_image)
I haven't used it in a while (blending methods changed slightly in subsequent versions of allegro) but I just tested it and it seemed to work. This image shows a mask image (top_image), a texture image (bottom_image), and the example program putting the two together on a blue background.
{"name":"609403","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/e\/cec488d8bf9f387b03b832c60122da4e.png","w":1921,"h":1079,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/e\/cec488d8bf9f387b03b832c60122da4e"}
If you go to the header file here, you'll see there are variants of the function, too. You can apply use your own blending modes, and/or transforms to the top/bottom images, and/or supply your own surface bitmap so that it's not created each time. There isn't a variant for looping the texture on the bottom to fit the mask, but you could use draw_textured_rectangle or draw_offset_textured_rectangle for that here).
All of this is still in the works, I don't have docs for this stuff, yet.
I guess I'm having trouble understanding when alpha moves
It's very confusing. Back when I was working on this function, I had a program that generated all possible blends (hundreds of images, if I recall) just so I could find the correct blending sequence & bitmap order I was looking for. Which bitmap do you draw on the surface? Which one do you draw on top of that? Does it read the surface or the drawn bitmap? ALLEGRO_BLEND_MODE_WHUT?
F it. Just brute force.
Maybe the documentation needs improving, but I figured it out rather easily by playing with ex_blend.
http://sourceforge.net/p/alleg/allegro/ci/5.1/tree/examples/ex_blend.c
Thanks for the help guys! I don't have time implement your most recent suggestions yet, but I'll keep you updated!
Here's a picture of it working so far (with ugly filler graphics):
{"name":"609406","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/d\/0d9885974075845e438514233b3417d1.png","w":642,"h":505,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/d\/0d9885974075845e438514233b3417d1"}
Background is drawn normally, the "map" (black for nothing, any other color for land) is converted to alpha, and then a texture is printed through that. The texture is tiled 2x2 right now to show its possible. Nothing too fancy, but it's baby steps.