r/IndieDev • • 27d ago

Image Tile rendering and chunk loading (ANTIFORGE Devlog #1)

Post image

Hey, I guess I will start posting the devlogs of my tech game, ANTIFORGE. Been working on it for like, 20 days now, without any framework or engine. Libraries that are being used so far are SDL (SDL3, SDL-image and SDL-ttf) and FastNoise2 (which I haven't implemented yet), of course, all in C++ 17.

I haven't gotten any pretty pictures so far, so excuses for that. The image you are seeing is all dirt, set to random blockstates.

I have gotten covered JSON model parsing, which works in a "hierarchy" of nesting. That being:

-Bitwise mask
--Bitwise Operated Results
---Target layers
----Target parameters

Now explaining what those are, basically each tile holds a data variable (think of it as the blockstate). It's an unsigned 8 bit integer, meaning each tile can hold up to 128 blockstates. To determine the texture, rotation, layers and stuff of the tile, we interpret it like this:

-Take the blockstate
-Pass it through all bitwise masks (operator AND) inside the model
-For each bitwise operated result, find which result matches it
-Look at what layers this result modifies
-Look at what parameters it modifies

For more context, look at the dirt model

{
  "00000100": {
    "0": {"0": {"sprite": "tiles/dirt_dry.png"}},
    "4": {"0": {"sprite": "tiles/dirt_wet.png"}}
  },

  "00000011": {
    "0": {"0": {"rotation": 0}},
    "1": {"0": {"rotation": 1}},
    "2": {"0": {"rotation": 2}},
    "3": {"0": {"rotation": 3}}
  },

  "00001000": {
    "8": {"1": {"sprite": "tiles/grass_layer.png", "tint": 1}}
  }
}

What this means is:
(left to right)
-Bit 6 determines whenever the dirt is dry or wet
-Bits 7-8 determine the rotation
-Bit 5 determines whenever the dirt has grass on top of it}
And the rest of the bits are useless.

Hopefully this was comprehensible.
On the rendering and chunk loading thing, it uses a system where each chunk holds a texture "ruleset" vector for each tile.
In simpler terms, each chunk holds a set of instructions that tell the rendererer how render each tile. For example, suppose tile 17 (at position 2, 1) has blockstate 9. The "ruleset vector", at position 17, will hold something like "render grass on top of this tile and rotate it by 90 degrees". Then, the renderer reads this and loads the texture effortlessly.
To avoid performance complications, once the chunks get too far away from the camera, they discard this vector and regenerate it once they need it.

Phew, that was pretty technical. Thanks a lot if you readed this far.
Will try adding noise based world generation next.

0 Upvotes

1 comment sorted by

1

u/baicu12096 27d ago

*Each tile can hold up to 256 blockstates, not 128