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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| #include <SDL/SDL.h> // for SDL_Surface
#include <SDL/SDL_ttf.h> // for TTF_Font
int main()
{
TTF_Font *font = NULL;
SDL_Surface *screen_surface = NULL;
SDL_Surface *font_surface = NULL;
char text[4];
strcpy(text, "aàâ\0");
SDL_Color color;
color.r = 255;
color.g = 255;
color.b = 255;
// Initialize SDL
if((SDL_Init(SDL_INIT_VIDEO)==-1))
{
printf("SDL_Init: %s\n", SDL_GetError());
exit(1);
}
// Enable UNICODE
SDL_EnableUNICODE(1);
// Create the screen surface
screen_surface = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
if(screen_surface == NULL)
{
printf("SDL_SetVideoMode: %s\n", SDL_GetError());
exit(1);
}
// Initialize TTF
if(TTF_Init()==-1)
{
printf("TTF_Init: %s\n", TTF_GetError());
exit(1);
}
// Load the font
font = TTF_OpenFont("font.ttf", 48);
if(font == NULL)
{
printf("TTF_OpenFont: %s\n", TTF_GetError());
exit(1);
}
// Display the text in the console
printf("text = %s\n", text);
// Create the font surface
font_surface = TTF_RenderUTF8_Solid(font, text, color);
if(font_surface == NULL)
{
printf("TTF_RenderUTF8_Solid: %s\n", TTF_GetError());
exit(1);
}
// Blit to the screen surface
if(SDL_BlitSurface(font_surface, NULL, screen_surface, NULL) < 0)
{
printf("SDL_BlitSurface: %s\n", SDL_GetError());
exit(1);
}
SDL_UpdateRect(screen_surface, 0, 0, 0, 0);
SDL_Event event;
while(SDL_WaitEvent(&event))
{
if(event.type == SDL_QUIT)
{
break;
}
if(event.type == SDL_KEYDOWN)
{
SDL_FillRect(screen_surface, NULL, 0);
// Display the text in the console
printf("%c\n", event.key.keysym.unicode);
// Create the font surface
font_surface = TTF_RenderUNICODE_Solid(font, &event.key.keysym.unicode, color);
if(font_surface == NULL)
{
printf("TTF_RenderUNICODE_Solid: %s\n", TTF_GetError());
exit(1);
}
// Blit to the screen surface
if(SDL_BlitSurface(font_surface, NULL, screen_surface, NULL) < 0)
{
printf("SDL_BlitSurface: %s\n", SDL_GetError());
exit(1);
}
SDL_UpdateRect(screen_surface, 0, 0, 0, 0);
}
}
// Quit
SDL_Quit();
exit(0);
} |
Partager