Skip to main content
gamedev.net
🔒 Locked

DirectInput class problem

Started by Barguast 1 reply 150+ views
Original Post
Barguast
Barguast
This might be a very specific problem, and maybe I''m just going about this wrong, but here''s the problem: I''m writing an Input Class with DirectInput, and I''m having bother with the CALLBACK functions. I have them as private friend functions of the class, but they still can''t access the class''s member variables (Such as the DIRECTINPUT8 object). Is there any way I can do so? Without the required variables becoming global? Thanks muchly
Using Visual C++ 7.0
GlaZZ
GlaZZ
Try declaring the variables that the friend functions should be able access as ''protected'' instead of ''private''.

I''m not absolutely sure if this works, but you can try...
Krunk
Krunk
I''m not sure if this is your problem, but since you haven''t mentioned anything about this, I''ll give it a shot. When I talk about "DirectInput objects/classes" below, I''m referring to the DirectInput class you are writing to manage input and any objects of that type (rather than IDirectInput8).

Basically, if your callback function is a friend of your DirectInput class, it is a non-member function. If you want to be able to access the contents of a particular DirectInput object, you have to pass the object to the function somehow.

The trick is to use the "LPVOID pvRef" parameter to enumdevices (and DIEnumDevicesCallback). If you pass the address of your DirectInput object to enumdevices (by casting it to void*) it gets passed to your callback function on each call. Then in the callback you cast it back to the appropriate pointer type to get at the object. Since the function is a friend of the class, you will then be able to get at the private/protected members of the class.

There''s a little example of how to do it in this thread. There, the callback is a static member of the class, but a friend works equally well (just remember to remove the classname:: bit from the function definition).

Topic Locked

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

Sign in to reply to this topic.