How fast is the OnEvent receiver?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
wizard4
Posts: 206
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

How fast is the OnEvent receiver?

Post by wizard4 »

So I am attempting to change the active camera when a key is released. The way is to set KeyDown[KEYS_MAX] to 0 and KeyUp[KEYS_MAX] to 1. The check works well and the virtual Toggle outputs when I release the key.
INPUT_EVENT no longer receives a key input. All the code is happening so fast within the OnEvent method.

I just wondered when to check a value within the Event system?

No, I probably haven't thought it through as much as I should have, but it's good to ask a question from time to time.

..

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
    // give input a gui quit button
    MyEventReceiver(Context & c) : context(c) { }


	// 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;
            // check here
            // D0 U1 (before event)
            // D1 U1 (line event.KeyInput.PressedDown
            // D1 U0 (line !event.KeyInput.PressedDown
            // D0 U0 (line event.KeyInput.PressedDown
            // D0 U1 (line !event.KeyInput.PressedDown
                                  
            if(KeyIsDown[event.KeyInput.Key] == 0 && KeyIsUp[event.KeyInput.Key] == 0)
                ToggleView();

            KeyIsUp[event.KeyInput.Key] = !event.KeyInput.PressedDown;

            // EVERY key released?
            //KeyIsReleased[event.KeyInput.Key] = 0;
        }

        if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
        {
            RightMouse = event.MouseInput.isRightPressed();

            mouse_xy.X = event.MouseInput.X;
            mouse_xy.Y = event.MouseInput.Y;
        }

        if(event.MouseInput.Event == EMOUSE_INPUT_EVENT::EMIE_RMOUSE_LEFT_UP)
            RightMouse = false;


		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) const
	{
	    return KeyIsUp[keyCode];
	}


	virtual bool IsRightClick() const
	{
	    return RightMouse;
	}

	virtual bool IsQuit()
	{
	    return context.quit_app;
	}

	virtual core::position2d<s32> getXY()
	{
	    return mouse_xy;
	}

	virtual void ToggleView()
	{
	    context.viewing_cam = !context.viewing_cam;
	    std::cout << "?: " << context.viewing_cam;
	}

	virtual bool getView()
	{
	    return context.viewing_cam;
	}

	// init

	MyEventReceiver()
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;

        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsUp[i] = true;
	}

private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
	bool KeyIsUp[KEY_KEY_CODES_COUNT];

	bool RightMouse = false;

	int event_tot = 0;
	core::position2d<s32> mouse_xy = {0,0};
	Context context;
};

The calling code is here, but so far it is set to IsKeyDown, which isn't what I want. It does work (sort of) but only when the key is down. I'd like it to trigger when the key is up.

Code: Select all

        if(receiver.IsKeyDown(irr::KEY_KEY_1))
        {
            if(receiver.getView())
            {
                smgr->setActiveCamera(no_camera);
                fps_mention->setText(L"NO FPS");
            }
            else
            {
                smgr->setActiveCamera(camera);
                fps_mention->setText(L"FPS");
            }
        }
        
wizard4
Posts: 206
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

Re: How fast is the OnEvent receiver?

Post by wizard4 »

I realised I'm sending an IsKeyUp on every keystroke now :)
wizard4
Posts: 206
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

Re: How fast is the OnEvent receiver?

Post by wizard4 »

I literally thought a keypress triggers OnEvent. My problem was in the code running at the device->run() level accessing a OnRelease array that only "triggers" and then resets. OnEvent is as fast as it needs to be. It "Events" on an "Event".

Say I checked a triggered array within draw/render in my game loop. I wondered how fast I could check that "1 bit blip"?
wizard4
Posts: 206
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

Re: How fast is the OnEvent receiver?

Post by wizard4 »

I guess an Event only cares about an Event when its happening. The FPS camera stops moving when a key is released. No more events are happening. Is the event system like, encapsulated to events only, and only cares about events? Why would my code in device->run care about inside an event? Event takes care of that?
CuteAlien
Admin
Posts: 9974
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How fast is the OnEvent receiver?

Post by CuteAlien »

device->run() polls the system events. So every event is passed on to Irrlicht in device->run. So basically you get all events which happened since last frame (or last run call) together once per frame.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
wizard4
Posts: 206
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

Re: How fast is the OnEvent receiver?

Post by wizard4 »

If the last event in OnEvent was an up toggle like my check does (D0U1/D1U1) and only needs OnRelease _at that exact time_, it makes sense now. Events are cool.
Post Reply