Advertisement
Benjamin_Loison

Text and image SDL

May 1st, 2017
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <SDL2/SDL_image.h>
  2. #include <SDL2/SDL.h>
  3. #include <SDL2/SDL_ttf.h>
  4.  
  5. int main (int argc, char** argv)
  6. {
  7.  
  8.     SDL_Window *ecran = NULL;
  9.     SDL_Surface *texte = NULL, *fond = NULL;
  10.     SDL_Rect position;
  11.     SDL_Event event;
  12.     TTF_Font *police = NULL;
  13.     SDL_Color couleurNoire;
  14.     couleurNoire.r = 0;
  15.     couleurNoire.g = 0;
  16.     couleurNoire.b = 0;
  17.     int continuer = 1;
  18.  
  19.     SDL_Init(SDL_INIT_VIDEO);
  20.     TTF_Init();
  21.  
  22.     //*** ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  23.     //*** SDL_WM_SetCaption("Gestion du texte avec SDL_ttf", NULL);***/
  24.  
  25.     ecran = SDL_CreateWindow("test", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
  26.     SDL_Surface *pSurf = SDL_GetWindowSurface(ecran);
  27.  
  28.     fond = IMG_Load("essai1.png");
  29.  
  30.     /* Chargement de la police */
  31.     police = TTF_OpenFont("arial.ttf", 55);
  32.     /* Écriture du texte dans la SDL_Surface texte en mode Blended (optimal) */
  33.     texte = TTF_RenderText_Blended(police, "al", couleurNoire);
  34.  
  35.     while (continuer)
  36.     {
  37.     SDL_WaitEvent(&event);
  38.     switch(event.type)
  39.     {
  40.         case SDL_QUIT:
  41.         continuer = 0;
  42.         break;
  43.     }
  44.         SDL_FillRect(pSurf, NULL, SDL_MapRGB(pSurf->format, 255, 255, 255));
  45.         position.x = 0;
  46.         position.y = 0;
  47.         SDL_BlitSurface(fond, NULL, pSurf, &position); /* Blit du fond */
  48.         position.x = 60;
  49.         position.y = 370;
  50.         SDL_BlitSurface(texte, NULL, pSurf, &position); /* Blit du texte */
  51.         SDL_UpdateWindowSurface(ecran);
  52.         }
  53.     TTF_CloseFont(police);
  54.     TTF_Quit();
  55.  
  56.     SDL_FreeSurface(texte);
  57.     SDL_Quit();
  58.  
  59.     return EXIT_SUCCESS;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement