Quote:
There's no way to create a Shape from an image is there?
Nope. How do you suppose that would work?

An image is a set of pixels, a shape is a set of points.
If you want to use Shape.contains, you'll need to create a polygon (i.e. Rectangle or RoundedRectangle) that lines up with whatever collision area you want in your image. You only need one polygon, and you don't need to move it around along with the card. Instead, just translate the mouse positions. If you rotate or scale the card, then yes, you'll need to also rotate/scale the polygon. (Since changing the polygon requires re-triangulation, which may be expensive, you should try to limit the amount of polygons you create and the number of times you rotate/scale/translate them per frame.)
The bitmap mask is faster and probably easier, but it also has drawbacks (i.e. no rotation/scaling). It might look like this:
Code:
... init ...
//ideally both of these would be in the same sprite sheet to reduce texture binds!
//an image of the playing card shape
cardImage = new Image("card.png");
//the mask
maskImage = new Image("card_mask.png");
... when it comes time to do collision handling ...
boolean collision(flota mouseX, float mouseY, float cardX, float cardY) {
float x = (int)(mouseX - cardX);
float y = (int)(mouseY - cardY);
if (x >= 0 && y >= 0 && x < maskImage.getWidth() && y < maskImage.getHeight()) {
Color c = maskImage.getColor(x, y);
return c == COLLISION_COLOR; //e.g. Color.white
}
}
I'm also curious how you're rendering your cards? Using a single PNG per card is not very efficient -- it will lead to long loading times and a lot of unnecessary texture swapping. (In OpenGL, it's relatively expensive to switch textures, which is why it's often suggested to use sprite sheets backed by the same internal texture.) If you're interested in better performance and just generally better practice, here's some considerations:
- Have a sprite sheet containing the "parts" to a card: a white rounded rectangle, and then a variety of transparent glyphs/images (numbers 2-10, jack/ace/king/queen). You then position each part appropriately, making it look like a single sprite. This is very efficient, requires almost no loading time overhead, etc. (You could take it a step further and use a 9-slice images to save texture space.)
- Alternatively, you could do the above but rely on a Slick Rectangle/RoundedRectangle to render the white card. This would allow you to then use the shape for collision-checking.
- If you're lazy, you could save each of the 52 cards as a separate image, and then pack them into a large (i.e. 1024x1024) texture atlas with an image packing tool. This is not an ideal solution as you'll be limited by the size of your texture atlas.
- If you're really lazy, you could save each of the 52 cards as a separate PNG file, and then just use Image for each. This is a really horrible solution; it will cause extremely long loading times, bad performance due to texture swapping, filesize overhead in distribution, etc.