Mocking SDL functions

Hello,

I’m considering writing some unit tests and would want to mock some SDL functions. Any idea what would be a good way to achieve that?

I’ve run into this: https://hg.libsdl.org/SDL/file/default/docs/README-dynapi.md. This document discloses existence of function table that SDL uses internally. This seems perfect for me, as it could be used to replace real functions with mocked ones as well as invoke real ones if needed. Is there any (not-entirely-hackish) way to access the table for this purpose?

In case anyone was having the same issue, here’s one way to do it (based on “How does the SDL dynamic API inject itself in statically linked code”):

  • create DLL containing the following function: Sint32 SDLCALL SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
  • store the table pointer and size when invoked
  • return -1 — important step that will cause SDL to fill the table with internal pointers
  • from main program ensure that the environment variable SDL_DYNAMIC_API contains path of the DLL before SDL initializes dynamic API (set it externally or from static class constructor)
  • after SDL has initialized dynamic API, call another function from the DLL to fetch the stored table pointer and size
  • have fun :slight_smile:
1 Like