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

Author Topic: Using sf::Font as sprite sheet.  (Read 5877 times)

0 Members and 1 Guest are viewing this topic.

Critkeeper

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Using sf::Font as sprite sheet.
« on: December 08, 2014, 09:22:44 am »
Has anyone ever tried this? I mean, if you create a font set that represents your sprite sheet I don't see why it wouldn't work. After all, when it is all said and done a glyph is just a fancy word for a sprite, and the texture that is associated with that glyph is just retrieved from the font file.

This way sf::Font takes care of loading and managing the sprites of your sprite sheet, and will only load the sprites that you require at the time that you need them.

Furthermore painting a scene with tiles based on text data is quite straightforward. Literally paint a scene like:

ADSDFFDSL
ADSDFIISL
DDDDDDDD
KDJDKSLIDL
DKJDFLKDJG

You could just make notepad or emacs or vi or whatever your text editor is your map editor. :)

I know it seems hacky, but is it really?

Why duplicate code? I'm just going to take a gander inside sf::Font's implementation, and if it looks curiously similar to my own sprite sheet implementation, then I'll just use that!

EDIT:

Furthermore, if you ever develop or use techniques for modifying properites of text, like stretching or "curving it" around a bezier, or making it move like a marquee or making the colors pan around, you could just use literally the exact same lines of code to make your sprites in your sprite sheet behave the same way, because you are interpreting a sprite sheet as if it were a font file.
« Last Edit: December 08, 2014, 09:29:50 am by Critkeeper »
if(CritKeeper.askQuestion()){return question.why();}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Using sf::Font as sprite sheet.
« Reply #1 on: December 08, 2014, 11:16:17 am »
It's an interesting idea and it might work good enough, however it won't make your code very readable and you'll get an unnecessary overhead from Freetype, sf::Font, sf::Text and sf::String/std::string and all of this, just for being lazy and not willing to write your own implementation. ;)

Not to forget that you'd have to either go with bitmap fonts, which then could only be used in one size (without scaling) or you'd have to use vector graphics, which usually isn't that good for 2D games.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Critkeeper

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Re: Using sf::Font as sprite sheet.
« Reply #2 on: December 08, 2014, 12:01:17 pm »
Hrmm...

Well I'm thinking of what could cause any trouble:

Font glyphs and sprites from sprite sheets aren't actually all that different, in fact they try to solve practically the same problem-- you've got this one texture and need a bunch of lightweight instances of it that you can move around and position however you would like.

Glyphs are practically never used alone, they are chained together as text onscreen and many glyph implementations probably assume so. That isn't the case with sprites. That means that you could run into trouble if your glyph implementation assumes too much.

Font file standards are also written with the assumption that the font file will store information about how to BUILD glyphs ( a slow process ), or it will contain the glyph itself. There are potential problems with misusing any particular font file standard as a sprite sheet. If you use a builder-type font file standard and you encode your sprites (glyphs) as vector art for example, then you will pay a performance price for that. If you use a raster-type font file standard then you will pay for loss of quality if you try to stretch or magnify your image too much ( that happens with bitmap images anyway, so no biggie ). Also, SFML doesn't support every font type out of the box, so a potentially tasty option might not actually be an option.

Various font file standards may also encode information about the width or the relative placement of the glyph along some line, or its endianess, which for most sprite sheets isn't exactly information that we really want. Others are quite complicated, for example take a gander at http://en.wikipedia.org/wiki/TrueType, which includes its own virtual machine!

As for referring to each sprite as a character, that leaves you with something to think about. It would certainly be unclear that you meant "that sprite that looks like a tree" if you encode that as the letter capital C, but a letter is just a number, and a number can be cast from an enum so maybe you don't have to be terse unless you want to (for example with a notepad "map" file that just contains character information).

Also consider that UTF8 and ASCII aren't the end all be all. You could very well use another font format with wide characters, and then you could configure your (otherwise arbitrary) choice of "terse" characters to actually look like the thing they represent in notepad or whatever. I'm sure that somewhere someone has made a unicode symbol that looks like a tree.

You are right about the performance for games which really spam the hell out of sprites, like particle engines.

But not every game has those requirements, and these benefits might outweigh the costs for non performance critical applications.

