Jump to content
  • Advertisement
Sign in to follow this  
Pogo708

Splitting a game into multiple *.cpp files

This topic is 5134 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
Advertisement
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
But how would I have the functions in multiple files? Sorry if this is a stupid question, I'm still kinda new to c++.

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
Sign in to follow this  

  • 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!