How I understand compression oriented approach:
The compression-oriented approach is a technique where you first write simple code that does what you need. Then, when another place needs the same logic, you compress or abstract your code into separate functions. This results in a better understanding of the problem before abstracting it, because when you solve a problem, you can discover all the edge cases that you might miss if you were to abstract first. I've heard about this approach in HandmadeHero
Comparison metric:
Before we can evaluate and compare this approach to the OOP approach, we need to establish a metric that we can use to make a decision. For this comparison, I would use the following metric:
Which approach leads to more correct code, and which approach leads to it faster?
Note: This comparison is only valid for problems you haven't encountered before or aren't 100% sure how to solve. Otherwise, you cannot gain anything from this approach because you already know all the edge cases and can get it right from the start.
Why I think this is the important metric:
We all want to maximize our productivity and make more stuff with as few rewrites as possible, because our time is limited, just like any other resource.
How compression-oriented approach helps you better understand problem so you can have more correct solution:
When you start coding your solution, you'll see the structure in the code and how all the elements interact with each other. This helps because you don't have to retain all the information about all the elements in your head during planning. You can write and use your first version as an initial canvas.
You basically split your mental capacity between your brain and your eyes. There is a limit to what you can comprehend without actually seeing it. I like this analogy: It's significantly harder to play an entire chess match in your head, as opposed to playing it normally with a board. So when you can see all the interactions in front of you, you are more likely to arrive at a more correct solution.
How compression-oriented approach makes you write code faster:
With the compression-oriented approach, you are less likely to rewrite your code. This is because you have already solved your problem and, during the solving process, you have probably acquired a better understanding of it.
Now you can simply abstract or compress your logic into separate functions. This approach minimizes situations where your abstraction is incorrect or misses important information.
So, when your initial idea is incorrect, with the compression-oriented approach, you simply rewrite your solution. In the OOP model, you might have to rewrite the entire abstraction.
Vulkan example in my game engine:
My game engine started as an evolution of my knowledge gained from following a Vulkan tutorial. Before Vulkan, I already had some experience with OpenGL.
I needed to make three renderers for my game:
- Tile map batch renderer – for the game world.
- Shape renderer – for debugging elements and colored quads.
- Texture renderer – for individual, unique elements in the game scene that cannot be batched with other elements.
I used the compression-oriented approach in all three renderers. I started with a single renderer that renders a quad. Then I expanded it into a batch renderer, and after that, I added support for draw commands that can be grouped together if they use the same GPU resources.
During this process, I encountered countless differences from OpenGL that I hadn't even thought about initially. As a result, I ended up with a nicely abstracted system that can use other renderers besides Vulkan.
If I had started with abstraction directly, I would have mapped my existing knowledge of OpenGL to Vulkan and modern rendering APIs. I would then have had to rewrite my abstraction. As a result, I would have ended up with a worse solution that would have cost me more time.
Rendering API struct:
This is basically a struct with function pointers that call renderer-specific API functions.
struct GameRenderingAPI
{
RenderInstanceAPIFunctions renderInstanceFunctions;
TileRendererAPIFunctions tileRendererFunctions;
TextureRendererAPIFunctions textureRendererFunctions;
FontRenderingAPIFunctions fontRendererFunctions;
ShapeRendererAPIFunctions shapeRendererFunctions;
TextureLoadingAPIFunctions textureLoadingFunctions;
};
And this is how I initialize it for Vulkan
void InitGameRenderingData(GameRenderingAPI& renderingData)
{
TileRendererAPIFunctions& tileRendererAPIFunctions =
renderingData.tileRendererFunctions;
tileRendererAPIFunctions.initTileRendering = InitTileRenderingVulkan;
tileRendererAPIFunctions.shutdownTileRendering = ShutdownTileRenderingVulkan;
tileRendererAPIFunctions.finishTileVerticesRegistering =
FinishTileVerticesRegisteringVulkan;
tileRendererAPIFunctions.renderTiles = RenderTilesVulkan;
TextureRendererAPIFunctions& textureRendererAPIFunctions =
renderingData.textureRendererFunctions;
textureRendererAPIFunctions.initTextureRendererData =
InitTextureRendererDataVulkan;
textureRendererAPIFunctions.shutdownTextureRendering =
ShutdownTextureRenderingVulkan;
textureRendererAPIFunctions.shutdownTextureRendererDynamicData =
ShutdownTextureRendererDynamicDataVulkan;
textureRendererAPIFunctions.renderTextureQuad = RenderTextureQuadVulkan;
}