Archived

This topic is now archived and is closed to further replies.

Question on Organizing C++ Code for a Game

This topic is 5643 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

G''day all! I have a pretty straight-forward question for the forums. Yours opinions and ''why'' is much appreciated. When creating the structure for a larger program/game, how do you organize the different parts of your code? What do you put in headers, what do you keep in the main .cpp file? Also, how do you organize the declations and members of your classes? currently, I have everything in one big .cpp that looks something like this.. #INCLUDES ... CLASS DEFINITIONS ... GLOBAL VARIABLES ... FUNCTION DECLARATIONS ... MAIN() FUNCTION { ... } CLASS FUNCTION DEFINITIONS ... OTHER FUNCTION DEFINITIONS ... END Since this program is going to be getting pretty big soon, what should I break out where to make it more readable and easier to update later? Thanks! ~~~~~~~~~~~ Chris Vogel ~~~~~~~~~~~

Share this post


Link to post
Share on other sites
i always put each class in it''s own file (this might be a leftover habit from Java, but it makes sense since you then know exactly where to look if you want to change something).
also, it is good to use a separate file for functions that logically group together (i.e. put all your network code in a network.cpp file, or something like that).
this is just how i do it though, it doesn''t really matter as long as you (and anyone else you want to share your code with) can figure out where stuff is without grep-ping the entire project.

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
I tend to put each class declaration in seperate .h files and the implementation in a seperate .cpp file that includes the .h file. It''s also good to put functions related to the class in these files. If you have many functions that are related in some way you can put them in a seperate header. You should also consider using namespaces to group your code.

Share this post


Link to post
Share on other sites
quote:
Original post by ArchMiffo
You should also consider using namespaces to group your code.


I''ve seen the using namespace std; before... but I have no clue what it means. What do you mean by this?


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
Everything in the standard library is in the namespace std. Namespaces is a relativly new addition to C++ (although most compilers should support them by now) and was created to avoid name conflicts. Say that you have two diffrent librarys you are using in you project. In both these librarys there is a function void f(). If you try to use f in your code the compiler wont know which one to use so you will get an error. However, if the persons who wrote the librarys put everything in a namespace (say, lib1 and lib2) than you can tell the compiler which function to use:

using lib1::f();

f() // will call the function in namespace lib1

or you could tell the compiler in what to do in each case:

lib1::f() // calls f in lib1
lib2::f() // calls f in lib2

Read a new C++ book or do a search for online tutorials to get a better explanation (I know I''m not good at explaining things)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I used give each class/struct its own pair of h and cpp files, but I have found that in practice putting more than one in each file works a lot better. You don''t have to switch between files constantly. On the other hand I have a big class and I gave it two cpp files, one for initialization and private functions, the other is for the public functions. So I think I average around two class per file, it works a lot nicer.

Share this post


Link to post
Share on other sites
Thanks for the Replies everyone...

So in general...

Class1.h
--Class Declaration

Class1.cpp
#include Class1.h
--Class Definition

Class2... etc...


MainProg.cpp
#include Class1.cpp
#include Class2.cpp
etc...


That sound about right?


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes, except for the last piece. Include the headers, not the cpp files.

#include "Class1.h"
#include "Class2.h"

quote:
Original post by Radagar
Thanks for the Replies everyone...

So in general...

Class1.h
--Class Declaration

Class1.cpp
#include Class1.h
--Class Definition

Class2... etc...


MainProg.cpp
#include Class1.cpp
#include Class2.cpp
etc...


That sound about right?


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~



Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Yes, except for the last piece. Include the headers, not the cpp files.

#include "Class1.h"
#include "Class2.h"


Okay...

but if my class1.cpp file includes class1.h

Then if I include class1.h how does class1.cpp get included into the compile?

Or in my class1.h should I include class1.cpp?





~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
If you place each new class into its own file (good thing to do) I would also suggest that you name the file to exactly what the class is called. In other words: class CPoint should be found in the files called CPoint.cpp / CPoint.h.

