- Android NDK Game Development Cookbook
- Sergey Kosarevsky Viktor Latypov
- 320字
- 2021-07-21 18:21:23
Unifying the cross-platform code
Right now, we have two different versions of a simple program (Win_Min2
and App3
). Let us see how to unify the common parts of the code.
Getting ready
In Android, the application initialization phase is different, and since we use a mixed Java plus C++ approach, the entry points will be different. In C++, we are tied to, int main()
or DWORD WinMain()
functions; whereas in Android it is up to us to choose which JNI function we may call from our Java starter code. Event handling and rendering the initialization code are also quite different, too. To do so, we mark sections of the code with pre-processor definitions and put the different OS code into different files—Wrappers_Android.h
and Wrappers_Windows.h
.
How to do it...
We use the standard macros to detect the OS for which the program is being compiled: Windows-targeted compilers provide the _WIN32
symbol definition, and the __linux__
macro is defined on any Linux-based OS, including Android. However, the __linux__
defination is not enough, since some of the APIs are missing in Android. The macro ANDROID
is a non-standard macro and we pass the -DANDROID
switch to our compiler to identify the Android target in our C++ code. To make this for every source file, we modify the CFLAGS
variable in the Android.mk
file.
Finally, when we write the low-level code, the detection looks like the following code:
#if defined(_WIN32) // windows-specific code #elif defined(ANDROID) // android-specific code #endif
For example, to make an entry point look the same for both the Android and Windows versions, we write the following code:
#if defined(_WIN32) # define APP_ENTRY_POINT() int main() #elif defined(ANDROID) # define APP_ENTRY_POINT() int App_Init() #endif
Later we will replace the int main()
definition with the APP_ENTRY_POINT()
macro.
There's more...
To detect more operating systems, compilers, and CPU architectures, it is useful to check out a list of predefined macros at http://predef.sourceforge.net.
- 觸摸屏實用技術與工程應用
- Linux KVM虛擬化架構實戰指南
- 計算機組裝與系統配置
- 計算機應用與維護基礎教程
- 嵌入式系統設計教程
- 電腦維護365問
- 筆記本電腦維修不是事兒(第2版)
- 微軟互聯網信息服務(IIS)最佳實踐 (微軟技術開發者叢書)
- 基于Apache Kylin構建大數據分析平臺
- 計算機組裝維修與外設配置(高等職業院校教改示范教材·計算機系列)
- 微型計算機系統原理及應用:國產龍芯處理器的軟件和硬件集成(基礎篇)
- Mastering Machine Learning on AWS
- USB應用分析精粹:從設備硬件、固件到主機端程序設計
- The Deep Learning with PyTorch Workshop
- 多媒體應用技術(第2版)