---
title: "Android原生(Native)C开发之四：SDL移植笔记"
author: "Ancool"
date: "2011-01-23T20:48:33Z"
tags: []
categories: ["编程语言"]
slug: "android-native-c-sdl"
draft: false
lastmod: "2011-01-23T20:48:33Z"
id: 891981
url: "https://ancool.iteye.com/blog/891981"
---

SDL（Simple DirectMedia Layer）是一套开放源码的跨平台多媒体开发库，使用C语言写成。SDL提供了多种图像、声音、键盘等的实现，可配置性与移植性非常高，开发者可以开发出跨多个平台（Linux、Windows、Mac OS X、Symbian、Widnows Mobiel等嵌入式系统，当然也包括今天要移植的平台：Android）的应用，目前SDL多用于开发游戏、模拟器、媒体播放器等多媒体应用。

目前，SDL的稳定版本是 1.2.13，1.3还在开发中，可以通过SVN得到最新的代码，本次移植以 1.2.13为准，没有测试 1.3版的源码。请从 SDL 的官方网站下载 1.2.13 的源码，文件名为：SDL-1.2.13.zip，并解压，将得到一个 SDL-1.2.13 目录。

在Native编译SDL之前，要先装 Code Sourcery公司的arm交叉编译器，如果是用Windows操作系统，则一定要装 Cygwin（一个在windows上模拟linux的软件），因为在编译时要用到一些 linux命令，具体的步骤请参见：Port SDL/TinySDGL to android with native C，或自已在网上搜一些资料。

因为SDL是用纯C写的一套类库，所以移植性非常好，官方支持的系统有：Linux, Windows, Windows CE, BeOS, MacOS, Mac OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX，非官方支持的有：AmigaOS, Dreamcast, Atari, AIX, OSF/Tru64, RISC OS, SymbianOS, and OS/2，而且网上也有人将SDL移植到很多其他嵌入式系统，甚至有人将SDL移植到 Moto A1200，如此强大的可移植性，其架构真是值得好好学习。

现在切入正题，如何为Android量身定做一个 SDL，下面就从视频，音频，输入事件，定时器（video,audio,events\[key,mouse],timer)，多线程等几个方面来分析：

1.首先讲视频方面，Android是一个定制的Linux操作系统，Linux显示要么用 X11，要么用framebuffer技术，很显然Android并没有集成 X11(也许Android的软件界面是基于X11的？！)，那只有唯一的选择：framebuffer！

打开$SDL/src/video目录，可以发现SDL支持多达30多种的视频显示技术，其中包括我们想要的fbcon及directfb，directfb我没有做测试，也许显示效果会比linux自带的fbcon好，有兴趣的朋友可以试一下，成功了别忘了告诉我；

2.再来谈音频，记得一个广告词：没有声音，再好的戏也出不来！可见音频对多媒体应用的重要性。

这次用的是OSS的driver，但用的是dsp及dma的实现，但在打开Android指定的音频文件 /dev/eac 时有误，所以音频这一块只是能编译通过，不能正常运行，正在考虑用ALSA (Advanced Linux Sound Architecture) 替代；

关于OSS大家可以参看IBM的文章: OSS--跨平台的音频接口简介，写得比较详细。

3.输入事件（键盘，鼠标）中的键盘事件不需要任何更改，就能正常处理，用的设备文件是 /dev/tty0，

但鼠标事件却不能正常处理，加上DEBUG\_MOUSE发现用的是PS2的鼠标，但其实Android用的不是PS2的鼠标，用的应该是触摸屏（TouchScreen）鼠标，设备文件是 /dev/input/event0，详情请参见本人的blog: Android原生(Native)C开发之三：鼠标事件篇(捕鼠记)，经过改动后，基本能实现鼠标的处理；

4.定时器用的是unix的实现；

5.多线程用的是pthread的实现，unix系统都是用pthread来实现多线程的，在 ln demo时别忘了加 -lpthread；

6.加载动态库用的是unix 的 dl库，同样，在ln demo时别忘了加 -ldl。

SDL提供了一个最小化的Makefile：Makefile.minimal，所有的实现都是 dummy，就是一个空的实现，编译能通过，但运行时什么都不能做，根据上面的分析，将 Makefile.minimal 的内容改成如下：

 # Makefile to build the SDL library

INCLUDE = -I./include  
CFLAGS  = -g -s -O2 $(INCLUDE)  
CC  = arm-none-linux-gnueabi-gcc  
AR  = arm-none-linux-gnueabi-ar  
RANLIB = arm-none-linux-gnueabi-ranlib

