Handmade Hero»Forums»Code
The_8th_mage
69 posts / 1 project
Write text to buffer
I recently made a small game using the *buffer format casie is using, independent of the os. i found myself wanting to add text to the buffer, and i don't know how to do it in a way that doesn't involve too much the os. anybody has any ideas?
Neil Blakey-Milner
45 posts
Write text to buffer
e1211
I recently made a small game using the *buffer format casie is using, independent of the os. i found myself wanting to add text to the buffer, and i don't know how to do it in a way that doesn't involve too much the os. anybody has any ideas?


If you already have something to draw rectangle bitmaps into the main bitmap, it should be fairly easy to do bitmap fonts.

You need something like FontBuilder to make the .bmp files and the metadata (ie, location of characters, which character, and so forth). FontBuilder supports mostly XML formats (which obviously are harder to write code to parse yourself), so I use the binary format ZFI. Here's an example of the font code itself: https://github.com/nxsy/hajonta/blob/master/source/hajonta/font.cpp

This currently only supports ASCII, but doing more is mostly just a matter of writing a hash table for lookup and not a fixed-size array with the character as the key.

Good luck!

Neil
Patrick Lahey
67 posts
Write text to buffer
A lot of people find the stb libraries easy to use and helpful. It looks like stb_truetype.h could fit the bill.
70 posts
innovation through irritation™
Write text to buffer
Edited by d7samurai on
nxsy
This currently only supports ASCII, but doing more is mostly just a matter of writing a hash table for lookup and not a fixed-size array with the character as the key.

I use a simple fixed-size lookup table with the character code as the key even for Unicode (reserve an array with 65536 'glyph description' elements and index directly into it). I don't care if most of it is empty - the memory footprint is negligible anyway.

My only concern was locality of reference, but I figured the hashing algorithm + collision walking would be as costly (but I haven't measured).

Edit: I lied. I just checked to refresh my memory and I actually use a set of tightly packed SOA style arrays, each containing individual glyph properties (size, kerning, vertical offset, UV coordinates etc) for the glyphs that are actually present in the font, and look up the correct index into them from the Unicode table (which is just 2 bytes per element = 128k). The main point remains the same, though..
Roderic Bos
70 posts
Write text to buffer
There was a post on the forum about adding debugtext on screen:

https://forums.handmadehero.org/i...p/forum?view=topic&catid=4&id=217

it suggested sysfont.hpp which works fine on my test project using a similar setup as casey.

http://www.mattiasgustavsson.com/temp/libs/sysfont.hpp
Chris
21 posts
Write text to buffer
I've also done something similar in the past:

https://github.com/ChrisG0x20/five-pixel-font