< SDL2.x
fin de la boite de navigation du chapitre

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:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <SDL2/SDL.h>
 4 
 5 int main()
 6 {
 7     int i;
 8 
 9     SDL_Init(SDL_INIT_VIDEO);
10 
11     SDL_Window *win =
12         SDL_CreateWindow("Hello SDL2",
13                 100, 100, 640, 480, SDL_WINDOW_SHOWN);
14 
15     SDL_Renderer *ren =
16         SDL_CreateRenderer(win, -1,
17                 SDL_RENDERER_ACCELERATED |
18                 SDL_RENDERER_PRESENTVSYNC);
19 
20     SDL_Surface *bmp =
21         SDL_LoadBMP("./my_img.bmp");
22 
23     SDL_Texture *tex =
24         SDL_CreateTextureFromSurface(ren, bmp);
25 
26     SDL_FreeSurface(bmp);
27 
28     for (i = 0; i < 20; i++) {
29         SDL_RenderClear(ren);
30         SDL_RenderCopy(ren, tex, NULL, NULL);
31         SDL_RenderPresent(ren);
32         SDL_Delay(100);
33     }
34 
35     SDL_DestroyTexture(tex);
36     SDL_DestroyRenderer(ren);
37     SDL_DestroyWindow(win);
38     SDL_Quit();
39 
40     return 0;
41 }
Cet article est issu de Wikiversity. Le texte est sous licence Creative Commons - Attribution - Partage dans les Mêmes. Des conditions supplémentaires peuvent s'appliquer aux fichiers multimédias.