Haxe on iPhone (Simulator) – First Look

iPhone Dev
iPhone Dev

The c++ backend for haxe generates standard c++, suitable for the gcc compiler. iPhone dev uses gcc, and can link against c++, which make you think that iPhone dev can use haxe. Simple? Well, actually it was pretty simple. The hardest bit for me was to grok the components of an Xcode project, moving from dynamic libraries to static ones and getting SDL working.

The iPhone SDK requires you statically link everything, and I wanted to make it easy as possible to change haxe code -> generate cpp -> link to Xcode -> test of iPhone (or simulator). The solution I am currently using is to generate a library from the haxe code using the standard command line make, and include this library in the Xcode project. I hope to add a “pre build” step to drive the make system automatically.

Hxcpp executables typically use the “NME” library for graphics, which is in turn based on libSDL. The good news is that the source version of libSDL compiles for the iPhone! I tried the svn download first, but this does not seem as nicely bundled as the Apirl 13 version, which worked very nicely indeed (besides a small problem with RenderRect args changing).

Getting the hxcpp backend to generate a library was almost trivial – you take the same obj files and put them in a lib instead of an exe. The only minor difference is you do not explicitly create a “main” (ie, program entry point) call, but instead create a function (currently called \_\_hxcpp\_lib\_init) that the user supplied main line must call. This may also be good for windows applicaions that want to use a “WinMain” instead of console based main function.

Compiling the hxcpp runtime as a static library was also pretty easy after the post 0.4 code reorganisation that assimilates thirdparty code rather than linking to it. Again, it was a matter of taking the same objs and putting them in a lib instead of an dso. Initally I got link error when linking with Xcode, but if you include 1 real, small c++ file in the project, these link error go away.

Compiling “plugin” modules as static libraries (eg, NME) was slightly more difficult. I could use c++ static initialisation to auto-register the exported functions, if I could get Xcode to link to the required obj. To force objs to be included, I needed to put a special symbol in each cpp file that exports functions, and make reference to these from the main code base. It is really only something that needs to be sorted out once, and it is done now, so it should not really be a problem any more.

I also have to cull out quite a bit of code (eg fonts, image loading, opengl & sound) from NME, but I can look at adding these bits in one by one.

The astute ones among you will notice that the colour of the above circle if RGB/BGR reversed. This is something that will obviously need to be fixed.

Not being used to Xcode, it took a bit of getting used to – things like frameworks etc. However, I think that ultimately, we could end up with a very nice solution. The idea would be to create frameworks for hxcpp and nme, and a project template to link it all together. You would then create a project from the template, modifiy the boiler-plate haxe code and hit build. This would also be good for standard mac apps (rather then iPhone apps). Still a way off this, but moving in that direction.

SDL, LGPL and you

Dynamically linking against SDL (or NME) normally discharges your obligations to the GPL, however in this case, we are statically linking to it so there are still some issues. However, all is not lost because my interpretation is that you must allow others to relink your application. (ie “so that the user can modify the Library and then relink to produce a modified executable containing the modified Library”, where Library is “SDL”). So you must forefiet your hxcpp compiled library file (rather than haxe or cpp source), as well as you project files (which should be boiler-plate anyhow). So this is actually borderline acceptable, although I will work towards a GPL free solution).