Then make directories that hold bundles of files of the same type... I mean, if you''ve got CPoint, CRect, CVertex, CMath etc you should place them in a folder called Math (or whatever). When you''ve done that you should make your project file have the same "layout" as the file folders.

Personally I always use namespaces because it makes it easier to track the location of a class/constant/function etc, and each namespace has its own folder, like this:

Library/Basic/CCharString.*
Library/Basic/CWideString.*
Library/Basic/CPoint.*
Library/Basic/CRect.*
(etc)
Library/Basic/Basic.*

The last files (Basic.h and Basic.cpp) holds general constants and information that is used by the whole namespace. You could call this the "namespace base". And, as I said above, the file layout in my project workspace use the same folders as the files, ie:

Library
|- Basic
| |- CCharString.h
| |- CCharString.cpp
etc...

You might think this is to over-do it, but I''ve found it invaluable when it comes to introducing new programmers to the project because it''s easy for them to find the files and to understand their purpose. It also makes it easier to move classes between different projects.



My Stuff : [ Whispers in Akarra (online rpg) || L33T WAR (multiplayer game) || The Asteroid Menace (another game) ]

Share this post


Link to post
Share on other sites
quote:
Original post by Radagar


Okay...

but if my class1.cpp file includes class1.h

Then if I include class1.h how does class1.cpp get included into the compile?

Or in my class1.h should I include class1.cpp?



It defies logic at first. I thought the same thing a few days ago. But everythign is put togeather in the linking stage of building the program. That is why you have the header file, to tell the cpp files what functions have been defined.



It is foolish for a wise man to be silent, but wise for a fool.

Matthew
WebMaster
www.Matt-Land.com

All your Xbox base are belong to Nintendo.
All your GameDev.net base are belong to Myopic Rhino

[edited by - TheRealMAN11 on June 27, 2002 12:55:18 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Radagar
Then if I include class1.h how does class1.cpp get included into the compile?

quote:
Original post by TheRealMAN11
It defies logic at first.

Actually, it''s very simple. Every development suite has a project file/workspace of some sort, and you add files to that workspace. When you build the application/project, all source (.cpp) files in the project are compiled (the compilation process is interesting in itself, though the model could use some updating) and, if successful, linked together. So it''s all about the project file (or makefile on some platforms/compilers).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yes, simply don''t include cpp files. Also class definitions go in the headers.

Share this post


Link to post
Share on other sites
Ok.. I have my program setup like this, and I''m getting all types of compiler errors.

Class1.cpp
--Class1 Declaration
Class1.h
--Class1 Definition

Main.cpp
#include Class1.h
etc...


When I compile from the main.cpp file, the compiler reports errors in the included class files. Do I need to #include the standard files like "iostream" and "conio" in each .cpp file other than the main as well?

I''m getting errors showing that my CLASS definitions are not getting read in. It''s not recognizing class types.


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
Sorry to promote myself here, but if anyone wants a little more insight into the compilation and linking process, my article (the ''code files'' link in my signature below) explains it a bit, as well as trying to help you get around the various pitfalls you come across when you first try splitting your code up into multiple files.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

Share this post


Link to post
Share on other sites
Just to add something quickly. I also use the method of each class gets a cpp/h combo with the header being the prototype and the cpp being the implementation. However, in the header file I do this:

// Class1.h - prototype for Class1
#ifndef __CLASS1_H__
#define __CLASS1_H__

class Class1
{
//All the junk you want in your class
}

#endif //#ifndef __CLASS1_H__

That way, no matter how many times you include your header file, it''ll only get defined once


Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
Sorry to promote myself here, but if anyone wants a little more insight into the compilation and linking process, my article (the ''code files'' link in my signature below) explains it a bit, as well as trying to help you get around the various pitfalls you come across when you first try splitting your code up into multiple files.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]


Kylotan - Your article on splitting code into seperate files really helped. Thanks!

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites