Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Image and sf::Text  (Read 2658 times)

0 Members and 1 Guest are viewing this topic.

finlaybob

  • Newbie
  • *
  • Posts: 3
    • View Profile
sf::Image and sf::Text
« 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]

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: sf::Image and sf::Text
« Reply #1 on: February 08, 2013, 10:00:18 pm »
Draw it on RenderTexture.
SFML.Utils - useful extensions for SFML.Net

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Image and sf::Text
« Reply #2 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();
Laurent Gomila - SFML developer

finlaybob

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: sf::Image and sf::Text
« Reply #3 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]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Image and sf::Text
« Reply #4 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.
Laurent Gomila - SFML developer

finlaybob

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: sf::Image and sf::Text
« Reply #5 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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Image and sf::Text
« Reply #6 on: February 09, 2013, 01:11:36 pm »
Using box.width + box.left and box.height + box.top should give you the exact size.
Laurent Gomila - SFML developer