Dark Bit Factory & Gravity
PROGRAMMING => C / C++ /C# => Topic started by: Stonemonkey on April 03, 2007
-
I'm wanting to split my source into some smaller files as it's getting a bit on the large side but am at a bit of a loss as to how exactly to do this. Any suggestions on the best way to go about it, just now I'm after something like graphics_buffers,2d_functions,windows,my_main and possibly all the structs in 1 file.
-
You can put the typedefs in a header file and include that in the many cpp files you're going to create. I guess how to split things up comes from experience, but you can try to keep things together in groups. Like in my pumpkin source there's a math file, a geometry file, a sprite file, a file for each fx etc. Each one of these has a header .h file that declares the types and functions that should be accessible to other part of the program. I like to keep things that are platform specific (bmp loader) separate from things that don't depend on the OS (maths) from things that depend on a specific API (OpenGL). It's not always totally achievable, but it makes it a load easier to port code to other platforms - eg. I ported ps2yabasic to the PSP in an afternoon.
In visual studio, you can right click the project in the solution browser and select 'Add New' and then pick C++ or .H files and they'll be added to your project, then just cut and paste bits 'til it works.
Jim
-
Thanks Jim, I can see a bit of messing around deciding where to put things coming up.
-
I'm a bit confused about what to do with global variables, where should they go?
-
Ideally you don't have any global variables...
If you must, you can use get/set functions to read static ones like this
file1.cpp
#include "file1.h"
static int how_many_legs_have_i_got = 5;
int get_num_legs(void)
{ return how_many_legs_have_i_got; }
file1.h
int get_num_legs(void);
file2.cpp
#include <stdio.h>
#include "file1.h"
int main(void)
{
printf("I've got %d legs\n", get_num_legs());
}
If you must have global variables, you can do it like this
file1.cpp
int nurmy_nurmy = 7;
file2.cpp
#include <stdio.h>
extern int nurmy_nurmy;
int main(void)
{
printf("nurmy %d\n", nurmy_nurmy);
}
If you're using classes, wrap all the globals into one class and use the 2nd technique to pass a pointer to the one global variable.
Don't put the globals in a .h file. That's crappy programming at best.
Jim
-
Thanks Jim, they were (well some of them anyway) variables from your windows example, quit and freq and i added some mouse stuff too and put that all into a struct but couldn't see any way other than making them global to get at them from the callback function.
-
@Stonemonkey:
When you seperate your functions and keep them in external files which you include
you should think about the future - in which way you can reuse certain function again
in other projects. Then you might form group of functions as Jim already said.
-
Thanks Benny, I've been working on it as a single file so far but it's getting a bit too large so that's what i'm doing.
-
Should help the compile times too - VS will only rebuild the files that you change and the ones that depend on the files that you change.
Jim