65.9K
CodeProject 正在变化。 阅读更多。
Home

在Intel®架构上使用Linderdaum引擎开发C++ Android NDK游戏

2015 年 7 月 6 日

CPOL

3分钟阅读

viewsIcon

11071

Linderdaum 引擎是一个开源的、纯面向对象的 3D 游戏引擎,使用 C++ 编写,适用于 Microsoft Windows、Google Android 和 BlackBerry OS 10。

英特尔® 开发者专区提供用于跨平台应用开发的工具和操作指南信息、平台和技术信息、代码示例和同行专家技术,以帮助开发者创新并取得成功。加入我们的 Android物联网英特尔® 实感™ 技术Windows 社区,下载工具、访问开发套件、与志同道合的开发者分享想法,并参与黑客马拉松、竞赛、巡展和本地活动。

Linderdaum* 引擎是一个开源的、纯面向对象的 3D 游戏引擎,使用 C++ 编写,适用于 Microsoft Windows*、Google Android* 和 BlackBerry OS 10。 它被设计为用于开发交互式 3D 应用程序的集成解决方案,适用于游戏、工业和科学可视化。

设置构建过程

要使用 Linderdaum 引擎,您首先需要安装

  • Linderdaum Engine SDK 0.6.08
  • OpenAL
  • Python* 3 (需要 3 或更高版本)

此外,您需要安装以下软件才能在 Android 上构建项目

您需要使用 Android 2.2 或更高版本的设备进行测试。 为了使 Android NDK 正常工作,安装 Linderdaum SDK 的文件夹名称不能有空格。 Linderdaum SDK 拥有以编译形式构建库的所有权限(进入 Libs.Win32/Libs.Win64BuildAndroid/jni ),因此您只需更新元信息和构建文件。 首先运行 SDK 存储库中的二进制文件 rebuildLSD.py,然后运行 makeconfig.py。 现在您已安装所有内容,您可以开始编写多平台应用程序了。

HelloAndroid 应用程序

为了不使引擎部件过载并详细说明多平台方面,我们将使用 Apps / Test_Android / Src / 文件中的最小 3D 应用程序,如下所示

Test_Android.cpp:
#include "Linderdaum.h"
sEnvironment* Env = NULL;
LMatrix4              Projection;
clVirtualTrackball    Trackball;
clGameCamera*         Camera = NULL;
clScene*              Scene  = NULL;
void DrawOverlay(LEvent Event, const LEventArgs& Args)
{
    // refresh camera
    LMatrix4 Trans( Trackball.GetRotationMatrix() * Camera->GetCamera().GetModelViewMatrix() );
    Scene->SetCameraTransform( Trans );
    Scene->SetCameraProjection( Projection );

    // draw the scene
    Scene->SetUseOffscreenBuffer( false, false );
    Scene->RenderForward();

    // update the virtual trackball
    bool MousePressedL = Env->Console->IsKeyPressed( LK_LBUTTON );
    Env->Viewport->UpdateTrackball( &Trackball, 10.0f, MousePressedL );
}

void Update( LEvent Event, const LEventArgs& Args )
{
    // in Args.FFloatArg хранится DeltaTime in milliseconds
}

APPLICATION_ENTRY_POINT
{
    LString CommandLine;
    EXTRACT_COMMAND_LINE(CommandLine);
    Env = new sEnvironment();

    // CommonMedia used only on the PC, on Android, it is cleverly packaged in resources
    Env->DeployDefaultEnvironment( CommandLine, "..\..\CommonMedia" );
    Env->FileSystem->Mount("GameData");
    Projection = Math::Perspective( 45.0f, Env->Viewport->GetAspectRatio(), 0.4f, 2000.0f );

    // create a camera and assign it to events
    Camera = Env->Linker->Instantiate( "clGameCamera" );
    Camera->GetCamera().SetPosition( LVector3(0,-10,10) );
    CONNECTOBJ( L_EVENT_TIMER, &clGameCamera::Event_TIMER, Camera );
    Env->Connect( L_EVENT_DRAWOVERLAY, Utils::Bind( &DrawOverlay ) );
    Env->Connect( L_EVENT_TIMER,       Utils::Bind( &Update      ) );

    // create a scene
    Scene = Env->Linker->Instantiate("clScene");

    // and add it to the mesh
    int ID = Scene->AddGeom( Env->Resources->CreateIcosahedron( 3.0f, LVector3(0) ) );

    // set up the material
    clMaterial* Mtl = Env->Resources->CreateMaterial();
    Mtl->SetPropertyValue( "DiffuseColor", "1.0 0.0 0.0 0" );
    Mtl->SetPropertyValue( "CastShadow",   "false" );
    Scene->SetMtl( ID, Mtl );

    // set the position
    Scene->SetLocalTransform( ID, LMatrix4::GetTranslateMatrix( LVector3(  0.0f, 0.0f, 0.0f ) ) );
    Env->RunApplication( DEFAULT_CONSOLE_AUTOEXEC );
    APPLICATION_EXIT_POINT( Env );
}

APPLICATION_SHUTDOWN
{
}

此代码在屏幕中心显示一个红色二十面体,可以使用鼠标旋转。 我们还希望在不更改 Android 设备或 Windows PC 上的任何内容的情况下构建该应用程序。

为 Android 构建引擎

