Hi,
I'm working on a pacman clone in C++ and I'm using GLUT for the graphics. While I was coding the game, I was using global variables to pointers to objects pacman which is of type Pacman, and 4 pointers to objects of type Ghost. I know that it is bad practice to use global variables but I haven't been able to think of a better way to structure my program yet and I'm looking for ideas.
I have a driver called Manager.cpp which contains the main function as follows:
Code:
int main(int argc, char **argv) {
glutInit(&argc, argv); // initialize GLUT
glutInitDisplayMode(GLUT_RGBA); // set RGBA display mode
glutInitWindowPosition(325, 0);
glutInitWindowSize(600, 600);
glutCreateWindow("Pacman");
init();
srand((unsigned)time(0)); //for random number generation
Game game = new Game();
glutDisplayFunc(displayScene); // set the rendering function
glutTimerFunc(0,Timer,0);
glutSpecialFunc(arrow_keys);
glutIdleFunc(update);
glutMainLoop(); // process events and wait until exit
return 0;
This file (Manager.cpp) is the one which contains the global variables mentioned above and the global variables are being used in the callback function listed in main (i.e. displayScene, arrow_keys, etc).
I want to continue to have access to single instances of the 5 pointers to the characters that are currently assigned to global variables but I'm not sure what the best way to do this is.
Notice the line 'Game game = new Game();' in main(), I added this recently because I thought that a good way to do what I want would be to create another class, Game, which has 5 static pointers to my 5 characters as members. Would this be a clean way of solving my problem? Even if I could do it this way, how can I make the variable game available to all the functions in Manager.cpp (e.g. displayScene, arrow_keys, etc) if I don't want to make it a global variable?
I hope my question is clear, if not please ask for any clarification that might help. Thanks!
C