23. September 2017

SDL2_ttf for Android with TrueType font support

Common library for displaying a text in SDL2 application is SDL2_ttf. The library is a wrapper on FreeType library. It is necessary to add both libraries into a project.

Let’s start with FreeType dependency. Add build.gradle file. Just be aware that there will be many excluded files. The library itself is very huge, and only a fraction of functions is necessary for the application.

...
sources {
      main {
          jni {
              source {
                  srcDir "src"
                  exclude "autofit"
                  exclude "smooth/smooth.c"
...
                   // Including this file in build causes duplications, because it includes directly C files
                   exclude "truetype/truetype.c"
                   exclude "type1"
                   exclude "type42"
                   exclude "winfonts"
              }
          }
      }
}

Adding SDL2_ttf is similar to other libraries like SDL2_jpeg or SDL2_mixer.

SDL2_ttf and FreeType modules should be also registered at settings.gradle:

include ':freetype'
...
include ':SDL2_ttf'

The first step to displaying text on the screen is to initialize the library and load a font:

#include "SDL_ttf.h"
...    
if (TTF_Init() == -1) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "TTF_Init: %s\n", TTF_GetError());
    return 7;
}

The next step is to load a font. The file should be stored in app/src/main/assets.

TTF_Font *font = TTF_OpenFont("blazed.ttf", 32);
if (!font) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                 "Unable to load font: %s\n", TTF_GetError());
    return 8;
}

The next step is to render a text:

SDL_Color textColor = { 255, 240, 0, 255 };
SDL_Surface* solid = TTF_RenderText_Solid(font, "SDL2 Android Example", textColor);

SDL_Texture* solidTexture = SDL_CreateTextureFromSurface(renderer, solid);
SDL_RenderCopy(renderer, solidTexture, NULL, &dstrect);
SDL_FreeSurface(solid);

Here is the result:

You can find the source code at GitHub in sdl2-android-example repository. Further articles about SDL2 and Android are available under the tag SDL2.

20. September 2017

SDL2 Android application crash on device rotation

If SDL2 application should support rotation it is necessary to add the following line to AndroidManifest.xml activity:

android:configChanges="orientation"

The XML will look like this:

<activity
    android:name="rocks.georgik.sdlapp.MainActivity"
    android:label="@string/title_activity_main"
    android:configChanges="orientation">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

The rotation of the application now works without problem:

Thanks to José Luis Pereira for the hint how to solve the issue.

The source code of the sample SDL2 application is stored at GitHub.

13. September 2017

SDL2_mixer for Android playing WAV file

In previous articles, we were talking about PNG and JPEG support in SDL2 for Android. Let’s add some sounds to our application.

The initial steps are same as in case of SDL2_image.

Register library in settings.gradle:

include ':SDL2_mixer'

The library should be stored in SDL2_mixer directory with build.gradle that written in style for a library.

Now update our C application.

First of all, it is necessary to initialize the library. Then it is possible to load the sound file.

    if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1 ) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                     "Couldn't open mixer: %s", SDL_GetError());
        return 2;
    }
    Mix_Chunk *sample = Mix_LoadWAV("cuckoo.wav");
    if (sample == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, 
                     "Unable to load wave file\n");
        return 3;
    }

The file WAV should be stored in app/src/main/assets. Just small reminder: this is just a virtual directory, and you won’t be able to access it from other application on Android.

Playing the sound is simple:

Mix_PlayChannel(-1, sample, 0);

The last important thing about SDL2_mixer is that you should close SDL2_mixer when exiting the main. Otherwise, when you relaunch SDLActivity, it won’t be able to play any sound.

Mix_CloseAudio();

You can find the source code at GitHub in sdl2-android-example repository. Further articles about SDL2 and android are available under the tag SDL2.

11. September 2017

How to increase font size in Wireshark 2.4 on Windows

Wireshark is based on the new version of QT, and it has a similar problem on Windows like pgAdmin 4.

If you have a screen with higher density, the font will be too small.

The solution is to add one parameter for QT engine to handle DPI differently.

Open PowerShell as Administrator and go to the directory with Wireshark open qt.conf file:

cd "C:\Program Files\Wireshark"
notepad qt.conf

Write there following content:

[Platforms]
WindowsArguments = dpiawareness=0

Save the file and start Wireshark.

Note: make sure that the file name is just qt.conf. Notepad could add .txt file extension when saving the file directly from Notepad.

Enjoy bigger font:

7. September 2017

Gradle Experimental Plugin Android error: Cannot invoke method dependencies()

After initial setup of SDL2 application for Android, I decided to add one activity before launching SDL. So I created the new MainActivity which should launch SDLActivity after a tap on the button.

This time Gradle Experimental Plugin gave me a very fancy error: Cannot invoke method dependencies() on null object.

As always the error message does not provide too many hints how to solve the issue.

Android Studio automatically just appended a dependency into the Gradle file during creation of new MainActivity. So the code in build.gradle looked like this:

tasks.whenTaskAdded { task ->
    if (task.name.contains('compile')) {
        task.dependsOn ':main:distributeLib'
    }
}dependencies {
    compile 'com.android.support.constraint:constraint-layout:+'
}

Little bit weird code for dependencies. The solution was just to add a new line.

tasks.whenTaskAdded { task ->
    if (task.name.contains('compile')) {
        task.dependsOn ':main:distributeLib'
    }
}

dependencies {
    compile 'com.android.support.constraint:constraint-layout:+'
}

The whole project is available at github.com/georgik/sdl2-android-example project.

More articles about SDL2 for Android are available under tag sdl2.