First snippet

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
wowdevver24
Posts: 26
Joined: Mon Jan 04, 2010 8:02 pm

First snippet

Post by wowdevver24 »

Not sure if anyone else has done this bt I have modified the demo's eventreciever to fix a bug in toggling commands, it works much the same as checking keypresses within the reciever but it abstracts checking to be an outside process and can be more sensetive than the other examples in source

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
	// This is the one method that we have to implement
	virtual bool OnEvent(const SEvent& event)
	{
		// Remember whether each key is down or up
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
		{
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
		}
		return false;
	}

	// This is used to check whether a key is being held down
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}
	virtual bool IsKeyUp(EKEY_CODE keyCode)
	{
		if((KeyIsDown[keyCode]) && (!KeyIsDownPrev[keyCode]))
		{
			KeyIsDownPrev[keyCode] = KeyIsDown[keyCode];
			return false;
		}
		if( (!KeyIsDown[keyCode]) && (KeyIsDownPrev[keyCode]) )
		{
			KeyIsDownPrev[keyCode] = false;
			return true;;//the key has been pressed and released
		}
		return false;
	}
	
	virtual bool IsKeyToggled(EKEY_CODE keyCode)
	{
		if(KeyIsDown[keyCode])
		{
			if(!KeyIsToggled[keyCode])
			{
				KeyIsToggled[keyCode] = true;
				return true;
			}
			KeyIsDownPrev[keyCode] = KeyIsDown[keyCode];
		}
		else
		{
			if(IsKeyUp(keyCode))
			{
				if(KeyIsToggled[keyCode])
				{
					if(KeyIsDownPrev[keyCode])
					{
						KeyIsToggled[keyCode] = false;
						KeyIsDownPrev[keyCode] = false; 
						return true;
					}
					else
					{
						return true;
					}
				}
			}
		}
		return false;
	}
	virtual void clearKeyDown(EKEY_CODE keyCode)
	{
		KeyIsDown[keyCode] = false;
	}
	MyEventReceiver()
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
		{
			KeyIsDown[i] = false;
			KeyIsDownPrev[i] = false;
			KeyIsToggled[i] = false;
			KeyInToggle[i] = false;
		}
	}

private:
	// We use this array to store the current state of each key
	bool KeyIsDownPrev[KEY_KEY_CODES_COUNT];
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
	bool KeyIsToggled[KEY_KEY_CODES_COUNT];
	bool KeyInToggle[KEY_KEY_CODES_COUNT];
};
the only part I coded was the void clearKeydown for getting less response from a key & I added the IsKeyToggled part but it is neccesarry to have something like this for if you need the key to toggle something such as player crouching for example or if you need certain keys to be less responsive as otherwise players can tend to shoot forward at an alarming rate

usage of clearKeyDown can be used in conjunction with the IsKeyDown event reciever however it dows not need to be used.

isKeyDown() (including clearKeyDown)

Code: Select all

if(receiver.IsKeyDown(irr::KEY_KEY_2))
{
	//do something
	receiver.clearKeyDown(irr::KEY_KEY_1); // clears keypress
}
quite painless I'm assuming

isKeyUp()

Code: Select all

if(receiver.IsKeyUp(irr::KEY_KEY_1))
{
	//do something
}
gets used in isKeyToggled but also a good alternative to linear control that does not need toggling

isKeyToggled()

Code: Select all

if(receiver.IsKeyToggled(irr::KEY_KEY_1))
{
	//do something like toggle crouching or using objects
}
for toggling, crouching etc

hope you all find this useful
Remember all information is contextual and if I do not understand the context I cannot gras the information
Post Reply