PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Surface Masking

I am trying to do AND/XOR masking with a surface in PocketFrog, but I'm not having much luck. Basically, I have a black/white mask that I want to apply to a surface (black areas keep, white areas remove). After applying the mask to the surface, I want to blit the surface to the main window.

Does PocketFrog have any built-in masking functions? If not, any suggestions on how this can be done? I should clarify that I'm NOT interested in alpha masking, just plain-ol' "vanilla" masking.

Thanks...
:D
Go ahead and apply the mask to your image in the editor and use
img->SetColorMask(Color(255,0,255));
after loading your image.

This will NOT draw any areas that contain the color 255,0,255 (or whatever color you wish to use).
Well, that's really not "masking"...that's "transparency". Doing as you said will require me to have multiple images instead of just 1 image.

For example, if I have an image of a ball and I want the image to slowly shrink, I can take a single image and apply different masks to achieve this result. Each mask will be smaller and smaller which, when applied to the image, will give the effect that the ball is shrinking.

If I use a color mask, I will have to have many images (for each frame of the ball shrinking). Of course, I guess either way, I have to have multiple images OR multiple masks. Perhaps this is my only option in PF...I was just hoping there was a better solution.

Thanks for your input!
:D
I think it would be useful to know exactly what you are trying to achieve.

There are no built-in masking functions though if you search this & other forums they are discussed here & there. GD uses masks for pixel perfect collision detection for instance.

Your suggestion re a shrinking ball does not sound like a good way to achieve this given that PF can already scale & rotate directly. For simple sprites which have pixels on or off the transparency method works just fine & alpha blending allows semi-transparent parts.

Anyway I'm sure if you explain your aim we can all help more re: what PocketFrog can do.
Shrinking the mask and not the ball wouldn't really give the effect that the ball is shrinking - would it? You could blit the color key onto the image in order to do that.

Or, you can scale blit with a mask, which would make the ball shrink...


I have written my own modifications to PF that take 2 surfaces as an input - using one for an alpha mask and one as an image source. This does allow something similar to what you were wanting - in that you can animate the mask separately from the image. This requires modifying several of the frog source files. If you're really interested, maybe I can post some directions.
Thanks guys...I appreciate your responses. Now that I think about it, you are right. Shrinking the mask, doesn't really shrink the ball...it only draws less and less of the original image.

I'm currently working on a puzzle game with game pieces that "dissolve" when they are removed from the board. I'm trying to give the effect that the "ball" pieces are shrinking until they disappear. There are different shaped pieces, so not all of them are round. I figured the best option would be a mask for each type of shape.

I suppose the easiest option is to create multiple images for each frame and then cycle thru these images to create the animation. Perhaps this is better than using a mask because it would be faster and require less CPU overhead.

Yeah, I could scale blit to achieve the desired effect, but this would certainly require more CPU cycles. I'm already fighting to stay above 20 FPS on my Dell Axim.
How big are your balls? (Groan...)

Your comment that your fighting to stay above 20FPS makes me wonder if you might not be doing some inefficient things besides, unless you've got really big balls (sorry, couldn't resist ;) ) because a stretchBlt shouldn't kill your performance that much if your balls aren't huge (can't... stop... myself...)

Just curious because in K&G; Arcade I do stretches and rotations in a couple of places with a fair amount of other stuff going on at the same time and I'm getting 24FPS on my Axim Basic with no trouble (it's hard-coded to 24, otherwise it would be higher).
Well, the balls are about 20x20 pixels each, and there are about 100 balls on the screen at one time. I haven't tried stretchBlt, but it may be the solution if you don't think it will kill my FPS.

About my FPS: After a lot of frustrations, I finally ended up drawing the *entire* screen each time thru the game loop. This does hurt my FPS a bit, but it simplifies the code greatly. My frame limiting code may need some tweaking. It's currently constrained at 30FPS, and I'm getting about 20FPS. However, if I remove the frame-limiting code altogether, I can get FPS in the 30's. But, I'm afraid to remove it complete ly as the faster devices will run the animation too quickly. Doesn't make sense to me....?
:?
Well, two things, and I say this from experience...

(1) Most people will tell you that you should be doing time-based framerates. The logic is sound and the results are clearly good based on how many top-notch games out there do it. However, it does add code, and it does, in my mind, make things more complicated.

(2) If your going to limit frame rate, which is what I've always done too, you have to be sure that firstly you pick a frame rate that even the worst devices can manage (I've found 24FPS to be the magic number) and to pull it off you will really need to redraw only the parts of the screen that you really need to.

I know exactly what you mean about it simplifying things to redraw everything every frame, but it's kind of a mutually-exclusive thing... if you want to use the simpler frame limiting approach, you pretty much have to optimize things as much as possible. This is ESPECIALLY true on the Axim's, since they suck. :)

When developing K&G; Arcade it was amazing to me that my band-spanking new Axim (at the time) running at 300MHz could BARELY get 30FPS in some parts, whereas my old Casio E-125 at 150MHz could bang out 50FPS with the same exact code steady as a rock.

In your case, I don't know if you have multiple balls disappearing at a time or not. If not, I don't thing the stretchblt will be any kind of problem. If it's multiple balls, you definitely want to do it to a temp surface and then just blit that multiple times.
Thanks for the advice! I guess developing on the Axim may turn out to be a good thing, since it seems to be one of the worst (slowest) Pocket PCs out there. :wink: Honestly, I've been very surprised at the poor performance.

I do have multiple balls disappearing at the same time, so I will render them to a temp surface and then blit to the main screen.
Two quick solutions for doing masking (without changing PF):

1. Use multiple blits with different masks

A. Make a copy of your original image surface:
Surface* temp = image->Clone();
B. Blit your B&W mask onto temp using white as colormask:
mask->SetColorMask(Color(255,255,255));
Surface* old_target = display->SetRenderTarget(temp);
display->Blit(0,0,mask);
C. Blit your temporary image to the screen, now containing a lot of black pixels to be masked away.
temp->SetColorMask(Color(0,0,0));
display->SetRenderTarget(old_target); (passing NULL or 0 as argument will default to the backbuffer so that would work for us also)
display->Blit(x,y,temp);
D. Don't forget to free the temporary surface if it is no longer needed (but if you keep doing this in a loop you will benefit from reusing the temporary surface by blitting the original image on top of it).

2. Loop over each pixel and do your masking (this could be extended to more than just on/off masking)

Code:

Rasterizer* imgrast = display->CreateRasterizer(image);
Rasterizer* maskrast = display->CreateRasterizer(mask);
for (int y=0; y<image.GetHeight(); y++)
for (int x=0; x<image.GetWidth(); x++)
if (maskrast->GetPixel(x,y) == Color(255,255,255))
display->SetPixel(x,y,imgrast->GetPixel(x,y));


Please bare in mind that I have NOT compiled this since I'm currently on a linux platform so this might not compile if you just copy-past it. And even if it does, there is no guarantee that it will actually work (i.e. produce the wanted effect). Possibly you will need to lock (and then unlock) the surface before doing the SetPixel(). Why post it then you might ask? Well, it can serve as a direction of what I think is possible just looking at the declaration of PF.

Please let me know if this works and which of my two solutions is faster since I'm interested in the results even though I'm not currently coding anything for the PPC.