Tile Masks

BlitzMax Forums/BlitzMax Beginners Area/Tile Masks

ponyboy(Posted 2008) [#1]
For the game I'm working on, I want to be able to use tile masks on the first and second layers of a map. To do this, what needs to happen is I need draw the first layer (like grass) and then take the mask (a black and white image) and blit it onto the second layer tile using either black or white as the transparent image. At this point I have the second layer tile and tile mask as one image and can then blit that on top of the first layer tile using the opposite color as the transparent color to get transition effect amongst tiles.

The problem I foresee is that in BlitzMax, I can't blit to offscreen images right? So how would I go about creating this effect then in BlitzMax?


grable(Posted 2008) [#2]
Since Max2D is 3D, cant you just use alpha channels?
I assume it would be faster than 2D bit masking.


ponyboy(Posted 2008) [#3]
By alpha channels I'm assuming you mean when I save the tiles have a transparent alpha channel in the png file?

I could do that but then every single ground tile that I use (there are 20+), would have to be saved with every single transition used (there are 30+) which results in 600+ tiles, ~550 of which are not needed if I can just use a tile mask.

I'm trying to avoid this as it's a lot of extra work and a waste of memory having to hold on to that many extra tiles.


Dreamora(Posted 2008) [#4]
You can not blit to images at all. BM is 2D through 3D.

You can only blit to the backbuffer or paste a pixmap into another pixmap.

But none of these actually helps as masking isn't a "realtime" effect as well, changing an image means reupload of its pixmap to the graphic card.

For your purpose, you most likely would have a list of pixmaps with the masks and then paste that into a copy of the corresponding image (after you locked it to get its pixmap).


But be aware that this means that each of those different images uses its own space in VRAM.


grable(Posted 2008) [#5]
Im not sure how your transitions work, but you can get this kind of thing on the cheap with 3D.

By drawing 2 quads with alternating alphas. (ie one quad goes from left->right, the other in reverse)

How to do this with Max2D is unclear to me though.

Since what you are after seems like a pure 2D operation, you might be better of with SDL or DirectDraw...


ImaginaryHuman(Posted 2008) [#6]
So you're basically trying to do a cross-fade between two tiles, which is basically alpha blending.

What you sound like you need is to be able to have multiple frames of animation in the alpha channel of any given tile that you want to use it with. That's pretty tricky on graphics hardware because usually the alpha has to be embedded in with the RGB data.

You could sort of do it manually whereby you draw the background layer and then you fade it out by the inverse of the alpha fade value by drawing a black rect/mask over it at the appropriate level of transparency. Then you could do the same as you said with another image somewhere else and fade it up to white a bit. But then you need to `grab` that second composite image so that you can draw it with LIGHTBLEND which will add it to the other image, producing the effect of a cross-fade. You sort of need offscreen drawing to do the second image and the grab, so you could draw it to the backbuffer, do your fade-to-white, grab it, clear the screen, draw the background layer, fade it, then draw the other image on top additively. But grabbing is not something you really want to do many times in little pieces and direct render to texture would be faster.

An alternative is to make sure your app opens in a 32-bit screen with a destination alpha channel, draw your alpha mask to the destination alpha only, draw the background object as RGB and not to the alpha channel, then set up a blend more which uses the destination alpha to determine the fade value, and then draw the second image on top as RGB using the dest alpha. But this won't work in 16-bit or even 24-bit screens, must be 32-bit and you'll have to write your own code for DirectX or OpenGL.


tonyg(Posted 2008) [#7]
Texture splatting might help.
There is some more code later here . It's DX only there but OGL should be possible.


ponyboy(Posted 2008) [#8]
That's pretty much exactly what I want to do but for multiple tiles on the map. Gonna need it for OpenGL though also. Looks like I got some work cut out for me.


Dreamora(Posted 2008) [#9]
I would actually precalculate the needed images on load of the tile map and then store them. That way you only need your base tex and the masks stored on the disk and the rest will be generated dynamically once for each map.
The reason is that the images etc are no static mesh so multi texturing is nice but a quite overnice setup overhead as well.


tonyg(Posted 2008) [#10]
Here are some more links then :
Tiled Terrain
Tile/Map-Based Game Techniques: Handling Terrain Transitions
Multitexturing in DX6
NeHe OGL multitexture basics
Texture splatting in D3D