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

SDL_eventmap.cpp

00001 #include <SDL/SDL_endian.h>
00002 #include "SDL_eventmap.h"
00003 #include "SDL_eventmap_internal.h"
00004 #include <map>
00005 using namespace std;
00006 
00007 #define SDL_EM map<Uint32, int>
00008 
00009 void SDLH_Clear(SDL_EventMap *emap)
00010 {
00011   SDL_EM *em=(SDL_EM *)emap;
00012 
00013   if(em==NULL) return;
00014 
00015   em->clear();
00016 }
00017 
00021 int SDLH_RemoveID(SDL_EventMap *emap, int id)
00022 {
00023   SDL_EM *em=(SDL_EM *)emap;
00024   int removed=0;
00025 
00026   if((em==NULL)||(id<0))
00027     return(-1);
00028 
00029   SDL_EM::iterator i=em->begin();
00030 
00031   while(i!=em->end())
00032   {
00033     SDL_EM::iterator next=i;
00034     ++next;
00035 
00036     if((i->second)==(id+1))
00037     {
00038       em->erase(i);
00039       removed++;
00040     }
00041     i=next;
00042   }
00043 
00044   return(removed);
00045 }
00046 
00054 Uint32 SDLH_Learn(SDL_EventMap *emap, int id, Uint32 flag)
00055 {
00056   int running=1;
00057   SDL_Event event;
00058   if((emap==NULL)||(id<0))
00059     return(0);
00060 
00061   while(running)
00062   {
00063     while(SDL_PollEvent(&event))
00064     {
00065       Uint32 hash=0;
00066       switch(event.type)
00067       {    
00068       case SDL_QUIT:
00069         return(0);
00070 
00071       default:
00072         hash=SDLH_HashEvent(&event,flag);
00073         break;
00074       }
00075 
00076       if(hash!=0)
00077       {
00078         if(SDLH_AddHashEvent(emap,hash,id)<0)
00079           return(0);
00080         else
00081           return(hash);
00082       }
00083     }
00084 
00085     SDL_Delay(10);
00086   }
00087   return(0);
00088 }
00089 
00090 SDL_EventMap *SDLH_Load(SDL_RWops *stream)
00091 {
00092   SDL_EM *em=NULL;
00093   Uint32 maps=0;
00094   int n=0;
00095 
00096   if(stream==NULL)
00097     return(NULL);
00098 
00099   if(SDL_RWread(stream,&maps,sizeof(Uint32),1)<=0)
00100     return(NULL);
00101 
00102   maps=SDL_SwapLE32(maps);
00103 
00104   em=new SDL_EM;
00105 
00106   for(n=0; n<maps; n++)
00107   {
00108     Uint32 hash;
00109     int    id;
00110 
00111     if(SDL_RWread(stream,&hash,sizeof(Uint32),1)<=0)
00112     {
00113       delete em;
00114       return(NULL);
00115     }
00116     else if(SDL_RWread(stream,&id,sizeof(int),1)<=0)
00117     {
00118       delete em;
00119       return(NULL);
00120     }
00121 
00122     hash=SDL_SwapLE32(hash);
00123     id=SDL_SwapLE32(id);
00124 
00125     (*em)[hash]=id;
00126   }
00127 
00128   return((SDL_EventMap *)em);
00129 }
00130 
00131 int SDLH_Save(SDL_EventMap *emap, SDL_RWops *stream)
00132 {
00133   SDL_EM *em=(SDL_EM *)emap;
00134 
00135   if((stream==NULL)||(stream==NULL))
00136     return(-1);
00137   else if(em->size()==0)
00138     return(-1);
00139 
00140   if(SDL_WriteLE32(stream,em->size())<0)
00141     return(-1);
00142 
00143   SDL_EM::iterator i=em->begin();
00144   while(i!=em->end())
00145   {
00146     if(SDL_WriteLE32(stream,i->first)<0)
00147       return(-1);
00148     if(SDL_WriteLE32(stream,i->second)<0)
00149       return(-1);
00150 
00151     i++;
00152   }
00153 
00154   return(0);
00155 }
00156 
00161 int SDLH_RemoveEvent(SDL_EventMap *eventmap, Uint32 hash)
00162 {
00163   SDL_EM *em=(SDL_EM *)eventmap;
00164 
00165   if((em==NULL)||(hash==0))
00166     return(-1);
00167 
00168   SDL_EM::iterator i=em->begin();
00169 
00170   while(i!=em->end())
00171   {
00172     if(i->first==hash)
00173     {
00174       em->erase(i);
00175       return(0);
00176     }
00177 
00178     i++;
00179   }
00180 
00181   return(-1);
00182 }
00183 
00188 int SDLH_AddEvent(SDL_EventMap *eventmap, SDL_Event *event, int id, Uint32 flag)
00189 {
00190   Uint32 hash=SDLH_HashEvent(event,flag);
00191 
00192   return(SDLH_AddHashEvent(eventmap,hash,id));
00193 }
00194 
00195 
00199 int SDLH_AddHashEvent(SDL_EventMap *eventmap, Uint32 hash, int id)
00200 {
00201   SDL_EM *em=(SDL_EM *)eventmap;
00202 
00203   if((eventmap==NULL)||(id<0)||(hash==0))
00204     return(-1);
00205 
00206   (*em)[hash]=(id+1);
00207 
00208   return(0);
00209 }
00210 
00215 int SDLH_TranslateEvent(SDL_EventMap *emap, SDL_Event *event, Uint32 flag)
00216 {
00217   int hash;
00218   SDL_EM *em=(SDL_EM *)emap;
00219 
00220   if((emap==NULL)||(event==NULL))
00221     return(-1);
00222 
00223   hash=SDLH_HashEvent(event,flag);
00224   if(hash==0)
00225     return(-1);
00226 
00227   if(!(*em)[hash])
00228     return(-1);
00229   else
00230   {
00231     SDL_Event uevent;
00232     uevent.type=SDL_USEREVENT;
00233     uevent.user.code=(*em)[hash]-1;
00234     uevent.user.data1=(void *)(long int)hash;
00235     uevent.user.data2=NULL;
00236     SDL_PushEvent(&uevent);
00237     return(0);
00238   }
00239 }
00240 
00241 SDL_EventMap *SDLH_Create()
00242 {
00243   return((SDL_EventMap *)(new SDL_EM));
00244 }
00245 
00246 void SDLH_Free(SDL_EventMap *eventmap)
00247 {
00248   if(eventmap!=NULL)
00249   {
00250     delete ((SDL_EM *)eventmap);
00251   }
00252 }

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