SDL2_image for Android with JPEG image format

In the previous article, we were discussing how to add support for PNG image format to SDL2 application for Android. Let’s add another more common format. What about JPEG?

The first step is to enable JPEG in SDL2_image. Just add a proper definition to SDL2_image/build.gradle.

ndk {
...
CFlags.addAll([
    "-DLOAD_JPG"
    "-DLOAD_PNG",
    "-DLOAD_XPM"
])}

Then it is necessary to add C implementation of JPEG library. There is one big catch. Do not call it jpeg. The problem is that the name will collide with another library in Android system and function jpeg_create_decompress(&cinfo) will crash. Thanks to DevMultiTech for a hint about the solution.

Let’s call the jpeg library SDL2_jpeg.

Register modules in settings.gradle:

include ':SDL2_jpeg'

Build all libraries:

gradle distributeLib

Here is a sample code for loading and displaying JPEG image:

SDL_Surface *backgroundSurface = IMG_Load("brno-snow.jpg")
SDL_Texture *backgroundTexture = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
SDL_RenderCopy(renderer, backgroundTexture, NULL, &dstrect);

Result looks like this:

You can find whole source code at https://github.com/georgik/sdl2-android-example.

You can find about SDL2 and libraries more under the topic SDL2.