Skip to content

Instantly share code, notes, and snippets.

@fouric
Last active August 28, 2019 18:30
Show Gist options
  • Save fouric/23381fab924afd1d54d0cbc6750e4b32 to your computer and use it in GitHub Desktop.
Save fouric/23381fab924afd1d54d0cbc6750e4b32 to your computer and use it in GitHub Desktop.
quick demo of the C module hack for SDL2
// https://snai.pe/posts/modules-in-c99
// ah, much nicer!
#include <SDL.h>
struct m_sdl {
int (*init)(Uint32);
SDL_Window* (*create_window)(const char*, int, int, int, int, Uint32);
void (*delay)(Uint32);
void (*destroy_window)(SDL_Window*);
void (*quit)(void);
Uint32 INIT_VIDEO;
Uint32 WINDOWPOS_UNDEFINED;
Uint32 WINDOW_SHOWN;
};
const struct m_sdl sdl = {
.init = SDL_Init,
.create_window = SDL_CreateWindow,
.delay = SDL_Delay,
.destroy_window = SDL_DestroyWindow,
.quit = SDL_Quit,
.INIT_VIDEO = SDL_INIT_VIDEO,
.WINDOWPOS_UNDEFINED = SDL_WINDOWPOS_UNDEFINED,
.WINDOW_SHOWN = SDL_WINDOW_SHOWN,
};
typedef SDL_Window* Window;
int main() {
sdl.init(sdl.INIT_VIDEO);
Window w = sdl.create_window("no modules", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 400, 400, sdl.WINDOW_SHOWN);
sdl.delay(1000);
sdl.destroy_window(w);
sdl.quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment