Application wrapper for SDL to accomodate desktop, mobile, and web.
With SDL3, this is no longer required as you can use SDL_MAIN_USE_CALLBACKS. See https://github.com/Ravbug/sdl3-sample for a demonstration.
When building SDL applications for desktop, mobile, and web, there are subtle differences to accomodate the application workflow. SDL_App.h aims to clean up those differences, and make the code easier to read for a unified application entry. This pattern is inspired by sokol_app.
#include <SDL2/SDL.h>
#define SDL_APP_IMPLEMENTATION
#include "SDL_App.h"
typedef struct AppData {
SDL_Window* window;
SDL_Renderer* renderer;
} AppData;
SDL_bool Init(void* userData) {
SDL_Init(SDL_INIT_EVERYTHING);
AppData* appData = (AppData*)userData;
appData->window = SDL_CreateWindow("SDL_App: Example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 450, SDL_WINDOW_SHOWN);
appData>renderer = SDL_CreateRenderer(appData->window, -1, SDL_RENDERER_PRESENTVSYNC);
return SDL_TRUE;
}
SDL_bool Update(void* userData) {
AppData* appData = (AppData*)userData;
SDL_Event event;
while (SDL_PollEvent(&event) != 0) {
switch (event.type) {
case SDL_QUIT:
return SDL_FALSE;
}
}
SDL_SetRenderDrawColor(appData->renderer, 102, 191, 255, 255);
SDL_RenderClear(appData->renderer);
SDL_RenderPresent(appData->renderer);
return SDL_TRUE;
}
void Close(void* userData) {
AppData* appData = (AppData*)userData;
SDL_DestroyRenderer(appData->renderer);
SDL_DestroyWindow(appData->window);
SDL_Quit();
}
SDL_App Main(int argc, char* argv[]) {
return (SDL_App) {
.init = Init,
.update = Update,
.close = Close,
.userData = SDL_malloc(sizeof(AppData)), // Optional
};
}Rather than having your own int main(), define your own Main function.
SDL_App Main(int argc, char* argv[]) {
return (SDL_App) {
.init = Init, // The init callback that is called when the application initializes
.update = Update, // The update callback that is called when the application should render
.close = Close, // The close callback which is called when the application is closed
.userData = NULL // Custom user data that is passed through all the callbacks (Optional)
};
}SDL_App is licensed under an unmodified zlib/libpng license, which is an OSI-certified, BSD-like license that allows static linking with closed source software. Check LICENSE for further details.