为 Android 构建应用程序很容易,但您必须设置多个工具

  • JAVA_HOME 环境变量必须指向包含 JDK 的文件夹(在 Ubuntu JDK 上,该文件夹通常是 /usr/lib/kvm/default-java
  • NDK_ROOT 环境变量必须指向包含 Android NDK 的文件夹
  • 文件 Apps / Test_Android / local.properties 需要指定参数 'sdk.dir =' 指向包含 Android SDK 的文件夹

我们需要为 Android 构建一个引擎(SDK 中包含并且已经在进行中是 libLinderdaumEngineCore.a,因此如果您想自己深入研究引擎,则只需要手动重建)。 运行 Cygwin Bash Shell,转到该目录,然后执行 BuildAndroid ndk-build -j4。 结果,您应该看到 libLinderdaumEngineCore.a 略大于 100MB。

为 Android 构建项目

构建 Android 项目需要更改 Apps/Test_Android 中的几个文件。 以下是您应该做的更改

ant.properties
android.library.reference.1=..\\\\..\\\\BuildAndroid

project.properties
target=android-21

local.properties
sdk.dir=<path to Android SDK, for example C:\\android-sdk>

res\values\strings.xml
<?xml version="1.0" encoding="utf-8"?>
  <resources>
      <string name="app_name">Test_Android</string>
  </resources>

jni\Application.mk
APP_OPTIM := release
APP_PLATFORM := android-21
APP_STL := gnustl_static
APP_CPPFLAGS += -frtti 
APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -DANDROID
APP_ABI := x86

要使用 Android SDK 中的工具,您需要一个清单 AndroidManifest.xml。 例如

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.linderdaum.engine.myfirstandroidapp"
      android:versionCode="1"
      android:versionName="0.6.00"
      android:installLocation="auto">
    <!--require Android 2.1 and higher-->
    <uses-sdk android:minSdkVersion="7" />
    <uses-sdk android:targetSdkVersion="9" />
    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true" />
    <uses-feature android:glEsVersion="0x00020000"/>
    <uses-feature android:name="android.hardware.telephony" android:required="false" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application android:label="@string/app_name"
                 android:installLocation="preferExternal"
                 android:debuggable="false">
        <activity android:name="com.linderdaum.engine.LinderdaumEngineActivity"
                  android:launchMode="singleTask"
                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                  android:screenOrientation="landscape"
                  android:configChanges="orientation|keyboardHidden"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

在不同的应用程序中,您可能需要更改 package=«com.linderdaum.engine.myfirstandroidapp»。 转到 Apps\Test_Android 并在 Cygwin 上运行 ndk-build。 您应该看到以下内容

$ ndk-build
Compile++ thumb  : LinderdaumEngine <= LAndroid.cpp
Compile thumb  : LinderdaumEngine <= LJNI.c
Compile++ thumb  : LinderdaumEngine <= Test_Android.cpp
Prebuilt       : libLinderdaumEngineCore.a <= jni/../../../BuildAndroid/obj/local/x86/
Prebuilt       : libFreeImage.a <= jni/../../../BuildAndroid/jni/x86/
Prebuilt       : libFreeType.a <= jni/../../../BuildAndroid/jni/x86/
Prebuilt       : libVorbis.a <= jni/../../../BuildAndroid/jni/x86/
Prebuilt       : libOGG.a <= jni/../../../BuildAndroid/jni/x86/
Prebuilt       : libOpenAL.a <= jni/../../../BuildAndroid/jni/x86/
Prebuilt       : libModPlug.a <= jni/../../../BuildAndroid/jni/x86/
Prebuilt       : libstdc++.a <= /sources/cxx-stl/gnu-libstdc++/libs/x86/
SharedLibrary  : libLinderdaumEngine.so
Install        : libLinderdaumEngine.so => libs/armeabi-v7a/libLinderdaumEngine.so

这是此项目的 C++ 部分的结尾。 您仍然需要添加 Java 并在 .apk 中构建一个分发包。 为此,您需要创建一个 build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Asteroids" default="help">
    <import file="../../BuildAndroid/CommonMedia.xml"/>
    <property file="local.properties" />
    <property file="ant.properties" />
    <loadproperties srcFile="project.properties" />
    <fail
            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
            unless="sdk.dir"
    />
    <target name="copy-game-data">
    <!-- Game data -->
        <delete dir="assets"/>
        <copy todir="assets/Data">
            <fileset dir="GameData">
            </fileset>
        </copy>
        <copy todir="assets/Data">
            <fileset dir="Data">
            </fileset>
        </copy>
    </target>
    <import file="${sdk.dir}/tools/ant/build.xml" />
</project>

运行 ant copy-common-media debug 并准备好在 Apps\Test_Android\bin\Test_Android-debug.apk 中进行分发。 可以使用以下命令将 apk 文件安装在设备上

adb install Test_Android-debug.apk

运行该应用程序。 您应该看到以下内容

相关文章与资源

关于作者

Vitaliy Kalinin 在英特尔公司的软件与服务部门工作。 他是俄罗斯下诺夫哥罗德州立大学 Lobachevsky 的博士生。 他拥有经济学和数学学士学位以及应用经济学和信息学硕士学位。 他的主要兴趣是移动技术和游戏开发。

© . All rights reserved.