Toasty's blog

A naive tilemap renderer

One of the biggest mistakes I've made so far is writing code. Lots of people have told me over the years that I should Just Use This Engine, for some definition of This. I've tried using some engines, and some of them were even very good (LÖVE and Godot spring to mind). But I don't often find that entertaining. And this project is not about me making a game for the players to enjoy. It's about me having fun tinkering. If the end result is playable, great. If it's fun, that would be remarkable.

The renderer

My tilemap renderer sucks. I forget the details, but I do recall that it does some smart things, and a lot of daft things.

For the sake of entertainment, here's one silly thing it did while I was working on it.

A failed attempt at rendering a dungeon using tiled 2D pixel art sprites. The image should depict a square dungeon containing a box; some posts or wands (it's not clear, to tell you the truth); and a couple of fountains. Due to an offset error, however, the walls become staggered and form a jagged diagonal line across the image, with a sandy floor and scattered items on both sides.
The woes of off-by-one errors. Uses Kenney's Tiny Dungeon sprites.

This particular issue is fixed now, but hopefully now you understand the level of quality we're dealing with.

This is okay, probably

The appearance of the game will definitely change in future:

Right now, fully functional gameplay is the priority, so I can experiment more with the puzzles. Appearance is important, but the game will only be fun if the puzzles and gameplay are solid.

Mistakes

I wrote the renderer using OpenGL. Two mistakes I made:

  1. writing a renderer at all;
  2. doing it naively, without accounting for pixel art being, well, pixelly.

I didn't need to write a renderer. I could have used an engine, a library, or just done the obvious thing, given that I'm using SDL3, of using SDL_Renderer, SDL_Texture et al. Probably the output would have looked better with the latter. But I felt that this custom renderer would give me more flexibility later on (questionable), and also, I just wanted to.

Embracing bugs

The worst remaining problem with this naive implementation is that it represents pixel locations using floats. This introduces inaccuracies in the location of rendered sprites, and so we get tears in the image (the thin lines appearing between tiles).

An image of a pixellated square dungeon containing a couple of fountains, some stones and a box. Clearly the dungeon is composed of tiles, because you can see tearing between some of the tiles: thin lines where the dark turquoise background cuts through the dungeon image. Some of the flooring is also renderered using un-rotated tiles, causing some odd, dark lines of shadow in the top-left corner.
Tearing (and a few un-rotated tiles). Uses Kenney's Tiny Dungeon sprites.

I haven't fixed the tearing yet. Possibly the easiest fix would be to just render pixels to a texture and then perform scaling. Again, this could have been done easily with e.g. SDL_Renderer et al., but also again, I like making my life hard and gameplay is more important. So, let me apologise now for all the terrible screenshots you poor readers will be subjected to in future.

Footnotes

  1. I don't have a lot of spare time, so my projects move very slowly.