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 – drawing a Ship sprite

Let's write the necessary code to handle a sprite first:

  1. First, we need a class to contain sprites coordinates. Update jni/Types.hpp to define a new structure Location:
    ...
    namespace packt {
        typedef int32_t status;
    
        const status STATUS_OK   = 0;
        const status STATUS_KO   = -1;
        const status STATUS_EXIT = -2;
    
        struct Location {
            Location(): mPosX(0), mPosY(0) {};
            void setPosition(float pPosX, float pPosY)
            { mPosX = pPosX; mPosY = pPosY; }
    
            void translate(float pAmountX, float pAmountY)
            { mPosX += pAmountX; mPosY += pAmountY; }
    
            float mPosX; float mPosY;
        };
    }
    ...
  2. Create GraphicsSprite.hpp in folder jni. A sprite is loaded when GraphicsService starts with load() and rendered when screen is refreshed with draw()

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