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

Author Topic: Get Character At Coord  (Read 7494 times)

0 Members and 2 Guests are viewing this topic.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Get Character At Coord
« on: November 07, 2022, 09:41:14 pm »
SFML's texts are considered a single drawable object so interaction with them is usually based on their boundary boxes.

Get Character At Coord informs you of which character in a text object is at the given co-ordinate.

Get Character At Coord is a free-function that can be added to any code.



It's very easy to use and is pretty small.

Wiki page:
https://github.com/SFML/SFML/wiki/Source:-Get-Character-At-Coord

GitHub repository (it's a part of SfmlSnippets):
https://github.com/Hapaxia/SfmlSnippets/tree/master/GetCharacterAtCoord

Example used to create the above screenshots is included in the repository and also on the wiki page.

Feel free to ask any questions or make any comments.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

kimci86

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Re: Get Character At Coord
« Reply #1 on: November 08, 2022, 09:02:55 pm »
That is a nice utility function. I imagine it can be useful to implement a text input field.

I did not test it, but looking at the code, I guess it is not working if the text has some transformation on it, like a 180° rotation for example, or even a maybe more realistic 90° rotation. I am not sure what would be the best way to support that knowing that sf::Text::findCharacterPos returns global coordinates.

Regarding time complexity, note that sf::Text::findCharacterPos has a linear complexity. Your current implementation, by making a linear amount of calls to it, has a quadratic complexity. That might not be noticeable on small enough strings, but using a binary search would probably be better.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Get Character At Coord
« Reply #2 on: November 10, 2022, 04:56:53 pm »
Thank you! :)

Indeed. It would not as accurate if there is rotation involved due to the reason you mentioned.
If there were a "findCharacterPos" that worked on the local positions, it would be easily rectified but there isn't unfortunately.

A solution (albeit a bit clunky) might be to use the text on a render target and draw it there first and then rotate it afterwards. The co-ordinate would need more work too.

You're correct again, of course. I've already done some timing tests and it takes 'longer' to find the character at the end of the string than the first character. That said, it's microseconds :)
Interestly, in the example, the multi-line one is faster for the first line or two than any of the first characters in the single-line string.

Thanks again for checking it out. It's there if you need it! :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything