Quote: Original post by adam23:
I am working on a game engine that encapsulates everything, DirectInput, DirectSound, Direct3D, and so on. Right now I have seven header files and five cpp files. What I was trying to accomplish was one point of contact with the engine.
This is bad, m'kay...[smile]
In your example you create a header file called "Engine.h" which includes many other header files. Any time you modify one of those files, you will be forced to recompile anything that included that file AND anything that contains "engine.h"...which if I understand you correctly will be many files. In essence, changing any of those header files means a recompile of your entire code-base, whether or not the source file actually used the class, function, etc...which you modified within the header file.
Additionally, Engine.h is now a fairly large header, and the entire contents of engine.h will be included EVERYWHERE you include engine.h.
What you're talking about here is very close to a pre-compiled header, without the benefits of being a pre-compiled header; ie. pre-caching of symbols. As well, even when using precompiled headers it is very uncommon to include your own header files, as chances are high you will modify those files frequently, thus requiring a full recompile of your entire code-base. It IS however common to include system libraries in your PCH, such as the DirectX and Windows headers you show in your example.
Quote: Original post by adam23:
See in this situation ResourceManager needs LinedList, Font needs Geometry.h. Am I doing this correctly by having all my includes in the header for the engine and then linking everything here. How do I get around worrying about the order?
Unfortunately no. The first thing is to determine whether ResourceManager needs LinkedList or just a reference or pointer to a LinkedList. If you declare a member variable of type LinkedList, but it is either a pointer(allocated on the heap) or a reference (allocated externally), then you don’t need to #include LinkedList.h, you only need to forward declare class LinkedList at the top of ResourceManager.h.
However, if you do need a local variable of type LinkedList allocated on the stack, within ResourceManager, then #include it IN ResourceManager.h. This removes the problem of "order mattering" when it comes to header files, and means no matter where you #include ResourceManager.h you're guaranteed that LinkedList will be declared.
The same holds true for the relationship between Geometry.h and Font.h. If Font uses Geometry.h then #include it. If it just references the classes, etc...inside of Geometry.h, then use forward declarations instead.
Quote: Original post by adam23:
I could include LinkedList in ResourceManager and Geometry in Font, but I also need these classes for variables in Engine.h. For example I am creating a linked list of states as a private member of the Engine class.
Again, do you NEED LinkedList in Engine.h, or do you just need a reference? If so..forward declare. As well, what is Engine.h? Is there a corresponding Engine.cpp? If Engine.h is just a "Superheader" then you need to rethink your design as I outlined before. But if Engine.h just contains a class which needs LinkedList, then no worries, just #include "LinkedList.h". As long as you have #pragma once (Visual Studio only) or include guards in each of your header files, then it doesn’t matter how many times you tell the compiler to include a header within the current source file, it'll ignore repeated attempts.
To help you get some perspective, its quite common to have 1 header file for each source file, a precompiled header, and that's about it. If you find you have far more header files than source files or vice-versa, you might consider re-evaluating your dependencies.
In general, '#include' in Source Files, not in header files. You help prevent dependency chains, and you keep your code efficient and compiling quickly. However, if you NEED the contents of a header file within another header file, then include it, don’t create an "order matters" dependency within your source files.
Cheers!