Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Why can't my class find these callback functions?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Chrono1081   Members   -  Reputation: 108

Like
0Likes
Like

Posted 09 May 2011 - 04:16 AM

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?


//HEADER FILE
class OGLAppManager
{
public:
    OGLAppManager();
    void reshape(int width, int height);
    void display(void);
private:
    float rotationAngle;
};

//SOURCE FILE
#include "OGLAppManager.h"

OGLAppManager::OGLAppManager() 
{
    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
    glMatrixMode(GL_PROJECTION); 
    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
    glMatrixMode(GL_MODELVIEW);
};

//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
	glPushMatrix();
	glRotatef(rotationAngle, 0.0, 1.0, 0.0);
    glBegin(GL_TRIANGLES);
    
    //SNIP
    glutSwapBuffers(); //Flush buffers to the window
    
	rotationAngle++; //Rotation angle for pyramid
};


Sponsor:

#2 Jan-Lieuwe   Members   -  Reputation: 206

Like
0Likes
Like

Posted 09 May 2011 - 04:31 AM

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.






#3 rip-off   Moderators   -  Reputation: 8762

Like
1Likes
Like

Posted 09 May 2011 - 04:37 AM

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.

#4 Chrono1081   Members   -  Reputation: 108

Like
0Likes
Like

Posted 09 May 2011 - 04:58 AM

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).

#5 D.Chhetri   Members   -  Reputation: 181

Like
0Likes
Like

Posted 09 May 2011 - 10:26 AM

You could do something like so :
class OGLAppManager{
 public:
   typedef void (*DisplayFunc)();
 private:
  DisplayFunc _display;
 public:
   OGLAppManager(): _display( _defaultDispFunc ) {}
   OGLAppManager(const DiplayFunc& f) : _display(f) {}
  void setDisplayFunc(const DisplayFunc& f){ _display = f; }
  void initialize(){ /* all glut initialization here */ }
 private:
 static void _defaultDispFunc(){ /* default display function */ }
}


Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS