Jump to content
  • Advertisement
Sign in to follow this  

Why can't my class find these callback functions?

This topic is 2774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

I've been working on OpenGL tutorials and now am moving what I'm learning into a class, but I ran into a problem. When I call glutDisplayFunc and glutReshapeFunc they can't find the callback functions (even though they exist). I'm really at a loss here and I either have a syntax error (even though everything works when I take it outside the class) or there is something going on under the hood that I am not aware of? Any help would be greatly appreciated :)

I'll admit I'm pretty rusty with classes but I can't seem to spot what I am doing wrong. Anyone have any idea?

class OGLAppManager
void reshape(int width, int height);
void display(void);
float rotationAngle;

#include "OGLAppManager.h"

rotationAngle = 0;

//Set display properties
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); //Set up a display buffer, enable double buffering, blending and depth testing
glutInitWindowSize(1000, 1000); //Set the width and height of the window
glutInitWindowPosition(100, 100); //Set window position
glutCreateWindow("GSP 420"); //Set window caption

//Window Control Functions
glutDisplayFunc(display); //Tell glut to draw window based on function
glutReshapeFunc(reshape); //Tell glut to reshape the window when dragged

//Function to reshape the window when dragged
void OGLAppManager::reshape(int width, int height)
//Set viewport to window size
glViewport(0, 0, (GLsizei)width, (GLsizei)height);

// Switch to projection matrix to manipulate how scene is viewed
glLoadIdentity(); //Reset projection matrix to avoid artifacts

//Set FOV, + Near and Far clip planes
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);

//Switch back to modelview matrix

//Function to draw the scene
void OGLAppManager::display(void)
//Handle drawing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Clear background of window
glClear(GL_COLOR_BUFFER_BIT); //Clear the color buffer
glLoadIdentity(); //Load identity matrix to reset drawing locations

//Translate scene back
glTranslatef(0.0f, 0.0f, -6.0f);

//Draw a pyramid
glRotatef(rotationAngle, 0.0, 1.0, 0.0);

glutSwapBuffers(); //Flush buffers to the window

rotationAngle++; //Rotation angle for pyramid

Share this post

Link to post
Share on other sites
A class member function will receive the this pointer of the class as its first (hidden) argument, so you're not passing in a 'void func(void)' but a 'void func(OGLAppManager*)'.

The this pointer won't be passed in as first hidden argument when you declare the function as static.

Share this post

Link to post
Share on other sites
Member functions are not normal functions, you cannot pass a pointer to a member function to something expecting a pointer to a function. To even refer to a member function, you must use &ClassName::functionName, unlike normal functions. There are many other caveats on member function pointers, but long story short glut cannot interact with c++ member functions. You'll need to write non-member functions. From these you may of course call member functions on any globally accessible object.

I'd recommend using SDL rather than GLUT if you can.

Share this post

Link to post
Share on other sites
Posted · Hidden
I tried this earlier before I posted:

[color=#008d00][color=#480083]glutDisplayFunc[color=#000000](*[color=#255b5f]display[color=#000000]); //Tell glut to draw window based on function

[color=#008d00][color=#480083]glutReshapeFunc[color=#000000](*[color=#255b5f]reshape[color=#000000]); //Tell glut to reshape the window when dragged


[color=#008d00][color=#000000][font=arial, verdana, tahoma, sans-serif] But when I run the program it crashes (although it compiles and links just fine.) Am I doing something wrong? None of my C++ books cover using callback functions in a class :([/font]

Share this post

Link to post
Thanks so much for the responses guys!

I did notice the pointer thing when I double clicked the error message (and just to try I put a * in front of the name which compiled and linked just fine, but crashed the program on startup :P

I will look into using SDL since I am fairly familiar with it although I've never mixed it with SDL. Are you able to just use the functions of each? (It looked that way when I saw it on Lazyfoo's site).

Share this post

Link to post
Share on other sites
You could do something like so :

class OGLAppManager{
typedef void (*DisplayFunc)();
DisplayFunc _display;
OGLAppManager(): _display( _defaultDispFunc ) {}
OGLAppManager(const DiplayFunc& f) : _display(f) {}
void setDisplayFunc(const DisplayFunc& f){ _display = f; }
void initialize(){ /* all glut initialization here */ }
static void _defaultDispFunc(){ /* default display function */ }

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!