编译SDL_image

 http://lazyfoo.net/tutorials/SDL/53_extensions_and_changing_orientation/android_windows/index.php

里面有一个错误的地方,SDL2_image是新建在jni下的而非src,经过试验能成功在win7下编译生成.so文件!

 

1)  First, download the SDL_image source on   this page.

2)  Download the   demo source/assets. Copy the directory to android-project/assets. Remember: if the application needs to load "53_extensions_and_changing_orientation/portrait.png" it needs to be at "android-project/assets/53_extensions_and_changing_orientation/portrait.png" when building.

3)  Copy demo source to android-project/jni/53_extensions_and_changing_orientation.cpp. Open the game make file at android-project/jni/src/Android.mk. Change local source files to include the new demo source file:
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \ 53_extensions_and_changing_orientation.cpp
Run ndk-build. You'll get the error:
jni/src/53_extensions_and_changing_orientation.cpp:6:23: fatal error: SDL_image.h: No such file or directory #include <SDL_image.h> ^ compilation terminated. make.exe: *** [obj/local/armeabi/objs/main/53_extensions_and_changing_orientation.o] Error 1
This error means that our game source file can't find SDL_image.h which makes sense since we haven't set it up yet.

4)  SDL_image is just another shared object library that needs to be built along SDL2 and our C++ application. So that means we need to create a folder for it at android-project/jni/src/SDL2_image. After you create the folder the extension library, extract the SDL_image source. Inside of the SDL2_image-2.0.0 directory there should be some *.h header files, *.c source files, and an Android.mk folder. Copy all the header/source files along with the Android.mk file and paste them in android-project/jni/src/SDL2_image.

With SDL_image in the jni directory along with SDL2 and our C++ source, run ndk-build. You'll get the following error:
jni/SDL2_image/IMG_jpg.c:34:21: fatal error: jpeglib.h: No such file or directory #include <jpeglib.h> ^ compilation terminated. make.exe: *** [obj/local/armeabi/objs/SDL2_image/IMG_jpg.o] Error 1
5)  The error you just got is the NDK complaining it can't find the jpeglib library. See, SDL_image has libraries it needs to run and they need to be compiled as a shared object with SDL2, SDL_image, and our C++ source. Go back into the SDL_image directory we extracted and there should be a folder called external. Inside of there there should be a bunch a folders containing header, source, and Android makefiles for the external libraries used by SDL_image. Copy all the folders located at SDL2_image-2.0.0/external and paste them to android-project/jni so they can be compiled along SDL2, SDL2_image, and our source.

SDL_image will probably have (version numbers may be different and new libraries may be added/removed):
  • zlib-1.2.8
  • jpeg-9
  • libpng-1.6.2
  • libwebp-0.3.0
  • tiff-4.0.3
SDL_mixer will have:
  • libmikmod-patches
  • libmodplug-0.8.8.4
  • libogg-1.3.1
  • libvorbis-1.3.3
  • libvorbisidec-1.2.1
  • smpeg2-2.0.0
  • flac-1.2.1
  • libmikmod-3.1.12
SDL_ttf will have:
  • freetype-2.4.12
If you run ndk-build you'll get the same error as before because we still need to set up SDL_image to find the header files for the external libraries. Open android-project/jni/SDL2_image/Android.mk and find the section that sets the external library paths.

 

# Enable this if you want to support loading JPEG images # The library path should be a relative path to this directory. SUPPORT_JPG := true JPG_LIBRARY_PATH := external/jpeg-9 # Enable this if you want to support loading PNG images # The library path should be a relative path to this directory. SUPPORT_PNG := true PNG_LIBRARY_PATH := external/libpng-1.6.2 # Enable this if you want to support loading WebP images # The library path should be a relative path to this directory. SUPPORT_WEBP := true WEBP_LIBRARY_PATH := external/libwebp-0.3.0

 

Change the paths to point to their folders in the android-project/jni/ directory.

 

# Enable this if you want to support loading JPEG images # The library path should be a relative path to this directory. SUPPORT_JPG := true JPG_LIBRARY_PATH := ../jpeg-9 # Enable this if you want to support loading PNG images # The library path should be a relative path to this directory. SUPPORT_PNG := true PNG_LIBRARY_PATH := ../libpng-1.6.2 # Enable this if you want to support loading WebP images # The library path should be a relative path to this directory. SUPPORT_WEBP := true WEBP_LIBRARY_PATH := ../libwebp-0.3.0

 

Run ndk-build. SDL_image should build but we should get an old error:
jni/src/53_extensions_and_changing_orientation.cpp:6:23: fatal error: SDL_image.h: No such file or directory #include <SDL_image.h> ^ compilation terminated. make.exe: *** [obj/local/armeabi/objs/main/53_extensions_and_changing_orientation.o] Error 1
6)  We managed to get the extension library to build but our application need to be able find the headers for SDL_image. Open android-project/jni/src/Android.mk and add in the path to the includes so our game source can find the includes for SDL_image:
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include $(LOCAL_PATH)/../SDL2_image
Run ndk-build and you'll get a new new error:
[armeabi] SharedLibrary : libmain.so jni/src/53_extensions_and_changing_orientation.cpp:120: error: undefined reference to 'IMG_Load' jni/src/53_extensions_and_changing_orientation.cpp:432: error: undefined reference to 'IMG_Init' jni/src/53_extensions_and_changing_orientation.cpp:477: error: undefined reference to 'IMG_Quit' collect2.exe: error: ld returned 1 exit status make.exe: *** [obj/local/armeabi/libmain.so] Error 1
7)  That last error was a linker error. While SDL_image managed to compile and our game managed to compile, we didn't tell the NDK to link our game against SDL_image. In android-project/jni/src/Android.mk, add SDL2_image to the shared libraries:
LOCAL_SHARED_LIBRARIES := SDL2 SDL2_image
Run ndk-build. Hopefully you should get the application to build, but you may get an error.

8)  You may have gotten the following error when trying to build the x86 version:
jni/SDL2_image/../jpeg-9/jidctfst.S:17:34: fatal error: machine/cpu-features.h: No such file or directory #include <machine/cpu-features.h> ^ compilation terminated. make.exe: *** [obj/local/x86/objs/SDL2_image/__/jpeg-9/jidctfst.o] Error 1
It's a compatibility issue with x86 Android. Fortunately, most Android devices are ARM based which means we can ignore x86. Open android-project/jni/Application.mk and change:
APP_ABI := armeabi armeabi-v7a x86
to
APP_ABI := armeabi armeabi-v7a
Run ndk-build and the NDK should build our native binary.

9)  Next we need to get our Java activity load the SDL extension library. Open android-project/src/org/libsdl/app/SDLActivity.java and look for this section:

 

// Load the .so static { System.loadLibrary("SDL2"); //System.loadLibrary("SDL2_image"); //System.loadLibrary("SDL2_mixer"); //System.loadLibrary("SDL2_net"); //System.loadLibrary("SDL2_ttf"); System.loadLibrary("main"); }

 

Uncomment the libraries you'll be using so they'll be loaded.

10)  One last thing we need to do to support orientation changes. To support orientation change with SDL2 on Android we need to enable screen size change too. Open android-project/AndroidManifest.xml and change:
android:configChanges="keyboardHidden|orientation"
to
android:configChanges="keyboardHidden|orientation|screenSize"
11)  Run ant debug install. The application should run and rotate. Now that the application built, it's time to go over the source code.

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值