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
| void SDL_TxtInput(int nbchar_max, char *string, SDL_Rect txtPosition, SDL_Color color, TTF_Font *font, SDL_Surface *screen)
{
SDL_Event event;
char continueLoop = 1;
unsigned char key = 0;
int curent_charNumber = 0;
SDL_Surface *txtSurface = NULL;
SDL_Surface *cursorSurface = NULL;
SDL_Rect cursorPosition;
cursorSurface = TTF_RenderText_Blended(font, "|", color);
cursorPosition.x = txtPosition.x;
cursorPosition.y = txtPosition.y;
SDL_EnableUNICODE(1);
SDL_EnableKeyRepeat(90, 90);
string[0] = '\0';
while(continueLoop)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.unicode == SDLK_RETURN)
continueLoop = 0;
else if(event.key.keysym.unicode == SDLK_BACKSPACE && string[0] != '\0')
{
curent_charNumber--;
}
else if(event.key.keysym.unicode >= 32 && event.key.keysym.unicode <= 255 && curent_charNumber < nbchar_max - 1)
{
key = event.key.keysym.unicode;
string[curent_charNumber] = key;
curent_charNumber++;
}
string[curent_charNumber] = '\0';
event.key.keysym.unicode = 0;
SDL_FreeSurface(txtSurface);
txtSurface = TTF_RenderText_Blended(font, string, color);
if(string[0] != '\0')
cursorPosition.x = txtPosition.x + txtSurface->w;
else if(string[0] == '\0')
cursorPosition.x = txtPosition.x;
}
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
SDL_BlitSurface(txtSurface, NULL, screen, &txtPosition);
SDL_BlitSurface(cursorSurface, NULL, screen, &cursorPosition);
SDL_Flip(screen);
SDL_Delay(5);
}
SDL_EnableUNICODE(0);
SDL_FreeSurface(cursorSurface);
SDL_FreeSurface(txtSurface);
} |
Partager