Original Post
Hi, I've been coding a pacman game in C++ using the openGL GLUT library. Right now I've got three main files 1. Pacman.cpp 2. Ghost.cpp 3. Manager.cpp Pacman.cpp defines the class pacman which I used to instatiate pacman. Ghost.cpp is a base class I used to create ghost objects. Manager.cpp is not a class but is the driver code (contains the 'main' function). Right now, Manager.cpp has global variables declared as follows:
Pacman * pacman;
Ghost * blinky;
and my main() looks like this:
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
pacman = new Pacman();
blinky = new Ghost();
glutDisplayFunc(displayScene); // set the rendering function
glutTimerFunc(0,Timer,0);
glutSpecialFunc(arrow_keys);
glutIdleFunc(update);
glutMainLoop(); // process events and wait until exit
return 0;
}
My issue is that I don't want to use global variables but I don't see how else I can organize the files since my glutDisplayFunc callback procedure (displayScene) needs to access the global variables pacman and blinky to call their draw functions... i.e.
void displayScene()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
Map::drawMap();
pacman->draw();
blinky->draw();
glPopMatrix();
glFlush();
}
Any ideas as to how I might be able to organize these files (or how I could use a new class) so that I can avoid using the global variables? Note that the reason I'm using global variables in "Manager" in the first place is because I need to be able to access pacman and blinky (and soon the other 3 ghosts) all in one place so I can compare their variables (positions, states, etc.). Thanks, Omelas UPDATE: I tried creating a class Game as follows: Game.h:
#ifndef GAME_H
#define GAME_H
#include "Character.h"
#include "Pacman.h"
#include "Ghost.h"
class Game
{
public:
Game();
void displayScene();
private:
Pacman * pacman;
Ghost * blinky;
/*Ghost pinky;
Ghost inky;
Ghost clyde;*/
};
#endif Game.cpp:
#include "Game.h"
using namespace std;
void Game::displayScene()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
Map::drawMap();
pacman->draw();
blinky->draw();
glPopMatrix();
glFlush();
}
Game::Game()
: pacman(new Pacman()), blinky(new Ghost())
{
}
then I removed displayScene from Manager.cpp since its now in Game and I modified main as follows:
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;
glutDisplayFunc(game.displayScene()); // set the rendering function
glutTimerFunc(0,Timer,0);
glutSpecialFunc(arrow_keys);
glutIdleFunc(update);
glutMainLoop(); // process events and wait until exit
return 0;
}
but I get an error saying: error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from 'void' to 'void (__cdecl *)(void)' 1> Expressions of type void cannot be converted to other types Any idea how to get around this? Am I on the right track? [Edited by - Omelas0469 on December 17, 2008 9:50:16 PM]