[fpc-pascal] Android porting ideas
Michalis Kamburelis
michalis.kambi at gmail.com
Sun Feb 21 19:34:52 CET 2016
2016-02-21 4:36 GMT+01:00 Ryan Joseph <ryan at thealchemistguild.com>:
> I’m going to attempt to port a game to Android but I have some questions about potential problems. Now at least I think building a library and loading using the JNI would be best but using the JVM could be helpful also (the entire project couldn’t be compiled though). Not sure which way is best or if they can be mixed perhaps.
In Castle Game Engine the whole Pascal code is compiled into an .so
library (for Android+Arm, though we could also target Android+x86). It
is then packaged into an apk, with the activity class set to the
"NativeActivity" Java class.
You can communicate with the Java code using JNI. In the base version,
the NativeActivity (part of Android since Android 2.3) already does a
lot for you, and you don't even need to touch the "Java side". For
more complicated stuff, you need to write some Java code, and
communicate with it using JNI. Of course if you use an engine like
Castle Game Engine, you get a lot of help with this:) --- most things
are already implemented for you, and for adding your own Java
extensions we have CastleMessaginig unit and a "component" system for
Android projects,
https://github.com/castle-engine/castle-engine/wiki/Android-Project-Components-Integrated-with-Castle-Game-Engine
.
See http://castle-engine.sourceforge.net/engine.php and download the
engine and try it:) You can use the stable release 5.2.0, or the (soon
to be released as 5.3.0) version from GitHub on
https://github.com/castle-engine/castle-engine .
We have a lot of documentation, some Android/mobile specific stuff:
- http://castle-engine.sourceforge.net/tutorial_mobile.php (tutorial
showing how to write cross-platform game code using CGE --- for
standalone, Android, iOS, web browser plugin...)
- https://github.com/castle-engine/castle-engine/wiki/Android - how to
install Android development environment
- https://github.com/castle-engine/castle-engine/wiki/Android-Project-Components-Integrated-with-Castle-Game-Engine
- extra Android components, e.g. for in-app purchases or analytics.
>
> 1) All the file I/O in the FPC RTL is not available so what possible work arounds are there for loading data from files?
Actually, all the IO in FPC works perfectly. You can read / write
files just like on any other Unix (although you need to declare
appropriate permissions in the AndroidManifest.xml first ---
READ_EXTERNAL_STORAGE , WRITE_EXTERNAL_STORAGE, otherwise access to
"/sdcard/..." files will be restricted). For example, config files are
just normal files on Android. You only use a special function to get
the proper config file location, but this is done for you by CGE, you
just use CastleFilesUtils.ApplicationConfig:)
The important thing is that your program data (what you put in apk) is
*not* stored on disk as normal files. You need to use AssetManager to
read it. With some helpers, you can just open an "asset file" as a
Pascal TStream, and then read from there. In CGE, you just use URL
like "assets:/some_subdirectory/blahblah.png", pass it to any
file-reading function and it will read the file
"some_subdirectory/blahblah.png" from your data. More info on
http://castle-engine.sourceforge.net/tutorial_network.php .
>
> 2) OpenGL functions are not available so how can I call into the Android library? Callback wrappers perhaps?
You use OpenGL ES for rendering (version 2 or 3, for modern devices).
Pascal bindings are available already in FPC, see unit GLES20. Of
course engines like CGE give you more comfortable API for rendering,
on top of this.
>
> 3) I use ReadXMLFile from XMLRead but that uses file loading internally so is there anyway to load XML on Android?
As said above --- you can still use these functions to read / write
regular files on Android.
To read XML in your apk data, you need to get a TStream from
AssetManager, and read / write XML using this stream. This works
perfectly:)
We use XML files in CGE a lot, and we have helpers like URLReadXML
that reads TXMLDocument from an URL, and URL can be "file:/..." or
"assets:/..." (or even http or others, see the mentioned tutorial
about networking:)
Regards,
Michalis
More information about the fpc-pascal
mailing list