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

Author Topic: I need help with font.getTexture  (Read 3015 times)

0 Members and 3 Guests are viewing this topic.

nfect

  • Newbie
  • *
  • Posts: 16
    • View Profile
I need help with font.getTexture
« on: May 29, 2020, 04:46:39 am »
Hello, I want to create some kind of a poor man's interactive text editor to include in my project. I started with sf::Text, but it got really really slow. I thought of using font->getTexture and font->getGlyph to get a little more control and cut the middle men.
I couldn't find a way to make it work (most certainly due to my lack of knowledge). font->getTexture always returns me small square. Does it return only the letters that have been used and font->getTexture only shows that?

My goal is to be able to retrieve positioning of the letters and interact with them using a mouse.

exemple code:

sf::RectangleShape shape;
sf::Font newFont;
newFont.loadFromFile("fonts/consola.ttf");
shape.setTexture(&newFont.getTexture(20));
shape.setSize(sf::Vector2f(200, 200));
window.draw(shape);

« Last Edit: May 29, 2020, 09:48:48 am by nfect »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: I need help with font.getTexture
« Reply #1 on: May 29, 2020, 01:24:17 pm »
Yes, as the documentation says:

Quote
Retrieve the texture containing the loaded glyphs of a certain size.

The contents of the returned texture changes as more glyphs are requested, thus it is not very relevant. It is mainly used internally by sf::Text.

The texture only contains the glyphs that have so far been loaded.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

nfect

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: I need help with font.getTexture
« Reply #2 on: May 29, 2020, 01:32:05 pm »
      sf::Glyph glyph;
      glyph = font->getGlyph(sf::Uint32('a'), size, false);
      TextRaw* newLetter = new TextRaw;
      newLetter->states.texture = &font->getTexture(size);
      newLetter->shapeUpdate();
      add(*newLetter);

that did the job, thanks a lot exploiter !!!
« Last Edit: May 29, 2020, 01:49:17 pm by nfect »

nfect

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: I need help with font.getTexture
« Reply #3 on: May 29, 2020, 01:56:29 pm »
just another question, there's a white dot in the top left corner, is there something I should do to get rid of it?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: I need help with font.getTexture
« Reply #4 on: May 29, 2020, 02:57:13 pm »
The white dot is not part of any glyph, it should never appear if you correctly use sf::Glyph's texture coordinates.

I don't really understand what you're trying to achieve. How are you going to handle more than one glyph with such a code? Do you think it will be simple to get something more efficient than sf::Text?
Laurent Gomila - SFML developer

nfect

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: I need help with font.getTexture
« Reply #5 on: May 29, 2020, 09:57:24 pm »
Oh no no no, Laurent, don't get me wrong! My only goal is to interact with the text. I split my string into a vector of chars, then create quads to which I apply the char textures, so I can keep track of positioning of each letters. It's for a text block that can be resized, adjusted/ justified and different style/font/color applied per letter. Since I'm not particularly advanced as a coder, this is what I came up with to do this. Of course, if there's another way to do this, I'd be really happy to know it, or have some pointers since I'm not that advanced on this part of the project, it's always time to get better ideas/take better decisions.

the code I shared here was just to show that I kind of understood the way to take, in no way a real code! Thanks for your help again guys, and amazing job on sfml!

And yeah, for the white dot I kind of figured I didn't apply any coordinates, that was a silly question :/

late edit:

well, it seems that my method is painfully slow, and I over sought the ability to get the position of a char by index in sf::text, that will speed up the whole thing by a very, very large amount. Ahhh, I love sfml!
« Last Edit: May 30, 2020, 12:33:32 am by nfect »