CONFIG\_H = include/SDL\_config.h  
TARGET  = libSDL.a  
SOURCES = \\  
 src/\*.c \\  
 src/audio/\*.c \\  
 src/cdrom/\*.c \\  
 src/cpuinfo/\*.c \\  
 src/events/\*.c \\  
 src/file/\*.c \\  
 src/joystick/\*.c \\  
 src/stdlib/\*.c \\  
 src/thread/\*.c \\  
 src/timer/\*.c \\  
 src/video/\*.c \\  
 src/audio/dsp/\*.c \\  
 src/audio/dma/\*.c \\  
 src/video/fbcon/\*.c \\  
 src/joystick/dummy/\*.c \\  
 src/cdrom/dummy/\*.c \\  
 src/thread/pthread/\*.c \\  
 src/timer/unix/\*.c \\  
 src/loadso/dlopen/\*.c \\

OBJECTS = $(shell echo $(SOURCES) | sed -e 's,\\.c,\\.o,g')

all: $(TARGET)

$(TARGET): $(CONFIG\_H) $(OBJECTS)  
 $(AR) crv $@ $^  
 $(RANLIB) $@

$(CONFIG\_H):  
 cp $(CONFIG\_H).default $(CONFIG\_H)

clean:  
 rm -f $(TARGET) $(OBJECTS)  
 

最后将$SDL\\include\\SDL\_config\_minimal.h的内容改成如下：

#ifndef \_SDL\_config\_minimal\_h  
#define \_SDL\_config\_minimal\_h

#include "SDL\_platform.h"

#include &lt;stdarg.h&gt;

typedef signed char int8\_t;  
typedef unsigned char uint8\_t;  
typedef signed short int16\_t;  
typedef unsigned short uint16\_t;  
typedef signed int int32\_t;  
typedef unsigned int uint32\_t;  
typedef unsigned int size\_t;  
//typedef unsigned long uintptr\_t;

#define HAVE\_LIBC 1

#ifdef  HAVE\_LIBC  
   
 #define HAVE\_ALLOCA\_H  1  
 #define HAVE\_SYS\_TYPES\_H 1  
 #define HAVE\_STDIO\_H  1  
 #define STDC\_HEADERS  1  
 #define HAVE\_STDLIB\_H  1  
 #define HAVE\_STDARG\_H  1  
 #define HAVE\_MALLOC\_H  1  
 #define HAVE\_MEMORY\_H  1  
 //#define HAVE\_STRING\_H   1  
 //#define HAVE\_STRINGS\_H  1  
 #define HAVE\_INTTYPES\_H  1  
 #define HAVE\_STDINT\_H  1  
 #define HAVE\_CTYPE\_H  1  
 #define HAVE\_MATH\_H   1  
 //#define HAVE\_ICONV\_H   1  
 #define HAVE\_SIGNAL\_H  1  
 #define HAVE\_ALTIVEC\_H  1

   
 #define HAVE\_MALLOC   1  
 #define HAVE\_CALLOC   1  
 #define HAVE\_REALLOC  1  
 #define HAVE\_FREE   1  
 #define HAVE\_ALLOCA   1  
 #define HAVE\_GETENV   1  
 #define HAVE\_PUTENV   1  
 #define HAVE\_UNSETENV  1  
 #define HAVE\_QSORT   1  
 #define HAVE\_ABS   1  
 //#define HAVE\_BCOPY   1  
 //#define HAVE\_MEMSET   1  
 //#define HAVE\_MEMCPY   1  
 //#define HAVE\_MEMMOVE   1  
 //#define HAVE\_MEMCMP   1  
 //#define HAVE\_STRLEN   1  
 //#define HAVE\_STRLCPY   1  
 //#define HAVE\_STRLCAT   1  
 //#define HAVE\_STRDUP   1  
 #define HAVE\_\_STRREV  1  
 #define HAVE\_\_STRUPR  1  
 #define HAVE\_\_STRLWR  1  
 //#define HAVE\_INDEX   1  
 #define HAVE\_RINDEX   1  
 //#define HAVE\_STRCHR   1  
 #define HAVE\_STRRCHR  1  
 #define HAVE\_STRSTR   1  
 #define HAVE\_ITOA   1  
 #define HAVE\_\_LTOA   1  
 #define HAVE\_\_UITOA   1  
 #define HAVE\_\_ULTOA   1  
 #define HAVE\_STRTOL   1  
 #define HAVE\_STRTOUL  1  
 #define HAVE\_\_I64TOA  1  
 #define HAVE\_\_UI64TOA  1  
 #define HAVE\_STRTOLL  1  
 #define HAVE\_STRTOULL  1  
 #define HAVE\_STRTOD   1  
 #define HAVE\_ATOI   1  
 #define HAVE\_ATOF   1  
 #define HAVE\_STRCMP   1  
 #define HAVE\_STRNCMP  1  
 #define HAVE\_\_STRICMP  1  
 #define HAVE\_STRCASECMP  1  
 #define HAVE\_\_STRNICMP  1  
 #define HAVE\_STRNCASECMP 1  
 #define HAVE\_SSCANF   1  
 #define HAVE\_SNPRINTF  1  
 #define HAVE\_VSNPRINTF  1  
 //#define HAVE\_ICONV  
 #define HAVE\_SIGACTION  1  
 #define HAVE\_SETJMP   1  
 #define HAVE\_NANOSLEEP  1  
 //#define HAVE\_CLOCK\_GETTIME 1  
 #define HAVE\_DLVSYM   1  
 #define HAVE\_GETPAGESIZE 1  
 #define HAVE\_MPROTECT  1  
