Text- more difficult than you’d think

Rendering text in OpenGL ends up being much harder than I thought it was.  It seems like such a fundamental thing, but it took me a while to really believe that there isn’t native functionality to do it.  I tried a few libraries which claimed to be a sort of “Drop it in and it will work” solution, but they either relied on deprecated OpenGL methods or they seemed they’d lack flexibility.  So I went with the plain freetype 2 library, which is far from a ready-made solution to the problem, but does handle reading font data.  Then a few hours more figuring out that opengl displays bitmaps incorrectly when the bitmap doesn’t have even dimensions (Like, a 50×30 would display right, but a 49×29 wouldn’t).  (Ends up there actually is a good reason, I had just never needed to know before now).

That was day one, I went to bed rendering a single letter to the screen as a texture, thinking I was pretty much done.

Day two I spent figuring out how to get the large amount of data required to put text on the screen as a string rather than a single letter.  Let me put it this way: you end up using every arrow labeled on this chart before you can know where to put it and how big to make it in every direction.  The nice thing, though, is that now I can effortlessly drop in new fonts just by feeding the font class the path to the .ttf file, and write to the screen with them.

Also, I’ve detatched the “view” from the rendering, so I can now change resolutions and keep the same parts of the game visible in the correct aspect ratio, or zoom in and out, or otherwise manipulate the view.  As long as it is done it right (which I almost achieved when I first wrote it), it’s super easy to move the view around with a single function call.  I haven’t actually done anything with it, but it’s definitely something that needed to be done.

ver11

This entry was posted in State of the Game. Bookmark the permalink.

Comments are closed.