Gtk is weird

Fixing a weird bug in my game engine

Published:2026-02-03

Recently, I updated my game engine, stellar-enigma[1], to SFML 2.5 to SFML 3. This SFML update added many useful features and improvements, such as better angle handling and many new vector utility functions. There was one change, though, that caused me a headache in a weird and roundabout way: In SFML 3, when initializing certain container objects, like Sprites and Sounds, you are required to supply the data that it will contain. For Sprites, this is the Texture. For Sounds, this is the SoundBuffer.

This is a good idea. RAII is a safer way to deal with these sorts of objects, and helps to prevent weird bugs. This was only a problem for me because I structured my initialization differently. I initialized the Sprites with no texture, then gave them a texture during rendering.

I circumvented this problem by initializing all Sprites with a blank Texture. This seemingly worked. It's certainly not an elegant way to fix the issue, but it works.[2]

It worked, until I looked at the editor. The editor is a gtk application used for editing stellar-enigma levels. Currently, it only works with Objects, but I have plans to expand its scope.

One of the reasons I wrote my own editor program, rather than using an extant one, was to implement special features. I've previously spent time modding the Super Mario Galaxy games, and a frequent issue I faced was invalid object configurations. SMG will crash if any object is setup incorrectly. There's a way to enable a stack trace, but that's all the debugging information you get. Finding the issue can be a real pain, as you have to open the emulator, get through the title screen, file select, world map, and then play the level in question each time you make a change.

To try to mitigate this issue, I decided to implement object verification into my editor. In brief, each Object-derived class contains a verify function. This checks all the properties to see if they are setup correctly. If not, it returns an error. During level loading in the engine, each object's verify function is called, and level loading is only completed if all objects return a success.

To verify the objects from the editor, I construct all the Objects, then run the verify function. During the SFML3 migration, I created a null sprite to use to construct each Object.

This seemed to work fine. Until I tried to verify a level. Upon doing so, the editor promptly crashed, complaining about GL Contexts. I spent a while troubleshooting this issue. My first thought

Eventually, I figured out that I needed to deactivate the Gtk gl_context using a gtk function, then activate the SFML context using a sfml function.

I did this using

Footnotes

[1] https://github.com/xavenna/stellar-enigma (return)

[2] Although stellar is much more elegant than its predecessors, it is still kind of a janky mess. (affectionate) (return)