Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

RobLoach/SDL_App

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SDL_App

Application wrapper for SDL to accomodate desktop, mobile, and web.

Deprecated

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.

Introduction

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.

Example

#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
    };
}

API

SDL_App Main(int argc, char* argv[])

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)
    };
}

License

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.

About

Application wrapper for SDL to unify desktop, mobile, and web SDL code.

Topics

Resources

License

Stars

Watchers

Forks