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

Author Topic: A way to get a font's characters and TextEntered improvement  (Read 5082 times)

0 Members and 1 Guest are viewing this topic.

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
A way to get a font's characters and TextEntered improvement
« on: February 14, 2010, 01:38:05 am »
Hey all,

I recently started working on a Console for my game and I quickly noticed that the font I was using is having trouble rendering certain escape characters ('\r', '\b' to name a few). Instead it renders the "glyph not found" square. I was wondering if it would be possible to have a way to check if a certain character is part of a font, a bit like XNA's SpriteFont.Characters which returns a collection (or vector I guess for you C++ guys) that contains a list of drawable characters.

I was also wondering about TextEntered's behavior. First of all why does it return a string instead of a char? (That's for .NET, not sure about the other libraries). Also, should this event be raised even when the entered characters are escape sequences? Stuff like backspaces, enters and the like should be handled by the KeyPress event? Of course this problem can be easily fixed with a way to verify if I font contains a certain character.

Thanks!

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
A way to get a font's characters and TextEntered improvement
« Reply #1 on: February 14, 2010, 01:57:31 pm »
If you're using SFML 1.x, you can check against the sf::Font::ourDefaultCharset (or its equivalent in .NET). It contains many of printable-only characters. if that isn't enough for your case, you may specify your own charset.

As of SFML 2, there isn't a default charset anymore, since glyphs are created on demand. Here you have to define your own charset for checking against printable characters.

TextEntered contains a string (in C++ it's a 32 Bit value) because you get the Unicode codepoint. I guess it's directly transformed to a string value in .NET.

I think it's okay that TextEntered is also fired with non-printable characters, since they're still characters.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
A way to get a font's characters and TextEntered improvement
« Reply #2 on: February 14, 2010, 04:02:10 pm »
Quote
I quickly noticed that the font I was using is having trouble rendering certain escape characters ('\r', '\b' to name a few). Instead it renders the "glyph not found" square

Those are not printable characters, you must handle their effect manually. SFML doesn't define a "glyph not found" square, so it's probably defined by the font itself.

Quote
I was wondering if it would be possible to have a way to check if a certain character is part of a font, a bit like XNA's SpriteFont.Characters which returns a collection (or vector I guess for you C++ guys) that contains a list of drawable characters.

For what? Are you going to use a font if you realize that it doesn't contain the glyphs that you need? Won't you instead choose another font?

Quote
I was also wondering about TextEntered's behavior. First of all why does it return a string instead of a char? (That's for .NET, not sure about the other libraries)

This is indeed only for .Net. I don't really remember why I chose to return a string, maybe I just didn't find the proper character type?

Quote
Also, should this event be raised even when the entered characters are escape sequences? Stuff like backspaces, enters and the like should be handled by the KeyPress event?

If the TextEntered event is triggered, then it's because the OS itself triggered one (ie. I'm not responsible for filtering which character are "text" and which are not). By the way, \b for example has a valid Unicode codepoint, so this is probably the reason why it is considered as text ;)
Laurent Gomila - SFML developer

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
A way to get a font's characters and TextEntered improvement
« Reply #3 on: February 14, 2010, 08:20:17 pm »
Roger that, got it.

As for proper character type, not sure what you mean but there is a type in .NET, System.Char (alias char).

Thanks for the replies guys.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
A way to get a font's characters and TextEntered improvement
« Reply #4 on: February 14, 2010, 09:03:37 pm »
I had a look at my code to find out why I was not using a simple char, and here is the reason: .Net strings use UTF-16, so a UTF-32 codepoint may require two UTF-16 elements (ie. two System.Char) when converted :)
Laurent Gomila - SFML developer

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
A way to get a font's characters and TextEntered improvement
« Reply #5 on: February 15, 2010, 12:09:29 am »
Ok so if I understand correctly, the string will contain at most 2 chars?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
A way to get a font's characters and TextEntered improvement
« Reply #6 on: February 15, 2010, 07:41:27 am »
Yep.
Laurent Gomila - SFML developer

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
A way to get a font's characters and TextEntered improvement
« Reply #7 on: February 15, 2010, 12:44:04 pm »
Okay cool, thanks for the answers.