Skip to main content
gamedev.net
🔒 Locked

Passing function pointers to DXUTSetCallback* methods

Started by BlackGhost Oct 5, 2004 at 11:56 AM 2 replies 2.4k views
Original Post
BlackGhost
BlackGhost
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.
Muhammad Haggag
Muhammad Haggag
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.

jasonsa
jasonsa
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    }}

IFooBar
IFooBar
If only the dx library made use of boost eh?
[size=2]aliak.net

Topic Locked

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

Sign in to reply to this topic.