I'm learning C++ by making a small game. As the code grows I find myself spending more and more time on finding variables and functions. To avoid this I split it into multiple files, all files get imported to a single file in this order:
- utils.cpp: Contains macros, aliases for variable types, and some general purpose functions
- win32.cpp: Contains the WinMain entry point and the game loop which calls update() and from game.cpp
- structures.cpp: Definitions of most structs and classes used in all files below
- globals.cpp: Variables like map_size etc, accessible by all functions
- renderer.cpp: functions for drawing by modifying pixel values in a buffer
- game.cpp: more game-specific functions, contains a function to initialize the game and the game logic update() function which makes calls to renderer.cpp
I like having a lot of variables in the same place, like in globals.cpp, because I edit their values frequently while testing.
Now this works ok, but the order of the functions and objects and variables causes problems. For example, some structs need access to global variables while some global variables refer to structs. Sometimes it becomes a big puzzle of where to put what code.
Is there a better way to structure things to minimize this sort order-dependance?