SFML community forums

Help => Graphics => Topic started by: finlaybob on February 08, 2013, 09:40:04 pm

Title: sf::Image and sf::Text
Post by: finlaybob on February 08, 2013, 09:40:04 pm
Hi all.

I want to extract the rendered text from a sf::Text object into an sf::Image. Is that even possible? If so, please can someone let me know how?.

I'm new to SFML, but not to OpenGL and C++, so I hope I'm not doing anything wrong!

I've tried this:

sf::Font f;
f.loadFromFile("Resources/Fonts/Exo.otf");
sf::Text t;
t.setString("Hello There");
t.setFont(f);
t.setCharacterSize(24);
sf::Texture tx = f.getTexture(24);
sf::Image img = tx.copyToImage();
 
But that's no good to me, I've attached the GDEbugger run of the code to get a peek at what is in the texture and it's not what I want at all.

I need to use the sf::Image for getting the texture into OpenGL as I'm managing my own textures i.e.
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,img.getSize().x,img.getSize().y,0,GL_RGBA,GL_UNSIGNED_BYTE,img.getPixelsPtr());


Any ideas would be helpful,
Thanks.

[attachment deleted by admin]
Title: Re: sf::Image and sf::Text
Post by: krzat on February 08, 2013, 10:00:18 pm
Draw it on RenderTexture.
Title: Re: sf::Image and sf::Text
Post by: Laurent on February 08, 2013, 10:00:54 pm
sf::Text text;
...

sf::RenderTexture target;
target.create(text.getGlobalBounds().width, text.getGlobalBounds().height);
target.clear(sf::Color::Transparent);
target.draw(text);
target.display();

sf::Image image = target.getTexture().copyToImage();
Title: Re: sf::Image and sf::Text
Post by: finlaybob on February 08, 2013, 11:37:55 pm
Thanks very much to both, I hadn't thought of that!

However the text is being cut off at the bottom and right hand sides...



[attachment deleted by admin]
Title: Re: sf::Image and sf::Text
Post by: Laurent on February 09, 2013, 11:49:16 am
That was just an idea, now you need to tweak it a bit to get what you want.
Title: Re: sf::Image and sf::Text
Post by: finlaybob on February 09, 2013, 11:58:16 am
I thought that. I've multiplied the width and height of the render target by 20% and it seems to do okay.

Thanks for your help!
Title: Re: sf::Image and sf::Text
Post by: Laurent on February 09, 2013, 01:11:36 pm
Using box.width + box.left and box.height + box.top should give you the exact size.