Preparing the SDL2 project for Android deployment

Hello everyone. Today we'll look at how to prepare a project using the SDL2 library to launch a game on Android.

First, you need to download Android Studio, install it, and set up everything required for development in this environment. For example, I currently have Kde Neon, and this system has the file /etc/environment, which is also present in Ubuntu. You need to define certain variables there.

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

You also need to download the NDK from the official website, unpack it in your home directory, and rename it to NDK. Next, download the SDL2 library from the site libsdl.org. To use SDL2 for Android, it’s crucial not to compile it for a PC, as that would prevent it from compiling for Android. To compile the project, you must create any project in Android Studio to accept the license; otherwise, SDL2 will request a license during the build.

To read files in Android from assets, you need to use the SDL_RWops functions. Here’s an example in code for working with fonts. In this case, we cannot use FT_New_Face, so instead, we will use FT_New_Memory_Face to utilize already read data.

#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

I also created a header file to include SDL2 headers. NO_SDL_GLEXT is required for successful compilation on 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, and the shaders are ready for OpenGL ES 3.0. Now you need to create an android-project. To do this, extract the SDL2 archive. Enter the build-scripts directory. And execute as follows.

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

You will see the following message.

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

Navigate to com.xverizex.test. Go to com.xverizex.test/app/jni/src. Copy your game project here. And modify 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 already noticed, I am connecting another library called Freetype2. I found a ready-made version on GitHub for Android, but it didn't work; some adjustments were needed. We will also create the directory app/src/main/assets. We will place our resources (fonts, sprites, 3D models) there.

Now let's set up Freetype2 for Android. Download it from my GitHub. Link, and copy the Freetype2 directory into the app/jni/ directory. Everything is ready. Now run the command ./gradlew installDebug in com.xverizex.test. To add this game to Android, debugging must be enabled on the device. To do this, go to settings, navigate to 'System', then 'About tablet', and tap the 'Build number' option about six times. Then go back, and an option for developers will appear. Enter it and enable it, along with the 'USB debugging' option. Now we need to obtain a key for the tablet. To do this, we install the adb program. Run adb shell in the console, and a key will appear on the tablet that must be accepted. That's it; now games can be loaded onto the tablet.

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster