Writing /net/www/auesnard/teaching/data/meta/projtec/sdl2.meta failed
projtec:sdl2
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
projtec:sdl2 [2017/02/08 21:39] – orel | projtec:sdl2 [2024/03/18 15:06] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== SDL2 ====== | ====== SDL2 ====== | ||
- | ==== Une petite coquille pour bien débuter ==== | + | SDL2 est un blibliothèque qui permet de développer des applications graphiques, comme de jeux 2D. Un des points fort de SDL2 est incontestablement sa simplicité et sa portabilité sous Android ! |
- | Notre coquille contient la boucle d' | + | * https://wiki.libsdl.org/ |
+ | * https:// | ||
+ | * https:// | ||
- | * init() | + | Voici quelques tuoriels utiles pour bien commençer |
- | * render() : ... | + | |
- | * process() : ... | + | |
- | * clean() : ... | + | |
- | Voici le code de cette coquille | + | * http:// |
- | <code C model.c> | + | Nous proposons ici un modèle de programme avec SDL2, et une petit exemple d' |
- | // SDL2 Demo by aurelien.esnard@u-bordeaux.fr | + | |
- | #include < | ||
- | #include < | ||
- | #include < | ||
+ | {{ : | ||
+ | |||
+ | |||
+ | ==== Un Modèle de Programme SDL2 ==== | ||
+ | |||
+ | Notre modèle se compose de trois fichiers : //main.c//, //model.h// et // | ||
+ | |||
+ | * La fonction //init()// permet l' | ||
+ | * La fonction //clean()// permet principalement de terminer votre programme, en libérant les allocations mémoire effectuées dans la phase d' | ||
+ | * La fonction // | ||
+ | * La fonction // | ||
+ | |||
+ | Voici la déclaration des fonctions utiles à notre modèle : | ||
+ | |||
+ | <code C model.h> | ||
+ | // SDL2 Model by aurelien.esnard@u-bordeaux.fr | ||
+ | |||
+ | #ifndef MODEL_H | ||
+ | #define MODEL_H | ||
+ | |||
+ | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
+ | |||
+ | typedef struct Env_t Env; | ||
/* **************************************************************** */ | /* **************************************************************** */ | ||
+ | |||
+ | #define APP_NAME "Hello World!" | ||
+ | #define SCREEN_WIDTH 600 | ||
+ | #define SCREEN_HEIGHT 600 | ||
+ | /* **************************************************************** */ | ||
+ | | ||
#ifdef __ANDROID__ | #ifdef __ANDROID__ | ||
#define PRINT(STR, ...) do { SDL_Log(STR, | #define PRINT(STR, ...) do { SDL_Log(STR, | ||
#define ERROR(STR, ...) do { SDL_Log(STR, | #define ERROR(STR, ...) do { SDL_Log(STR, | ||
- | // #define LOG(TAG, STR, ...) __android_log_print(ANDROID_LOG_VERBOSE, | ||
# else | # else | ||
#define PRINT(STR, ...) do { printf(STR, ## | #define PRINT(STR, ...) do { printf(STR, ## | ||
Line 35: | Line 58: | ||
/* **************************************************************** */ | /* **************************************************************** */ | ||
- | #define SCREEN_WIDTH 640 | + | Env * init(SDL_Window* win, SDL_Renderer* ren, int argc, char* argv[]); |
- | #define SCREEN_HEIGHT 480 | + | void render(SDL_Window* win, SDL_Renderer* ren, Env * env); |
+ | void clean(SDL_Window* win, SDL_Renderer* ren, Env * env); | ||
+ | bool process(SDL_Window* win, SDL_Renderer* ren, Env * env, SDL_Event * e); | ||
/* **************************************************************** */ | /* **************************************************************** */ | ||
- | /* your main environment structure */ | + | #endif |
- | typedef struct | + | </code> |
- | | + | |
- | } Env; | + | |
- | /* **************************************************************** */ | ||
- | void init(SDL_Window* win, SDL_Renderer* ren, Env * env) | + | Voici le code du programme principal : |
- | { | + | |
- | /* PUT YOUR CODE HERE TO INIT TEXTURES, ... */ | + | |
- | } | + | |
+ | <code C main.c> | ||
+ | // SDL2 Demo by aurelien.esnard@u-bordeaux.fr | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
/* **************************************************************** */ | /* **************************************************************** */ | ||
- | + | ||
- | 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[]) | int main(int argc, char * argv[]) | ||
{ | { | ||
/* initialize SDL2 and some extensions */ | /* initialize SDL2 and some extensions */ | ||
- | if(SDL_Init(SDL_INIT_VIDEO) != 0) ERROR(" | + | if(SDL_Init(SDL_INIT_VIDEO) != 0) ERROR(" |
- | if(IMG_Init(IMG_INIT_PNG & IMG_INIT_PNG) != IMG_INIT_PNG) ERROR(" | + | if(IMG_Init(IMG_INIT_PNG & IMG_INIT_PNG) != IMG_INIT_PNG) ERROR(" |
- | if(TTF_Init() != 0) ERROR(" | + | if(TTF_Init() != 0) ERROR(" |
+ | | ||
/* create window and renderer */ | /* create window and renderer */ | ||
- | SDL_Window * win = SDL_CreateWindow("Hello World!" | + | SDL_Window * win = SDL_CreateWindow(APP_NAME, SDL_WINDOWPOS_UNDEFINED, |
- | | + | |
- | if(!win) ERROR(" | + | if(!win) ERROR(" |
SDL_Renderer * ren = SDL_CreateRenderer(win, | SDL_Renderer * ren = SDL_CreateRenderer(win, | ||
- | if(!ren) ERROR(" | + | if(!ren) ERROR(" |
+ | | ||
/* initialize your environment */ | /* initialize your environment */ | ||
- | Env env; | + | Env * env = init(win, ren, argc, argv); |
- | | + | |
- | + | ||
/* main render loop */ | /* main render loop */ | ||
bool quit = false; | bool quit = false; | ||
while (!quit) { | while (!quit) { | ||
+ | |||
/* manage events */ | /* manage events */ | ||
SDL_Event e; | SDL_Event e; | ||
while (SDL_PollEvent(& | while (SDL_PollEvent(& | ||
/* process your events */ | /* process your events */ | ||
- | quit = process(win, | + | quit = process(win, |
if(quit) break; | if(quit) break; | ||
} | } | ||
- | | + | |
/* background in gray */ | /* background in gray */ | ||
SDL_SetRenderDrawColor(ren, | SDL_SetRenderDrawColor(ren, | ||
SDL_RenderClear(ren); | SDL_RenderClear(ren); | ||
+ | |||
/* render all what you want */ | /* render all what you want */ | ||
- | render(win, ren, &env); | + | render(win, ren, env); |
SDL_RenderPresent(ren); | SDL_RenderPresent(ren); | ||
SDL_Delay(10); | SDL_Delay(10); | ||
} | } | ||
- | | + | |
/* clean your environment */ | /* clean your environment */ | ||
- | clean(win, ren, &env); | + | clean(win, ren, env); |
| | ||
SDL_DestroyRenderer(ren); | SDL_DestroyRenderer(ren); | ||
Line 129: | Line 130: | ||
TTF_Quit(); | TTF_Quit(); | ||
SDL_Quit(); | SDL_Quit(); | ||
+ | |||
return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||
} | } | ||
Line 135: | Line 136: | ||
- | ==== Petite Démo ==== | + | Et voici une coquille vide pour implanter les fonctions de notre modèle : |
- | 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 tout cela dans l' | + | <code C model.c> |
+ | // SDL2 Demo by aurelien.esnard@u-bordeaux.fr | ||
- | Pour compiler : | + | #include < |
+ | #include < | ||
+ | #include < | ||
- | gcc -std=c99 -O3 -Wall -c -o demo.o demo.c | + | #include <stdio.h> |
- | gcc -rdynamic -lm -lSDL2 -lSDL2_ttf -lSDL2_image demo.o -o demo | + | #include < |
+ | #include <stdbool.h> | ||
+ | #include "model.h" | ||
- | Attention, vous devez satisfaire quelques dépendances. Sous Debian : | + | /* **************************************************************** */ |
+ | |||
+ | struct Env_t { | ||
+ | |||
+ | /* PUT YOUR VARIABLES HERE */ | ||
+ | |||
+ | }; | ||
+ | |||
+ | /* **************************************************************** */ | ||
+ | |||
+ | Env * init(SDL_Window* win, SDL_Renderer* ren, int argc, char* argv[]) | ||
+ | { | ||
+ | Env * env = malloc(sizeof(struct Env_t)); | ||
+ | |||
+ | /* PUT YOUR CODE HERE TO INIT TEXTURES, ... */ | ||
+ | |||
+ | return env; | ||
+ | } | ||
+ | |||
+ | /* **************************************************************** */ | ||
+ | |||
+ | void render(SDL_Window* win, SDL_Renderer* ren, Env * env) | ||
+ | { | ||
+ | /* PUT YOUR CODE HERE TO RENDER TEXTURES, ... */ | ||
+ | } | ||
+ | |||
+ | /* **************************************************************** */ | ||
+ | |||
+ | |||
+ | 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; | ||
+ | } | ||
+ | |||
+ | /* **************************************************************** */ | ||
+ | |||
+ | void clean(SDL_Window* win, SDL_Renderer* ren, Env * env) | ||
+ | { | ||
+ | /* PUT YOUR CODE HERE TO CLEAN MEMORY */ | ||
+ | |||
+ | free(env); | ||
+ | } | ||
+ | </ | ||
- | sudo apt-get install libsdl2-dev libsdl2-image-dev | ||
==== Portage sous Android | ==== Portage sous Android | ||
- | //coming soon!// | + | Le code proposé ci-dessus est 100% portable sous Android : [[projtec: |
projtec/sdl2.1486589962.txt.gz · Last modified: 2024/03/18 15:05 (external edit)