Bonjour,

J'ai déjà posté sur la mailing-list de SDL pour ce problème mais je n'ai pas eu de réponse... Alors j’essaie ici en espérant avoir plus de chance.

J'ai un programme écrit en C[1] utilisant la bibliothèque SDL.
Le code source est encodé en UTF-8.
Donc quand je souhaite afficher du texte codé en dur je fais ça:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
TTF_RenderUTF8_Solid(font, text, color);
Maintenant, je souhaite afficher également le texte saisi par l'utilisateur.
Les caractères saisis par l'utilisateur semblent être encodés en UTF-16:
typedef struct{
[...]
Uint16 unicode;
} SDL_keysym;
Donc quand je souhaite afficher le texte saisis par l'utilisateur je fais ça :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
TTF_RenderUNICODE_Solid(font, &event.key.keysym.unicode, color);
Le problème c'est que je souhaite avoir le même encodage (UTF-8) dans les deux cas.

J'ai entendu parlé d'une solution avec iconv mais peut être que SDL a déjà une solution toute faite pour ça.

Y a t'il une solution simple pour récupérer la saisie de l'utilisateur en UTF-8 ?
Est-ce que vous pouvez me confirmer que j'ai bien de l'UTF-16 dans SDL_keysym.unicode ?
Pourquoi les caractères en UTF-16 s'affichent très bien dans ma console sans conversion nécessaire vers de l'UTF-8 alors que quand j'essaie de les afficher avec la fonction TTF_RenderUTF8_Solid, les caractères ne s'affichent pas correctement?
Est-ce que vous pouvez me montrer sur ce petit exemple comment appliquer la solution iconv pour convertir mon caractère UTF-16 en UTF-8 ?

Merci.

[1]
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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);
}