1

Ok, so basically I need to create a simple game using Allegro 5 and C++. I want to split it into separate modules so it's easier to manage. My question is: what is the proper way to manage "moving" between different files? Specifically, I'd like to have a file called Menu.cpp, which would be the main file and then depending on what the user selects, it does things specified in other files, like Singleplayer.cpp or Options.cpp (is this even a good approach?). My idea is to do it like this:

//Menu.cpp
#include "Singleplayer.h"

int main()
{
int parameter;
if(user_selects_singleplayer) singleplayer(parameter);
return 0;
}


//Singleplayer.cpp
void singleplayer(int parameter)
{
...
handle_singleplayer
...
}

//Singleplayer.h
#ifndef SINGLEPLAYER_H
#define SINGLEPLAYER_H //or maybe #pragma once  ?

void singleplayer(int parameter);

#endif

I'd like to know if it's a proper way to do that, and if so, if I should have a main() function in Singleplayer.cpp (and how to change the file to make it work) and if I need to include Singleplayer.h in Singleplayer.cpp .

In my example i omitted some stuff like including allegro libraries, initializing more variables etc. to make the general idea clearer. Also I'd like to note that I can't use classes and streams (I know it's stupid, but it's a requirement as this project is a homework assignment for a programming class).

EDIT: the question linked by @Leiaz is relevant and answers to it are certainly helpful to understand how headers work, but my question was more about correct design approaches. Anyways, thanks for the answers, I will certainly read up on the subject.

Virritus
  • 39
  • 1
  • 7
  • Maybe you should download some opensource projects and get ideas from them. – ern0 Apr 29 '15 at 09:30
  • Read a book about design patterns and the software architecting it is really broad question. – cerkiewny Apr 29 '15 at 09:31
  • You should only have one `main` function (your program enters here). Designing your project as modular is good, but look at using `classes` rather than just functions (the key idea behind `OOP`). – Kvothe Apr 29 '15 at 09:33
  • 1
    There are already [some questions on this topic](http://stackoverflow.com/questions/9224537/c-c-header-and-implementation-files-how-do-they-work?rq=1). Look in the "Related" list on the right. Don't think of your program as "doing things in other files". Once it is compiled it doesn't matter which function is in which file. – Leiaz Apr 29 '15 at 09:35

1 Answers1

0

Here are some rules which should work in most situations:

  • Every cpp file has a h-file which is included within the cpp

    main.cpp includes main.h , Singleplayer.cpp includes Singleplayer.h

  • class definitions and function prototypes are in the header file, the implementation is in the cpp file

    h: int add(int x, int y); cpp: int add(int x, int y){ return x+y; }

  • Only include header files, never include cpp files

    avoids multiple definition problems, since headers have code guards (your defines) and cpp have not

  • Only include the files you actually need to reduce compile time

    Dont make an all.h that includes each header you have.

  • If possible, include the h files within cpp files. Only include it in the header, if it is needed there.

    Reduces compile time and avoids circular references

You should do some research on how to proper design a c++ project to understand the principles behind it.

maja
  • 17,250
  • 17
  • 82
  • 125