PDA

View Full Version : [SOLVED]Draw a class



DirkWillem
03-27-2012, 07:46 AM
Hello

I'm pretty new to OpenGL, and I understand the basics. Now, I have this Player class, with a function to draw a rectangle at its position. But how do I draw this, since all my drawing goes via a drawScene function, but I can't use it there. I also can't use a glutDisplayFunc, so how do I draw my player. The function is called DrawPlayer.

Thanks already;
DirkWillem

carsten neumann
03-27-2012, 08:25 AM
Why can't you call DrawPlayer from drawScene - I assume drawScene is the function you registered with glutDisplayFunc?

I'd say a fairly usual way to set things up with GLUT would look like something along these lines:



Player p; // global variable, holding player instance

void drawScene(void)
{
// buffer clearing, state set up, etc.

p.DrawPlayer();

// draw everything else that is visible
}

void init(void)
{
// initialize GLUT

glutDisplayFunc(&drawScene);

// register other event handlers with GLUT
}


The trick to not produce too messy code is to keep the number of global variables small, but since GLUT does allow registering member functions as callbacks [1] there is not really a good way around a few of them.

[1] GLUT is a C API, so it knows nothing about classes and member functions. Also the latter of course require a class instance to be called on and there is no way for GLUT to know which instance to use.

DirkWillem
03-27-2012, 08:57 AM
Thanks, that worked. I actually never knew you could create a class as a global variable. Thanks!