The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Android multitouch SDL 1.3 XOOM work HTC Desire not
leanid


Joined: 17 May 2011
Posts: 8
Hello,
I start porting game to android and add multitouch to SDL 1.3, but I don't fully understand how works this subsystem in SDL 1.3.
I test my code on motorola XOOM - and it works, and on HTC Desire - no luck. Please sugest any help!

java part
Code:

   public boolean onTouch(View v, MotionEvent event) {
      {
         final int touchDevId = event.getDeviceId();
         final int pointerCount = event.getPointerCount();
         // touchId, pointerId, action, x, y, pressure
         int actionPointerIndex = event.getActionIndex();
         int pointerFingerId = event.getPointerId(actionPointerIndex);
         int action = event.getActionMasked();

         float x = event.getX(actionPointerIndex);
         float y = event.getY(actionPointerIndex);
         float p = event.getPressure(actionPointerIndex);

         if (action == MotionEvent.ACTION_MOVE && pointerCount > 1) {
            // TODO send motion to every pointer if it's position has
            // changed since prev event.
            for (int i = 0; i < pointerCount; i++) {
               pointerFingerId = event.getPointerId(i);
               x = event.getX(i);
               y = event.getY(i);
               p = event.getPressure(i);
               SDLActivity.onNativeTouch(touchDevId, pointerFingerId,
                     action, x, y, p);
            }
         } else {
            SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action,
                  x, y, p);
         }
      }
      return true;
   }


c++ part
Code:

void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
{
//   SDL_Log(
//         "Android_OnTouch touchId=%d, pointerId=%d, action=%d, x=%f, y=%f, p=%f",
//         touchId_, pointerId, action, x, y, p); // LEON ADD LINE
    if (!Android_Window) {
        return;
    }

    if ((action != ACTION_CANCEL) && (action != ACTION_OUTSIDE)) {
        SDL_SetMouseFocus(Android_Window);
        SDL_SendMouseMotion(Android_Window, 0, (int)x, (int)y);
        switch(action) {
        case ACTION_DOWN:
            SDL_SendMouseButton(Android_Window, SDL_PRESSED, SDL_BUTTON_LEFT);
            break;
        case ACTION_UP:
            SDL_SendMouseButton(Android_Window, SDL_RELEASED, SDL_BUTTON_LEFT);
            break;
        default:
           break;
        }
    } else {
        SDL_SetMouseFocus(NULL);
    }

    // multitouch work
   SDL_TouchID touchDeviceId = (SDL_TouchID)touch_device_id_in;
   if (!SDL_GetTouch(touchDeviceId)) {
      SDL_Touch touch;

      touch.id = touchDeviceId;
      touch.x_min = 0.f;
      touch.x_max = 1.f;
      touch.native_xres = touch.x_max - touch.x_min;
      touch.y_min = 0;
      touch.y_max = 1;
      touch.native_yres = touch.y_max - touch.y_min;
      touch.pressure_min = 0;
      touch.pressure_max = 1;
      touch.native_pressureres = touch.pressure_max - touch.pressure_min;
      // TODO: correct this code, to understand look SDL_AddTouch
      touch.xres = 1; //1280; // SDL_touchPads[index]->xres = (1<<(16-1)); 32768
      touch.yres = 1; //727;
      touch.native_xres = 32768.f;
      touch.native_yres = 32768.f;

      if (SDL_AddTouch(&touch, "") < 0) {
         SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
      }
   }

   SDL_FingerID fingerId = (SDL_FingerID)pointer_finger_id_in;
   switch (action)
   {
   case ACTION_DOWN:
   case ACTION_POINTER_DOWN:
      SDL_SendFingerDown(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
      break;
   case ACTION_MOVE:
      SDL_SendTouchMotion(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
      break;
   case ACTION_UP:
   case ACTION_POINTER_UP:
      SDL_SendFingerDown(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
      break;
   default:
      break;
   }
}
leanid


Joined: 17 May 2011
Posts: 8
I just test my code again - it works! On motorola XOOM and on HTC Desire.