O'Reilly logo

Android NDK Beginner's Guide - Second Edition 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 – displaying raw graphics

Let's make DroidBlaster more interactive with some graphics and game components.

  1. Edit jni/Types.hpp and create a new structure Location to hold entity positions. Also, define a macro to generate a random value in the requested range as follows:
    #ifndef _PACKT_TYPES_HPP_
    #define _PACKT_TYPES_HPP_
    ...
    struct Location {
        Location(): x(0.0f), y(0.0f) {};
    
        float x; float y;
    };
    
    #define RAND(pMax) (float(pMax) * float(rand()) / float(RAND_MAX))
    #endif
  2. Create a new file, jni/GraphicsManager.hpp. Define a structure GraphicsElement, which contains the location and dimensions of the graphical element to display:
    #ifndef _PACKT_GRAPHICSMANAGER_HPP_ #define _PACKT_GRAPHICSMANAGER_HPP_ #include "Types.hpp" #include <android_native_app_glue.h> ...

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