Difference between revisions of "GE"

From GIMX
Jump to: navigation, search
(Polling-driven)
Line 1: Line 1:
 
=The GIMX Event Library=
 
=The GIMX Event Library=
  
GE is a cross-platform event library that can be used in Windows or Linux.
+
GE is a cross-platform event library that can be used in Windows or Linux.<br />
In Linux, it directly interfaces with [http://en.wikipedia.org/wiki/Evdev evdev].
+
In Linux, it directly interfaces with [http://en.wikipedia.org/wiki/Evdev evdev].<br />
In Windows, it uses a modified SDL library.
+
In Windows, it uses a modified SDL library.<br />
  
 
This page shows how to use it.
 
This page shows how to use it.
Line 9: Line 9:
 
=Linux examples=
 
=Linux examples=
  
==Polling-driven==
+
==Interrupt-driven, with periodic task==
 
 
  int process_event(GE_Event* event)
 
  {
 
    //handle the event
 
  }
 
 
 
  int main(int argc, char* argv[])
 
  {
 
    if (!GE_initialize())
 
    {
 
      //handle the error
 
    }
 
 
 
    struct timespec period = {.tv_sec = 0, .tv_nsec = 10000000};
 
 
 
    GE_TimerStart(&period);
 
 
 
    GE_SetCallback(process_event);
 
 
 
    while(!done)
 
    {
 
      GE_PumpEvents();
 
    }
 
 
 
    GE_quit();
 
   
 
    return 0;
 
  }
 
 
 
==Interrupt-driven (with periodic task)==
 
  
 
   int process_event(GE_Event* event)
 
   int process_event(GE_Event* event)
Line 71: Line 41:
 
   }
 
   }
  
==Interrupt-driven (without periodic task)==
+
==Interrupt-driven, without periodic task==
  
 
   int process_event(GE_Event* event)
 
   int process_event(GE_Event* event)
 
   {
 
   {
     //handle the event
+
     //process the event
 
   }
 
   }
 
    
 
    
Line 91: Line 61:
 
       GE_PumpEvents();//retrieve 1 event and return (blocks until event reception)
 
       GE_PumpEvents();//retrieve 1 event and return (blocks until event reception)
 
    
 
    
       num_evt = GE_PeepEvents(events, sizeof(events) / sizeof(events[0]));
+
       num_evt = GE_PeepEvents(events, 1);
 
    
 
    
 
       if (num_evt > 0)
 
       if (num_evt > 0)
 
       {
 
       {
         for (event = events; event < events + num_evt; ++event)
+
         //process the event
        {
 
          //process the event
 
        }
 
 
       }
 
       }
 
     }
 
     }
Line 120: Line 87:
 
     while(!done)
 
     while(!done)
 
     {
 
     {
       GE_PumpEvents();//retrieve all available events and return (does not block)
+
       GE_PumpEvents();//retrieve all pending events and return (does not block)
 
    
 
    
 
       num_evt = GE_PeepEvents(events, sizeof(events) / sizeof(events[0]));
 
       num_evt = GE_PeepEvents(events, sizeof(events) / sizeof(events[0]));
Line 131: Line 98:
 
         }
 
         }
 
       }
 
       }
 +
 
 +
      //do something periodically
 
    
 
    
 
       usleep(1000);//sleep some time
 
       usleep(1000);//sleep some time

Revision as of 10:24, 14 January 2013

The GIMX Event Library

GE is a cross-platform event library that can be used in Windows or Linux.
In Linux, it directly interfaces with evdev.
In Windows, it uses a modified SDL library.

This page shows how to use it.

Linux examples

Interrupt-driven, with periodic task

 int process_event(GE_Event* event)
 {
   //handle the event
 }
 
 int main(int argc, char* argv[])
 {
   if (!GE_initialize())
   {
     //handle the error
   }
 
   struct timespec period = {.tv_sec = 0, .tv_nsec = 10000000};
 
   GE_TimerStart(&period);
 
   GE_SetCallback(process_event);
 
   while(!done)
   {
     GE_PumpEvents();//blocks until timer fires
 
     //do something periodically
   }
 
   GE_quit();
   
   return 0;
 }

Interrupt-driven, without periodic task

 int process_event(GE_Event* event)
 {
   //process the event
 }
 
 int main(int argc, char* argv[])
 {
   if (!GE_initialize())
   {
     //handle the error
   }
 
   GE_SetCallback(GE_PushEvent);
 
   while(!done)
   {
     GE_PumpEvents();//retrieve 1 event and return (blocks until event reception)
 
     num_evt = GE_PeepEvents(events, 1);
 
     if (num_evt > 0)
     {
       //process the event
     }
   }
 
   GE_quit();
   
   return 0;
 }

Windows example

Polling-driven

 int main(int argc, char* argv[])
 {
   if (!GE_initialize())
   {
     //handle the error
   }
 
   while(!done)
   {
     GE_PumpEvents();//retrieve all pending events and return (does not block)
 
     num_evt = GE_PeepEvents(events, sizeof(events) / sizeof(events[0]));
 
     if (num_evt > 0)
     {
       for (event = events; event < events + num_evt; ++event)
       {
         //process the event
       }
     }
 
     //do something periodically
 
     usleep(1000);//sleep some time
   }
 
   GE_quit();
   
   return 0;
 }