At one point while making the engine, I realised that I had to hardcode everything in C++. All texture loads, entity creation, shader loads, materials…

I decided therefore to create a JSON parser (downloaded from github) to parse files to create certain objects like entities.

However, then, I decided to merge every object creation like textures and materials to be via JSON files.

But this was long

I needed to create a somewhat generic way to create JSON parsers for each file. So I wrote this:

template <typename Functor, bool ParseSub = true> class json_loader;

It takes a Functor type which gets called for each JSON node under the top of the hierarchy. ParseSub is for whether or not to call specific functors derived from Functor mapped to an unordered_map to parse elements within the nodes at the top of the hierarchy.

For example for entities.json:

"ground": {
  "direction": {
    "x": 1.0,
    "y": 0.0,
    "z": 0.0
  },
  "model_matrix": true,
  "position": {
    "x": 0.0,
    "y": 0.0,
    "z": 0.0
  },
  "render": {
    "material":  "water",
    "model": "plane"
  },
  "scale": {
    "x": 20.0,
    "y": 20.0,
    "z": 20.0
  }
},

The parser would go through everything at the level of "ground". For each, a lambda gets called and from then, the functors get called for each sub-node. For "direction", a functor gets called to assign the entitiy the direction specified.

The ultimate goal, is to make almost everything loaded in from JSON files, or some data file.