1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
int main(int argc, char *argv[])
{
SDL_Surface *ecran,*text; /*surface de l'ecran*/
TTF_Font *police;
SDL_Event event;
SDL_Color bleu = {70,100,255};
SDL_Rect pos = {0,0};
char ch, chaine[100] = "Tapez Votre Text...";
char continu = 1, taille = 0;
if (SDL_Init(SDL_INIT_VIDEO) < 0) // Démarrage de la SDL. Si erreur alors...
{
fprintf(stderr, "Erreur d'initialisation de la SDL : %s", SDL_GetError()); // Ecriture de l'erreur
exit(EXIT_FAILURE); // ...On quitte le programme
}
ecran = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);
TTF_Init();
police = TTF_OpenFont("FreeSerif.ttf",50);
if( police == NULL)
{
fprintf(stderr, "Erreur chargement de la police\n");
return EXIT_FAILURE;
}
SDL_EnableUNICODE(1);
while(continu)
{
if(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT : continu = 0;
break;
case SDL_KEYDOWN:
if((event.key.keysym.unicode & 0xFF80) == 0 && taille < 100)
{
ch = event.key.keysym.unicode & 0x7F;
chaine[taille] = ch;
chaine[++taille] = '\0';
}
break;
}
}
else
{
SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,0,0,0));
text = (SDL_Surface*) TTF_RenderText_Solid(police,chaine,bleu);
SDL_BlitSurface(text,NULL,ecran,&pos);
SDL_FreeSurface(text);
SDL_Flip(ecran); /*Dessiner la surface*/
}
}
TTF_CloseFont(police);
TTF_Quit();
SDL_Quit();
return 0;
} |
Partager