Placing text in an Image object

2019-09-23 16:00发布

问题:

I am using SFML 2.1 and have an Image with some objects that I would like to number, i.e. I have a set of origin coordinates for each of them so I would place the number there.

However, I can't seem to find a good way to do this: the most relevant function seems to be Image::copy() which places a rectangle in the image. To use that, I would have to create a number map image and then do offsets for successive numbers, which seems tedious, cumbersome and unreliable, as an image with too many objects would eventually run out of numbers.

I also thought of conceivably using a RenderWindow object and printing the Image along with numbers as sf::Text objects with the right coordinates, but the API does not seem to provide a way to save the contents of the window to an image.

So the question is: is there a straightforward way to place numbers (text in general) in an Image object?

回答1:

Your question is rather unclear, thus I guess the negative ratings.

If you want to render text, you can simply draw an sf::Text object to an sf::RenderWindow object. If you want to reuse the final result you can switch out the sf::RenderWindow with a sf::RenderTexture. If you want to manipulate single pixels, you'll have to go with an sf::Image, but keep in mind the conversion from image to texture is a slow process, so don't do it, if it isn't necessary.

See the official documentation and tutorials on how to work with SFML.



标签: image text sfml