Book Image

Android NDK: Beginner's Guide

By : Sylvain Ratabouil
Book Image

Android NDK: Beginner's Guide

By: Sylvain Ratabouil

Overview of this book

Table of Contents (18 chapters)
Android NDK Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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>
    
    struct GraphicsElement {
        GraphicsElement(int32_t pWidth, int32_t pHeight):
            location(),
            width(pWidth), height(pHeight) {
        }
    
        Location location;
        int32_t width;  int32_t height;
    ...