[原]零基础学习SDL开发之移植SDL2.0到Android

时间:2022-12-26 13:32:16

[原]SDL开发教程我们知道了如何在pc下使用SDL进行开发,在android上面是否一样可以使用呢?答案是肯定的。

下面我们进行移植SDL到Android,这里都是基于SDL最新版进行移植的,在Eclipse中编译。

开发环境:Ubuntu 14.04 64位,Eclipse + CDT + ADT,NDK

一、到官网下载http://www.libsdl.org/hg.php 下载2.0的源码也可以通过hg来clone最新的源码
hg clone http://hg.libsdl.org/SDL

二、编译SDL

进入到下载的文件SDL2-2.0.3中将android-project导入到Eclipse中,进入jni文件夹,新建一个SDL文件,将SDL2-2.0.3\src 、SDL2-2.0.3\include、SDL2-2.0.3\Android.mk文件拷贝到android-project\jni\SDL\中。
 
android-project\jni\SDL\Android.mk文件内容如下:
LOCAL_PATH := $(call my-dir)

###########################
#
# SDL shared library
#
###########################

include $(CLEAR_VARS)

LOCAL_MODULE :
= SDL2

LOCAL_C_INCLUDES :
= $(LOCAL_PATH)/include

LOCAL_EXPORT_C_INCLUDES :
= $(LOCAL_C_INCLUDES)

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/*.c) \
$(wildcard $(LOCAL_PATH)/src/cpuinfo/*.c) \
$(wildcard $(LOCAL_PATH)/src/dynapi/*.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/filesystem/dummy/*.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) \
$(wildcard $(LOCAL_PATH)/src/test/*.c))

LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid

include $(BUILD_SHARED_LIBRARY)

###########################
#
# SDL static library
#
###########################

LOCAL_MODULE := SDL2_static

LOCAL_MODULE_FILENAME := libSDL2

LOCAL_SRC_FILES += $(LOCAL_PATH)/src/main/android/SDL_android_main.c

LOCAL_LDLIBS :=
LOCAL_EXPORT_LDLIBS := -Wl,--undefined=Java_org_libsdl_app_SDLActivity_nativeInit -ldl -lGLESv1_CM -lGLESv2 -llog -landroid

include $(BUILD_STATIC_LIBRARY)

在Eclipse中配置NDK, Eclipse->Window->Prefreneces->Android->NDK.

三、如果直接编译的话会提示如下错误

 

fatal error: EGL/egl.h: No such file or directory  

 

查看EGL的支持是在2.3版本以上,所以我们需要添加平台的支持,所以在jni目录修改下Application.mk文件来指定编译平台:

# Uncomment this if you're using STL in your project
# See CPLUSPLUS-SUPPORT.html in the NDK documentation for more information
# APP_STL :
= stlport_static

APP_ABI :
= armeabi armeabi-v7a x86
APP_PLATFORM :
= android-9

四、在Eclipse中执行Ctrl+B,就会编译出libSDL2.so,编译过程如下:

12:14:54 **** Build of configuration Default for project Android_SDL4 ****
/opt/android-ndk/ndk/ndk-build all
Compile thumb : SDL2
<= SDL_assert.c
Compile thumb : SDL2
<= SDL.c
Compile thumb : SDL2
<= SDL_error.c
Compile thumb : SDL2
<= SDL_hints.c
Compile thumb : SDL2
<= SDL_log.c
Compile thumb : SDL2
<= SDL_audio.c
Compile thumb : SDL2
<= SDL_audiocvt.c
Compile thumb : SDL2
<= SDL_audiodev.c
Compile thumb : SDL2
<= SDL_audiotypecvt.c
Compile thumb : SDL2
<= SDL_mixer.c
Compile thumb : SDL2
<= SDL_wave.c
Compile thumb : SDL2
<= SDL_androidaudio.c
Compile thumb : SDL2
<= SDL_dummyaudio.c
Compile thumb : SDL2
<= SDL_atomic.c
Compile arm : SDL2
<= SDL_spinlock.c
Compile thumb : SDL2
<= SDL_android.c
Compile thumb : SDL2
<= SDL_cpuinfo.c
Compile thumb : SDL2
<= SDL_dynapi.c
Compile thumb : SDL2
<= SDL_clipboardevents.c
Compile thumb : SDL2
<= SDL_dropevents.c
Compile thumb : SDL2
<= SDL_events.c
Compile thumb : SDL2
<= SDL_gesture.c
Compile thumb : SDL2
<= SDL_keyboard.c
Compile thumb : SDL2
<= SDL_mouse.c
Compile thumb : SDL2
<= SDL_quit.c
Compile thumb : SDL2
<= SDL_touch.c
Compile thumb : SDL2
<= SDL_windowevents.c
Compile thumb : SDL2
<= SDL_rwops.c
Compile thumb : SDL2
<= SDL_haptic.c
Compile thumb : SDL2
<= SDL_syshaptic.c
Compile thumb : SDL2
<= SDL_gamecontroller.c
Compile thumb : SDL2
<= SDL_joystick.c
Compile thumb : SDL2
<= SDL_sysjoystick.c
Compile thumb : SDL2
<= SDL_sysloadso.c
Compile thumb : SDL2
<= SDL_power.c
Compile thumb : SDL2
<= SDL_syspower.c
Compile thumb : SDL2
<= SDL_sysfilesystem.c
Compile thumb : SDL2
<= SDL_d3dmath.c
Compile thumb : SDL2
<= SDL_render.c
Compile thumb : SDL2
<= SDL_yuv_mmx.c
Compile thumb : SDL2
<= SDL_yuv_sw.c
Compile thumb : SDL2
<= SDL_render_d3d11.c
Compile thumb : SDL2
<= SDL_render_d3d.c
Compile thumb : SDL2
<= SDL_render_gles2.c
Compile thumb : SDL2
<= SDL_shaders_gles2.c
Compile thumb : SDL2
<= SDL_render_gles.c
Compile thumb : SDL2
<= SDL_render_gl.c
Compile thumb : SDL2
<= SDL_shaders_gl.c
Compile thumb : SDL2
<= SDL_render_psp.c
Compile thumb : SDL2
<= SDL_blendfillrect.c
Compile thumb : SDL2
<= SDL_blendline.c
Compile thumb : SDL2
<= SDL_blendpoint.c
Compile thumb : SDL2
<= SDL_drawline.c
Compile thumb : SDL2
<= SDL_drawpoint.c
Compile thumb : SDL2
<= SDL_render_sw.c
Compile thumb : SDL2
<= SDL_rotate.c
Compile thumb : SDL2
<= SDL_getenv.c
Compile thumb : SDL2
<= SDL_iconv.c
Compile thumb : SDL2
<= SDL_malloc.c
Compile thumb : SDL2
<= SDL_qsort.c
Compile thumb : SDL2
<= SDL_stdlib.c
Compile thumb : SDL2
<= SDL_string.c
Compile thumb : SDL2
<= SDL_thread.c
Compile thumb : SDL2
<= SDL_syscond.c
Compile thumb : SDL2
<= SDL_sysmutex.c
Compile thumb : SDL2
<= SDL_syssem.c
Compile thumb : SDL2
<= SDL_systhread.c
Compile thumb : SDL2
<= SDL_systls.c
Compile thumb : SDL2
<= SDL_timer.c
Compile thumb : SDL2
<= SDL_systimer.c
Compile thumb : SDL2
<= SDL_blit_0.c
Compile thumb : SDL2
<= SDL_blit_1.c
Compile thumb : SDL2
<= SDL_blit_A.c
Compile thumb : SDL2
<= SDL_blit_auto.c
Compile thumb : SDL2
<= SDL_blit.c
Compile thumb : SDL2
<= SDL_blit_copy.c
Compile thumb : SDL2
<= SDL_blit_N.c
Compile thumb : SDL2
<= SDL_blit_slow.c
Compile thumb : SDL2
<= SDL_bmp.c
Compile thumb : SDL2
<= SDL_clipboard.c
Compile thumb : SDL2
<= SDL_egl.c
Compile thumb : SDL2
<= SDL_fillrect.c
Compile thumb : SDL2
<= SDL_pixels.c
Compile thumb : SDL2
<= SDL_rect.c
Compile thumb : SDL2
<= SDL_RLEaccel.c
Compile thumb : SDL2
<= SDL_shape.c
Compile thumb : SDL2
<= SDL_stretch.c
Compile thumb : SDL2
<= SDL_surface.c
Compile thumb : SDL2
<= SDL_video.c
Compile thumb : SDL2
<= SDL_androidclipboard.c
Compile thumb : SDL2
<= SDL_androidevents.c
Compile thumb : SDL2
<= SDL_androidgl.c
Compile thumb : SDL2
<= SDL_androidkeyboard.c
Compile thumb : SDL2
<= SDL_androidtouch.c
Compile thumb : SDL2
<= SDL_androidvideo.c
Compile thumb : SDL2
<= SDL_androidwindow.c
Compile thumb : SDL2
<= SDL_test_assert.c
Compile thumb : SDL2
<= SDL_test_common.c
Compile thumb : SDL2
<= SDL_test_compare.c
Compile thumb : SDL2
<= SDL_test_crc32.c
Compile thumb : SDL2
<= SDL_test_font.c
Compile thumb : SDL2
<= SDL_test_fuzzer.c
Compile thumb : SDL2
<= SDL_test_harness.c
Compile thumb : SDL2
<= SDL_test_imageBlitBlend.c
Compile thumb : SDL2
<= SDL_test_imageBlit.c
Compile thumb : SDL2
<= SDL_test_imageFace.c
Compile thumb : SDL2
<= SDL_test_imagePrimitivesBlend.c
Compile thumb : SDL2
<= SDL_test_imagePrimitives.c
Compile thumb : SDL2
<= SDL_test_log.c
Compile thumb : SDL2
<= SDL_test_md5.c
Compile thumb : SDL2
<= SDL_test_random.c
SharedLibrary : libSDL2.so
Install : libSDL2.so
=> libs/armeabi/libSDL2.so
Compile thumb : main
<= SDL_android_main.c
Compile thumb : main
<= SDL_Lesson.c
SharedLibrary : libmain.so
Install : libmain.so
=> libs/armeabi/libmain.so

12:15:40 Build Finished (took 45s.806ms)

 

后面我们就可以来使用sdl进行开发了