by default a Sprite's color is solid black (0, 0, 0, 255), right?
No. The default colour for a sprite is solid white as a sprite's colour effectively filters the colours in the sprite's texture. White allows all of the texture through as is, and black changes all of the colours in the texture to black.
Ok - I'm not sure I got the origin part above.
Do you mean?
- get the centre of the sprite using the local bounds method, i.e. x=width/2, y=height/2.
- Then create my circle which is slightly large radius that the sprite.
- Then I set the origin to centre of my sprite and set my position accordingly?
- Then draw the circle and sprite?
Sort of but not exactly.
Basically, to set the origin of the circle to its centre:
circle.setOrigin(circle.getRadius(), circle.getRadius());
You'd then need to just update its position to match whatever the centre of the sprite is.
Although it might be easier to set the origin of the sprite to its centre as well as the origin of the circle to its centre. That way, you can set both positions identically. You may need to alter some of your original positioning but, since the sprite is a circle, it's likely that positioning using its centre may actually be more simple.
Then draw circle and sprite
(The origin is whereabouts on the object is placed at the position you specify with setPosition)
It works quite well, except it looks like my textures are not perfect circles and there is a small transparent gap between my texture and the outline.
One option is to stretch the texture slightly past the edge to cut off the anti-aliased part of the sprite's circle.
You can do this by setting the texture coordinates to be a pixel or two from the edge of the texture.
It could look something like this:
const int amountToTrim = 1; // number of pixels to remove from the edge
sf::IntRect trimmedTextureRect = sprite.getTextureRect();
trimmedTextureRect.width -= amountToTrim * 2;
trimmedTextureRect.height -= amountToTrim * 2;
trimmedTextureRect.left += amountToTrim;
trimmedTextureRect.top += amountToTrim;
sprite.setTextureRect(trimmedTextureRect);
(only do this once)
1) Does the misalignment look something like this:
[image]
Yes the alignment is exactly like this. I have done a little bit of tinkering where I minus the circle's x and y position by a few pixels...however it is never exact, requiring a lot of trial and error.
The amount to subtract from the circle's x and y
should be:
// semi-pseudo code
circleX -= circleRadius - (spriteWidth / 2)
circleY -= circleRadius - (spriteHeight / 2)
so, to position the circle you could use something like this:
circle.setPosition(sf::Vector2f(
sprite.getPosition().x - (circle.getRadius() - (sprite.getLocalBounds().width / 2.f)) // x
sprite.getPosition().y - (circle.getRadius() - (sprite.getLocalBounds().height / 2.f)) // y
);
but the quality of this positioning can depend on the accuracy of the sprite's circle within the texture.