Hot Reloading

Hot Reloading

Recently I have been working on a C++ 2D renderer that I hope to use for a couple of projects, including the game I have discussed in previous posts. One project is a mini C++ graphics coding playground, inspired in-part by the simplicity of Processing and the instant-feedback of Professor Ken Perlin’s GLSL shader graphics assignments, in which you can type code into the web browser code editing window and see your changes immediately. Here, I wanted to expose the functionality of my renderer to allow users to create collages of moving, colorful shapes or test some new functions without needing to recompile. The video below shows my progress. I remove and modify the shapes drawn simply by editing the code of my draw function in an external text editor and saving the file. (I can also add any other C++ functions or code in the file that I may want.) If there is a compiler error, the drawing proceeds uninterrupted based on the previously correct code, but a red ‘x’ indicates that something is wrong. (Text rendering will come later and will allow me to show which line has the error.)

My implementation uses hot-reloaded dynamic libraries to create the illusion of instantaneous run-time code editing. The main program initializes all of the graphics plumbing and loads the setup and draw functions from the dynamic library with dlopen, dlsym and so on. Main passes pointers to the renderer and its api, which the dynamic library can use. drawis called in a loop. When Main checks and sees that the relevant files have been modified/re-saved, the dynamic library is recompiled and all pointers are reloaded. This is quick since the heavy initialization and compilation times come from Main, which isn’t recompiled.

For next steps, I would like to support textures, custom shader code, and the aforementioned code error highlighting to replace the red x. For now, I like the idea of using the dynamic library hot reloading system to make development easier and faster. Less waiting should amount to more productivity.

Karl Rosenberg
Posted on:
Post author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.