<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="
false"
android:theme="@style/Theme.Test"
android:hasCode="false"
tools:targetApi="31">
<activity
android:name="
android.app.NativeActivity"
android:exported="true">
<meta-data android:name="android.app.lib_name"
android:value="native-lib" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />
</application>
</manifest>
2. 次に画面左側のリストから『app』→『cpp』の中にある『CMakeLists.txt』を
ダブルクリックして内容を表示します。
その内容は以下のようになっていると思いますが、
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.
# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)
# Declares the project name. The project name can be accessed via ${PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("test")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
add_library(${CMAKE_PROJECT_NAME} SHARED
# List C/C++ source files with relative paths to this CMakeLists.txt.
native-lib.cpp)
# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME}
# List libraries link to the target library
android
log)
これを以下のように変更します。( 色が緑や赤の部分が変更箇所や追加箇所です )
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.
# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)
include_directories( DXライブラリAndroid版パッケージ内の『Lib_AndroidStudio』のフルパス/${ANDROID_ABI} )
link_directories( DXライブラリAndroid版パッケージ内の『Lib_AndroidStudio』のフルパス/${ANDROID_ABI} )
# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("test")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
add_library(
native-lib
SHARED
# List C/C++ source files with relative paths to this CMakeLists.txt.
native-lib.cpp)
# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# List libraries link to the target library
android
log
GLESv1_CM
EGL
GLESv2
OpenSLES
m
DxLib
DxUseCLib
jpeg
png
zlib
tiff
theora_static
vorbis_static
vorbisfile_static
ogg_static
bullet
opus )
『DXライブラリAndroid版パッケージ内の『Lib_AndroidStudio』のフルパス』と
書かれている部分は、DXライブラリAndroid版パッケージ内のフォルダ『Lib_AndroidStudio』のパスに
置き換えてください。( 更にディレクトリの区切りは \ ではなく / にする必要があります )
例えばダウンロードした『DXライブラリAndroid版』のパッケージを Cドライブの直下( C:\ )に展開した場合は
C:\DxLib_Android\Lib_AndroidStudio
がフルパスとなりますので、その場合は include_directories と link_directories の2行の記述は
include_directories( C:/DxLib_Android/Lib_AndroidStudio/${ANDROID_ABI} )
link_directories( C:/DxLib_Android/Lib_AndroidStudio/${ANDROID_ABI} )
となります。( \ を / に置き換えるのを忘れないようにしてください )
尚、DXライブラリをダウンロードフォルダやドキュメントフォルダに入れるとこれらのパス指定が面倒になりますので
DXライブラリはなるべく浅いフォルダに入れておくことをお勧めします。
プロジェクトの設定は以上です。
3.プログラムを組む
Android Studio の画面左側のリストの『app』→『cpp』の中にある『native-lib.cpp』をダブルクリックして内容を表示すると
最初から約10行のプログラムが書かれていますので、まずこれをすべて削除します。
そして何もプログラムに書かれていない状態になった native-lib.cpp に、以下のプログラムを入力します。
#include "DxLib.h"
// プログラムは android_main から始まります
int android_main( void )
{
if( DxLib_Init() == -1 ) // DXライブラリ初期化処理
{
return -1 ; // エラーが起きたら直ちに終了
}
DrawBox( 220, 140, 420, 340, GetColor( 255,255,255 ), TRUE ) ; // 四角形を描画する
WaitKey() ; // キー入力待ち
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
プログラムはこれだけです、どの部分が何をしているのか簡単に説明します。
まず最初の1行はDXライブラリを使用するために必要なファイル( DxLib.h )をインクルードしています。
次の『int android_main( void )はDXライブラリを使用した Androidアプリのプログラムのスタート地点となる関数の宣言です。
( 因みにDXライブラリを使用しない Androidアプリのスタート地点も android_main ですが、戻り値や引数が異なります… )
中括弧内の最初の文『if( DxLib_Init() == -1 ){ return -1 ; }』はDXライブラリを初期化して使える状態にするために必要な関数『DxLib_Init』を呼んでいます。
この関数はDXライブラリを使うプログラムを組む際には例外を除いてまず最初に呼び出す必要があります。
因みに『if(...』と書かれているのは初期化が失敗したらその時点でソフトを終了させるという処理を行うための物です。
『DrawBox』は関数名そのまま四角形を描画するための関数です。
その次の『WaitKey』はキーボードのキーが押されるまで処理を止める関数です。( Androidアプリでもキーボードを接続すればキーボードを使うことができます )
最後の『DxLib_End() ;』は注釈にも書いてある通りDXライブラリの使用を終了する処理を行う関数を呼んでいる文です。
DXライブラリを使用しているプログラムは最後に必ずこの関数を呼ばなくてはなりません。
4.Androidエミュレーターの準備
プログラムの入力も終わったので、早速実行!
と、行きたいところですが、その前に『Windows上で動作する Android端末のエミュレーター』を用意する必要があります。
Android のスマートフォンやタブレットを所持している場合は、その端末上で実行することもできるのですが
開発中は主に Androidエミュレーターでプログラムを実行することになるので、下記の手順を踏んでエミュレーターの準備を行います。
① Android Studio のメニューの『Tools』→『Device Manager』を選択して
『Device Manager』を表示します。
② 最初は何もエミュレーターが作成されていない状態ですので、表示内の『+』→『Create Virtual Device』をクリックします。
③ 次に『Add Device』の画面が表示されます、左の選択項目では『Phone』を選択して、右の選択項目では『Pixel 10』を選択してから
『Next』をクリックします。
④ 次の画面では使用する『API』や『System Image』を選択するのですが、基本的に何も変更せずに『Finish』をクリックして大丈夫です。
⑤ 初回ではシステムイメージが無い状態なので、画面に『Confirm Download』と、システムイメージをダウンロードするかを確認する
ダイアログが表示されますので、『Yes』をクリックしてシステムイメージをダウンロードします。(数GBあるので、少し時間がかかります)
⑥ システムイメージのダウンロードとインストールが完了したらダイアログ右下の『Finish』をクリックしてダイアログを閉じます。
⑦ 『Device Manager』の画面に戻ると、作成したエミュレーターが追加されていますので、エミュレーター名( Pixel 10 )の右側に
表示されている右三角マーク( ▶ )をクリックしてエミュレーターを起動します。
⑧ エミュレーターの起動には時間が掛かりますので暫く待ちます。
これで Androidエミュレーターの準備は完了です。
5.プロジェクトのビルド、実行
エミュレーターの準備が完了したところで早速プログラムを実行してみましょう。
① Android Studio のメニューから『Run』→『Run 'app'』を選択します。
② すると、ビルドが行われ、エラーが無ければプログラムが実行されます。
成功すれば、仮想デバイス( エミュレーター )の画面の中心に四角形が表示されます。
さてできあがった Androidアプリのパッケージファイルですが、それはプロジェクトのフォルダの中の
『test\app\build\intermediates\apk\debug』の中に『app-debug.apk』というファイル名で作成されます。
( プロジェクト名を『test』以外にした場合は、『test』の代わりに付けたプロジェクト名となります )
この apk ファイルを Android端末にインストールすれば Android Studio を介さなくても作成したアプリが動作します。
( Android の Playストアに提出するファイルも、この apk ファイルとなります )
これでDXライブラリを使っての Androidアプリ開発の方法はわかりました。後は好きにプログラムを組んで
ゲームを作るだけです。ですがまだDXライブラリの機能は初期化と終了とドットを描画する関数しかわかっていません。
この他の関数は DXライブラリ関数リファレンスのページ で解説されていますのでそちらを参照して下さい。
あと、Androidアプリの開発に関する基礎的な情報や注意点を Androidアプリ開発の基礎的な情報や注意点など で
解説していますので、こちらも併せてご覧ください。
戻る