Skip to main content
gamedev.net
🔒 Locked

Function pointer to OpenGL

Started by Endar Jan 25, 2006 at 6:31 PM 5 replies 700+ views
Original Post
Endar
Endar
I'm attempting to do a little something like this:

void COpenGLDriver::setFeature(Feature f, bool b) const
{
	// Create a function pointer for gl functions depending on whether 'b'
	// is true or false (enable or disable).
	// Just so I wouldn't have to have and if/else pair for each feature.
	void (*glFunc)(GLenum);

	if( b )
		glFunc = glEnable;
	else
		glFunc = glDisable;

	switch(f){

	// set feature for lighting
	case FEATURE_LIGHTING:
		glFunc(GL_LIGHTING);
		break;

	// set feature for depth testing
	case FEATURE_DEPTH_TESTING:
		glFunc(GL_DEPTH_TEST);
		break;

	default:
		break;

	};// switch

}


But, i'm getting this error;

C:\Documents and Settings\Naim\My Documents\Programming\everything\engine\video\COpenGLDriver.cpp(795)
 : error C2440: '=' : cannot convert from 'void (__stdcall *)(unsigned int)' to 'void (__cdecl *)(unsigned int)'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Is this because glEnable/Disable is within a dll?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
MaulingMonkey
MaulingMonkey
Quote:
Original post by Endar
Is this because glEnable/Disable is within a dll?


It's a calling convention issue - a compiler error, not a linker one, so no - although the calling convention was based on the DLLs calling convention, so yes sort of indirectly as well.

Try adding the macro CALLBACK in front of your function pointer definition - it most likely defines to something like __cdecl or __attribute__((cdecl)), which will change the calling convention of that function as appropriate, allowing it to be assigned to OpenGL's callback functions, which appear to be expecting that convention.

Also, I suspect Boost.Function will handle both calling conventions seamlessly. Could be wrong though.
rip-off
rip-off
probably, but i think your use of function pointers is not required here

this is what i would do:

void COpenGLDriver::setFeature(Feature f, bool b) const{        GLenum feature;         switch(f){	// set feature for lighting	case FEATURE_LIGHTING:		feature = GL_LIGHTING;		break;	// set feature for depth testing	case FEATURE_DEPTH_TESTING:		feature = GL_DEPTH_TEST;		break;	default:                // error handling		break;	};// switch		if( b )		glEnable(feature);	else		glDisable(feature);	}
Endar
Endar
Where is CALLBACK defined? I keep getting an error.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
LilBudyWizer
LilBudyWizer
I believe it's defined in one of the window header files. Try just looking at how it is defined int he header and copying that.
Keys to success: Ability, ambition and opportunity.
cypherx
cypherx
Have you tried changing "void (*glFunc)(GLenum);" to "void (__cdecl *glFunc)(GLenum);"?

-Alex
Endar
Endar
I actually had to make it "void (__stdcall *glFunc)(GLenum);". Thanks guys.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.