34 Replies to “Haxe on iPhone (Simulator) – First Look”

    1. Hi, AFAIK, in order to get stuff onto the iPhone, it must go via Xcode on MacOS. Apple has this locked down pretty tight – especially if you want to get on the App store, you have to play by their rules. That said, you could develop your code on the PC, get it working, running in a browser etc and port to the mac as a final stage. I’m not sure if there is a service for compiling iPhone code? It may violate their TOS, or it may be a nice business for some enterprising young mac owner. Submit your “haxe.lib” on a web page and get back a .app for your phone. (Probably only for jailbroken phones).

  1. Sam, It’s all held togther with sticky tape at the moment. I could give some instructions, but you would have to compile haxe, hxcpp, SDL 1.3 and nme from source and use snv version of neash.
    My current plan is to follow this for a week or two and see what changes are required to hxcpp & nme, and then do a proper release of everything. At this stage, I should be able to give some reasonale instructions for creating your own project.
    After that, I might be looking for some help to make templates and frameworks to make everything super easy.

  2. Also, if I get something interesting going, I may release a tar of the project directory, just to offer a taste of what it will look like so you can see how you can step through the c++ code etc.

  3. Any ideas on speed comparison VS a objective c created project? Just curious.

    The developer in house are saying it will be to big and painfully slow on based on the limitations of the iphone.

    I think it sounds awesome

  4. I should also point out that there are now licensing options for SDL 1.3, in which you can acquire a commercial license. So you don’t have to worry about having to cooperate with the GPL.

  5. hoseyhosey – the performance should be pretty good. The code gets compiled with an optimising c++ compiler so all numerical code will be lighting fast. Object creation will be the drag – same issues as for and langauge that “handles the memory for you”, but there are well known techniques for getting around this such as object pooling.
    Currently, it will use the software vector renderer which will be painfully slow. However, the basic MO will be to draw these to a texture and then use opengles to blit quads – absolutely as fast as possible on the hardware.
    As for bloat, haxe classes come with full class reflectance, so there is a bit of overhead there. But then again, you might find this very useful, so this could be a good thing. And dynamic functions may save you inheriting from a class just to do something simple, so there may be a saving there.
    I don’t have any performance numbers yet, so it is pure speculation at this stage. But I am confident it will be pretty close to anyting else you can do.

  6. So it will be software render at first – but opengl support would be possibe in the future i assume? Would this be hard to implement- or just not a first step thing?

    1. OpenGL will come pretty soon – all I have to do is change my OpenGL calls to OpenGLES calls. SDL iPhone already allows for this I think.

  7. Hi. Can u help me with compiling my Simple code to a library for xCode?? I just got executable file Simple – no library (
    And where I can get next libs: libstatic_nme.a, libstatic_hxcpp.a – I try to build they from haxe source – and got many errors. And I built SDL and got only libSDLmain.a – no libSDLSimulator.a and libSDLiPhoneOS.a. Please, help me..

  8. Some additional:
    When I try to download SDL for iPhone from SVN I’v got error:
    amanitas-macbook:~ Amanita$ svn checkout http://iphone-sdl-1-3.googlecode.com/svn/trunk/ iphone-sdl-1-3-read-only
    svn: URL ‘http://iphone-sdl-1-3.googlecode.com/svn/trunk’ doesn’t exist
    But how can i get all needed library – I don’t know… (

    Sorry for many questions but I need to port some flash projects on iPhone… (And sorry for my poor English πŸ™‚ )

  9. Sorry again… I resolved some problems and now i need to get only libstatic_nme.a, libstatic_hxcpp.a.

    1. Amanita, sounds like you are making good progress!
      To make the static libraries, you will need to compile from source. From the latest verson version at code.google.com/p/hxcpp, and from the runtime directory you need to do “make lib IPHONE=1” for the iphone os-version or “make lib” for the simulator version. From nme, you do “make iphone” and it will create both version. I hope to give a butter tutorial on this in about a week or so.

    1. Hi,
      I have added a bunch of static libraries required for the iphone build to the svn version of nme. You will also need the latest haxe and latest hxcpp as well. I will be doing a coordinated release of these shortly after the next haxe version, whech should be in a week or two.

  10. insane πŸ™‚
    btw, to you know any tool that translates AS3 code into haxe? that would be perfect for targeting huge amound of flash games to iphone πŸ™‚

    1. Yes – I have had a few thoughts about this. There have been a few ad hoc attempts as as3->haxe, but to do it properly, some find of formal parser would be required, and this is no small task. Other ideas might be a as3 front-end for haxe, so haxe can simply read as3 code and compile it to any of the targets. Another idea would be a swf -> cpp converter – this may be a bit easier because the syntax is easier to implement – however you may miss out on some speed – although this may not be too bad. A As3ToHaxe project would be a very good thing for haxe – just need someone to do it πŸ™‚

  11. Thanks for the great article. I have successfully tested the sample application which worked fine. Is there a way to invoke this sample application with in the my application? I do not want to have this sample application as a separate icon on the iPhone screen.

  12. Vijay,
    Sorry, I don’t know enough about the iphone/xcode setup at this stage. There is probably a setting in the xcode project somewhere. Or perhaps only things from the iphone store show up there? There is also the “Organiser” window that my help. Maybe iDevGames or an apple forum can help because I think this is more a iphone-dev issue.

  13. Huge,

    Thanks for your reply.

    Can I use iPhone SDK API’s along with your code? What I need is to invoke my own application with URL within your code. How do I use the iPhone API [[UIApplication sharedApplication] openURL:app_url]?

    1. Hi Vijay,
      In order to use native functions, you need to wrap them in “foreign function interface”. Normally, in haxe, you would place these functions in a DLL. However, for the iPhone, you can just add the wrappers to you project. You will need to include hxCFFI.h in your code, and create a function that takes “value” as args, and returns a “value”. You then use the DEFINE_PRIM macro to export the function to haxe. Inside the function, you can use the methods defined in hxCFFIAPI.h to get at the haxe values. Inside haxe, you use cpp.Lib.load to get the function.
      This is very similar to how neko does things.
      This may sound a bit complex, bit that is essentially how NME does things.
      A nice idea might be to gather some native iphone calls togther into a library for use by everyone.

  14. i want to install mac OS (latest) on my pc . can you provide free full download link or freeware. my requirement is iphone application development tools which are run on mac (SDK , Xcode ) .
    and also provide me user manual or helping books regarding above issue .
    thanks

  15. Try googling for mac vmware pc hack etc and you can find some workarounds to install osx on any intel based pc.

Leave a Reply

Your email address will not be published. Required fields are marked *