I just think a WYSIWYG map editor in a simple text editor like notepad is the coolest thing since koolaid. You could programatically generate maps for a roguelike just as easily as you could parse text! You could allow the user to specify his own maps without a lot of hassle. Modifying the map becomes equivalent to modifying text, so any text processing code that exists also becomes map modifying code, which is quite a useful feature. Imagine that you have a big game and you want to provide the user a utility that allows them to search for a specific combination or arrangement of tiles, you could implement that easily with a text search or even allow him to pattern match with something like perl.

Imagine that the user opens up his dinky in game map, and he sees the same notepad representation that I have been suggesting here. I mean if you stop to really think about it, its not really that ugly when you consider all the advantages.

If no specific font format fits the needs of a tilemap, I could just make my own :)

All that would boil down to is modifying the source code of SFML to work with my totally made up newfangled file format. Aint nothing wrong with that.

« Last Edit: December 08, 2014, 12:25:05 pm by Critkeeper »
if(CritKeeper.askQuestion()){return question.why();}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Using sf::Font as sprite sheet.
« Reply #3 on: December 08, 2014, 01:16:34 pm »
I find it a bit paradox that you don't want to implement a simple thing such as a tile map container and loader in your user code, but you'd be willing to go and create your own "font" format and modify SFML to support that format.

I still don't see any advantage, other than being lazy and not writing a custom tile map system. But I can count a few disadvantages:
  • As said, you can get performance issues.
  • You'll constantly be fighting against how SFML deals with its font internally, meaning as well that you actually have to study the internal bits and every updat to SFML could break it.
  • Having to modify SFML just for this custom "font" format is a really ugly hack.
  • You take your freedom on flexibility, you'll be stuck with whatever SFML has implemented. If you want to load/process/rotate/scale/optimze/whatever differently from how SFML does it, you'd again have to hack SFML or you can't change it.
  • Reading the code will be quite confusing, since "Font" and "Text" will suddenly turn into sprites.

I'm sure I could find more if I wanted to.

This all the while it's really simple to use for example JSON and make a character to image & position "lookup" table and then have the map "drawn" with the defined characters.
That way you wouldn't have to touch SFML, you'll get all the flexibility you want and need, there wouldn't be such ugly overheads and the code base would be understandable.
It's really not that hard to do.
« Last Edit: December 08, 2014, 02:07:55 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Critkeeper

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Re: Using sf::Font as sprite sheet.
« Reply #4 on: December 08, 2014, 02:05:42 pm »
On second thought I think you are right.

I'm just trying to be too clever. I have that problem sometimes and it causes me to do ugly things.
if(CritKeeper.askQuestion()){return question.why();}

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Re: Using sf::Font as sprite sheet.
« Reply #5 on: December 08, 2014, 07:08:17 pm »
That would definitely be an awesome project for sure, but I agree that it isn't the best way of handling sprites.

But think about it: we could use separate font files for separate colors on the sprites, and have vectorized graphics for each part of the sprite with the right color. Like, a "red font", a "white font", a "blue font", etc. Then you just render the same "text" with all of the fonts on the same place, and you have your colored vectorized map drawn on screen :P
I would definitely check that out if someone would do it.

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Using sf::Font as sprite sheet.
« Reply #6 on: December 08, 2014, 09:09:10 pm »
This is actually what Dwarf Fortress and some other games do. They originated as pure ASCII art games, yet people started modding the fonts to mimic tilesets rather than classic font glyphs.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Re: Using sf::Font as sprite sheet.
« Reply #7 on: December 09, 2014, 06:42:18 am »
But that is using bitmaps, right?
With bitmaps there is no reason at all to use fonts. Just show sprites as images like usual, it will be faster and allow for easy customizations, as pointed out by eXpl0it3r.

The only interesting aspect I see is to use vector graphics through font files. I am sure there are better libraries specific to render vector graphics out there, but with this font concept it would be possible to just plug the font on SFML and render the maps...
Well, I'm not willing to go through all that trouble to build the vectorized font files, so I guess we're arguing over nothing here.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
AW: Using sf::Font as sprite sheet.
« Reply #8 on: December 09, 2014, 07:42:48 am »
It's a bitmap font of sorts. So if they just assume that it is a font and some text, it would essentially be the same concept. SFML just doesn't support these kind of bitmap "fonts", thus one would have to resort to sprites.

If you want vector graphics, there are definitely better, easier snd cleaner ways, than to misuse the font system.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything