其實1.3以後的版本就添加了android的支援,所以下載完源碼後,在SDL目錄下建立一個jni的目錄,然後將原先SDL目錄下面的檔案以及檔案夾都拷貝到jni目錄下面,在jni目錄下面我們會看見一個android.mk的檔案,這就是官方給我們編寫的編譯檔案,由於手機基本上都是使用的arm編譯器,所以我在裡面添加了LOCAL_ARM_MODE=arm,模組的編譯使用arm編譯器來編譯,具體的內容如下:
[cpp] ###########################
#
# SDL shared library
#
###########################
include $(CLEAR_VARS)
LOCAL_MODULE := SDL2
LOCAL_ARM_MODE=arm
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := \
$(subst $(LOCAL_PATH)/,, \
$(wildcard $(LOCAL_PATH)/src/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/dummy/*.c) \
$(LOCAL_PATH)/src/atomic/SDL_atomic.c \
$(LOCAL_PATH)/src/atomic/SDL_spinlock.c.arm \
$(wildcard $(LOCAL_PATH)/src/core/android/*.cpp) \
$(wildcard $(LOCAL_PATH)/src/cpuinfo/*.c) \
$(wildcard $(LOCAL_PATH)/src/events/*.c) \
$(wildcard $(LOCAL_PATH)/src/file/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/dummy/*.c) \
$(wildcard $(LOCAL_PATH)/src/joystick/*.c) \
$(wildcard $(LOCAL_PATH)/src/joystick/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/loadso/dlopen/*.c) \
$(wildcard $(LOCAL_PATH)/src/power/*.c) \
$(wildcard $(LOCAL_PATH)/src/power/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/render/*.c) \
$(wildcard $(LOCAL_PATH)/src/render/*/*.c) \
$(wildcard $(LOCAL_PATH)/src/stdlib/*.c) \
$(wildcard $(LOCAL_PATH)/src/thread/*.c) \
$(wildcard $(LOCAL_PATH)/src/thread/pthread/*.c) \
$(wildcard $(LOCAL_PATH)/src/timer/*.c) \
$(wildcard $(LOCAL_PATH)/src/timer/unix/*.c) \
$(wildcard $(LOCAL_PATH)/src/video/*.c) \
$(wildcard $(LOCAL_PATH)/src/video/android/*.c))
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog
include $(BUILD_SHARED_LIBRARY)
###########################
#
# SDL shared library
#
###########################
include $(CLEAR_VARS)
LOCAL_MODULE := SDL2
LOCAL_ARM_MODE=arm
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := \
$(subst $(LOCAL_PATH)/,, \
$(wildcard $(LOCAL_PATH)/src/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/dummy/*.c) \
$(LOCAL_PATH)/src/atomic/SDL_atomic.c \
$(LOCAL_PATH)/src/atomic/SDL_spinlock.c.arm \
$(wildcard $(LOCAL_PATH)/src/core/android/*.cpp) \
$(wildcard $(LOCAL_PATH)/src/cpuinfo/*.c) \
$(wildcard $(LOCAL_PATH)/src/events/*.c) \
$(wildcard $(LOCAL_PATH)/src/file/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/dummy/*.c) \
$(wildcard $(LOCAL_PATH)/src/joystick/*.c) \
$(wildcard $(LOCAL_PATH)/src/joystick/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/loadso/dlopen/*.c) \
$(wildcard $(LOCAL_PATH)/src/power/*.c) \
$(wildcard $(LOCAL_PATH)/src/power/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/render/*.c) \
$(wildcard $(LOCAL_PATH)/src/render/*/*.c) \
$(wildcard $(LOCAL_PATH)/src/stdlib/*.c) \
$(wildcard $(LOCAL_PATH)/src/thread/*.c) \
$(wildcard $(LOCAL_PATH)/src/thread/pthread/*.c) \
$(wildcard $(LOCAL_PATH)/src/timer/*.c) \
$(wildcard $(LOCAL_PATH)/src/timer/unix/*.c) \
$(wildcard $(LOCAL_PATH)/src/video/*.c) \
$(wildcard $(LOCAL_PATH)/src/video/android/*.c))
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog
include $(BUILD_SHARED_LIBRARY)三、如果直接編譯的話會提示如下錯誤
[cpp] /home/SDL/jni/src/core/android/SDL_android.cpp:30:21: fatal error: EGL/egl.h: No such file or directory
/home/SDL/jni/src/core/android/SDL_android.cpp:30:21: fatal error: EGL/egl.h: No such file or directory
查看EGL的支援是在2.3版本以上,所以我們需要添加平台的支援,所以需要在jni目錄下面建立一個Application.mk檔案來指定編譯平台,內容如下:
[cpp] APP_ABI := armeabi
APP_PLATFORM := android-9
APP_ABI := armeabi
APP_PLATFORM := android-9四、在jni目錄終端下運行ndk-build,就會編譯出libSDL2.so,編譯過程如下:
[cpp] root@zhangjie:/home/SDL/jni# ndk-build
Compile arm : SDL2 <= SDL_assert.c
Compile arm : SDL2 <= SDL.c
Compile arm : SDL2 <= SDL_error.c
Compile arm : SDL2 <= SDL_fatal.c
Compile arm : SDL2 <= SDL_hints.c
Compile arm : SDL2 <= SDL_log.c
Compile arm : SDL2 <= SDL_audio.c
Compile arm : SDL2 <= SDL_audiocvt.c
Compile arm : SDL2 <= SDL_audiodev.c
Compile arm : SDL2 <= SDL_audiotypecvt.c
Compile arm : SDL2 <= SDL_mixer.c
Compile arm : SDL2 <= SDL_wave.c
Compile arm : SDL2 <= SDL_androidaudio.c
Compile arm : SDL2 <= SDL_dummyaudio.c
Compile arm : SDL2 <= SDL_atomic.c
Compile arm : SDL2 <= SDL_spinlock.c
Compile++ arm : SDL2 <= SDL_android.cpp
Compile arm : SDL2 <= SDL_cpuinfo.c
Compile arm : SDL2 <= SDL_clipboardevents.c
Compile arm : SDL2 <= SDL_dropevents.c
Compile arm : SDL2 <= SDL_events.c
Compile arm : SDL2 <= SDL_gesture.c
Compile arm : SDL2 <= SDL_keyboard.c
Compile arm : SDL2 <= SDL_mouse.c
Compile arm : SDL2 <= SDL_quit.c
Compile arm : SDL2 <= SDL_touch.c
Compile arm : SDL2 <= SDL_windowevents.c
Compile arm : SDL2 <= SDL_rwops.c
Compile arm : SDL2 <= SDL_haptic.c
Compile arm : SDL2 <= SDL_syshaptic.c
Compile arm : SDL2 <= SDL_gamecontroller.c
Compile arm : SDL2 <= SDL_joystick.c
Compile arm : SDL2 <= SDL_sysjoystick.c
Compile arm : SDL2 <= SDL_sysloadso.c
Compile arm : SDL2 <= SDL_power.c
Compile arm : SDL2 <= SDL_syspower.c
Compile arm : SDL2 <= SDL_render.c
Compile arm : SDL2 <= SDL_yuv_mmx.c
Compile arm : SDL2 <= SDL_yuv_sw.c
Compile arm : SDL2 <= SDL_render_d3d.c
Compile arm : SDL2 <= SDL_render_gles2.c
Compile arm : SDL2 <= SDL_shaders_gles2.c
Compile arm : SDL2 <= SDL_render_gles.c
Compile arm : SDL2 <= SDL_render_gl.c
Compile arm : SDL2 <= SDL_shaders_gl.c
Compile arm : SDL2 <= SDL_render_psp.c
Compile arm : SDL2 <= SDL_blendfillrect.c
Compile arm : SDL2 <= SDL_blendline.c
Compile arm : SDL2 <= SDL_blendpoint.c
Compile arm : SDL2 <= SDL_drawline.c
Compile arm : SDL2 <= SDL_drawpoint.c
Compile arm : SDL2 <= SDL_render_sw.c
Compile arm : SDL2 <= SDL_rotate.c
Compile arm : SDL2 <= SDL_getenv.c
Compile arm : SDL2 <= SDL_iconv.c
Compile arm : SDL2 <= SDL_malloc.c
Compile arm : SDL2 <= SDL_qsort.c
Compile arm : SDL2 <= SDL_stdlib.c
Compile arm : SDL2 <= SDL_string.c
Compile arm : SDL2 <= SDL_thread.c
Compile arm : SDL2 <= SDL_syscond.c
Compile arm : SDL2 <= SDL_sysmutex.c
Compile arm : SDL2 <= SDL_syssem.c
Compile arm : SDL2 <= SDL_systhread.c
Compile arm : SDL2 <= SDL_timer.c
Compile arm : SDL2 <= SDL_systimer.c
Compile arm : SDL2 <= SDL_blit_0.c
Compile arm : SDL2 <= SDL_blit_1.c
Compile arm : SDL2 <= SDL_blit_A.c
Compile arm : SDL2 <= SDL_blit_auto.c
Compile arm : SDL2 <= SDL_blit.c
Compile arm : SDL2 <= SDL_blit_copy.c
Compile arm : SDL2 <= SDL_blit_N.c
Compile arm : SDL2 <= SDL_blit_slow.c
Compile arm : SDL2 <= SDL_bmp.c
Compile arm : SDL2 <= SDL_clipboard.c
Compile arm : SDL2 <= SDL_fillrect.c
Compile arm : SDL2 <= SDL_pixels.c
Compile arm : SDL2 <= SDL_rect.c
Compile arm : SDL2 <= SDL_RLEaccel.c
Compile arm : SDL2 <= SDL_shape.c
Compile arm : SDL2 <= SDL_stretch.c
Compile arm : SDL2 <= SDL_surface.c
Compile arm : SDL2 <= SDL_video.c
Compile arm : SDL2 <= SDL_androidclipboard.c
Compile arm : SDL2 <= SDL_androidevents.c
Compile arm : SDL2 <= SDL_androidgl.c
Compile arm : SDL2 <= SDL_androidkeyboard.c
Compile arm : SDL2 <= SDL_androidtouch.c
Compile arm : SDL2 <= SDL_androidvideo.c
Compile arm : SDL2 <= SDL_androidwindow.c
StaticLibrary : libstdc++.a
SharedLibrary : libSDL2.so
Install : libSDL2.so => libs/armeabi/libSDL2.so
root@zhangjie:/home/SDL/jni# ndk-build
Compile arm : SDL2 <= SDL_assert.c
Compile arm : SDL2 <= SDL.c
Compile arm : SDL2 <= SDL_error.c
Compile arm : SDL2 <= SDL_fatal.c
Compile arm : SDL2 <= SDL_hints.c
Compile arm : SDL2 <= SDL_log.c
Compile arm : SDL2 <= SDL_audio.c
Compile arm : SDL2 <= SDL_audiocvt.c
Compile arm : SDL2 <= SDL_audiodev.c
Compile arm : SDL2 <= SDL_audiotypecvt.c
Compile arm : SDL2 <= SDL_mixer.c
Compile arm : SDL2 <= SDL_wave.c
Compile arm : SDL2 <= SDL_androidaudio.c
Compile arm : SDL2 <= SDL_dummyaudio.c
Compile arm : SDL2 <= SDL_atomic.c
Compile arm : SDL2 <= SDL_spinlock.c
Compile++ arm : SDL2 <= SDL_android.cpp
Compile arm : SDL2 <= SDL_cpuinfo.c
Compile arm : SDL2 <= SDL_clipboardevents.c
Compile arm : SDL2 <= SDL_dropevents.c
Compile arm : SDL2 <= SDL_events.c
Compile arm : SDL2 <= SDL_gesture.c
Compile arm : SDL2 <= SDL_keyboard.c
Compile arm : SDL2 <= SDL_mouse.c
Compile arm : SDL2 <= SDL_quit.c
Compile arm : SDL2 <= SDL_touch.c
Compile arm : SDL2 <= SDL_windowevents.c
Compile arm : SDL2 <= SDL_rwops.c
Compile arm : SDL2 <= SDL_haptic.c
Compile arm : SDL2 <= SDL_syshaptic.c
Compile arm : SDL2 <= SDL_gamecontroller.c
Compile arm : SDL2 <= SDL_joystick.c
Compile arm : SDL2 <= SDL_sysjoystick.c
Compile arm : SDL2 <= SDL_sysloadso.c
Compile arm : SDL2 <= SDL_power.c
Compile arm : SDL2 <= SDL_syspower.c
Compile arm : SDL2 <= SDL_render.c
Compile arm : SDL2 <= SDL_yuv_mmx.c
Compile arm : SDL2 <= SDL_yuv_sw.c
Compile arm : SDL2 <= SDL_render_d3d.c
Compile arm : SDL2 <= SDL_render_gles2.c
Compile arm : SDL2 <= SDL_shaders_gles2.c
Compile arm : SDL2 <= SDL_render_gles.c
Compile arm : SDL2 <= SDL_render_gl.c
Compile arm : SDL2 <= SDL_shaders_gl.c
Compile arm : SDL2 <= SDL_render_psp.c
Compile arm : SDL2 <= SDL_blendfillrect.c
Compile arm : SDL2 <= SDL_blendline.c
Compile arm : SDL2 <= SDL_blendpoint.c
Compile arm : SDL2 <= SDL_drawline.c
Compile arm : SDL2 <= SDL_drawpoint.c
Compile arm : SDL2 <= SDL_render_sw.c
Compile arm : SDL2 <= SDL_rotate.c
Compile arm : SDL2 <= SDL_getenv.c
Compile arm : SDL2 <= SDL_iconv.c
Compile arm : SDL2 <= SDL_malloc.c
Compile arm : SDL2 <= SDL_qsort.c
Compile arm : SDL2 <= SDL_stdlib.c
Compile arm : SDL2 <= SDL_string.c
Compile arm : SDL2 <= SDL_thread.c
Compile arm : SDL2 <= SDL_syscond.c
Compile arm : SDL2 <= SDL_sysmutex.c
Compile arm : SDL2 <= SDL_syssem.c
Compile arm : SDL2 <= SDL_systhread.c
Compile arm : SDL2 <= SDL_timer.c
Compile arm : SDL2 <= SDL_systimer.c
Compile arm : SDL2 <= SDL_blit_0.c
Compile arm : SDL2 <= SDL_blit_1.c
Compile arm : SDL2 <= SDL_blit_A.c
Compile arm : SDL2 <= SDL_blit_auto.c
Compile arm : SDL2 <= SDL_blit.c
Compile arm : SDL2 <= SDL_blit_copy.c
Compile arm : SDL2 <= SDL_blit_N.c
Compile arm : SDL2 <= SDL_blit_slow.c
Compile arm : SDL2 <= SDL_bmp.c
Compile arm : SDL2 <= SDL_clipboard.c
Compile arm : SDL2 <= SDL_fillrect.c
Compile arm : SDL2 <= SDL_pixels.c
Compile arm : SDL2 <= SDL_rect.c
Compile arm : SDL2 <= SDL_RLEaccel.c
Compile arm : SDL2 <= SDL_shape.c
Compile arm : SDL2 <= SDL_stretch.c
Compile arm : SDL2 <= SDL_surface.c
Compile arm : SDL2 <= SDL_video.c
Compile arm : SDL2 <= SDL_androidclipboard.c
Compile arm : SDL2 <= SDL_androidevents.c
Compile arm : SDL2 <= SDL_androidgl.c
Compile arm : SDL2 <= SDL_androidkeyboard.c
Compile arm : SDL2 <= SDL_androidtouch.c
Compile arm : SDL2 <= SDL_androidvideo.c
Compile arm : SDL2 <= SDL_androidwindow.c
StaticLibrary : libstdc++.a
SharedLibrary : libSDL2.so
Install : libSDL2.so => libs/armeabi/libSDL2.so
編譯出sdl庫後,我們就可以調用它的相關函數來實現我們的功能了。