PistonDevelopers / piston

A modular game engine written in Rust

Home Page:https://www.piston.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Event pushing at application level

po-stulate opened this issue · comments

In order to push an event, implementing a window backend your self seems to be the only way to the best of my knowledge. There should be some generic/higher-level ways that accepts events to be raised in application code, since the window may not only update because of the input.

One option is to write a generic window wrapper that pops from a queue of pushed events.

Do you mean to wrap over an existing window backend implementation, and then poll events from both window backend and custom event source?

Does it mean we not being able to know the order between custom and backend events?

I would love to hear about a feature such as pushing/pulling events like sending/receiving them from a channel.

Imo it is not a good idea to keep everything that may raise any event all inside the main thread. We need an event system because we want to collect information from different parts of a program which may run concurrently. There will be no point having an event queue if everything can only be done with only one single thread (which is the case since we aren't able to push/pull events from other threads for most of the backends for now), since with single thread we are able to deal with events right away (for example by overriding on_input / on_create / on_custom etc...) without having to push them into a queue first.

I would guess that the order can be determined by e.g. polling custom events before or after input events.

Of course, you can just use sender/receivers and build a custom event system in addition to the piston events.

What I meant by the order was that there is no way to determine if an input event or a custom event is raised first. Since there are two queues to poll from, we don't know which one of them is raised first. The same thing also applies to event pushing, you must loss the order of events if you push them into two different queues.

There are two ways to deal with this problem.

First, if we are only supporting single thread event pushing (which is the case, there is no way to push events from any thread other then main thread for now), it is quite confusing to have a queue and an event loop, which makes people believe it supports multi-threading. A better way might be to require override to on_input / on_size_change etc... or simply on_event method, and call these methods right away when an event is raised.

Second, if we are supporting multi-thread event pushing (not necessary polling), then there must be something like a sender so we can receive events from different threads which owns one, and push those events received into event queue.

By supporting multi-thread event pushing, it automatically supports event pushing at application code, and it definitely is a good way to make a game engine more generic and powerful.