bit of a problem here

Started by
2 comments, last by Providence 18 years, 11 months ago
I simply cannot figure out what's wrong here. The situation is, when I press the "d" key, the program closes. This is great. I want it to close when I press any of the wasd keys. But it doesn't close when I press "w", "a", or "s". man.moving is an int.

int ReadKeyboard() {

	BYTE buffer[256];
	ZeroMemory(buffer,256);

	HRESULT hr = keyboard->GetDeviceState(sizeof(buffer),buffer);

	if (FAILED(hr)) {
		if (hr == DIERR_NOTACQUIRED) return 0x13;
	}

	// important index 
	// 0x01 escape
	// 0x11 w
	// 0x1e a
	// 0x1f s
	// 0x20 d
	// 0xc8 up
	// 0xcb left
	// 0xd0 down
	// 0xcd right

	if (buffer[0x11])  {		// w 
		man.moving |= 0x01;
	}
	else {
		man.moving &= 0x01;
	}

	if (buffer[0x1e]) {			// a
		man.moving |= 0x02;
	}	
	else {
		man.moving &= 0x02;
	}

	if (buffer[0x1f]) {			// s
		man.moving |= 0x04;
	}
	else {
		man.moving &= 0x04;
	}

	if (buffer[0x20]) {			// d
		man.moving |= 0x08;
	}
	else {
		man.moving &= 0x08;
	}


	return man.moving;
}
As you can see I want it to set/unset flags based on which keys are down. If man.moving is true, the program exits. It only exits if I press d though. in WinMain():

if (int errorc = ReadKeyboard()) return errorc; 

...and I just realized my error handling wasn't releasing any memory, just rather abruptly exiting the program.
Advertisement
Why not just return a -1 when those buttons are pressed?
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
Because my ultimate goal is to use the information, not exit the program. However, when I tried to use the information in the man.moving variable only the "d" seemed to have worked. So I came back to this function and looked at it, to see if the problem lies in it, and I believe that it does.
I figured out the problem.

I wasn't using the "&" operator properly to unset a flag.

This topic is closed to new replies.

Advertisement