I'm currently trying to rewrite a small engine using a style which is more towards C than C++, and at the same time I'm trying to figure out a way to keep the platform specific code separated from the platform-independent code.
The basic idea is to have a folder structure with one folder containing all the generic code and one folder per platform where all the platform-dependent code will live. Each platform will then include the generic code where needed. Super simple structure and it's very easy to see which files belong to which platform. The whole point of this is to get rid of randomly placed #ifdef PLATFORMX spread throughout the code.
Do you have any tips for how this can be done in a nice and clean way?
So far I've got these ideas:
The platform-independent code can define and use a basic struct which contains data that must be present on all platforms. For a sprite object it could be stuff like position, rotation, scale, list of animations etc.
The struct could then also define a void* platformData at the end, where the platform layer can define the data it needs (file handles, render info, etc). It could also work with platform-specific sub-structs, where Win32Sprite contains a Sprite object at the top plus the platform-dependent info. So casting a Win32Sprite to Sprite would be fine since both have the same data layout at the start.
For a potential Sprite::render() function, it could just push the generic sprite info (material, position, etc) onto a render queue which can then be processed and rendered to screen on the platform-dependent side however it sees fit.
The loading code would however need to be implemented per platform, so there's really no way to implement a Sprite::load() function without it being platform-dependent. For these kinds of functions I really have no idea how to properly do it. One way would be to define a function in the platform-independent code that must be implemented in the platform-specific code, but in that case it would be nice if those "required functions" could be kept in the same header so it's easy to see what a platform layer is required to implement.
If you have any tips or links to articles, I would greatly appreciate it!