Splitting a game into multiple *.cpp files

Started by
4 comments, last by Pogo708 19 years, 7 months ago
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.
---"What? What is it? Is it the monkey in the pants? It's the monkey in the pants, isn't it. Don't give me that look."
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.
-----DevZing Blog (in Spanish)
But how would I have the functions in multiple files? Sorry if this is a stupid question, I'm still kinda new to c++.
---"What? What is it? Is it the monkey in the pants? It's the monkey in the pants, isn't it. Don't give me that look."
You might want to start by reading this article which explains the basics of mulitple source files in C++.
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 declarationvoid main( ){  myFunction( ) ;}



file2.cpp
#include "stdio.h"void myFunction(){   printf( "Hello other file!" ) ;}


Hope that helps!
Thanks, all of this helped a bunch!
---"What? What is it? Is it the monkey in the pants? It's the monkey in the pants, isn't it. Don't give me that look."

This topic is closed to new replies.

Advertisement