O'Reilly logo

Android NDK Beginner's Guide by Sylvain Ratabouil

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – turning your device into a joypad

First, we need to handle sensor events in the event loop.

  1. Open InputHandler.hpp and add a new method onAccelerometerEvent(). Include android/sensor.h official header for sensors.
    #ifndef _PACKT_INPUTHANDLER_HPP_
    #define _PACKT_INPUTHANDLER_HPP_
    
    #include <android/input.h>
    #include <android/sensor.h>
    
    namespace packt {
        class InputHandler {
        public:
          virtual ~InputHandler() {};
    
          virtual bool onTouchEvent(AInputEvent* pEvent) = 0;
          virtual bool onKeyboardEvent(AInputEvent* pEvent) = 0;
          virtual bool onTrackballEvent(AInputEvent* pEvent) = 0;
          virtual bool onAccelerometerEvent(ASensorEvent* pEvent) = 0;
        };
    }
    #endif
  2. Update jni/EventLoop.hpp class by adding a static callback dedicated to sensors named callback_sensor() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required