Preparing an SDL2 project to run on android

Hi all. Today we will see how to prepare a project using the sdl2 library to run the game on android.

First you need to download Android Studio, install it and everything that is required in this development environment. For example, I currently have Kde Neon, and there is a /etc/environment file on this system, the same file exists in ubuntu. There it is necessary to register such variables.

ANDROID_HOME=/home/username/Android/Sdk
ANDROID_NDK_HOME=/home/username/ndk

You also need to download the NDK from the official site, unzip it to your home directory and rename it to NDK. Next, you need to download the SDL2 library from the site libsdl.org. To use sdl2 for android it is important not to compile it for computer because then it will not compile for android. In order for the project to compile, you need to create a project in android studio, any one, to accept the license, otherwise when building SDL2 it will ask for a license.

To read files in android from assets, you need to use the SDL_RWops functions. Here is an example of using it in code for working with a font. In this case, we cannot use FT_New_Face, but instead we will use FT_New_Memory_Face to use the data already read.

#ifdef __ANDROID__
        snprintf ( path, 254, "fonts/%s", file );
        SDL_RWops *rw = SDL_RWFromFile(path, "r" );
        char *memory = ( char * ) calloc ( rw->hidden.androidio.size, 1 );
        SDL_RWread(rw, memory, 1, rw->hidden.androidio.size );

        FT_New_Memory_Face(*this->ft_library, ( const FT_Byte  * )memory, rw->hidden.androidio.size, 0, &this;->face );
        SDL_RWclose(rw);
        free ( memory );
#else
        snprintf ( path, 254, "%s/fonts/%s", DEFAULT_ASSETS, file );
        if ( access ( path, F_OK ) ) {
                fprintf ( stderr, "not found font: %sn", path );
                exit ( EXIT_FAILURE );
        }
        struct stat st;
        stat ( path, &st; );
        FILE *rw = fopen ( path, "r" );
        char *memory = ( char * ) calloc ( st.st_size, 1 );
        fread ( memory, 1, st.st_size, rw );

        FT_New_Memory_Face ( *this->ft_library, ( const FT_Byte * ) memory, st.st_size, 0, &this;->face );
        fclose ( rw );
        free ( memory );
#endif

Also I created a header file to connect SDL2 headers. NO_SDL_GLEXT is required for compilation to succeed for Android.

#ifdef __ANDROID__
#include "SDL.h"
#include "SDL_video.h"
#include "SDL_events.h"
#define NO_SDL_GLEXT 
#include "SDL_opengl.h"
#include "SDL_opengles2.h"
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_opengles2.h>
#endif

So the project is ready, shaders are ready for Opengl Es 3.0. Now we need to create an android-project. To do this, unpack the archive SDL2. Go to build-scripts. And we do so.

./androidbuild.sh com.xverizex.test main.cpp

This message will appear.

To build and install to a device for testing, run the following:
cd /home/cf/programs/SDL2-2.0.10/build/com.xverizex.test
./gradlew installDebug

Go to com.xverizex.test. Go to com.xverizex.test/app/jni/src. Copy your game project. And we change the Android.mk file, in my case it looks like this.

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := main

SDL_PATH := ../SDL
FREETYPE_PATH := ../Freetype2

LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include $(LOCAL_PATH)/$(FREETYPE_PATH)/include

# Add your application source files here...
LOCAL_SRC_FILES := ./engine/lang.cpp ./engine/actor.cpp ./engine/sprite.cpp ./engine/shaders.cpp ./engine/box.cpp ./engine/menubox.cpp ./engine/load_manager.cpp ./engine/main.cpp ./engine/font.cpp ./engine/model.cpp ./engine/button.cpp ./theme.cpp ./level_manager.cpp ./menu/menu.cpp

LOCAL_SHARED_LIBRARIES := SDL2 Freetype2

LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog 

include $(BUILD_SHARED_LIBRARY)

As you may have noticed, I also include the Freetype2 library. I found a ready-made one on github for android, but it didn't work, I had to change something. We also create the app/src/main/assets directory. We put our resources in it (fonts, sprites, 3d models).

Now let's set up Freetype2 for Android. Download from my github Link, and copy the Freetype2 directory to the app/jni/ directory. All is ready. Now we run the ./gradlew installDebug command in com.xverizex.test. To be able to add this game to android, debugging must be enabled in android. To do this, you need to go to the settings, go to "System", go to "About tablet" and click on the "Build number" option about six times. Then go back and the option for developers will appear. We go in and turn it on, also turn on the option "Debugging via USB". Now we need to get the key for the tablet. To do this, install the adb program. We launch the adb shell in the console, and a key appears on the tablet that must be accepted. Everything, now the games can be downloaded to the tablet.

Source: habr.com

Add a comment