Created
July 7, 2024 17:04
-
-
Save Amiralgaby/ecba154a09141d82f99622ef4704dea4 to your computer and use it in GitHub Desktop.
SDL3 window app in one file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
TO COMPILE : | |
HAVE SDL3 installed in /usr/local/lib | |
gcc -Wall myapp_sdl3.c -L /usr/local/lib/ -lSDL3 -o output | |
PUBLIC DOMAIN | |
*/ | |
#define SDL_MAIN_USE_CALLBACKS | |
#include <SDL3/SDL_main.h> | |
#include <SDL3/SDL.h> | |
#include <SDL3/SDL_timer.h> | |
#include <SDL3/SDL_render.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct ContextApp { | |
SDL_Window* window; | |
SDL_Surface* surface; | |
int err; | |
unsigned long nbEvenements; | |
} Ctx; | |
// la couleur du contenu de la fenêtre par défaut | |
const SDL_Color color = {255, 255, 0}; | |
void MyApp_HandleError(const char* caller) { | |
printf("[%s] error %s\n", caller, SDL_GetError()); | |
} | |
void MyApp_FreeContextApp(Ctx* ctx) { | |
if (!ctx) return; | |
if (ctx->window != NULL) { | |
int *w = malloc(sizeof(int)), *h = malloc(sizeof(int)); | |
int err = SDL_GetWindowSize(ctx->window, w, h); | |
if (err != 0) { | |
MyApp_HandleError("SDL_GetWindowSize"); | |
} else { | |
printf("MA WINDOW FAIT %d*%d\n", *w, *h); | |
} | |
free(w); | |
free(h); | |
} | |
SDL_DestroyWindow(ctx->window); | |
free(ctx); | |
} | |
int MyApp_UpdateSurfaceColor(Ctx* ctx) { | |
ctx->surface = SDL_GetWindowSurface(ctx->window); | |
int err = SDL_FillSurfaceRect(ctx->surface, NULL, SDL_MapRGB( ctx->surface->format, color.r, color.g, color.b)); | |
if (err != 0) { | |
MyApp_HandleError("SDL_FillSurfaceRect"); | |
return SDL_EVENT_QUIT; | |
} | |
err = SDL_UpdateWindowSurface(ctx->window); | |
if (err != 0) { | |
MyApp_HandleError("SDL_UpdateWindowSurface"); | |
return SDL_EVENT_QUIT; | |
} | |
return 0; | |
} | |
// Initialisation de notre application | |
int SDL_AppInit(void **appstate, int argc, char **argv) { | |
Ctx* ctx = malloc(sizeof(Ctx)); | |
if (!ctx) { | |
printf("malloc returned null"); | |
return SDL_EVENT_QUIT; | |
} | |
ctx->nbEvenements = 0; | |
// on affecte l'état de l'application avec notre structure qui reflète le contexte d'application | |
*appstate = ctx; | |
ctx->window = SDL_CreateWindow("MA FENETRE", 400, 200, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); | |
if (!ctx->window) { | |
MyApp_HandleError("SDL_CreateWindow"); | |
return SDL_EVENT_QUIT; | |
} | |
MyApp_UpdateSurfaceColor(ctx); | |
return 0; | |
} | |
// Boucle infinie | |
int SDL_AppIterate(void *appstate) { | |
Ctx* ctx = appstate; | |
int state = MyApp_UpdateSurfaceColor(ctx); | |
// avoir un Délai pour ne pas surcharger le CPU | |
SDL_Delay(60); | |
return state; | |
} | |
// déclenchement d'un événement | |
int SDL_AppEvent(void *appstate, const SDL_Event *event) { | |
// NE PAS SUPPRIMER LE HANDLE DE L'ÉVÉNEMENT SDL_EVENT_QUIT | |
if (event->type == SDL_EVENT_QUIT || (event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_Q)) { | |
return SDL_EVENT_QUIT; | |
} | |
Ctx* ctx = appstate; | |
ctx->nbEvenements++; | |
printf("\r%ld événements se sont produits", ctx->nbEvenements); | |
return 0; | |
} | |
// Quand l'application doit être terminée | |
void SDL_AppQuit(void *appstate) { | |
printf("\nSQL_QUIT : ON FREE NOTRE CONTEXTE D'APPLICATION\n"); | |
Ctx* context_to_free = appstate; | |
MyApp_FreeContextApp(context_to_free); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment