Android native C Development 4: SDL porting notes

Source: Internet
Author: User

Original article: http://blog.sina.com.cn/s/blog_4a0a39c30100b1n1.html

 

SDL (Simple DirectMedia
Layer) is an open source multi-platform multimedia development library written in C language. SDL provides a variety of image, sound, keyboard, and Other implementations, with high configuration and portability. developers can develop cross-platform (Linux, windows, Mac)
OS X, Symbian, and Widnows
Embedded systems such as mobiel, of course, include the platform to be transplanted today: Android) applications. Currently, SDL is mostly used to develop multimedia applications such as games, simulators, and media players.

 

Currently, the stable SDL version is 1.2.13 and 1.3 are still under development. You can use SVN to obtain the latest code. This port is subject to 1.2.13 and has not been tested.
Source code of version 1.3. Please download the source code for 1.2.13 from the SDL official website, file name: SDL-1.2.13.zip, and unzip, will get
A SDL-1.2.13 directory.

 

Install the code before native compiles SDL.
Sourcery's arm cross compiler must be installed in windows.
Cygwin (a software that simulates Linux on Windows), because some Linux commands are required during compilation. For detailed steps, see port SDL/tinysdgl to Android with native C, or you can search for some information on the Internet.

 

Because SDL is a set of class libraries written in pure C, the portability is very good. Officially Supported systems include Linux, windows, Windows CE,
BEOs, MacOS, Mac OS x, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris,
IRIX, and QNX, which are not officially supported: amigaos, Dreamcast, Atari, Aix, OSF/Tru64,
Some people also transplanted SDL to many other embedded systems on the Internet, or even some people transplanted SDL
Moto a1200 is so powerful and portable that its architecture is worth learning.

 

Now let's get started with the question: how to customize
SDL, the following is an analysis of video, audio, input events, timers (video, audio, events [key, Mouse], timer), and multithreading:

1. In terms of video, Android is a customized Linux operating system.
X11, either using framebuffer technology, apparently Android is not integrated
X11 (maybe the Android software interface is based on X11 ?!), There is only one choice: framebuffer!

Open the $ SDL/src/video directory and you will find that SDL supports more than 30 video display technologies, including the fbcon and directfb we want. I didn't perform a test on directfb, maybe the display effect will be better than the built-in fbcon in Linux. If you are interested, you can try it. Don't forget to tell me if it succeeds;

2. Let's talk about audio again. Remember an advertisement word: no sound, no good drama! The importance of audio to multimedia applications.

This time, the OSS driver is used, but the DSP and DMA implementation are used. However, when opening the audio file/dev/EAC specified by Android
Audio can only be compiled and cannot run normally. ALSA (Advanced
Linux sound architecture;

For details about OSS, refer to IBM's article: OSS-cross-platform audio interface introduction.

3. the keyboard event in the input event (keyboard, mouse) can be processed without any changes. The device file used is/dev/tty0,

However, the mouse event cannot be handled normally. In addition, debug_mouse found that the PS2 mouse was used. However, Android did not use the PS2 mouse, but the touchscreen mouse was used, the device file is
/Dev/input/event0. For details, see my blog: Android native C Development 3: mouse event (Mouse capture). After modification, the mouse can be processed;

4. The timer is implemented in UNIX;

5. multithreading uses the implementation of pthread, while UNIX systems use pthread to implement multithreading. Do not forget to add it during ln demo.
-Lpthread;

6. the Unix DL library is used to load the dynamic library. Similarly, do not forget to add-LDL during ln demo.

 

SDL provides a minimal makefile: makefile. Minimal. All implementations are
Dummy is an empty implementation, which can be compiled but cannot be done at runtime. According to the above analysis
The contents of makefile. Minimal are changed to the following:

 

# 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.
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)

 

Finally, change $ SDL/include/sdl_config_minimal.h to the following:

# Ifndef _ sdl_config_minimal_h
# DEFINE _ sdl_config_minimal_h

# Include "sdl_platform.h"

 

# Include <stdarg. h>

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
Have1_strrev 1
# Define
Have1_strupr 1
# Define
Have1_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
Have1_ltoa 1
# Define
Have__uitoa 1
# Define
Have__ultoa 1
# Define
Have_strtol 1
# Define
Have_strtoul 1
# Define
Havepolici64toa 1
# Define
Have1_ui64toa 1
# Define
Have_strtoll 1
# Define
Have_strtoull 1
# Define
Have_strtodd 1
# Define
Have_atoi 1
# Define
Have_atof 1
# Define
Have_strcmp 1
# Define
Have_strncmp 1
# Define
Have1_stricmp 1
# Define
Have_strcasecmp 1
# Define
Have1_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
<Stdarg. h>
# Endif

// # Define have_stdio_h 1
// # Define have_stdint_h 1

// # Define sdl_input_tslib 1 // Touch Screen
Input

# DefineSdl_audio_driver_oss1 //
Sdl_audio_driver_dummy

# Define sdl_cdrom_disabled 1

# Define sdl_joystick_disabled 1

# Define
Sdl_loadso_dlopen1
// Sdl_loadso_disabled 1 // # UNDEF

# Define
Sdl_thread_pthread1
// Sdl_threads_disabled

// Sdl_timers_disabled
# Define
Sdl_timer_unix1

// Sdl_video_driver_dummy
# Define
Sdl_video_driver_fbcon1

# Endif

 

Note that some macro definitions are opened to enable the SDL implementation one by one.

After these changes, some code needs to be modified, mainly for video, because android
The framebuffer device file in Linux is different from that in standard Linux. the FB device file in Linux is generally
/Dev/fb0, but the Android device file is/dev/graphics/fb0. Open
$ SDL/src/Video/fbcon/sdl_fbvideo.c.
"/Dev/fb0"Replace
"/Dev/graphics/fb0", Save it.

Modify $ SDL/src/thread/pthread/sdl_sysmutex.c and change row 30th to: # define
Fake_recursive_mutex
1 is to add a "1" child to the end, which may be a bug in the compiler. The default define should be "1.

Now you can compile libsdl. A. In cygwin or Linux, enter the SDL directory and enter:

Make-F makefile. Minimal

Depending on the situation, the android SDL can be compiled successfully in a few minutes. After the compilation is successful, you need to compile Several SDL test demos to test it.
SDL performance.

Go to the test directory, copy the makefile. In file, and change it to makefile. Change the previous content:

 

# 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

 

Replace all @ mathlib @ with $ (mathlib). After saving, compile a testsprite
Demo, enter the following in the command line:

Make testsprite

After the compilation is successful, start the androidsimulator and upload the compiled testspriteand icon.bmp to a directory, such
/Dev/sample. The command is as follows:

 

ADB push testspirte/dev/sample/testsprite

ADB push icon.bmp/dev/sample/icon.bmp

 

Finally, go to the shell: ADB shell of Android, enter the/dev/sample directory, and run testsprite.
Demo:

 

# Cd/dev/sample

# Chmod 755 testsprite

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

 

You can adjust the width and height according to the simulator settings, because the default program is
640xx, initialization will fail. If everything is normal, the simulator will have a lot of yellow smiling faces! Press any key to exit. the number on your machine is 60.
FPS, as follows:

If you are interested in other demos, You can compile and test them by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.