Main Page | Modules | Data Structures | File List | Data Fields

eventmap_test.c

00001 #include <SDL/SDL.h>
00002 #include <SDL/SDL_joystick.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include "SDL_eventmap.h"
00007 
00012 
00014 SDL_Joystick **OpenAllJoysticks(int sticks);
00016 void CloseAllJoysticks(SDL_Joystick **);
00017 
00019 #define SAVEFILE_NAME "eventmap.0"
00020 
00021 #define DEFAULT_FILTER FILTER_ALL
00022 
00024 int main(int argc, char *argv[])
00025 {
00026   int running=1;
00027   int learned=0;
00028   SDL_Event event;
00029   SDL_Joystick **stick=NULL;
00030   SDL_EventMap *map=NULL;
00031 
00032   if(argc==1)
00033   {
00034     map=SDLH_Create();
00035   }
00036   else if(argc==2)
00037   {
00038     SDL_RWops *rw=SDL_RWFromFile(argv[1],"rb");
00039     if(rw==NULL)
00040     {
00041       fprintf(stderr,"Couldn't open %s\n",argv[1]);
00042       return(1);
00043     }
00044 
00045     map=SDLH_Load(rw);
00046     if(map==NULL)
00047     {
00048       fprintf(stderr,"Couldn't load %s\n",argv[1]);
00049       return(1);
00050     }
00051 
00052     SDL_RWclose(rw);
00053   }
00054   else
00055   {
00056     fprintf(stderr,"Usage: eventmap_test [mapfile]\n\n");
00057     exit(1);
00058   }
00059 
00060   if(map==NULL)
00061   {
00062     fprintf(stderr,"Couldn't create event map!\n");
00063     exit(1);
00064   }
00065 
00066   if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK)<0)
00067   {
00068     fprintf(stderr,"Can't init SDL: %s\n",SDL_GetError());
00069     return(1);
00070   }
00071 
00072   atexit(SDL_Quit);
00073 
00074   if(SDL_SetVideoMode(320,240,24,SDL_ANYFORMAT)<0)
00075   {
00076     fprintf(stderr,"Can't set video mode: %s\n",SDL_GetError());
00077     return(2);
00078   }
00079 
00080   stick=OpenAllJoysticks(SDL_NumJoysticks());
00081   if(stick==NULL)
00082     fprintf(stderr,"Warning, no joysticks!\n");
00083 
00084   /* Flush events */
00085   running=0;
00086     /* protect against infinite loop - once had a jittery
00087        joystick that filled the event que with SDL_JOYAXISEVENTs,
00088        the que never emptied.                                    */
00089   while(SDL_PollEvent(&event)&&(running<100))
00090     running++;
00091 
00092   fprintf(stderr,
00093   "eventmap_test:  A simple example program for SDL_EventMap.  SDL_EventMap\n"
00094   "  is an API to map joystick, keyboard, and mouse events to SDL_UserEvents.\n"
00095   "  Joysticks, mice, and keyboards can all be mapped to the same program\n"
00096   "  event, like 'fire' being left-click, enter-key, or button 0.\n"
00097   "This program demonstrates how it works.  Press 0-9 on the keyboard to\n"
00098   "  select a SDL_UserEvent, then produce a joystick, mouse, or keyboard\n"
00099   "  event.  The program will remember it, and respond to that event by\n"
00100   "  printing the triggered event number.\n\n"                               );
00101 
00102   running=1;
00103   while(running)
00104   {
00105     while(SDL_PollEvent(&event))
00106       switch(event.type)
00107       {
00108       Uint32 hash;
00109 
00110       case SDL_QUIT:
00111         running=0;
00112         break;
00113 
00114       case SDL_USEREVENT:
00115         printf("UserEvent %d!\n",event.user.code);
00116         break;
00117 
00118       case SDL_KEYDOWN:
00119         if((event.key.keysym.sym>='0')&&(event.key.keysym.sym<='9'))
00120         {
00121           int userevent=event.key.keysym.sym-'0';
00122           printf("Defining userevent %d:\n",userevent);
00123 
00124           hash=SDLH_Learn(map,userevent,DEFAULT_FILTER);
00125 
00126           if(hash==0)
00127             printf("*  Failed to learn event\n");
00128           else
00129             printf("*  Event set as %s\n",SDLH_HashName(hash));
00130 
00131           break;
00132         }
00133         else if(event.key.keysym.sym==SDLK_TAB)
00134         {
00135           SDL_RWops *rw=NULL;
00136           printf("Saving to %s\n",SAVEFILE_NAME);
00137 
00138           rw=SDL_RWFromFile(SAVEFILE_NAME,"wb");
00139           if(rw==NULL)
00140           {
00141             fprintf(stderr,"Couldn't open %s\n",SAVEFILE_NAME);
00142             break;
00143           }
00144 
00145           if(SDLH_Save(map,rw)<0)
00146             fprintf(stderr,"Couldn't save to %s\n",SAVEFILE_NAME);
00147 
00148           SDL_RWclose(rw);
00149           break;
00150         }
00151         else if(event.key.keysym.sym==SDLK_BACKSPACE)
00152         {
00153           printf("Clearing event map!\n");
00154           SDLH_Clear(map);
00155           break;
00156         }
00157 
00158       default:
00159         if(SDLH_TranslateEvent(map,&event,DEFAULT_FILTER)<0)
00160         {
00161           hash=SDLH_HashEvent(&event,DEFAULT_FILTER);
00162           if(hash!=0)
00163             printf("Unhandled input %s\n",SDLH_HashName(hash));
00164         }
00165 
00166         break;
00167       }
00168 
00169     SDL_Delay(10);
00170   }
00171 
00172   if(stick!=NULL)
00173   {
00174     CloseAllJoysticks(stick);
00175     stick=NULL;
00176   }
00177 
00178   SDLH_Free(map);
00179   map=NULL;
00180 
00181   return(0);
00182 }
00183 
00184 SDL_Joystick **OpenAllJoysticks(int sticks)
00185 {
00186   int n;
00187   int opened=0;
00188   SDL_Joystick **stick=NULL;
00189 
00190   if(sticks<=0) return(NULL);
00191 
00192   stick=(SDL_Joystick **)malloc(sizeof(SDL_Joystick *)*(sticks+1));
00193   if(stick==NULL)
00194   {
00195     fprintf(stderr,"Couldn't allocate mem for joysticks!\n");
00196     return(NULL);
00197   }
00198 
00199   memset(stick,0,sizeof(SDL_Joystick *)*(sticks+1));
00200 
00201   SDL_JoystickEventState(SDL_ENABLE);
00202 
00203   for(n=0; n<sticks; n++)
00204   {
00205     stick[n]=SDL_JoystickOpen(n);
00206     if(stick[n]!=NULL)
00207     {
00208       opened++;
00209       printf("Opened joystick %d\n",n);
00210       printf("  -%s\n",SDL_JoystickName(n));
00211       printf("  -%d axes\n",SDL_JoystickNumAxes(stick[n]));
00212       printf("  -%d buttons\n",SDL_JoystickNumButtons(stick[n]));
00213       printf("  -%d balls\n",SDL_JoystickNumBalls(stick[n]));
00214       printf("  -%d hats\n\n",SDL_JoystickNumHats(stick[n]));
00215     }
00216     else
00217       printf("Couldn't open joystick %d\n",n);
00218   }  
00219 
00220   if(opened==0)
00221   {
00222     free(stick);
00223     return(NULL);
00224   }
00225 
00226   return(stick);
00227 }
00228 
00229 void CloseAllJoysticks(SDL_Joystick **stick)
00230 {
00231   int n;
00232   if(stick==NULL) return;
00233 
00234   for(n=0; stick[n]!=NULL; n++)
00235   {
00236     SDL_JoystickClose(stick[n]);
00237   }  
00238 
00239   free(stick);
00240 }
00241 

Generated on Tue Jun 22 16:25:25 2004 by doxygen 1.3.5