Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1.  
  2. static struct {
  3.     pthread_mutex_t mutex; // this mutex is locked while not running frame, used for events synchronization
  4.     pthread_mutex_t framemutex; // this mutex is locked while engine is running and unlocked while it reading events, used for pause in background.
  5.     event_t queue[ANDROID_MAX_EVENTS];
  6.     volatile int count;
  7.     float mousex, mousey;
  8. } events = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER };
  9.  
  10. #define Android_Lock() pthread_mutex_lock(&events.mutex);
  11. #define Android_Unlock() pthread_mutex_unlock(&events.mutex);
  12. #define Android_PushEvent() Android_Unlock()
  13.  
  14.  
  15. event_t *Android_AllocEvent()
  16. {
  17.     Android_Lock();
  18.     if( events.count == ANDROID_MAX_EVENTS )
  19.     {
  20.         events.count--; //override last event
  21.         __android_log_print( ANDROID_LOG_ERROR, "SourceSDK2013", "Too many events!!!" );
  22.     }
  23.     return &events.queue[ events.count++ ];
  24. }
  25.  
  26.  
  27. void Android_RunEvents()
  28. {
  29.     Android_Lock();
  30.     pthread_mutex_unlock( &events.framemutex );
  31.  
  32.     for( int i = 0; i < events.count; i++ )
  33.     {
  34.         event_t *event = events.queue + i;
  35.        
  36.         switch( event->type )
  37.         {
  38.         case event_touchmotion:
  39.             g_Touch.TouchMotion( event );
  40.             break;
  41.         case event_touchdown:
  42.             g_Touch.ButtonPress( event );
  43.             break;
  44.         case event_touchup:
  45.             g_Touch.ButtonPress( event );
  46.             break;
  47.         }
  48.     }
  49.     events.count = 0;
  50.    
  51.     Android_Unlock();
  52.     pthread_mutex_lock( &events.framemutex );
  53. }
  54.  
  55. DLLEXPORT int TouchEvent( int touchDevId, int fingerid, int x, int y, int action )
  56. {
  57.     event_t *ev = Android_AllocEvent();
  58.     switch( action )
  59.     {
  60.         case ACTION_DOWN:
  61.             ev->type = event_touchdown;
  62.         break;
  63.         case ACTION_UP:
  64.             ev->type = event_touchup;
  65.         break;
  66.         case ACTION_MOVE:
  67.             ev->type = event_touchmotion;
  68.             ev->touchDevId = touchDevId;
  69.         break;
  70.         default: break;
  71.     }
  72.  
  73.     ev->x = x;
  74.     ev-> y = y;
  75.     ev->fingerid = fingerid;
  76.     LogPrintf( "TouchEvent, %d, %d", x, y );
  77.  
  78.     Android_PushEvent();
  79. }
  80.  
  81. void CTouchControls::TouchMotion( event_t *ev ) //g_Touch
  82. {
  83.     float x = ev->x / screen_w;
  84.     float y = ev->y / screen_h;
  85.  
  86.     LogPrintf( "TouchMotion, %f, %f", x, y );
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement