I didn’t believe what Bob said here for a minute. The only real
exception to the idea that most existing platforms (thoguh Bob said
"OSes") AFAIK are platforms like NES/SNES/Atari/etc… In fact I
recently interfaced a microcontroller with a SNES controller bus, and
polling is all there is at that level. But that doesn’t mean that at
the application level you must also poll, and it in no way implies
that a kernel polling a data source means that polling is the “easiest
way to interact” with the data at the application level.
I think most (interesting) low-level hardware is interrupt-driven,
nowadays, no? In any case, it’s much easier to make a non-blocking API
that requires polling look like a blocking API than the reverse…
You can use the X11 connection with select(), Win32 has
WaitForMultipleObjects, and Mac OS X has CFRunLoop (it’s arguably the
most event-driven of them, in fact, but SDL uses it in a rather hacky
way that artificially makes it polling, mostly because most game
developers follow grand old traditions dating from DOS).
I think it’s awfully arrogant to assume SDL uses CFRunLoop incorrectly
"because most game developers follow grand old traditions." But I
greatly appreciate your vote of confidence for event-driven
programming.
I didn’t say “incorrectly”, I said “in a rather hacky way”. The
SDLmain.m that I used (provided with SDL) set an NSApplication up for
itself, and hooked its “run loop has just started” event, which is
fired exactly once, and calls the SDL_main() from there, followed by a
harsh exit() (rather than a more normal posting of a “quit” event to
the run loop), while using some rarely used lower level (or maybe
they’re just weird Carbon APIs on the brink of extinction) functions
to peek at the event queue and remove events from it. From the point
of view of NSApplication, an SDL game does a “rough exit” as soon as
it’s started, with an extremely convoluted exit process (that is,
running the game!).
At the very least, it’s kind of colourful and original, you have to admit.
That’s in fact a good example of how it can be difficult to make a
blocking API behave in a non-blocking way.
I’m saying the “grand old tradition” in such a bad way. SDL was
originally developed to help port software (I’m thinking Doom era)
that came from those kinds of platforms such as DOS where there was
not really an OS or other processes to be nice to, you were the only
process, and SDL_WaitEvent would be coded without the SDL_Delay! At
best, you had the equivalent of sched_yield() to help Windows 3.11 or
DESQview (whoa!), but that was it. I remember myself, a very long
time ago, hearing about DirectX, and thinking that was non-sense,
running a game without having the whole CPU to yourself would never be
as good…
Converting a game like ioquake3 (which isn’t that old! if I
remember, it more or less uses SDL_PollEvent? it’s been a while I
looked) for an inverted flow of control like what we are both
suggesting (I agree with your suggested API, sounds good to me)
wouldn’t be trivial, and you’ve got people like Ryan making a living
of porting games that like a certain way of working (although,
hopefully, things must be changing, I’d be curious to see an even more
recent vintage of source code)… The API you’re suggesting involves
giving up some control, and game developers are not been so keen on
that, in my experience.
The bad part about SDL_WaitEvent() isn’t really that you have to call
it. What’s bad about it is these two things:
I’d like to add another reason, and for many games it’s the big one
(even if I’ve thought differently in the past):
Latency: In fact I think it was Bob who wrote fastevents to combat
exactly that issue.
The very first point after the bit I wrote that you quoted was
latency. So, yes, very important! ;-)On Tue, Jan 6, 2009 at 2:13 AM, Donny Viszneki <donny.viszneki at gmail.com> wrote:
–
http://pphaneuf.livejournal.com/