Skip to content

Instantly share code, notes, and snippets.

@magcius
Forked from startling/eventloop.c
Created May 20, 2012 06:18
Show Gist options
  • Save magcius/2755650 to your computer and use it in GitHub Desktop.
Save magcius/2755650 to your computer and use it in GitHub Desktop.
#include <sdl.h>
struct _EventFuncs {
void (*on_keydown) (char key, void *user_data);
void (*on_loop) (void *user_data);
};
typedef struct _EventFuncs EventFucs;
void event_loop(EventFuncs *event_funcs, void *user_data) {
/* Given a function pointer to call on each new keypress, catch keypresses
* and pass them to it.
*
* TODO: a callback on quit. */
SDL_Event event;
int cont = 1;
while (cont) {
while (SDL_PollEvent(&event)) {
switch(event.type){
case SDL_KEYDOWN:
event_funcs->on_keydown(event.key.keysym.sym, user_data);
break;
case SDL_QUIT:
cont = 0;
puts("Quitting.");
break;
}
}
event_funcs->on_loop(user_data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment