Source:Traitement des entrées avec SDL/input.pas

(*
 *  Version Pascal des tutoriaux SDL de Mokona pour Prografix.
 *
 *  Ce programme a été écrit en FreePascal - http://www.freepascal.org/
 *  C'est un Pascal très proche du Delphi et accompagné d'un compilateur libre
 *  
 *  Les bindings SDL proviennent du projet JEDI - http://www.delphi-jedi.org/
 *  JEDI-SDL est de loin l'adaptation de SDL la plus complète pour Delphi
 *
 *)


program input;

{ Pour utiliser SDL }
uses SDL;

var
   { Les joysticks }
   tableJoysticks : array [0..3] of PSDL_Joystick;


{ Gestion du clavier }
procedure InputKeyboardEvent(event : TSDL_KeyboardEvent);
var
   { type et state contiennent en fait la même information }
   { On utilise type }
   keysym : TSDL_keysym;
   
begin
   keysym := event.keysym;
   
   if event.type_ = SDL_KEYDOWN then
   begin
      if ((keysym.unicode and $FF80) = 0) and (keysym.unicode > 32) then
	 writeln('Touche enfoncee : ',event.keysym.unicode)
      else
	 writeln('Touche enfoncee non traduite');
      
      writeln('Nom SDL de la touche : ',SDL_GetKeyName(keysym.sym));
   end
   else
      writeln('Touche relevee : ',SDL_GetKeyName(keysym.sym));
end;


{ Gestion des mouvements de la souris }
procedure InputMouseMotionEvent(event : TSDL_MouseMotionEvent);
begin
   write('Mouvement de la souris (',event.xrel,',',event.yrel,
	   ') . Coordonnees dans la fenetre (',event.x,',',event.y,')');
   
   if (event.state <> 0) and (SDL_BUTTON(1) <> 0) then
      write(' - B1');

   if (event.state <> 0) and (SDL_BUTTON(2) <> 0) then
      write(' - B2');
   
   if (event.state <> 0) and (SDL_BUTTON(3) <> 0) then
      write(' - B3');
   
   writeln('');
end;


{ Gestion des boutons de la souris }
procedure InputMouseButtonEvent(event : TSDL_MouseButtonEvent);
begin
   if event.type_ = SDL_MOUSEBUTTONDOWN then 
      write('Bouton ',event.button,' appuye')
   else
      write('Bouton ',event.button,' relache');
      
   writeln(' aux coordonnees (',event.x,',',event.y,')');
   end;



{ Initialisation des joysticks }
procedure InputInitJoysticks();
var
   { Etat initial. Le hot-plug est specifique OS }
   numJoystick : integer;
   i	       : integer;
begin
   numJoystick := SDL_NumJoysticks();
   
   writeln('Nombre de joysticks detectes : ',numJoystick);
   
   if numJoystick > 4 then
   begin
      writeln('Seuls les quatre premiers joysticks seront pris en compte');
      numJoystick := 4;
   end;
   
   for i:=0 to numJoystick-1 do
   begin
      writeln('Nom du joystick ',i,' : ',SDL_JoystickName(i));
      tableJoysticks[i] := SDL_JoystickOpen(i);
      if tableJoysticks[i] <> nil then
      begin
	 writeln('Nombre d''axes : ',SDL_JoystickNumAxes(tableJoysticks[i]));
	 writeln('Nombre de boutons : ', SDL_JoystickNumButtons(tableJoysticks[i]));
	 writeln('... joystick ouvert');
      end
      else
	 writeln('... probleme d''ouverture avec ce joystick');
   end;
   
   for i:=numJoystick to 3 do
      tableJoysticks[i] := nil;
end;


{ Ferme correctement les joysticks }
procedure InputCloseJoysticks();
var
   i : integer;
   
begin
   for i:=0 to 3 do
   begin
      if tableJoysticks[i] <> nil then
      begin
	 SDL_JoystickClose(tableJoysticks[i]);
	 writeln('Joystick ',i,' ferme');
      end;
   end;
end;


{ Gestion des axes d'un joystick }
procedure InputJoystickAxisEvent(event : TSDL_JoyAxisEvent);
begin
   writeln('Axe ',event.axis,' du joystick ',event.which,' : ',event.value);
end;


{ Gestion des buttons d'un joystick }
procedure InputJoystickButtonEvent(event : TSDL_JoyButtonEvent);
begin
   write('Bouton ',event.button,' du joystick ',event.which,' : ');
   if event.state = SDL_PRESSED then
      writeln('appuye')
   else
      writeln('relache');
end;


procedure Quitter();
begin
   InputCloseJoysticks();
   SDL_Quit();
end;


var
   event	  : TSDL_Event;
   sdlMainScreen  : PSDL_Surface;   { Surface écran }
   quitProgram	  : boolean;        { Drapeau de sortie }


{ Boucle principale }
begin
   quitProgram := false;
   { Initialisation de la partie Vidéo de SDL }
   if SDL_Init(SDL_INIT_VIDEO or SDL_INIT_JOYSTICK) < 0 then
   begin
      { En cas d'échec, sortie }
      Quitter();
      halt(1);
   end;

   { Demande d'affichage en 640x480 16 bits }
   sdlMainScreen := SDL_SetVideoMode (64, 48, 16, SDL_SWSURFACE);

   { En cas d'echec, on sort }
   if sdlMainScreen = nil then
   begin
      Quitter();
      halt(2);
   end;

   
    SDL_EnableUNICODE(1);
    
   
   InputInitJoysticks();
   
   { Boucle principale }
   while not quitProgram do
   begin
      { Lecture des évènements dans la queue d'évènements }
      while SDL_PollEvent(@event) = 1 do
      begin
	 case event.type_ of
	 SDL_QUITEV:	{ Evenement de fermeture d'application, on sort la boucle }
	    quitProgram := true;

	 SDL_KEYDOWN, SDL_KEYUP:
	    InputKeyboardEvent(event.key);

	 SDL_MOUSEMOTION:
	    InputMouseMotionEvent(event.motion);

	 SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP:
	    InputMouseButtonEvent(event.button);

	 SDL_JOYAXISMOTION:
	    InputJoystickAxisEvent(event.jaxis);

	 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP:
	    InputJoystickButtonEvent(event.jbutton);
	 end;
      end;
      
      { Image suivante }
      SDL_Flip(sdlMainScreen);
   end;

   Quitter();
end.