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 : 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() : …
  • render() : …
  • process() : …
  • clean() : …

Voici le code du programme principal :

main.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.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, argc, argv);
 
  /* 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.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;
 
/* **************************************************************** */
 
#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
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 <stdlib.h>
#include <stdbool.h>
#include "model.h"
 
/* **************************************************************** */
 
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);
}

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.

Portage sous Android

coming soon!

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