0

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?

Jompa
  • 59
  • 1
  • 7
  • Forward declarations: https://stackoverflow.com/a/4757718/449722 – g01d May 03 '21 at 15:35
  • One should never import .cpp files. Instead move declarations to a header and then import the headers. As a bonus, small changes should compile faster. – Mooing Duck May 03 '21 at 16:22
  • 1
    "I'm learning C++ by making a small game" ok, but please consider also a good foundational book. – MatG May 03 '21 at 17:44
  • @MatG of course. Any suggestion on where to start? Syntax is easy to find online but I'm looking for something that goes into "best practices" too – Jompa May 03 '21 at 17:57
  • 1
    @Jompa I'd start with *A Tour of C++ (Stroustrup)* very easy and fast to read. That gives a good base. Then, while you develop: The C++ Programming Language (Stroustrup). C++ Coding Standards (Sutter,Alexandrescu) and [c++ core guidelines](https://github.com/isocpp/CppCoreGuidelines) – MatG May 03 '21 at 19:25

1 Answers1

0

I think most people would advise you to stay away from global variables as much as possible. From an object oriented perspective, I would probably organise variables into classes which they are associated with and then create a header file (.h) for each class and import the header files where I need them. So for instance:

You may have a class "MapObject" or "MapBuilder" which has member variables such as "map_size", you can access these through an instance of the "MapObject", say "mapObject", whenever you want (as long as there exists an instance of the class) by calling "mapObject.map_size = newValue". As for finding these values for editing the initialised values, you have essentially organised your constants in a filing system. You have a .cpp and header file called MapObject, which you know has all map object related constants in it. You can probably search through your files in your IDE for say "map" to get map related constants.

This should make it easier to manage the ordering as well because you know that the constants only exist when they are needed (ie. when there is an instance of the object which uses them).

You could also look into a design pattern called a "singleton" which may be useful to you in some cases.

Relstoch
  • 21
  • 4
  • 2
    Instead of singletons, the advice should probably be to use header files – Mooing Duck May 03 '21 at 16:23
  • Hmm, yeah now that I read it again. I kind of assumed OP was already using header files if he was building something this complicated, and that the problem was a design pattern one. I do mention how I would structure things with header files though. Should I delete my answer @MooingDuck? – Relstoch May 03 '21 at 17:35