I have a program, it starts in a winmain which calls run_game in its loop.
Because I treat run_game like a main function I am having to define many global variables.
The win main is in one file and everything is in another .cpp file. It started as a tutorial from a book and I have kept adding to it for a while now. Because my code is getting so big I want to break it into several .cpp files but I have a few question about doing so.
-
I see a lot of people having one giant source file. I am self taught so not sure if this has a benefit or is just style.
-
People say globals should be avoided so how can I make variables I declare in functions available in other files.
-
Is my program in a function approach a bad way of doing it as my plan was to break my run_game function into several smaller functions, each function would than be a state of the game.
I know people may complain this is vague so if you vote to close please comment on how I could make the question clearer.
Edit: I do know how to use classes and have 4 classes and 3 structs in the header file for this program, but an instance of a class acts the same as a variable so does not solve the problem of scope.
Its usually better to have smaller well defined files instead of one giant source file. Each file would either have a single C++ class definition or a set of interrelated functions. A rough guideline is no more than a few hundred lines of C++ code, but its subjective and difficult to quantify. You may often need much less or a lot more.
You need to pass those variables as parameters to those functions. For example if run_game calls foo, and foo needs to know the current score, pass the score in as an argument, like so:
This is known as “pass by value”, foo is getting its own copy of score. So if foo updates score, the caller’s copy won’t get updated. You can let foo update score by using pass by reference as so:
Now you may ask, well I’ve got 50 variables, how could I possibly pass all 50 into foo? This is where the hard work of software engineering comes into play. You have a couple of options. In any option, you want to think about how pieces of variables group together. You might find you’re always passing the same 5 variables into the function foo. Maybe its some representation of the players accomplishments so far in the game. The simplest way to gorup these together is with a struct:
Now you can declare variables of this type, set the specific fields, and pass them around:
The next step in abstraction would be wrapping a set of variables and common pieces of functionality into a class. This lets us easily share common functionality and enforce a safe, well way for manipulating these objects–as defined by that object’s member functions. So for example if we had a player class.
Then the code above becomes:
I hope you can see that there’s a lot of ways to think about these problems, and its not easy to figure out how to seperate things out. This answer barely scratches the surface. I recommend a good course or book in software engineering. This stuff is hard. Really smart people with decades of experience struggle with the best way to organize and write software. But its great to think about this stuff. Its good to work hard to find ways to improve your code so you’ll be able to make sense of what you’re trying to do today, tomorrow, and six months from now.