デスクトップとandroid両用でOpenGL ESなコードが書けるlibgdxの使い方

http://code.google.com/p/libgdx/

のHelloWorldプロジェクトをmaven管理にする手順。

Windows7上のcygwinからmavenを使う環境cygwin上でmavenを使う - 三次元日誌

インストール

libgdxのインストール。
http://code.google.com/p/libgdx/downloads/listから
libgdx-0.9.2.zip
を入手した。

libgdxはmavenリポジトリが見当たらないので、アーカイブを解凍してローカルリポジトリに登録する。

$ cd libgdx-0.9.2
# jarのインストール
$ for f in *.jar;do mvn install:install-file -Dfile=$f -DgroupId=com.badlogic.gdx -DartifactId=${f%%.jar} -Dversion=0.9.2 -Dpack
aging=jar -DgeneratePom=true -DcreateChecksum=true; done
# androidのndkインストール
$ cd armeabi
$ for f in *.so;do mvn install:install-file -Dfile=$f -DgroupId=com.badlogic.gdx -DartifactId=${f%%.so} -Dversion=0.9.2 -Dpackaging=so -DgeneratePom=true -DcreateChecksum=true; done

maven2以前にinstall:installだったものが、maven3でinstall:install-fileになったぽい。注意。

プロジェクト作成

3つのサブモジュール(本体、デスクトップ用、アンドロイド用)を持つ構成にする。

parent
  + common
  + desktop
  + android

とりあえず

  • groupId:jp.ousttrue.gdxsample
  • artifactId:gdxxample
  • version:1.0-SNAPSHOT

でいく。

$ mkdir gdxsample
$ cd gdxsample

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>jp.ousttrue.gdxsample</groupId>
  <artifactId>gdxsample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <gdx.version>0.9.2</gdx.version>
  </properties>
</project>

gdxのhelloworldをゲット

以降はgdx-helloworldのソースをmavenプロジェクトで動くように配置する手順
になる。
http://code.google.com/p/libgdx/downloads/list
から
gdx-helloworld-0.9.2.zip
をゲット。
${HELLOWORLD}に解凍したとする。

commonサブモジュール

サブモジュール作成

$ mvn archetype:create -DgroupId=jp.ousttrue.gdxsample -DartifactId=common

commonディレクトリにサブモジュール生成されてpom.xml

  <modules>
    <module>common</module>
  </modules>

が追記される。

$ rm common/src/main/java/jp/ousttrue/gdxsample/App.java
$ rm common/src/test/java/jp/ousttrue/gdxsample/AppTest.java
$ cp ${HELLOWORLD}/gdx-helloworld/src/com/badlogic/gdx/helloworld/HelloWorld.java common/src/main/java/jp/ousttrue/gdxsample/

HelloWorld.javaのpackageを調整

package jp.ousttrue.gdxsample;

common/pom.xmlにgdxに依存ライブラリを記述する

    <dependency>
      <groupId>com.badlogic.gdx</groupId>
      <artifactId>gdx</artifactId>
      <version>${gdx.version}</version>
      <scope>compile</scope>
    </dependency>

ビルド確認

$ mvn install

desktopサブモジュール

サブモジュール作成

$ mvn archetype:create -DgroupId=jp.ousttrue.gdxsample -DartifactId=desktop
$ rm desktop/src/main/java/jp/ousttrue/gdxsample/App.java
$ rm desktop/src/test/java/jp/ousttrue/gdxsample/AppTest.java
$ cp ${HELLOWORLD}/gdx-helloworld/src/com/badlogic/gdx/helloworld/HelloWorldDesktop.java.java common/src/main/java/jp/ousttrue/gdxsample/
$ mkdir data
$ cp ${HELLOWORLD}/gdx-helloworld/data/badlogic.jpg data/

HelloWorld.javaのpackageを調整

package jp.ousttrue.gdxsample;
import jp.ousttrue.gdxsample.HelloWorld;

desktop/pom.xmlにgdxに依存ライブラリを記述する

    <!-- commonの参照 -->
    <dependency>
      <groupId>jp.ousttrue.gdxsample</groupId>
      <artifactId>common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <!-- gdxのdesktop用ライブラリ -->
    <dependency>
      <groupId>com.badlogic.gdx</groupId>
      <artifactId>gdx-backend-jogl</artifactId>
      <version>${gdx.version}</version>
      <scope>compile</scope>
    </dependency>

ビルド確認

$ mvn install

desktop実行

desktop/pom.xmlに追記

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <configuration>
          <mainClass>jp.ousttrue.gdxsample.HelloWorldDesktop</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>

desktop/pom.xmlにgdxに依存ライブラリを記述する

    <!-- gdxのdesktopランタイム -->
    <dependency>
      <groupId>com.badlogic.gdx</groupId>
      <artifactId>gdx-natives</artifactId>
      <version>${gdx.version}</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>com.badlogic.gdx</groupId>
      <artifactId>gdx-backend-jogl-natives</artifactId>
      <version>${gdx.version}</version>
      <scope>runtime</scope>
    </dependency>

実行

$ mvn -f desktop/pom.xml exec:java


"*-nativesの-*.jar"の中にはdllが入っている。mvn exec:java時に/tmpに一時ディレクトリが作られて中にdllを展開してそこにpathを通しているらしい。

androidに続く・・・