CALLBACK - sure you will.

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

CALLBACK - sure you will.

Post by albinopapa » September 13th, 2013, 2:26 pm

Lousy callback functions. I spent a few hours yesterday trying to work my way through changing Joystick class from winmm to dinput. For those of you that have figured this out please explain it to me, because I can't seem to figure it out.

Code: Select all

#pragma once
#include <dinput.h>
#include <vector>

class DIJoystick
{
public:
	DIJoystick();
	~DIJoystick();

	BOOL CALLBACK EnumCallBack( LPDIDEVICEINSTANCE lpDevInst, LPVOID pvRef);
	IDirectInput* pDirectInput;
	IDirectInputDevice* GamePad;
	std::vector< LPDIDEVICEINSTANCE> EnumDevList;
};
DInput.cpp

Code: Select all

#include "DInput.h"


DIJoystick::DIJoystick()
{
	HINSTANCE hInst;

	pDirectInput = 0;
	GamePad = 0;
	
	pDirectInput->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumCallBack, NULL, DIEDFL_ALLDEVICES);

	pDirectInput->CreateDevice(EnumDevList[0]->guidProduct, &GamePad, NULL);
}

BOOL CALLBACK DIJoystick::EnumCallBack( LPDIDEVICEINSTANCE lpDevInst, VOID* pvRef)
{
	EnumDevList.push_back(lpDevInst);

	EnumDevList[EnumDevList.size() -1]->dwSize = sizeof(LPDIDEVICEINSTANCE);
	return DIENUM_CONTINUE;
}

DIJoystick::~DIJoystick()
{
}
Here are some links to the functions on msdn if anyone wants to see.
http://msdn.microsoft.com/en-us/library ... s.85).aspx
http://msdn.microsoft.com/en-us/library ... s.85).aspx

The line

Code: Select all

pDirectInput->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumCallBack, NULL, DIEDFL_ALLDEVICES);
keeps giving me an error. The EnumCallBack parameter is suppose to be the address of the callback function and chili said that the address of a function is the name of the function without the parameter list, but it isn't working.

I had this same problem trying to make my own win32 program with the msgproc function. Someone please explain what I'm doing wrong.
Attachments
callback_error.png
here is the error message
(15.79 KiB) Downloaded 113 times
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: CALLBACK - sure you will.

Post by chili » September 13th, 2013, 2:32 pm

Using callbacks (or function pointers in general really) gets kinda messy with methods (class functions). Remember, the stuff I showed you in the tutorials was with regular global scope functions.
Chili

User avatar
Nosferatu
Posts: 36
Joined: July 25th, 2012, 7:08 am
Location: Slovakia

Re: CALLBACK - sure you will.

Post by Nosferatu » September 13th, 2013, 2:45 pm

Don't know if this is what you are asking, but some time ago I had similar problem. Later I found out that if I want to use pointer to a NON-static function you have to pass not only the pointer to the function but also the object which has the function implemented. So I guess it's easier to just pass the pointer to the object and then use the function from it. When pointing to a static or global function the pointer alone will suffice.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: CALLBACK - sure you will.

Post by albinopapa » September 13th, 2013, 2:50 pm

Ok, moved the function out of the class and moved to top of cpp file just below the include.
Error 1 error C2664: 'IDirectInputW::EnumDevices' : cannot convert parameter 2 from 'BOOL (__stdcall *)(LPDIDEVICEINSTANCE,void *)' to 'LPDIENUMDEVICESCALLBACKW' e:\documents and settings\josh\my documents\visual studio 2012\projects\platformer\assets\dinput.cpp 20 1 Chili DirectX Framework
I still get what seems to say that my function pointer still is incompatible with the function pointer it's looking for.
Attachments
DInput.zip
Here are the changed files
(697 Bytes) Downloaded 126 times
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: CALLBACK - sure you will.

Post by albinopapa » September 13th, 2013, 3:01 pm

Nosferatu wrote:Don't know if this is what you are asking, but some time ago I had similar problem. Later I found out that if I want to use pointer to a NON-static function you have to pass not only the pointer to the function but also the object which has the function implemented. So I guess it's easier to just pass the pointer to the object and then use the function from it. When pointing to a static or global function the pointer alone will suffice.
I never instantiated the class, I tried just using the class name and scope "operator?" ::, when doing this VisStudios suggests using the &classname::functionname to point to the address. I tried that and the result is the same:
Error 1 error C2664: 'IDirectInputW::EnumDevices' : cannot convert parameter 2 from 'BOOL (__stdcall DIJoystick::* )(LPDIDEVICEINSTANCE,LPVOID)' to 'LPDIENUMDEVICESCALLBACKW' e:\documents and settings\josh\my documents\visual studio 2012\projects\platformer\assets\dinput.cpp 13 1 Chili DirectX Framework
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: CALLBACK - sure you will.

Post by LuisR14 » September 13th, 2013, 4:49 pm

lol, i've already implemented the dinput joystick stuff :P, all i need is to upload it (which i have mentioned in my post in the other thread, with edits and all lol)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: CALLBACK - sure you will.

Post by albinopapa » September 13th, 2013, 5:14 pm

Guess I'll have to wait until you stop laughing to share with the rest of the class, lol.

I know you said you would, and the edits said you did so thank you.

Seeing as how you said you wouldn't upload what you have until we have a "repo" option, perhaps you could let me in on what is going wrong in the mean time?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: CALLBACK - sure you will.

Post by LuisR14 » September 13th, 2013, 5:56 pm

albinopapa wrote:Guess I'll have to wait until you stop laughing to share with the rest of the class, lol.
lol, sorry :)
albinopapa wrote:perhaps you could let me in on what is going wrong in the mean time?
well, what you have there should work :P, but the problem is that your callback uses a LPDIDEVICEINSTANCE struct and the DIEnumDevicesCallback function prototype uses a LPCDIDEVICEINSTANCE struct, so in turn compiler can't convert properly
LPDIDEVICEINSTANCE = non-const
LPCDIDEVICEINSTANCE = const xD
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: CALLBACK - sure you will.

Post by albinopapa » September 13th, 2013, 6:22 pm

ok, that worked. Thank you. I thought I tried the constant pointer but i didn't do that plus moving into global scope.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: CALLBACK - sure you will.

Post by LuisR14 » September 13th, 2013, 6:31 pm

you could've also left the enumcallback function in the class, but you would have to make the function static then :P
and another prob with callback functions is the inability to address class member variables lol (unless the variables are static as well lol)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Post Reply