Android之编译SDL2教程

// 下载 SDL version 2.0.12 (stable)
// Source Code SDL2-2.0.12.zip 
https://www.libsdl.org/download-2.0.php

// 拷贝 SDL2-2.0.10/android-project 到项目目录
拷贝 SDL2-2.0.10/android-project 至 E:/android-project 

// Android Studio 3.6.1 导入项目
File -> New -> import Project -> E:/android-project

// 移动 jni 目录
E:\android-project\app\jni 至 E:\android-project\app\src\main\jni

// 在 jni 目录中创建一个 sdl 文件夹
E:\android-project\app\src\main\jni\sdl
拷贝 SDL2-2.0.10 源码中 include、src、Android.mk 到 sdl 目录中

// 编辑 E:\android-project\app\src\main\jni\Application.mk 文件
# Uncomment this if you're using STL in your project
# You can find more information here:
# https://developer.android.com/ndk/guides/cpp-support
APP_STL := c++_shared
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
# Min runtime API level
APP_PLATFORM=android-21

// 编辑 E:\android-project\app\src\main\jni\src\Android.mk 文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := main
SDL_PATH := ../sdl
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include
# Add your application source files here...
LOCAL_SRC_FILES := YourSourceHere.c
LOCAL_SHARED_LIBRARIES := SDL2
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
include $(BUILD_SHARED_LIBRARY)

// 在 E:\android-project\app\src\main\jni\src 目录下创建 YourSourceHere.c
int main(int argc, char *argv[]) 
{
}
// 或者
#include "SDL.h"
int main(int argc, char *argv[]) {
     SDL_Window *window;
     SDL_Renderer *renderer;
     SDL_Event event;
     //配置一个图像缩放的效果,linear效果更平滑,也叫抗锯齿
     //SDL_setenv(SDL_HINT_RENDER_SCALE_QUALITY,"linear",0);
     // 初始化SDL
     if (SDL_Init(SDL_INIT_VIDEO) < 0)
     return 1;
     // 创建一个窗口
     window = SDL_CreateWindow("SDL_RenderClear" , SDL_WINDOWPOS_CENTERED,
                   SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_SHOWN);
     // 创建一个渲染器
     renderer = SDL_CreateRenderer(window, -1, 0);
     // 创建一个Surface,加载刚才下载的图片
     SDL_Surface *bmp = SDL_LoadBMP("image.bmp" );
     //设置图片中的白色为透明色
     SDL_SetColorKey(bmp, SDL_TRUE, 0xffffff);
     // 创建一个Texture
     SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, bmp);
     //清除所有事件
     SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
     //进入主循环
     while  (1) {
     if  (SDL_PollEvent(&event)) {
         if  (event.type == SDL_QUIT || event.type == SDL_KEYDOWN || event.type == SDL_FINGERDOWN)
         break;
     }
     //使用红色填充背景
     SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
     SDL_RenderClear(renderer);
     // 将纹理布置到渲染器
     SDL_RenderCopy(renderer, texture, NULL, NULL);
     // 刷新屏幕
     SDL_RenderPresent(renderer);
     }
      // 释放Surface
     SDL_FreeSurface(bmp);
     //  释放Texture
     SDL_DestroyTexture(texture);
     //释放渲染器
     SDL_DestroyRenderer(renderer);
     //释放窗口
     SDL_DestroyWindow(window);
     //延时
     //SDL_Delay(8000);
     //退出
     SDL_Quit();
     return  0;
}

// build.gradle 修改 ...
def buildAsLibrary = project.hasProperty('BUILD_AS_LIBRARY');
def buildAsApplication = !buildAsLibrary
if (buildAsApplication) {
    apply plugin: 'com.android.application'
}
else {
    apply plugin: 'com.android.library'
}

android {
    compileSdkVersion 29
    defaultConfig {
    if (buildAsApplication) {
        applicationId "org.libsdl.app"
    }
    minSdkVersion 21
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    externalNativeBuild {
        ndkBuild {
        arguments "APP_PLATFORM=android-21"
        abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        }
    }
    }
    buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    if (!project.hasProperty('EXCLUDE_NATIVE_LIBS')) {
    sourceSets.main {
        jniLibs.srcDir 'src/main/jni'
    }
    externalNativeBuild {
        ndkBuild {
        path 'src/main/jni/Android.mk'
        }
    }

    }
    lintOptions {
    abortOnError false
    }

    if (buildAsLibrary) {
    libraryVariants.all { variant ->
        variant.outputs.each { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith(".aar")) {
            def fileName = "org.libsdl.app.aar";
            output.outputFile = new File(outputFile.parent, fileName);
        }
        }
    }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
}
// so 会生成在 app/build/ndkBuild/debug/obj/local/ 目录中 ...