PDA

View Full Version : Pascal on Android: I made it work!



chronozphere
27-10-2011, 11:46 PM
Hey guys,

After a long time of silence, I decided to do some pascal development again. I bought an ASUS EEE pad transformer recently, and I would like to see if I can create some games/apps for it. :)

I allready fiddled with JAVA, which is the main tool for creating apps for android. The tools google delivers are really easy to use IMHO, so for any non-performance-critical apps JAVA is just fine. However, when it comes to games, I'd like to benefit from the extra performance that a compiled language can deliver.

So today checked out these tutorials to get started:

http://wiki.freepascal.org/Setup_Cross_Compile_For_ARM
http://wiki.lazarus.freepascal.org/Android_Interface

There are easier ways. In retrospect, I didn't HAVE to build everything from source lol ;D . On the other hand, I now have FPC 2.7.1 and Lazarus 0.9.31 (the brand new ones, I believe) to play with.

I must say that it was quite a struggle. It took me over 5 hours, but in the end, I got that sample working (2nd URL). To be honest, the UI was too small and didn't work that well, but it's still pascal code under the hood. Moreover, I won't be using pascal for any android UI's, because there isn't much support yet, and doing that part in JAVA is more effective.

But what's next? Does anybody know whether SDL works? And if not, how do I go about accessing audio/touch/accelometer functionality?

Thanks :)

paul_nicholls
28-10-2011, 02:49 AM
Congratulations mate! I will have to try some of this myself as I have an Android tablet :)

cheers,
Paul

paul_nicholls
28-10-2011, 03:05 AM
hmm...it seems that I need Android 3.x on my device first, so I don't think I will bother going this route then (I only have 2.3.x ATM).

I will have a play with RemObjects' Project Cooper (Oxygene for Java (and Android)) and see if I can get something running that way instead, even though I have to get used to the syntax first LOL

cheers,
Paul

chronozphere
28-10-2011, 08:17 AM
hmm...it seems that I need Android 3.x on my device first, so I don't think I will bother going this route then (I only have 2.3.x ATM).


I don't really see the problem with this. What happened?

paul_nicholls
28-10-2011, 11:00 AM
I don't really see the problem with this. What happened?

The problem is that I don't have a 3.x version of Android available for my device yet, only 2.3.x. This means I would have to wait to try this method out till after I can get a later version installed.

cheers,
Paul

code_glitch
28-10-2011, 02:21 PM
Does anybody know whether SDL works?
I believe that is possible, I recall a discussion about this earlier on in the year, I'll run a forum search and see if I can pull anything up, HOWEVER - if sdl is slow on a PC and that has many GhZ and many gigs of RAM (despite less pixels to shift) unless your on the new A(5 is it?) which have 2 cores I can't see it being the best idea. My suggestion would be to either look into OpenGlES and write a compact little unit or to look into ZenGl, I know Andru's been putting in some magic stuff there and it runs on just about anything that has a transistor in it.

Aside from all that, theres a FPC 2.7.X out?! We're still on 2.5.5 in the APT repos... I'll have to check out my debs manually then. Oh, and one last thing: does the new lazarus interface still have those one million and one pesky windows floating about?

chronozphere
28-10-2011, 02:34 PM
The problem is that I don't have a 3.x version of Android available for my device yet, only 2.3.x. This means I would have to wait to try this method out till after I can get a later version installed.

cheers,
Paul

Why not develop for 2.3? Nothing wrong with that. It's all possible. ;)

