Sign in to follow this  

Splitting a game into multiple *.cpp files

This topic is 4857 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

How would I go about doing this to keep my code from being cluttered? For example, having all the sprite functions in one file, all the physics functions in another, etc.

Share this post


Link to post
Share on other sites
If you're using OOP, then make a class in each cpp/h file. So if you need a sprite class, you'd have a Sprite.cpp/h pair. In the header, you include any other files that class will need. Then it's up to you to make a good class design.

If you aren't using OOP, then I'd just try to keep related functions in the same file as you said. You're going to have large files, though.

Share this post


Link to post
Share on other sites
Generally you should do what Sante05 said. There is an article about this stuff.. it's very easy once you get used to doing it. : http://www.gamedev.net/reference/programming/features/orgfiles/

But if all you want to do is access a function from another file, you just have to put a function declaration in the file that you want to use it in.

Here is a VERY simple example to get you started. So say you have two files.. file1.cpp and file2.cpp..

file1.cpp

#include "stdio.h"

void myFunction( ) ; // function declaration

void main( )
{
myFunction( ) ;
}




file2.cpp

#include "stdio.h"

void myFunction()
{
printf( "Hello other file!" ) ;
}



Hope that helps!

Share this post


Link to post
Share on other sites

This topic is 4857 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this