Source:Traitement des entrées avec SDL/input.c
#include <stdio.h>
#include <stdlib.h>
/* Pour utiliser SDL */
#include <SDL/SDL.h>
/* Les joysticks */
SDL_Joystick * tableJoysticks[4];
/* Gestion du clavier */
void InputKeyboardEvent(SDL_KeyboardEvent* event)
{
/* type et state contiennent en fait la même information */
/* On utilise type */
SDL_keysym * keysym = &event->keysym;
if(event->type == SDL_KEYDOWN)
{
if(((keysym->unicode & 0xFF80) == 0) && (keysym->unicode > 32))
{
printf("Touche enfoncee : %c\n",event->keysym.unicode);
}
else
{
printf("Touche enfoncee non traduite\n");
}
printf("Nom SDL de la touche : %s\n",SDL_GetKeyName(keysym->sym));
}
else
{
printf("Touche relevee : %s\n",SDL_GetKeyName(keysym->sym));
}
}
/* Gestion des mouvements de la souris */
void InputMouseMotionEvent(SDL_MouseMotionEvent* event)
{
printf("Mouvement de la souris (%i,%i) . Coordonnees dans la fenetres (%i,%i)",
event->xrel, event->yrel,
event->x , event->y);
if(event->state & SDL_BUTTON(1))
{
printf(" - B1");
}
if(event->state & SDL_BUTTON(2))
{
printf(" - B2");
}
if(event->state & SDL_BUTTON(3))
{
printf(" - B3");
}
printf("\n");
}
/* Gestion des boutons de la souris */
void InputMouseButtonEvent(SDL_MouseButtonEvent* event)
{
if(event->type == SDL_MOUSEBUTTONDOWN)
{
printf("Bouton %i appuye",event->button);
}
else
{
printf("Bouton %i relache",event->button);
}
printf(" aux coordonnees (%i,%i)\n",event->x,event->y);
}
/* Initialisation des joysticks */
void InputInitJoysticks()
{
/* Etat initial. Le hot-plug est specifique OS */
int numJoystick = SDL_NumJoysticks();
int i;
printf("Nombre de joysticks detectes : %i\n",numJoystick);
if(numJoystick>4)
{
printf("Seuls les quatre premiers joysticks seront pris en compte\n");
numJoystick = 4;
}
for(i=0 ; i<numJoystick ; i++)
{
printf("Nom du joystick %i : %s\n", i, SDL_JoystickName(i));
tableJoysticks[i] = SDL_JoystickOpen(i);
if(tableJoysticks[i])
{
printf("Nombre d'axes : %d\n", SDL_JoystickNumAxes (tableJoysticks[i]));
printf("Nombre de boutons : %d\n", SDL_JoystickNumButtons(tableJoysticks[i]));
printf("... joystick ouvert\n");
}
else
{
printf("... probleme d'ouverture avec ce joystick\n");
}
}
for(i=numJoystick ; i<4 ; i++)
{
tableJoysticks[i] = NULL;
}
}
/* Ferme correctement les joysticks */
void InputCloseJoysticks()
{
int i;
for(i=0; i<4; i++)
{
if(tableJoysticks[i])
{
SDL_JoystickClose(tableJoysticks[i]);
printf("Joystick %i ferme\n",i);
}
}
}
/* Gestion des axes d'un joystick */
void InputJoystickAxisEvent(SDL_JoyAxisEvent * event)
{
printf("Axe %i du joystick %i : %i\n",event->axis,event->which,event->value);
}
/* Gestion des buttons d'un joystick */
void InputJoystickButtonEvent(SDL_JoyButtonEvent* event)
{
printf("Bouton %i du joystick %i : ",event->button,event->which);
if(event->state == SDL_PRESSED)
{
printf("appuye\n");
}
else
{
printf("relache\n");
}
}
void Quitter()
{
InputCloseJoysticks();
SDL_Quit();
}
/* Boucle principale */
int main(int argc, char *argv[])
{
/* Surface écran */
SDL_Surface * sdlMainScreen;
/* Drapeau de sortie */
int quitProgram = 0;
/* Initialisation de la partie Vidéo de SDL */
if (SDL_Init (SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0)
/* En cas d'échec, sortie */
exit (1);
/* Lorsque le programme se termine, il ferme SDL correctement */
atexit(Quitter);
/* Demande d'affichage en 640x480 16 bits */
sdlMainScreen = SDL_SetVideoMode (64, 48, 16, SDL_SWSURFACE);
/* En cas d'echec, on sort */
if (sdlMainScreen == NULL)
exit (2);
SDL_EnableUNICODE(1);
InputInitJoysticks(/*tableJoysticks*/);
/* Boucle principale */
while (!quitProgram)
{
SDL_Event event;
/* Lecture des évènements dans la queue d'évènements */
while (SDL_PollEvent (&event))
{
switch (event.type)
{
/* Evenement de fermeture d'application, on sort la boucle */
case SDL_QUIT:
quitProgram = 1;
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
InputKeyboardEvent((SDL_KeyboardEvent*)&event);
break;
case SDL_MOUSEMOTION:
InputMouseMotionEvent((SDL_MouseMotionEvent*)&event);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
InputMouseButtonEvent((SDL_MouseButtonEvent*)&event);
break;
case SDL_JOYAXISMOTION:
InputJoystickAxisEvent((SDL_JoyAxisEvent*)&event);
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
InputJoystickButtonEvent((SDL_JoyButtonEvent*)&event);
break;
}
}
// Image suivante
SDL_Flip(sdlMainScreen);
}
}