Jump to content
  • Advertisement

Archived

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

Radagar

Question on Organizing C++ Code for a Game

This topic is 5983 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
Advertisement
Anyone?

Even general suggestions or pointers to articles that explain it are fine.

~~~~~~~~~~~
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
krez - Thanks!

Any other suggestions or ideas? Other ways that people organize their code and files?

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

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!