User Tools

Site Tools


projtec:sdl2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
projtec:sdl2 [2017/02/13 09:46] orelprojtec:sdl2 [2024/03/18 15:06] (current) – external edit 127.0.0.1
Line 6: Line 6:
   * https://www.libsdl.org/projects/SDL_ttf/   * https://www.libsdl.org/projects/SDL_ttf/
   * https://www.libsdl.org/projects/SDL_image/   * https://www.libsdl.org/projects/SDL_image/
 +
 +Voici quelques tuoriels utiles pour bien commençer :
 +
 +  * http://lazyfoo.net/tutorials/SDL/
 +
 +Nous proposons ici un modèle de programme avec SDL2, et une petit exemple d'utilisation de ce modèle. La démo montre diverses possibilités de SDL2, comme le texte TTF, les textures transparentes en PNG, ... Vous trouverez tout cela dans l'archive : {{:projtec:sdl2-demo.zip}}. Lire le README.txt pour démarrer.
 +
 +
 +{{ :projtec:sdl2-demo.png?direct&200 |}}
 +
  
 ==== Un Modèle de Programme SDL2 ==== ==== Un Modèle de Programme SDL2 ====
Line 11: Line 21:
 Notre modèle se compose de trois fichiers : //main.c//, //model.h// et //model.c//. Le fichier //main.c// contient le programme principal avec la boucle d'évènements. Il s'appuie sur les quatre fonctions declarées dans //model.h// et qu'il faut ensuite implanter dans //model.c//. Notre modèle se compose de trois fichiers : //main.c//, //model.h// et //model.c//. Le fichier //main.c// contient le programme principal avec la boucle d'évènements. Il s'appuie sur les quatre fonctions declarées dans //model.h// et qu'il faut ensuite implanter dans //model.c//.
  
-  * init() : cette fonction permet l'initialisation de la structure Env qui contient toutes les variables utiles à votre programme, comme par exemple les textures SDL. +  * La fonction //init()// permet l'initialisation de la structure Env qui contient toutes les variables utiles à votre programme, comme par exemple les textures SDL. 
-  * clean() : cette fonction permet principalement de terminer votre programme, en libérant les allocations effectuées dans la phase d'initialisation..+  * La fonction //clean()// permet principalement de terminer votre programme, en libérant les allocations mémoire effectuées dans la phase d'initialisation. 
-  * process() : cette fonction est appelée dans la boucle principale de votre programme, afin de traiter tous les èvenements SDL2 (souris, clavier, ...). +  * La fonction //process()// est appelée dans la boucle principale de votre programme, afin de traiter tous les èvenements SDL2 (souris, clavier, ...). 
-  * render() : cette fonction permet principalement de terminer votre programme, afin d'affectuer le rendu graphique de votre programme (affichage des différentes textures, ...).+  * La fonction //render()// est appelée dans la boucle principale de votre programme, afin d’effectuer le rendu graphique des différents éléments (textures, ...).
  
 Voici la déclaration des fonctions utiles à notre modèle : Voici la déclaration des fonctions utiles à notre modèle :
Line 41: Line 51:
 #define PRINT(STR, ...) do { SDL_Log(STR, ##__VA_ARGS__);  } while(0) #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 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 # else
 #define PRINT(STR, ...) do { printf(STR, ##__VA_ARGS__); } while(0) #define PRINT(STR, ...) do { printf(STR, ##__VA_ARGS__); } while(0)
Line 65: Line 74:
 // SDL2 Demo by aurelien.esnard@u-bordeaux.fr // SDL2 Demo by aurelien.esnard@u-bordeaux.fr
            
-#include <SDL2/SDL.h> +#include <SDL.h> 
-#include <SDL2/SDL_image.h>  // required to load transparent texture from PNG +#include <SDL_image.h>  // required to load transparent texture from PNG 
-#include <SDL2/SDL_ttf.h>    // required to use TTF fonts     +#include <SDL_ttf.h>    // required to use TTF fonts     
 #include <stdio.h> #include <stdio.h>
 #include <stdbool.h> #include <stdbool.h>
Line 77: Line 86:
  
   /* initialize SDL2 and some extensions */   /* initialize SDL2 and some extensions */
-  if(SDL_Init(SDL_INIT_VIDEO) != 0) ERROR("SDL_Init VIDEO");  +  if(SDL_Init(SDL_INIT_VIDEO) != 0) ERROR("Error: SDL_Init VIDEO (%s)", SDL_GetError());   
-  if(IMG_Init(IMG_INIT_PNG & IMG_INIT_PNG) != IMG_INIT_PNG) ERROR("IMG_Init PNG"); +  if(IMG_Init(IMG_INIT_PNG & IMG_INIT_PNG) != IMG_INIT_PNG) ERROR("Error: IMG_Init PNG (%s)", SDL_GetError());   
-  if(TTF_Init() != 0) ERROR("TTF_Init");  +  if(TTF_Init() != 0) ERROR("Error: TTF_Init (%s)", SDL_GetError());  
            
   /* create window and renderer */   /* create window and renderer */
   SDL_Window * win = SDL_CreateWindow(APP_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,   SDL_Window * win = SDL_CreateWindow(APP_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
           SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);           SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
-  if(!win) ERROR("SDL_CreateWindow");  +  if(!win) ERROR("Error: SDL_CreateWindow (%s)", SDL_GetError());  
   SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);   SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
-  if(!ren) ERROR("SDL_CreateWindow");    +  if(!ren) ERROR("Error: SDL_CreateRenderer (%s)", SDL_GetError());  
            
   /* initialize your environment */   /* initialize your environment */
Line 132: Line 141:
 // SDL2 Demo by aurelien.esnard@u-bordeaux.fr // SDL2 Demo by aurelien.esnard@u-bordeaux.fr
  
-#include <SDL2/SDL.h> +#include <SDL.h> 
-#include <SDL2/SDL_image.h>  // required to load transparent texture from PNG +#include <SDL_image.h>  // required to load transparent texture from PNG 
-#include <SDL2/SDL_ttf.h>    // required to use TTF fonts     +#include <SDL_ttf.h>    // required to use TTF fonts     
  
 #include <stdio.h> #include <stdio.h>
Line 192: Line 201:
 </code> </code>
  
-==== Démo  ==== 
- 
-Pour illustrer notre modèle, 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'archive : {{:projtec:sdl2-demo.zip}}, ainsi que le modèle. Lire le README.txt pour démarrer. 
  
 ==== Portage sous Android  ==== ==== Portage sous Android  ====
  
-Le code proposé ci-dessus est 100% portable sous Android... Plus dé détails bientôt.+Le code proposé ci-dessus est 100% portable sous Android : [[projtec:android|c'est ici !]]
  
projtec/sdl2.1486979161.txt.gz · Last modified: 2024/03/18 15:05 (external edit)