[SDL] Re: Events
Jone Marius Vignes
vignes at nsd.uib.no
Tue Apr 24 02:32:03 PDT 2001
On Monday 23 April 2001 18:21, you wrote:
> "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.
>
>
Granted you want an object to do something on a KeyDownEvent, here's how
you'd do it in KGB:
class LetsCallItButton:public KGB_KeydownHandler{
public:
LetsCallItButton():KGB_KeyDownHandler(){
game->registerKeydownHandler(this);
}
void handleKeydownEvent(SDLKey key){
do_stuff_here();
}
}
...to show another way of doing it in an object-oriented manner. You just
make the object responding to an event a subclass of the proper eventhandler,
and implement handle*Event(*).
BLiP!
More information about the SDL
mailing list