Jump to content
  • Advertisement
Sign in to follow this  
startreky498

Weird returns on GetDeviceState

This topic is 4867 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

First off, I'm using Visual C++ 2005 Express Edition Beta. Using DirectInput has given me some problems. Once I lose window focus, the character array passed to GetDeviceState goes crazy. Here's my init code:
	if( FAILED ( DirectInput8Create( hInstance, DIRECTINPUT_VERSION,
									 IID_IDirectInput8, (void**)&pdInput, NULL) ) )
		return false;

	if( FAILED ( pdInput->CreateDevice( GUID_SysKeyboard, &pdiDevice, NULL) ) )
		return false;

	if( FAILED ( pdiDevice->SetDataFormat(&c_dfDIKeyboard) ) )
		return false;

	if( FAILED( pdiDevice->SetCooperativeLevel( *(g_Window->gethWnd()),
									DISCL_FOREGROUND | DISCL_NONEXCLUSIVE) ) )
		return false;

	if( FAILED ( pdiDevice->Acquire() ) )
		return false;


During the check routine, I do a break (which causes the problem anyway, since that will force a lost focus, and thus show me the problem next round), and the character array is full of non-zero garbage. The same problem arrises when using when using Exercise 1 provided by "Beginning DirectX 9" by Wendy Jones. Thing is, once I change the coop level from FOREGROUND to BACKGROUND, this problem doesn't arrise. Can anyone figure out what's going on? In case this is an issue, I'm still on the December 2004 SDK (ever since the summer update screwed with the sprite functions, I've been paranoid about updating). edit: Just for the record, here's a bit of information from the character array containing key states: before the problem (and nothing's pressed): buffer = "" after focus switching (no matter what's pressed): buffer = "@ァソゥッコoヨj聞~-ツキ含ヤ椢叱鯣f・.・ォ4・ツlWニ}潛,L1aシ゚:/&*i・8妾ミyaコSqヘ"ハVム}eB&鉢擔・ネョカケイr癢ェr・豐メM@M}ワ噪・>O6`JI!ヨw`・D」}ラ俑q齏519P$,ヤWムS卆Fヒ・Bノ(;K・_粐3ィtxlX椄E+F幾拓臨ンヤ・ヘ飮・ヤフ└_エfY ァ:~槻J5€Yvマ・゚2逶゚"m・兊フ周iマニマPモVィ・チ}-:" [Edited by - startreky498 on June 21, 2005 1:20:08 AM]

Share this post


Link to post
Share on other sites
Advertisement
Forgot to add a possibly important piece of code. Here's my check sequence that runs constantly:


inline bool KEYDOWN(char buf[],int keycode) { if(buf[keycode]) return true; else return false; };

void Render()
{
pdiDevice->GetDeviceState( sizeof(&buffer), (LPVOID)&buffer);

if(KEYDOWN(buffer,DIK_LEFT))
::OutputDebugString("Left\r");
if(KEYDOWN(buffer,DIK_UP))
::OutputDebugString("Up\r");
if(KEYDOWN(buffer,DIK_RIGHT))
::OutputDebugString("Right\r");
if(KEYDOWN(buffer,DIK_DOWN))
::OutputDebugString("Down\r");
}




So anyway, the debug string puts out all four variations -- constantly -- once I lose focus on the window. Has this ever happened to anyone else? Any idea why the character array is getting corrupted even though GetDeviceState (should?) be fixing such problems with the array?

Share this post


Link to post
Share on other sites
In your GetDeviceState(), you have "sizeof(&buffer)"... so you're saying your buffer size is like 4 bytes :). Try with sizeof(buffer).

Share this post


Link to post
Share on other sites
Try modifying your code like this:


inline bool KEYDOWN(char *buf,int keycode) { if(buf[keycode]) return true; else return false; };

void Render() {

pdiDevice->GetDeviceState( sizeof(buffer), buffer); //and if buffer is not a pointer: pdiDevice->GetDeviceState( sizeof(buffer), &buffer);

if(KEYDOWN(buffer,DIK_LEFT))
::OutputDebugString("Left\r");
if(KEYDOWN(buffer,DIK_UP))
::OutputDebugString("Up\r");
if(KEYDOWN(buffer,DIK_RIGHT))
::OutputDebugString("Right\r");
if(KEYDOWN(buffer,DIK_DOWN));
}


Share this post


Link to post
Share on other sites
Shouldn't that be:

pdiDevice->GetDeviceState(sizeof(buffer),&buffer)

assuming buffer is defined as "char buffer[256];" or somesuch? That's what I have in my code, and works fine.

Also, you might want to check whether GetDeviceState succeeds (if it fails, you need to re-acquire it and do necessary checks).

Share this post


Link to post
Share on other sites
Quote:
Original post by Replicon
Shouldn't that be:

pdiDevice->GetDeviceState(sizeof(buffer),&buffer)

assuming buffer is defined as "char buffer[256];" or somesuch? That's what I have in my code, and works fine.

Also, you might want to check whether GetDeviceState succeeds (if it fails, you need to re-acquire it and do necessary checks).


Well yeah...that's what i'm talking about, I mentioned it in my previous post It's:

pdiDevice->GetDeviceState(sizeof(buffer),&buffer); if buffer is "char buffer[256];"

And:

pdiDevice->GetDeviceState(sizeof(buffer),buffer); if buffer is "char *buffer = new char[256];"...a pointer.

However if you use a pointer don't forget to delete the buffer after you finish using it;)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!