Author Topic: organising projects?  (Read 4946 times)

0 Members and 1 Guest are viewing this topic.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
organising projects?
« 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.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: organising projects?
« Reply #1 on: April 03, 2007 »
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
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: organising projects?
« Reply #2 on: April 03, 2007 »
Thanks Jim, I can see a bit of messing around deciding where to put things coming up.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: organising projects?
« Reply #3 on: April 04, 2007 »
I'm a bit confused about what to do with global variables, where should they go?

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: organising projects?
« Reply #4 on: April 04, 2007 »
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
Code: [Select]
#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
Code: [Select]
int get_num_legs(void);

file2.cpp
Code: [Select]
#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
Code: [Select]
int nurmy_nurmy = 7;

file2.cpp
Code: [Select]
#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
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: organising projects?
« Reply #5 on: April 04, 2007 »
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.

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: organising projects?
« Reply #6 on: April 04, 2007 »
@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.

[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: organising projects?
« Reply #7 on: April 04, 2007 »
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.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: organising projects?
« Reply #8 on: April 04, 2007 »
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
Challenge Trophies Won: