projtec:sdl2
This is an old revision of the document!
Table of Contents
SDL2
Une petite coquille pour bien débuter
Notre coquille contient la boucle d'évènements principale dans la fonction main() et s'appuie sur quatre fonctions spécifiques :
- init() : …
- render() : …
- process() : …
- clean() : …
Voici le code de cette coquille :
- demo.c
// SDL2 Demo by aurelien.esnard@u-bordeaux.fr #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> // required to load transparent texture from PNG #include <SDL2/SDL_ttf.h> // required to use TTF fonts #include <stdio.h> #include <stdbool.h> /* **************************************************************** */ #ifdef __ANDROID__ #define PRINT(STR, ...) do { SDL_Log(STR, ##__VA_ARGS__); } while(0) #define ERROR(STR, ...) do { SDL_Log(STR, ##__VA_ARGS__); exit(EXIT_FAILURE); } while(0) // #define LOG(TAG, STR, ...) __android_log_print(ANDROID_LOG_VERBOSE, TAG ,STR, ##__VA_ARGS__) # else #define PRINT(STR, ...) do { printf(STR, ##__VA_ARGS__); } while(0) #define ERROR(STR, ...) do { fprintf(stderr, STR, ##__VA_ARGS__); exit(EXIT_FAILURE); } while(0) #endif /* **************************************************************** */ #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 /* **************************************************************** */ /* your main environment structure */ typedef struct { /* PUT YOUR VARIABLES HERE */ } Env; /* **************************************************************** */ void init(SDL_Window* win, SDL_Renderer* ren, Env * env) { /* PUT YOUR CODE HERE TO INIT TEXTURES, ... */ } /* **************************************************************** */ void render(SDL_Window* win, SDL_Renderer* ren, Env * env) { /* PUT YOUR CODE HERE TO RENDER TEXTURES, ... */ } /* **************************************************************** */ void clean(SDL_Window* win, SDL_Renderer* ren, Env * env) { /* PUT YOUR CODE HERE TO CLEAN MEMORY */ } /* **************************************************************** */ bool process(SDL_Window* win, SDL_Renderer* ren, Env * env, SDL_Event * e) { if (e->type == SDL_QUIT) { return true; } /* PUT YOUR CODE HERE TO PROCESS EVENTS */ return false; } /* **************************************************************** */ int main(int argc, char * argv[]) { /* initialize SDL2 and some extensions */ if(SDL_Init(SDL_INIT_VIDEO) != 0) ERROR("SDL_Init VIDEO"); if(IMG_Init(IMG_INIT_PNG & IMG_INIT_PNG) != IMG_INIT_PNG) ERROR("IMG_Init PNG"); if(TTF_Init() != 0) ERROR("TTF_Init"); /* create window and renderer */ SDL_Window * win = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); if(!win) ERROR("SDL_CreateWindow"); SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if(!ren) ERROR("SDL_CreateWindow"); /* initialize your environment */ Env env; init(win, ren, &env); /* main render loop */ bool quit = false; while (!quit) { /* manage events */ SDL_Event e; while (SDL_PollEvent(&e)) { /* process your events */ quit = process(win, ren, &env, &e); if(quit) break; } /* background in gray */ SDL_SetRenderDrawColor(ren, 0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(ren); /* render all what you want */ render(win, ren, &env); SDL_RenderPresent(ren); SDL_Delay(10); } /* clean your environment */ clean(win, ren, &env); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); IMG_Quit(); TTF_Quit(); SDL_Quit(); return EXIT_SUCCESS; }
Petite Démo
Pour illustrer notre petite coquille, voici une petite démo qui montre les différentes possibilités de SDL2 (texte TTF, texture transparente PNG, …). Vous trouverez dans l'archive sdl2-demo.zip les fichiers suivants :
├── Arial.ttf ├── background.png ├── bomb.png ├── demo.c ├── Makefile ├── mario.png └── README.txt
Pour compiler :
gcc -std=c99 -O3 -Wall -c -o demo.o demo.c gcc -rdynamic -lm -lSDL2 -lSDL2_ttf -lSDL2_image demo.o -o demo
Attention, vous devez satisfaire quelques dépendances. Sous Debian :
sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev
Portage sous Android
coming soon!
projtec/sdl2.1486588345.txt.gz · Last modified: 2024/03/18 15:05 (external edit)