I am new to C and I am wanting to make a video game. I was wondering what you all thought of this structure. My goal is to be able to easily access functions and data from a single struct. This way I only need to include one file everywhere.
#include <stdio.h>
#include "lib/manager/manager.h"
#include "lib/loop/loop.h"
#include "lib/utility/utility.h"
int main()
{
FN fn;
dev_loop_init(&fn);
dev_utility_init(&fn);
fn.util.log("test");
return 0;
}
A manager.h header file which I will include in every "module".
#ifndef DEV_MANAGER
#define DEV_MANAGER
typedef struct {
void (*start) (void);
void (*stop) (void);
} Loop;
typedef struct {
void (*log) (char* message);
} Utility;
typedef struct {
Loop loop;
Utility util;
} FN;
#endif
An example module, loop.h.
#include "../manager/manager.h"
#ifndef DEV_LOOP
#define DEV_LOOP
void dev_loop_init(FN *fn);
#endif
And the loop.c functions
#include "loop.h"
static void dev_loop_start(){}
static void dev_loop_stop(){}
void dev_loop_init(FN *fn)
{
fn->loop.start = &dev_loop_start;
fn->loop.stop = &dev_loop_stop;
}
If I do it this way then I only need to include my manager.h file everywhere in my modules. I still have to link all the headers in the main.c file but since they only contain the "init" functions that should be ok right?
As the project grows would this keep compile times reasonable? I read somewhere that using static is good for compile times.
You need to sign in to view this answers
Leave feedback about this