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
| #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_image.h>
#include "param.h"
typedef struct StringInput
{
//The storage string
char* str;
//The text surface
SDL_Surface *text;
} SDL_StringInput;
SDL_StringInput handle_input(SDL_StringInput textinput,SDL_Event event,TTF_Font *police,SDL_Color couleur)
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Keep a copy of the current version of the string
char* temp = textinput.str;
//If the string less than maximum size
if( strlen(textinput.str) <= 16 )
{
//If the key is a space
if( event.key.keysym.unicode == (Uint16)' ' )
{
//Append the character
textinput.str += (char)event.key.keysym.unicode;
}
//If the key is a number
else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
{
//Append the character
textinput.str += (char)event.key.keysym.unicode;
}
//If the key is a uppercase letter
else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
{
//Append the character
textinput.str += (char)event.key.keysym.unicode;
}
//If the key is a lowercase letter
else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
{
//Append the character
textinput.str += (char)event.key.keysym.unicode;
}
}
//If backspace was pressed and the string isn't blank
if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( strlen(textinput.str) != 0 ) )
{
//Remove a character from the end
textinput.str[strlen(textinput.str)-1] =0;
}
//If the string was changed
if( textinput.str != temp )
{
//Free the old surface
//SDL_FreeSurface( textinput.text );
//Render a new text surface
textinput.text = TTF_RenderText_Blended( police, textinput.str, couleur );
}
}
return textinput;
} |