Well, I will use SDL in conjunction with OpenGLES. However, it seems that SDL Audio doesn't completely work on android yet, so I might just go for pure OpenGLES with another sound library (any suggestion that works for android?). Maybe I should just wrap the JAVA audio API via JNI, but that's no fun at all. :(

And FPC 2.7.1 was build from the most recent development snapshot, so it's not the stable one. You can get it here: http://www.freepascal.org/develop.var The APT repo's (guess you are using an ubuntu/debian derivative) aren't updated frequently. I had to get debs or build things from source quite often to be honest.
Lazarus hasn't changed that much. The only difference I have found yet, is that you can't rebuild the LCL anymore via the menu's, because it's now a package (which is automatically rebuild for your target when neccesary ^^)

Ingemar
29-10-2011, 05:38 PM
How about the rumored "low level" APIs, C APIs into Android? Does that exist, how much of the Java garbage can be bypassed?

Stoney
29-10-2011, 06:12 PM
How about the rumored "low level" APIs, C APIs into Android?
They are available, but there are no Pascal header translations for that - at least not that I know of. Also, the C APIs aren't really for usual GUI app development, they are designed for OpenGL apps or other low-level stuff.

code_glitch
29-10-2011, 06:52 PM
@Stoney: Couldn't we just use the low level stuff and then dump the lazarus UI stuff onto that instead of Java as we currently do?

Historically, anything based on proper code is faster than Java... How many headers would have to be translated to get a minimal system up and running?

Ingemar
29-10-2011, 08:51 PM
@Stoney: Couldn't we just use the low level stuff and then dump the lazarus UI stuff onto that instead of Java as we currently do?

Historically, anything based on proper code is faster than Java... How many headers would have to be translated to get a minimal system up and running?

That sounds so right to me. If I can only get UI events in some decent way, I can do the output though OpenGL.

chronozphere
29-10-2011, 10:39 PM
Well, I've looked into it. The android-ndk contains a file STABLE-APIS.html which says the following:



Starting from API level 9, it is possible to entirely write an Android
application with native code (i.e. without any Java). That does not mean
that your code does not run inside a VM though, and most of the features
of the platform will still need to be accessed through JNI.


So there is some basic functionality to create native applications. However, most of this is a JNI wrapper so it's not much better in terms of performance to do it natively. Also note that only the high-level parts of the operating system are created in JAVA and run in a highly optimized Dalvik VM, so there isn't THAT much to gain anyway. Any performance critical part is natively coded.



They are available, but there are no Pascal header translations for that - at least not that I know of. Also, the C APIs aren't really for usual GUI app development, they are designed for OpenGL apps or other low-level stuff.

That is true. It's also the reason why I'd choose Java instead of pascal to develop normal apps. It's a huge amount of work to make all UI API's available to pascal and the benefits are not significant. When coding games however, it's actually worth some effort to get maximum performance.

I'm actually playing with this a little, to see if I can get a pascal game running on my android device. Today I played with JNI, which will be a very important part of the puzzle. I'll keep you guys posted and I might write an article if I have got something interesting to share. In the meantime, I can use all the advice and bits of info you can give me. For example, how does one do game audio on an android device???

WILL
30-10-2011, 09:42 PM
You know Cooper (oxygene for java) isn't such a bad product and it doesn't cost such a fortune as Delphi does. Maybe it would be worth it to ask to get on the beta for it? RemObjects does intend to support the Android platform directly so perhaps it would help streamline any Android development.

Just a thought.

paul_nicholls
30-10-2011, 10:00 PM
You know Cooper (oxygene for java) isn't such a bad product and it doesn't cost such a fortune as Delphi does. Maybe it would be worth it to ask to get on the beta for it? RemObjects does intend to support the Android platform directly so perhaps it would help streamline any Android development.

Just a thought.

Project Cooper does indeed support Android directly - I have tried some of the sample projects that comes with it and they compile and run on my Android tablet...yay!

This is even on the beta version too :D

Just send an email to:

marc hoffman (mh@remobjects.com)

and he should fix you up...

cheers,
Paul

paul_nicholls
30-10-2011, 10:04 PM
More info here:
http://www.remobjects.com/oxygene/java.aspx

cheers,
Paul

chronozphere
30-10-2011, 10:06 PM
Oh that's pretty cool. Nice it works well, even in the beta version. :)

I'm not sure whether I will use it since I'm comfortable with JAVA. I only truely benefit from pascal when it is actually compiled.

paul_nicholls
30-10-2011, 10:11 PM
I haven't done much with it yet, but it is much nicer having the 'Pascal' syntax instead of Java to be honest :)

WILL
30-10-2011, 10:46 PM
What if you wanted to make a cross-platform game for both android and iOs for example? One code-base not 2 would make thinks go half as long no?

If I got Garland's Quest on the iPad and then wanted to share the love with Android users I would strongly consider using oxygene for java to port it. I may even think about using prism to get it on the XBox 360 too, but that's a different topic. ;)

chronozphere
31-10-2011, 12:19 AM
Yes, being able to compile to both android and iOS is very interesting. But how is that possible? Cooper generates java bytecode, which doesn't run on apple devices.

WILL
31-10-2011, 02:23 AM
Yes, being able to compile to both android and iOS is very interesting. But how is that possible? Cooper generates java bytecode, which doesn't run on apple devices.

If your core game code is in object pascal you can easily rewrite the rest for each platform. So you take your object pascal units and other game specific code and create a new Xcode project using the Objective-Pascal templates and put them in there and rewrite the code required to support the API and other iOS specific framework to work with it.

Relfos
31-10-2011, 06:44 PM
I will hopefully soon have ready the first solution that will alow pascal developers to write game code once and have it running out of the box on all major platforms (Android, iOS, Windows, Mac and Linux).
Only need to tackle Android for now (and if anyone wants to help I've put a bounty up in the free pascal/lazarus wiki)
I'll put a website soon with more info :)

chronozphere
01-11-2011, 08:26 AM
As said on the FPC forum, I'm working on it too. ;) I'll let you know when I get there.

Andru
02-11-2011, 07:32 PM
Now I'm totally interested in Android development, and have already written simple OpenGL ES on Java and compiled FreePascal for arm... :) JNI seems not a hard technique, so I will try to spend my free time on investigation how to port ZenGL to Android.

chronozphere
22-11-2011, 11:26 PM
Today I managed to statically link library X to library Y (both written in pascal). Library Y is in turn called through JNI.

I know that it's a small step, but I'm still struggling with linkers and all those compiler options FPC has. If I can get a good understanding of android development with pascal, I might actually write an article about that. :)