Text layout with GlyphLayout and ScreenViewport in libGDX

I want to post some code (and screenshots!) for laying out text on screen with libGDX. libGDX changed the way it handles text recently, so I wanted to write new code to accommodate, and I also wanted to make the layout work relative to the bounds of a Viewport. GUI code can get to be a hassle real fast, so to keep it simple, there’s just three options for horizontal layout (left, center, right), and three for vertical layout (bottom, middle, top). Here’s the code: Continue reading →

Loading IntMaps from JSON with libGDX

libGDX comes with a JSON serializer/deserializer that let’s you move data between JSON files and your Java data structures with just a few lines of code. This works mostly automatically, even with more complex data hierarchies.

We’ll start with an example of how this works, then how it breaks when reading an IntMap, and finally how to fix it.

For the working example, let’s take this class:

And this JSON:

Then you can deserialize the JSON to an ObjectMap with

Boom! Done. You can focus on data structures and data, and not worry about how to move them back and forth. Unless you try to pull the same trick with an IntMap…
Continue reading →