Jump to content
  • Advertisement
Sign in to follow this  
BlackGhost

Passing function pointers to DXUTSetCallback* methods

This topic is 5396 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, I'm having a problem with function pointers in C++. The directX 9 sample framework allows us to create callbacks for significant events, e.g. the create device event can be handled by a function with this declaration:
HRESULT CALLBACK onCreateDevice(IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc); 
and we declare to the framework that this will be the callback with the command DXUTSetCallbackDeviceCreated(onCreateDevice). This function takes a function pointer as a parameter. The problem is that if the function passed in is not global, and instead is a member function of a class, the conversion of the parameter to the correct type does not work as before -I get a type cast error. The type that is expected is the function pointer HRESULT (CALLBACK *LPDXUTCALLBACKDEVICECREATED)( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc ); Can anyone with more experience on function pointers provide any insight on this situation? Furthermore, what happens if the function is virtual -can pointers to that function work in this case? Thanks for any replies.

Share this post


Link to post
Share on other sites
Advertisement
Class member functions have an implicit first parameter, the this pointer. That's why the compiler sees them as having a different signature than the callback's.
e.g.

class Boo
{
public:
HRESULT on_create_device(IDirect3DDevice9* device, const D3DSURFACE_DESC* bbs_desc);
};

Is seen by the compiler as:

HRESULT on_create_device(Boo* this, IDirect3DDevice9* device, ...);


To work around this, you can make it a static function. However, you won't be able to access non-static class members that way.

Share this post


Link to post
Share on other sites
Ya, you can do something like this:


class CFoo
{
void Init()
{
DXUTSetCallbackDeviceReset( StaticOnDeviceReset );
// etc...
}

// static methods which handle the DXUT callbacks and pass the calls to non-static class methods
// so they can easily access member methods
static HRESULT StaticOnDeviceReset( ... ) { g_Foo.OnDeviceReset(); }

HRESULT OnDeviceReset( .. )
{
// code from old framework's app method RestoreDeviceObjects() here
}
}


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!