I am building a hobby/minimalist/general 2d game engine using SDL and C as my programming language. Also following along with Lazy Foo Production tutorials. I use the C language for learning purposes.
I would like to implement a general event handler function, that would be able to run a user written function to handle a given event. Currently I am still in the beginning stages.
handler header file:
#ifndef HANDLER_H #define HANDLER_H #include <SDL2/SDL.h> #include <stdarg.h> int handle(SDL_Event e, void (*handler)(int args,...)); #endif Client code:
int quit = FALSE; SDL_Event e; while (quit == FALSE) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = TRUE; } //call to handle goes here } render_image(&dp,&i_p); update_display(&dp); } Would it be suffecient to us a function pointer as the parameter to handle() or is there a better method to implementing a generic event handler.
My core goal for the engine in terms of event handling is to just be able to have the handler handle events that are polled in the game loop by using a function written by the end-user instead of hard coding in functions to handle every type of possible event.