User Tools

Site Tools


projtec:sdl2

This is an old revision of the document!


SDL2

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 !

Une petite coquille pour bien débuter

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 :

  • init() : …
  • render() : …
  • process() : …
  • clean() : …

Voici le code de cette coquille :

model.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>
#include "model_func.h"
 
/* **************************************************************** */
 
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
 
/* **************************************************************** */
 
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);
 
  /* 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;
}
model_func.h
// SDL2 Demo by aurelien.esnard@u-bordeaux.fr
 
#ifndef MODEL_FUNC_H
#define MODEL_FUNC_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);
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
model_func.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 <stdlib.h>
#include <stdbool.h>
#include "model_func.h"
 
/* **************************************************************** */
 
struct Env_t {  
 
  /* PUT YOUR VARIABLES HERE */
 
}; 
 
/* **************************************************************** */
 
Env * init(SDL_Window* win, SDL_Renderer* ren)
{  
  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);
}     

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 : 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

coming soon!

projtec/sdl2.1486596066.txt.gz · Last modified: 2024/03/18 15:05 (external edit)