[SDL] Re: Events
Rainer Deyke
root at rainerdeyke.com
Mon Apr 23 09:21:05 PDT 2001
"David Hedbor" <newsgroups at animearchive.org> wrote in message
news:m3n1989h09.fsf at stellar.home.hedbor.org...
> In any case, I have a very nice event system in my opinion. Basically,
> you create a classed derived from EventHandler (documented at
> http://sdlmm.sourceforge.net/class_SDLmm__EventHandler.html). Each
> type of callback has one or more potential handlers. Here's a simple
> example of it's use:
Sorry, but that system is simply awful. Consider the following code:
void wait_for_key() {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_KEYDOWN) {
return;
}
}
}
That's eight lines of code. Then consider what I'd have to do in your
system:
class WaitForKeyEventHandler : public SDLmm::EventHandler {
public:
bool done;
WaitForKeyEventHandler() : done(false) {}
bool HandleKeyPressEvent(SDL_keysym&)
{
this->done = true;
return true;
}
};
void wait_for_key_press()
{
WaitForKeyEventHandler ev;
while (!ev.done) {
SDLmm::Event::HandleEvent(ev);
}
}
That's 18 lines of code: more than twice the original. It is also more
complicated and less readable. Now consider that there may be hundreds of
places in a program where events are handled differently (think dialogs).
Each of these would see a similar increase in size and complexity. True,
you can use inheritance to factor out common functionality - but this
provides no advantage over factoring through free functions.
--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor
More information about the SDL
mailing list