[BY TIMOTHY BOGDALA]

Android Studio with NDK OpenGL ES Sample

Published: December 07, 2014

Android Studio 1.0 RC4 was just recently pushed, so I decided now was the time to start refreshing my projects and ideas for Android development.

To get started, I wanted to see how difficult it would be to compile NDK samples with the new Android Studio IDE. I always found the process to be super clunky with Eclipse and had always resorted to doing everything by hand via the command line. It was obviously not ideal and was not a fun way to develop a game.

This article assumes:

The hello-gl2 Sample

If you download the recent Android NDK (version r10d as of this writing), you should be able to find the hello-gl2 sample in the android-ndk-r10d\samples directory. This sample still uses Java for the activities but it implements the OpenGL ES functionality in C++ and exposes the C++ code through a class called GL2JNILib.

In order to test this with Android Studio, I created a new project, copied the code over, changed the AndroidManifest.xml and updated the Gradle Scripts. Both this page and this page were helpful in figuring out the right steps.

Step 1: Create a New Project

When you start Android Studio, select the New Project option from the wizard. I created a project called NDKSample and left all of the settings at the default in the wizard except for selecting no activity instead of Blank Activity.

Step 2: Copy the Code

I put the hello-gl2 code into the new project by doing the following:

The empty.cpp file is literally an empty code file and is only there because there currently is a bug when compiling a NDK project with only one file. Android Studio will automatically make the equivalent of the Android.mk file, so all that is is needed is the C++ code.

The Java code copied in the com directory contains the Java activity and view classes as well as the class the wraps the JNI code.

Step 3: Modify the AndroidManifest.xml File

You can edit this file by selecting it in the Projects side panel in Android Studio under app > manifests > AndroidMainifest.xml. Replace the content of this file with the following:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.gl2jni">
    <application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme">
    <activity android:name="GL2JNIActivity"
          android:launchMode="singleTask"
          android:configChanges="orientation|keyboardHidden">
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
        </activity>
    </application>
</manifest>

What this does is change the package name to com.android.gl2jni so that none of the sample code needs to be edited and it changes the activity name so that it knows what activity to launch at startup.

Step 4: Update the Gradle Scripts

The last step is to change the Gradle build scripts. You need to define your path to the NDK by editing local.properties and adding the following line to the file:

ndk.dir=C\:\\Projects\\android-ndk-r10d

Then you need to define the NDK module to build by editing the build.gradle (Module: app) script, and add the following script code to the defaultConfig block in the file

ndk {
    moduleName "libgl2jni"
    ldLibs "GLESv2","log"
}

This controls the file name of the library and should match what is found in the Java file com.android.gl2jni.GL2JNILib.

Once you make these changes you can sync the project settings by selecting the sync option in the tooltip that should be at the top of the editor, or you can manually invoke it using the menu Tools > Android > Sync Project with Gradle Files

Step 5: Build and Run!

Now you should be able to build the project and run it in the emulator or on your device!

Creating Thumbnails For Justified Gallery With Bash

Published: February 25, 2014

Having just rebuilt my website from the ground up using Google’s Go, I’m now using a whole new set of tools to get things done. Previously I based my blog on Wordpress and I used NextGEN Gallery for my gallery needs. Now that I’m going light, I’m now using Jusitified Gallery combined with Magnific-popup.

For Justified to be able to do it’s thing and size the images to fit the page nicely, it needs a set of thumbnails of different max sizes. I wrote a quick bash script to generate these with GraphicsMagick’s convert utility.

#!/bin/bash

if [[ -d $1 ]]; then
    echo "$1 is a directory. pass in a file instead."
elif [[ -f $1 ]]; then
    ORIGFILE=$(echo $1 | sed 's/\.[^.]*$//')
    echo "Making thumbs for $ORIGFILE ..."
    
    gm convert -size 800x800 ${ORIGFILE}.jpg -resize 800x800 +profile "*" ${ORIGFILE}_c.jpg
    gm convert -size 640x640 ${ORIGFILE}.jpg -resize 640x640 +profile "*" ${ORIGFILE}_z.jpg
    gm convert -size 320x320 ${ORIGFILE}.jpg -resize 320x320 +profile "*" ${ORIGFILE}_n.jpg
    gm convert -size 240x240 ${ORIGFILE}.jpg -resize 240x240 +profile "*" ${ORIGFILE}_m.jpg
    gm convert -size 100x100 ${ORIGFILE}.jpg -resize 100x100 +profile "*" ${ORIGFILE}_t.jpg
else
    echo "Usage: $0 <filename>"
fi

The -resize flag keeps the aspect ratio intact, so that is the maximum allowable dimension for the image. The +profile flag should strip all of the extra data not needed.

To use it, run the script and pass it the name of the original image file. I opted not to use the 500px and 1024px thumbnails, so in my html I had to configure Justified with these options

$("#gallery").justifiedGallery({
    'sizeRangeSuffixes' : {
        'lt100':'_t', 
        'lt240':'_m', 
        'lt320':'_n', 
        'lt500':'_z', 
        'lt640':'_z', 
        'lt800':'_c',
        'lt1024':'_c'
    });

PThreads Win32 in Visual Studio 2012

Published: February 2, 2014

One popular option for portable multi-threading is using the POSIX threads  (pthreads) library. On Unix-like platforms like Linux this can be installed easily through a package manager. On Mac OS X you can use something like Homebrew to install it.

On Windows the situation is a little less clear. Most people appear to use the Win32 port found here. When you get the latest package, you’ll find that the project files are for a very old version of Visual Studio. The upgrade option gave me errors. Using git to grab the latest which had VS2012 solutions also did not compile correctly.

The solution was to actually RTFM found at ftp://sourceware.org/pub/pthreads-win32/dll-latest/README and use the command line tools to build. Open the Developer Command Prompt from the start menu, cd to the pthreads.2 source directory found in a package (like  ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip), and use one of the following commands:

nmake clean VCE (to build the MSVC dll with C++ exception handling)
nmake clean VSE (to build the MSVC dll with structured exception handling)
nmake clean VC (to build the MSVC dll with C cleanup code)
nmake clean VCE-inlined (to build the MSVC inlined dll with C++ exception handling)
nmake clean VSE-inlined (to build the MSVC inlined dll with structured exception handling)
nmake clean VC-inlined (to build the MSVC inlined dll with C cleanup code)
nmake clean VC-static (to build the MSVC static lib with C cleanup code)
nmake clean VCE-debug (to build the debug MSVC dll with C++ exception handling)
nmake clean VSE-debug (to build the debug MSVC dll with structured exception handling)
nmake clean VC-debug (to build the debug MSVC dll with C cleanup code)
nmake clean VCE-inlined-debug (to build the debug MSVC inlined dll with C++ exception handling)
nmake clean VSE-inlined-debug (to build the debug MSVC inlined dll with structured exception handling)
nmake clean VC-inlined-debug (to build the debug MSVC inlined dll with C cleanup code)
nmake clean VC-static-debug (to build the debug MSVC static lib with C cleanup code)

So a command like this will build the release version that doesn’t use exceptions:

nmake clean VC

After building the release version you can use VC-debug to build the debug copy of the DLL. Copy the .lib and the .dll files to a directory in your project, set the library and include paths and you’re good to go!

<- OLDER ENTRIES .|. NEWER ENTRIES ->