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/09 10:29] – [Petite Démo] orelprojtec:sdl2 [2024/03/18 15:06] (current) – external edit 127.0.0.1
Line 7: Line 7:
   * https://www.libsdl.org/projects/SDL_image/   * https://www.libsdl.org/projects/SDL_image/
  
-==== Une petite coquille pour bien débuter ====+Voici quelques tuoriels utiles pour bien commençer :
  
-Notre coquille 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// :+  * 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 ==== 
 + 
 +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//
 + 
 +  * La fonction //init()// permet l'initialisation de la structure Env qui contient toutes les variables utiles à votre programme, comme par exemple les textures SDL. 
 +  * La fonction //clean()// permet principalement de terminer votre programme, en libérant les allocations mémoire effectuées dans la phase d'initialisation. 
 +  * La fonction //process()// est appelée dans la boucle principale de votre programme, afin de traiter tous les èvenements SDL2 (souris, clavier, ...). 
 +  * 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 : 
 + 
 +<code C model.h> 
 +// SDL2 Model by aurelien.esnard@u-bordeaux.fr 
 + 
 +#ifndef MODEL_H 
 +#define MODEL_H 
 + 
 +#include <SDL2/SDL.h> 
 +#include <stdio.h> 
 +#include <stdbool.h> 
 + 
 +typedef struct Env_t Env; 
 + 
 +/* **************************************************************** */ 
 +      
 +#define APP_NAME "Hello World!" 
 +#define SCREEN_WIDTH 600 
 +#define SCREEN_HEIGHT 600 
 + 
 +/* **************************************************************** */ 
 +     
 +#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)  
 +# 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 
 + 
 +/* **************************************************************** */ 
 + 
 +Env * init(SDL_Window* win, SDL_Renderer* ren, int argc, char* argv[]); 
 +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); 
 + 
 +/* **************************************************************** */ 
 + 
 +#endif 
 +</code>
  
-  * init() : ... 
-  * render() : ... 
-  * process() : ... 
-  * clean() : ... 
  
 Voici le code du programme principal : Voici le code du programme principal :
Line 21: 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>
 #include "model.h" #include "model.h"
-      +         
-/* **************************************************************** */ +
-      +
-#define SCREEN_WIDTH 640 +
-#define SCREEN_HEIGHT 480 +
-     +
 /* **************************************************************** */ /* **************************************************************** */
            
Line 38: 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("Hello World!", 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 87: Line 135:
 </code> </code>
  
-<code C model.h> 
-// SDL2 Model by aurelien.esnard@u-bordeaux.fr 
  
-#ifndef MODEL_H +Et voici une coquille vide pour implanter les fonctions de notre modèle :
-#define MODEL_H +
- +
-#include <SDL2/SDL.h> +
-#include <stdio.h> +
-#include <stdbool.h> +
- +
-typedef struct Env_t Env; +
- +
-/* **************************************************************** */ +
-      +
-#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 +
- +
-/* **************************************************************** */ +
- +
-Env * init(SDL_Window* win, SDL_Renderer* ren, int argc, char* argv[]); +
-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); +
- +
-/* **************************************************************** */ +
- +
-#endif +
-</code>+
  
 <code C model.c> <code C model.c>
 // 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 184: Line 200:
 } }
 </code> </code>
- 
- 
- 
  
  
 ==== Portage sous Android  ==== ==== Portage sous Android  ====
  
-//coming soon!//+Le code proposé ci-dessus est 100% portable sous Android : [[projtec:android|c'est ici !]]
  
projtec/sdl2.1486636199.txt.gz · Last modified: 2024/03/18 15:05 (external edit)