Generate Source Files With CMake

Sometimes when writing c++ programs I have some textual data that I want to use in the program that I want to be part of the executable instead of in flat files next to the executable. However while developing it is far easier to have the textual data in separate files - instead of in source files as strings.

But then you suddenly have the issue, how do you create c++ strings out of textfiles at runtime? A quickly hacked c++ program and some cmake magic off course.

This c++ was hacked together within thirty minutes or so.

The header file referenced in the compilation program above is this simple:

The final piece of the puzzle is making CMake generate the implementation of the above interface whenever a file in the dirs characters, places or scenes change.

We have hardcoded the file paths - you could probably make CMake do this dynamically - but I like to have my dependencies explicitly named.

Now the loader class from this library can be written in many ways - I usually do a loader that tries to find a file by name (essentially its path) in the map defined - and fallback to trying the file system. This makes it easy to quickly iterate on a new file - and the add it to the compilation step once it is done.