Android之使用SDL播放图片

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

// 下载 SDL_image 2.0
// SDL2_image-2.0.5.zip
https://www.libsdl.org/projects/SDL_image/

解压 SDL2-2.0.12.zip 至 E:/SDL2-2.0.12
复制 E:/SDL2-2.0.12/android-project 至 E:\android-project
复制 E:/SDL2-2.0.12 至 E:\android-project\app\jni\sdl
解压 SDL2_image-2.0.5.zip 至 E:\android-project\app\jni\sdl_image
移除 E:/SDL2-2.0.12/android-project/app/jni/CMakeLists.txt
移除 E:/SDL2-2.0.12/android-project/app/jni/src/CMakeLists.txt
// 使用 android studio 打开 E:\android-project 项目

// 配置 local.properties 中 NDK 路径
sdk.dir=D\:\\Android\\SDK

// 使用的 NDK 版本
Pkg.Desc = Android NDK
Pkg.Revision = 20.1.5948944
// 在 app\jni\src 下新建 YourSourceHere.c 

#include <android/log.h>
#include "SDL.h"
#include "SDL_image.h"

#define LOG_TAG "xplay"

#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO , LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN , LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , LOG_TAG, __VA_ARGS__)

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
    IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
    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 *jpg = IMG_Load("a1.jpg");
//    SDL_Surface *bmp = SDL_LoadBMP("test.bmp");
//    //设置图片中的白色为透明色
//    SDL_SetColorKey(bmp, SDL_TRUE, 0xffffff);
    // 创建一个Texture
    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, jpg);
    //清除所有事件
    SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
    //进入主循环
    while  (1) {
    Uint32 a = SDL_GetTicks();
    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);
    Uint32 b = SDL_GetTicks();
    LOGD(">>> %d", b - a);
    }
    // 释放Surface
    SDL_FreeSurface(jpg);
    //  释放Texture
    SDL_DestroyTexture(texture);
    //释放渲染器
    SDL_DestroyRenderer(renderer);
    //释放窗口
    SDL_DestroyWindow(window);
    //延时
    //SDL_Delay(8000);
    //退出
    SDL_Quit();
    return  0;
}
// 修改 app\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 SDL2_image

LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog

include $(BUILD_SHARED_LIBRARY)
// 创建目录 app\src\main\assets
// 向目录拷贝一张 a1.jpg 图片
// 更改 org.libsdl.app 为 com.nljb.xplay
// 新建目录 app\src\main\java\com\danoo\xplay 
// 在目录中新建一个空白的 MainActivity.java

package com.nljb.xplay;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
}
// 修改 AndroidManifest.xml 中 org.libsdl.app 为 com.nljb.xplay

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nljb.xplay"
    android:installLocation="auto">
    <!-- OpenGL ES 2.0 -->
    <uses-feature android:glEsVersion="0x00020000" /> <!-- Touchscreen support -->
    <uses-feature
    android:name="android.hardware.touchscreen"
    android:required="false" /> <!-- Game controller support -->
    <uses-feature
    android:name="android.hardware.bluetooth"
    android:required="false" />
    <uses-feature
    android:name="android.hardware.gamepad"
    android:required="false" />
    <uses-feature
    android:name="android.hardware.usb.host"
    android:required="false" /> <!-- External mouse input events -->
    <uses-feature
    android:name="android.hardware.type.pc"
    android:required="false" /> <!-- Audio recording support -->
    <!-- if you want to capture audio, uncomment this. -->
    <!--
 <uses-feature
    android:name="android.hardware.microphone"
    android:required="false" />
    -->
    <!-- Allow writing to external storage -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- Allow access to Bluetooth devices -->
    <uses-permission android:name="android.permission.BLUETOOTH" /> <!-- Allow access to the vibrator -->
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    <activity android:name=".MainActivity"></activity>
    <!--
     Example of setting SDL hints from AndroidManifest.xml:
    <meta-data android:name="SDL_ENV.SDL_ACCELEROMETER_AS_JOYSTICK" android:value="0"/>
    -->
    <activity
        android:name="org.libsdl.app.SDLActivity"
        android:alwaysRetainTaskState="true"
        android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
        android:label="@string/app_name"
        android:launchMode="singleInstance">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <!-- Drop file event -->
        <!--
        <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="*/*" />
        </intent-filter>
        -->
    </activity>
    </application>

</manifest>
// 修改 build.gradle
applicationId "com.danoo.xplay"