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/08 23:21] 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 : //model.c////model_func.h// et //model_func.c//. Le fichier //model.c// contient le programme principal (fonction //main()// qui s'appuie sur quatre fonctions abstraites définies dans le module //model_func.h & model_func.c// :+  * http://lazyfoo.net/tutorials/SDL/
  
-  * init() : ... +Nous proposons ici un modèle de programme avec SDL2, et une petit exemple d'utilisation de ce modèleLa 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.
-  * render() : ... +
-  * process() : ... +
-  * clean() : ...+
  
-Voici le code de cette coquille : 
  
-<code C model.c+{{ :projtec:sdl2-demo.png?direct&200 |}} 
-// SDL2 Demo by aurelien.esnard@u-bordeaux.fr + 
-     + 
 +==== 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 <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 <stdio.h>
 #include <stdbool.h> #include <stdbool.h>
-#include "model_func.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>
 +
 +
 +Voici le code du programme principal :
 +
 +<code C main.c>
 +// SDL2 Demo by aurelien.esnard@u-bordeaux.fr
            
-#define SCREEN_WIDTH 640 +#include <SDL.h> 
-#define SCREEN_HEIGHT 480 +#include <SDL_image.h>  // required to load transparent texture from PNG 
-     +#include <SDL_ttf.h>    // required to use TTF fonts      
 +#include <stdio.h> 
 +#include <stdbool.h> 
 +#include "model.h" 
 +         
 /* **************************************************************** */ /* **************************************************************** */
            
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 */
-  Env * env = init(win, ren);+  Env * env = init(win, ren, argc, argv);
            
   /* main render loop */   /* main render loop */
Line 87: Line 135:
 </code> </code>
  
-<code C model_func.h> 
-// SDL2 Demo by aurelien.esnard@u-bordeaux.fr 
  
-#ifndef MODEL_FUNC_H +Et voici une coquille vide pour implanter les fonctions de notre modèle :
-#define MODEL_FUNC_H+
  
-#include <SDL2/SDL.h> +<code C model.c>
-#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); +
-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_func.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>
 #include <stdlib.h> #include <stdlib.h>
 #include <stdbool.h> #include <stdbool.h>
-#include "model_func.h"+#include "model.h"
  
 /* **************************************************************** */ /* **************************************************************** */
Line 144: Line 160:
 /* **************************************************************** */ /* **************************************************************** */
            
-Env * init(SDL_Window* win, SDL_Renderer* ren)+Env * init(SDL_Window* win, SDL_Renderer* ren, int argc, char* argv[])
 {   {  
   Env * env = malloc(sizeof(struct Env_t));   Env * env = malloc(sizeof(struct Env_t));
Line 182: Line 198:
  
   free(env);   free(env);
-    +}
 </code> </code>
  
- 
-==== 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 tout cela dans l'archive : {{:projtec:sdl2-demo.zip}}. Lire le README.txt pour démarrer. 
- 
-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  ==== ==== Portage sous Android  ====
  
-//coming soon!//+Le code proposé ci-dessus est 100% portable sous Android : [[projtec:android|c'est ici !]]
  
projtec/sdl2.1486596066.txt.gz · Last modified: 2024/03/18 15:05 (external edit)