#else  
   
 #include &lt;stdarg.h&gt;  
#endif

//#define HAVE\_STDIO\_H 1  
//#define HAVE\_STDINT\_H 1

//#define SDL\_INPUT\_TSLIB 1 //touch screen input

#define SDL\_AUDIO\_DRIVER\_OSS 1 // SDL\_AUDIO\_DRIVER\_DUMMY

#define SDL\_CDROM\_DISABLED  1

#define SDL\_JOYSTICK\_DISABLED 1

#define SDL\_LOADSO\_DLOPEN  1  
//SDL\_LOADSO\_DISABLED 1 //#undef

#define SDL\_THREAD\_PTHREAD  1 //SDL\_THREADS\_DISABLED

  //SDL\_TIMERS\_DISABLED  
#define SDL\_TIMER\_UNIX   1

// SDL\_VIDEO\_DRIVER\_DUMMY  
#define SDL\_VIDEO\_DRIVER\_FBCON 1

#endif  
 

注意黑体部分，其实就是打开一些宏定义，将SDL的实现一一打开。

改完了这些以后，还需要改一些代码，主要是video方面的，因为Android Linux的framebuffer设备文件与标准Linux不同，Linux的fb设备文件一般是 /dev/fb0，但Android的设备文件是 /dev/graphics/fb0，打开 $SDL/src/video/fbcon/SDL\_fbvideo.c，将191、499行的 "/dev/fb0" 替换成 "/dev/graphics/fb0"，保存即可。

再修改 $SDL/src/thread/pthread/SDL\_sysmutex.c，将第30行改成： #define FAKE\_RECURSIVE\_MUTEX 1，就是在后面加一个“1”子，这可能是编译器的一个bug，define默认应该就是“1”的。

现在可以开始编译libSDL.a了，在cygwin或Linux下，进入SDL目录，输入：

make -f Makefile.minimal

视情况面定，一般几分钟后能顺利编译 Android版的 SDL，编译成功后，就需要编译几个SDL的test demo来测试一下 SDL的性能。

进入 test目录，复制 Makefile.in 文件，并改名为 Makefile，将前面一点内容改为：

 # Makefile to build the SDL tests

srcdir  = .

INCLUDE = -I../include  
CC      = arm-none-linux-gnueabi-gcc  
EXE     =  
CFLAGS  = -g -s -O2 $(INCLUDE) -static  
LIBS    = -L.. -lSDL -lpthread  
MATHLIB = -lm  
 

并将所有的 @MATHLIB@ 替换成 $(MATHLIB)，保存后，先编译一个testsprite demo，在命令行输入：

make testsprite

编译成功后，启动Android模拟器，将编译出来的testsprite及icon.bmp上传至一个目录，如 /dev/sample，命令如下：

adb push testspirte /dev/sample/testsprite

adb push icon.bmp /dev/sample/icon.bmp  
 

最后进入 android 的shell: adb shell，再进入 /dev/sample目录，执行testsprite demo即可：

#cd /dev/sample

#chmod 755 testsprite

#./testsprite -width 320 -height 480 -bpp 32  
 

可以根据模拟器的设置调整 width 及 height，因为程序默认的是 640x480的，初始化时会失败，如果一切正常的话，模拟器就会出现很多黄色的笑脸！按任意键退出，在本人机器上能达到 60 FPS左右，效果图如下：

![](http://dl.iteye.com/upload/attachment/402822/e0aa963a-95d6-3460-91b6-3d51d0fe75b2.jpg)

其他的 demo有兴趣的朋友可以自己编译测试。