SDL2.x/Les textures
Apparence
< SDL2.x
La fonction SDL_LoadBMP() charge une image au format BMP dans un type SDL_Surface. On convertit alors cette surface en SDL_Texture avec la fonction SDL_CreateTextureFromSurface(). Cette texture peut alors être affichée de façon optimale dans la boucle principale:
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
int main()
{
int i;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *win =
SDL_CreateWindow("Hello SDL2",
100, 100, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer *ren =
SDL_CreateRenderer(win, -1,
SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC);
SDL_Surface *bmp =
SDL_LoadBMP("./my_img.bmp");
SDL_Texture *tex =
SDL_CreateTextureFromSurface(ren, bmp);
SDL_FreeSurface(bmp);
for (i = 0; i < 20; i++) {
SDL_RenderClear(ren);
SDL_RenderCopy(ren, tex, NULL, NULL);
SDL_RenderPresent(ren);
SDL_Delay(100);
}
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}