Haxe, iPhone & C++ At Last

Hxcpp 1.0, neash 1.0, NME 1.0

The release this week of haXe version 2.0.4 officially includes c++ as a build target, for Windows, Mac, Linux and iPhone. You can download and install from haxe.org. In addition to the standard includes, you will need the “hxcpp” library, which can be insatlled with the included haxelib management tool.

Coincident with the hxcpp release, I have updated the neash and NME libraries to versions 1.0. You can also download these via the haxelib tool too. There are several incrental improvements, and the iPhone target has been added!

Getting started with the iPhone

Getting started with the iPhone is quite tricky at the moment, mainly because of the pain of setting up an Xcode project. Also, getting the simplest program onto the device is hard due to the code signing requirements. So if you can already get one of the existing application templates to work, you are half way there.

Note that this solution uses the “SDL” library, and must statically link against this. SDL is covered by the LGPL license, and this has implications should you choose to release your software. I am hoping to remove the LGPL restiction at a later date.

The binaries used here are have been compiled for the “2.2.1” iPhone SDK. So choose this version when compiling for simulator or device.

  1. Download and install components
    • Get haxe & neko: Visit haxe.org
    • Get hxcpp: haxelib install hxcpp
    • Get nme: haxelib install nme
    • Get neash: haxelib install neash
    • Get the sdl-static libs for iphone: I have created a project with binary builds of these. You can get the latest builds directly from subversion svn code at:
      http://code.google.com/p/sdl-static/source/checkout.
      Or get the snapshot bundle from this site and install somewhere handy:
      sdl-static-iphone-1.0.zip
  2. Get Xcode with iphone sdk support – visit apple.com
  3. Get a Developer key (you can try simulator without it). You will need to pay to sign up as a developer on the apple site.
  4. Fire up Xcode and do File > New Project.

    Choose iPhone OS > Application. Here choose a “Windows-Based Application
    but infact we will use the delegate setup in the SDL code, so we will have
    to delete the one created by the wizard.

    Select a name & directory for the project. I’m calling it “Haxe Test”.

    Now as it stands, you should be able to build for the Simulator and
    get a lovely white screen and a program called “Haxe Test” in the simulator
    start screen.

    Next thing is to delete(to trash) the “…AppDelegate.h” “…AppDelegate.m”,
    the “Nib Files” group, Resources/MainWindow.xib and “main.m”.
    Finally, select the “Haxe Test” executable (in the Targets section) and from the “Get Info” –
    “Properties” tab, clear the reference to “MainWindow”.

    We will add replacements for these soon.

  5. Add “main.cpp” from the NME project.
    Select the top-level project folder and then use Action > Add > Existing Files.
    It is probably in /usr/lib/haxe/lib/nme/1,0/ndll/iPhone/ or
    similar depending on which version of NME you have installed. It can be
    very painful to get xcode to load from this location, unless you hit
    Command-Shift-G at the “Add” dialog and type (at least some) of this filename in.
    Choose to “Copy to destinations folder” so
    that you can mess with it if you wish. Note: you need to have a cpp mainline
    in order to automatically link in the correct runtime libraries.

  6. Add the libNME.iphoneos.a and libNME.iphonesim.a files from the haxelib NME project.
    You can add them both and the linker
    will select the correct on depending on your build. They are in the same place
    as main.cpp, you you should be able to use “iPhone” from the pull-down box
    in the add dialog. Probably best not to copy these files – in case you want
    to change them at some stage.
  7. Add the whole sdl-static/lib/iPhone directory.
    Again probably best not to copy.
    I used the “Recursively create groups” option. These will be where you stored them
    in step 1.

  8. Add the whole hxcpp/bin/iPhone directory like above.
    Again, this will
    be in a path like /usr/lib/haxe/lib/hxcpp/1,0,2/bin/iPhone/.
  9. Add the hxcpp include directory to the include path.
    Use the “Info” button
    to get the project properties, and on the build tab, under “Search Paths”
    add something like /usr/lib/haxe/lib/hxcpp/1,0,2/include/ to “Header Search Path”
  10. Now we are ready for the haxe code. If you have and existing project,
    then you can adapt the following instructions.

    Create a new file from Xcode (Other/Empty File] Here I have called it “HaxeTest.hx”, and unticked the “Targets” option. I’m prety sure there is a way to get “Haxe File” to appear as on option here – but I don’t know the details.

    In the haxe file, enter something like (Note the window size):

    import flash.display.Sprite;
    import flash.display.Shape;
    
    class HaxeTest extends Sprite
    {
    
       public function new()
       {
          super();
          flash.Lib.current.addChild(this);
    
          var circle:Shape = new Shape( );
          circle.graphics.beginFill( 0xff9933 , 1 );
          circle.graphics.drawCircle( 0 , 0 , 40 );
          circle.x = 150;
          circle.y = 200;
          addChild( circle );
       }
    
       static public function main()
       {
          neash.Lib.mOpenGL = true;
          neash.Lib.Init("HaxeTest",320,480);
          neash.Lib.SetBackgroundColour(0x447733);
    
          new HaxeTest();
    
          neash.Lib.ShowFPS();
          neash.Lib.Run();
       }
    }
    

    This is the “main” file for haxe, and the hxcpp compile will create a library matching
    this class name.

  11. Set up a build script to build changes you make to your haxe files into a library.
    Xcode has a few issues with a straight custom build script order due to incorrect
    dependency checking. This can be worked around by first adding a custom target.

    Highlight the “Targets” in the Groups & Files and use the “Action > Add > New Target..
    Choose “Other > Shell Script Target” and call it something like “Compile Haxe”.
    Close the pop-up and go back to the explorer. There should be a “Run Script”
    entry under the “Compile Haxe” target if you expand it out.

    Get info on “Run Scipt” and enter the following script

       if [ "$CURRENT_ARCH" = "i386" ]
       then
          haxe -main HaxeTest -cpp cpp -lib neash -lib nme  --remap neko:cpp --remap flash:neash -D iphonesim
       else
          haxe -main HaxeTest -cpp cpp -lib neash -lib nme  --remap neko:cpp --remap flash:neash -D iphoneos
       fi
    


    You can untick the “Show Environment” if you do not need to debug this.

    One last step – drag the “Compile Haxe” target into the “Haxe Test” target.
    It should now also show up as first item “under” the “Haxe Test” target.
    The build order should now be correct. (See image at end of post)

  12. Now you are ready to do the build. The first time you build, the build
    results will show “Running custom shell script…” for quite a while.
    Haxe compiles to cpp very quickly, but it take a while for the cpp files
    to compile to a library. You can see the progress if you expand out the
    middle tab bit.

    At this stage, you should get a bunch or errors when linking, but also haxe
    should have created a library for you. Add this library to the project –
    it should be in the local cpp/HaxeTest.iphonesim.a.

  13. Compiling now gets a bunch of unresolved functions from frameworks.
    Add the following frameworks to the project (Add > Existing Frameworks):

    • QuartzCore
    • OpenGLES
    • AudioToolbox

    These can be found in /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/.

  14. Run!
    So you should be good to go. Open up the debug console so you can see
    any traces/printfs.

  15. Change the target to “Device – IPhone OS” from the pull-down and hit “Build and Go”.
    Again, this takes quite a while the first time.
    Now add the new cpp/HaxeTest.iphoneos.a library to the project.

  16. Now you need to sort out your code signing. If you have not done so already,
    setup you apple developer account & certificates on the apple web site.
    Go to the info of the “Haxe Test” executable and the “properties” tab.
    Change the “Identifier” to match one of your cerificates. Make sure to
    match your company URL. You may want to use “*” when creating your
    profile for easy changing.

    Under the “Build” tab, under the “Code Signing” bit
    in the “Any iPhone Device” pull down your profile. If you don’t have one then
    you will need to create one on the apple website.

  17. Connect up your iPhone(iPod touch) and build! W00t!

HaxeTest

I have had all sorts of errors when trying to upload to the device.
So far, they have been solved by getting out of the car, walking around it and getting back in.
ie, Disconnect and power down ipod. Fully exit Xcode and the start it all up and try again. Also, uninstalling the app from the “Windows > Orgainiser” directory can help.

But now the easy bit. Change to HaxeTest.hx file, and hit Build & Go. It is that simple.
Errors should show up nicely in xcode.

You can add data files (eg, pngs, xml etc) to the project and they will be copied to device so you can open them with a relative path.

In the properties of the “Info.plist” you can set a Icon File – don’t forget to add the icon to the project too.

Not covered here (because I have not fully sorted it out myself):

  • Syntax highlighting in XCode
  • Debug build (hxcpp can do then – it’s a matter of setting up Xcode)
  • Code completion in Xcode
  • Automating this procedure!

Edit: Add framework path, SDL version, MainWindow clearing.

147 Replies to “Haxe, iPhone & C++ At Last”

  1. Hmm I can’t seem to compile HaxeTest.iphonesim.a… The build script is running but I’m getting a whole lot of errors when it trys to build the C++ code.

    Errors in nme/display/JointStyle.cpp:
    “wchar.h: no such file or directory”
    “string.h: no such file or directory”

    “declaration of ‘operator new’ as non-function”

    I’m getting 18 errors in total from that.

    Any ideas where I could have gone wrong?

  2. Hi Hugh,

    Excellent work!

    Is there any notes about what API is implemented? i.e. I’m pretty sure
    that perlin noise is missing from the BitmapData class?

    1. Simon,
      Sorry, I don’t have a proper coverage document at this stage. Sadly, the source code is the best resource here. Things should settle down soon, and then I can get onto that sort of thing.

  3. Sam, Ok it does look like you have the latest versions – it’s just that it can’t find these includes.
    It is probably to do with development paths – perhaps version numbers. I will have to have a think about this.

  4. Hi Sam,
    I use this in the makefile:
    -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk which is set with the “IPHONE_VER” in the BuildCommon.xml

    Do you have this directory in your:
    /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/ ?

    You could try changing this in the BuildComon.xml

    Hugh

  5. Hey hugh – I am thinking about the LGPL thing and how it affects making an iphone game. afaik it means that ALL of the generated c++ code would need to become LGPL. This would not necessarily mean that hx code, or releasing a compiled .a for linking (altho people could obviously recompile and link, but it would take a fair bit of work, and without graphics, would be more work for the average person that is probably worth while. The c++ code is a little obfuscated too as its generated (a little harder to understand)
    SO i’d have no issues with this atm. I’ll probably release the code to any game a month or two after releasing it anyway, just not as soon as its released.
    An LGPL solution would be good too – but not the highest priority i guess… just thinking aloud

  6. Tony, my understanding of the LGPL is that you need to allow people to *Relink* your application with their versions of the LGPL code (eg, put a “printf” in the *SDL* code). For this, your HaxeTest.iphoneos.a and main.cpp is all that is required. I think you have to make this practically possible too (rather than just theoretically possible) with enough project doco to allow it to happen. You keep copyright of all images etc (you could even encrypt them), so they can’t go publishing an app themselves, although I guess they need to be able to upload them to their own phone. I don’t think this is going to stop any sales.
    I think you can get a commercial license for SDL – not sure how much it costs.
    For the iphone, most of the SDL code is a thin layer over the OS code but presented though the “SDL” interface. Since NME has its own interface, it should be reasonably easy to go straight to the OS – at least for the iphone.

  7. Rick,
    The c++ code contains its own garbage collection code. Initially I tried “Boehm GC”, but that was too slow, so haxe generates it own code. I hope the problems with Boehm GC can be fixed (by someone else!) and it will all just magically run faster. As it stands, the GC is probably one of the weakest points of the current system, and I will look at speeding it up.
    ObjectiveC is only used where it is needed to interact with the OS. The haxe code is pure c++.

  8. Hmm so it seems it is not truly multi platform haxe solution. Rather way of coding for iPhone in MacOS XCode using haxe language.

    I wonder when someone will come up with polished way of doing it in say Eclipse on all platforms(Linux/Windows/MacOS) so that need for switching platform would be eliminated.

  9. Sam,
    I think you do not have the 2.2.1 SDK installed. Yuu can either get it from apple, or change the “IPHONE_VER” in the BuildCommon.hxml.

  10. Wonderwhy-er,
    It is mostly cross platform. The haxe code is still the same, it’s only the final bit of getting it onto the device. The problem is that apple has locked down this final step.
    This tutorial may give you the impression that most of the work is iPhone specific but that is only because the program is trivial.
    Once set up, the only remaining work you have to do the the haxe code.
    Another way to do it would be to write your code in eclipse (or FlashDevelop) and get it working in a broswer, and then do this final step to get it on the iPhone.
    You can compile for Mac, Linux and Windows from the command-line – no need for xcode.

  11. Interesting stuff. This is potentially pretty game-changing, but what’s the performance like compared to doing native Obj C code? I saw your previous post where you were getting sub-10 fps on some demo code. I’m assuming we have no access to the UIKit stuff through haxe? What about OpenGLES?

  12. Hi, Hugh.

    Probably there is a problem in the socket_send function of the std.dll at the line

    val_check(data,string);

    – validation is not passed.

    I think this is because haxe.io.BytesData is defined in neko as NativeString
    and defined in cpp as Array

  13. Hi, Hugh.

    Thanks for fix.
    There is problem with next code:

    package ;

    class TestClass{
    public static var AAA = 0;
    public static var BBB = 1.3;
    public static var CCC = “”;
    public static var DDD = true;
    public static var EEE : Dynamic = null;
    public static var FFF = new Hash();

    }

    class TestSwitch
    {
    static function printType(args : Array){
    for( i in 0…args.length ) {
    var a : Dynamic = args[i];
    switch (a) {
    case TestClass.AAA: trace(“AAA”);
    case TestClass.BBB: trace(“BBB”);
    case TestClass.CCC: trace(“CCC”);
    case TestClass.DDD: trace(“DDD”);
    case TestClass.EEE: trace(“EEE”);
    case TestClass.FFF: trace(“FFF”);
    }
    }
    }

    static function main() {
    var a : Array = [TestClass.AAA, TestClass.BBB, TestClass.CCC, TestClass.DDD, TestClass.EEE, TestClass.FFF];
    printType(a);
    }

    }

    When compiled as Neko target – it works ok.
    TestSwitch.hx:24: AAA
    TestSwitch.hx:25: BBB
    TestSwitch.hx:26: CCC
    TestSwitch.hx:27: DDD
    TestSwitch.hx:28: EEE
    TestSwitch.hx:29: FFF

    But as cpp target it gives next:
    TestSwitch.hx:24: AAA
    TestSwitch.hx:25: BBB
    TestSwitch.hx:24: AAA
    TestSwitch.hx:27: DDD
    TestSwitch.hx:28: EEE
    TestSwitch.hx:24: AAA

    1. Fedor,
      Looks like it is making a comparison using “doubles”, without checking the types of both sides are numeric. It should not be too hard to fix in hxcpp.

  14. When I try to compile it says haxe command not found. Where is the shell script executed so that I can explicitly set the path to haxe in the shell script? Please help.

  15. Hello Hugh,

    this project sounds really interesting. I did some first steps with haxe and wanted to try and compile some c++ with your libraries. I followed the instructions on the touchmypixel-blog to download & install the hxcpp libraries.

    The installer seems to perform fine, i am on windows vista and run the command line as administrator. The lib folder is created and the subfolders ‘hxcpp’ and ‘1,0,5’ too. But it seems to me as if the .exe Files are missing, e.g. hxcpp.exe. All i find inside ‘1,0,5’ are:
    -haxelib.xml
    -LICENSE.txt
    -run.n

    and subfolders: bin, build-tool, include, runtime.

    Do you have any idea what might have happened here?

    kind regards,
    Sebastian

  16. HaxeTrouble,
    What system are you on? Mac? I think it installs in /usr/bin/haxe which links to /usr/lib/haxe/haxe. This should be in your path – maybe a permissions problem somewhere? Windows, it is in the “Motion-Twin” programe files directory.

  17. Sebastian,
    I think you have a correct install!
    The project is generally run from the normal “haxe” compiler, which in turn runs “haxelib run hxcpp”, which in turn runs “neko” on the “run.n” file found in the current hxcpp directory.
    You can run, for example:
    haxelib run hxcpp BuildLibs.xml
    On its own, but normally you would just do:
    haxe -main Main -cpp cpp

    Hugh

  18. Great work! Functions rather nicely on my iPhone.

    My only criticisms currently are:

    1) It takes a significant time to build. Perhaps

    2) The licensing of the various components (e.g. SDL, NME) leaves many questions with regards to exploiting this in a real app store app.

    Other than that, rock on! ๐Ÿ˜€

  19. Huge,

    fantastic work!!
    Im loving it so far,

    I have noticed some issues with bitmaps?

    var mLdr:Loader = new Loader();
    mLdr.load(new URLRequest(“blah.png”));
    addChild(mLdr);

    i’ve tried jpg and both png outputs from photoshop and they display, but they come out black n white with weird lines going thru them, and a little distorted.

    I AM using the 2.2 framework (not 2.2.1) so maybe thats it? (im grabbing 3.0 now)

    Also is it possible to receive ‘mouse’/finger input yet?

    1. Hi Jarrad,
      That looks like a clasic case of wrong-number-of-bits-per-pixel (24/32). I have a “Convert to preferred format” call in nme that must be getting missed, or has a bug somewhere. Should be easy to fix.

  20. This looks great.

    I’ve just tried it on a Windows XP machine, eventually got one of the samples to complile (neash sample 1). But when I try to run the simple.exe which is in the cpp directory I get a crash.

    Do I need to copy over dlls?

    But in general this looks great, and I will test on the iphone later today once I get the pc version working.

    Thanks for your hard work.

    Phil

  21. k, I managed to get it working on the iphone nicely. A couple of issues, were location of the hx file, needed to be in the root not the classes directory where it was created.

    Other than that runs nicely on my 3GS phone.

    Now for the pc version.

    Phil

  22. One question, when compiling do you set the compile for thumb flag off? As I’ve seen in the past on other opengl games, that you can get a huge speed improvement with this flag turned off.

    Phil

  23. Phil,
    I used to have the thumb flag on, but I got a jump-out-of-bounds. I think the runtime + nme + neash + user code is too big. I’m not sure how to mix small jumps and long-jumps? But I’m always keen on some optimisations, so I will have a look at some stage.

  24. Huge, thanks! works perfectly now

    I saved png with Pixelmator instead of photoshop, how bizaare!

    Sorry to be high maintenance, but when do you think there will be MouseEvent’s?

    That and sound and this is my dream come true!

  25. Jarrad,
    By mouse events, you are talking about mouse-over/mouse out etc? The problem is that because the iPhone mouse is “Down” when it moves (ie, you press the screen), it is a “drag” event, not a “move” event. I guess I did not consider this a possibility, and it can be fixed. The events should get as far as neash, where it converts it into “mouse over” style events – you could have a sniff around there – otherwise the next version of neash should fix this.

    Hugh

  26. Yah, specifically mouse_down and mouse_move.

    How I see it is mouse_down would be when you touch the screen, mouse_move would be your finger down and moving and mouse_up when your picking your nose/scratching your head

    I had a poke around neash Lib.hx and the event .hx’s and saw a OnMouseDrag but i couldnt find an event that it fires.

  27. Okay, I followed your instructions to the letter, and I’m using the latest sdl-static (rev 27), hxcpp 1.0.5, neash 1.0 and nme 1.0. Building it doesn’t bring up any errors, but when trying to launch the app it just crashes, every time, on launch. Any recommendations?

    Also, I didn’t understand your “cpp mainline” comment in the instructions. I’m very new to anything more advanced than actionscript, so apologies if that’s a stupid question. I tried googling it but nothing appropriate came up. Perhaps that’s my problem?

    I copied all your code exactly, except for the Header Include stuff which I changed to 1,0,5 instead of 1,0,2 for obvious reasons.

  28. Okay, so I’ve now resolved my previous problem. Basically, I can only get this working at the moment under the 2.2.1 Simulator. Which is fine, except that I only own a iPhone 3.0, and, more to the point, it has to be able to work on *all* iPhones.

    I have no idea where my problem is, and I suspect it is indeed my problem and not something wrong with the project itself, but if you could point me in the right direction I’d be grateful.

    1. Hi Matthew,
      Did you try compiling for the “2.2.1” device, and then running on your 3.0 device? I thought that this would be the most compatible way. However, if it is crashing, I will upgrade sdl-static to 3.0 too. You could try this yourself if you like there is an “IPHONE_VER” in the sdl makefile – you could edit this, and then type “make”. A few people have had this issue, so I will address it soon.
      Huge

  29. I thought I’d tried pretty much all combinations trying to get it to work, but it wouldn’t surprise me if I’d missed one! Thanks for the advice and I’ll try it out right away.

    1. Hi Jozsef,
      You can run in landscape more by applying a rotation and translation to your game. I’m not sure if fonts will work properly though – will have to check it out.
      The touches on the screen show up as a mouse event. The accelerometer shows up as a 3-axis joystick. PVR textures ? are you talking about compressed ones? There is no api to do this at the moment, although under the covers, it uses OpenglES textures, which do support some compression.

  30. Thanks for your aswers!

    “PVR textures ? are you talking about compressed ones?”

    Yes. We have games with 2048×2048 multi-layered backgrounds – sliced up to four 1024×1024 textures – and we would like to port it to the iPhone using the compressed textures (PVRT).

  31. It would require a small change to the “NME” library – something along the lines of “create texture from compressed data”. Or do you create it from normal RGB data and ask opengles to compress it? Either way if you do not frequently update it, it should not be too complicated to add.

  32. Wow, impressive work!

    Jarrad,
    I listen MOUSE_DOWN and MOUSE_UP events from the stage and then use the stage.mouseX and stage.mouseY to get the coordinates on MOUSE_MOVE.

    Huge,
    You said that the accelerometer shows up as a 3-axis joystick. Can we get these values in Haxe?

    1. Hi,
      Yes, I think the mouse move events are not coming through because the mouse is “Down” – so the mouse position is a good work-around for now.
      The joystick is in nme.Joystick. You will have to “#if !flash” it at the moment, because there is no neash wrapper.
      You basically get the world’s “gravity” acceleration resolved along the devices axis.
      var joystick = new nme.Joystick(0);
      trace( joystick.getAxis(0),joystick.getAxis(1),joystick.getAxis(2));
      You can divide each value by 0x7fff because SDL scales them like this.

  33. ahhhh, i had my eventlsiteners on displayobjects. putting them onto stage works perfectly.

    Accelerometer is awesome too ๐Ÿ˜€

  34. @ Phil Harvey

    I have experienced pretty much the exact same thing as you.
    When I launch the compiled Simple.exe ,it opens up in a window but crashes immediatly.
    The same happens if I compile all the other provides Samples (thank Huge !).
    But if I write a most basic HelloWorld.hx with some basic math operations and String concats the compiled HelloWorld.exe runs as expected (no window of course).
    Is it more probable that I am missing some files during compilation or should I look for problems in neash?
    I will write a slightly more advanced prog without neash library,…
    Please let me know if you made any progress!
    o.krylow(at)gmx.de

    Bye, O.

  35. Hi Huge,

    unfortunatelly not, it got worse :).

    Now I get compiler errors about “unresolved external symbols”.
    They all revolve around the hxGC files.
    After compiling a second time (Simple.hx out of the Samples folder) with the -DINTERNAL_GC flag set I got the exact same errors.
    (I stored the compiler output in both cases, if you’d like to see…)

    Is this problem platform/cpu specific or have I simply overlooked to setup something the right way?

    Nonetheless, in my opinion HXCPP is breathtaking, I’m simply amazed! Thanks for sharing it!

  36. Oli, try completely deleting the directory that hxcpp creates (usually “cpp”) so all the obj files get completely recreated.

    Hugh

  37. YESSS !

    Simply amazing.

    If you could spare the time, would you explain a little about what is going on behind the secenes in hxcpp (just in general terms).
    I am really curious how this works! And tons of questions:
    How can we use existing c++ libraries with hxcpp?
    Out of curiousity I tried to convert a c++ sample library from adobe with alchemy to a swc file. then using –gen-hx-classes haxe provided the hx header files…
    Unfortunatelly ,when I compiled to swf I got 2 runtime errors and when compiled to cpp a fatal compiler error.
    I am guessing there is an easier way using the cpp API, isn’t there? ๐Ÿ™‚
    I apologize ,if my questions seem silly. I am still trying to wrap my head around C++… (and Haxe for that matter;) )
    Good nigth, O.

  38. Great work man! I’m hungry for more!

    I’ve ported my game from as3 to haxe->as3 and now I would like to also test haxe->cpp. I managed to get the compiler to compile smoothly, but the main problem are resources. I’m using FlashDevelop and embeded resources.

    What method for dealing with resoruces do you propose, so the code would be usable in both flash and cpp compilation targets?

    1. Hi yfan,
      You can use the haxe “resources” to embed data into flash and c++ executables, but I don’t think this is the most efficient.
      I think you will need some platform-dependent code to load from a file on the iPhone, and from swf-mill embedded resources on flash.
      You could make a reasonably simple class to handle the differences for you – infact I have started on one, but its not releasable yet.

      Hugh

  39. This looks really interesting. Curious, does the API coverage include the network elements for flash? I would love to be able to compile executables that can connect to FMS and sync with RSOs, etc.

    1. Milo,
      Sorry, I have not dome much in the wait of network API. haxe has some built-in http/socket access, but I have not looked at the potocols.

  40. What a great job. The only issue I have and probably a lot of people is the need for an apple to port it too. Oh and I need to charge the iPod Touch and update it (its been flat for a couple of months).

  41. Thanks for your answer. It seems that I’m not going to get away with not using command line compilation.

    Looking forward for your class for cross platform resource management.

    Can you post any kind of timeline for upcoming hxcpp releases?

    Cheers :).

  42. Jarrad, I had the same problem with garbled images. It looks like somewhere in neash or SDL it is assuming your images have an alpha channel. I switched to a PNG with alpha and now it comes out fine. There is one non-obvious trick to make PNGs work at all though. Xcode by default does some modification to PNG files when building an iPhone app. To disable that, select the image in the group tree on the left, press Cmd-I and then in the General tab change the File Type from image.png to just image.

  43. Hey I’m developing my app in Windows using Visual C++ with http://www.DragonFireSDK.com and it’s going great, do you have any experience with that program? DragonFireSDK uses a C++ layer to call Obj-C functions, it has nice documentation and the API is easy to learn.

    Is gamehaxe for Windows?

  44. Hi Huge,
    the possibility to use HAXE for develop iPhone app is very cool, my compliments.

    Unfortunately I have issues running this example in Simulator (3.0, 3.1, 3.1.2), the app start and close. I have traced and the app exit when run this code: neash.Lib.Init(“HaxeTest”,320,480);
    The IPHONE_VER in BuildCommon.xml is set to 3.1.2

    I have also 2 others questions:
    – what’s the status of the project? your last post was in August.
    – you know if there are apps submited in AppStore build with Haxe-c++?

    1. Hi monty,
      Funny you should mention this. I’ve been working in phases, where I will work on one project for a while to get momentum going and finish a chunk off before moving on. I’m just putting the finishing touches on the latest hxcpp backend, and about to re-start work on the iPhone project, and hopefully I can give this a new boost of energy. So as for the status, I would say “soon to be very active again”.
      I don’t think there is anything yet – currently you would need to license libSDL for that. However, I hope to resolve this and help get something to the app store soon – now I have freed up a bit more time.

      Hugh

  45. Hi,
    Re: crash in 3.0 device. I think I’ve fixed this now – you will need the last haxe, hxcpp, nme, neash & sdl-static. Also, I had to compile for “3.0” device, not “3.1” device otherwise I go a jump-out-of-range error.

    Hugh

  46. Hi Huge,
    thanks for the answers.

    I have tried to update all but still have issue, however I’ll try to remove all and restart clean project.

    I glad to heard that the project will continue, a question about SDL, the v 1.3 have changed license now is commercial for Iphone the price is not so hight but I think that will be great to have all opensource and free, what about openframework? can be a valid alternative.

    If you need beta-tester send me a email.

    Monty

  47. Hi Monty,
    If you can compile the sdl-static yourself, then you should get the line-number (or at least function name) that caused the crash – that would help.
    I have recently been compiling for the iPhone 3.0, so it can be made to work.
    As for libraries, I’ve started a rewrite that will not use SDL for iphone. Hopefully it will also be quite a bit more efficient than the current NME so it should be good all around.

  48. Hey Hugh,

    I’ve the latest XCode with 3.1.2 iPhone SDK, all libraries the latest version (obviously – I did download them today), and everything went pretty much fine until step 12…

    Yes, it was compiling for a minute or so. It DID throw all those errors, mainly in the “CompileHaxe”, but one or so also in the main project file.

    You said it was OK, and the haXe should’ve created a library in “local cpp”… What?

    Where can I find this library again?

    Thanks.

  49. Hi Aurel300,
    When you compile the haxe code using hxcpp, the output files are put in a sub directory. If you have followed the instructions, there should be a new “cpp” directory inside your project dir. The lib files should be in there. If you passed different commands the the haxe compiler, the sub directory may be of a different name.

  50. Oh… Hehe, I thought it will be visible somewhere in XCode, just now I realized it is in the actual directory.

    Thanks for the fast reply…
    Unfortunately…
    It still kidna fails. On step 14 – it should have run? It does not. If I kept the “Compile Haxe” script in the project, it gave me 27 errors, and when I removed it – 7.

    One of them is “hxObject.h was not found”, for example. Other include “printf was not defined” and random stuff with excepting “;” or “(“, and so on.

  51. Hi,
    If you get reference to “hxObject.h” it means you are using an old version of haxe (or maybe hxcpp).
    Try deleting the “cpp” directory before you start, and make sure haxe is version 2.05, and hxcpp is version 2.05.1.

    Hugh

  52. Not working at all.
    T_T
    I have the latest versions. Even if I knew I’ve downloaded them just two days ago, ./haxelib list clearly states the fact (lol).

    Tried deleting cpp directory and “build and run”, but it does the same…

    I have no idea what is the reason for this… It looks like it doesn’t know that main.cpp is a c++ file or something…

  53. Hi Aurel300,
    Oh yes, silly me, I forgot I even had the main.cpp in the nme directory! (You would not believe the number of files I had to update for the latest release). I have not updated this file for haxe 2.05. The change is simple, you need to change:

    #include <hxObject.h>

    to

    #include <hxcpp.h>

    Hope this works!

  54. Hey,

    thanks again – it did work! …almost -.- I get some different error now, though:

    Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++-4.2 failed with exit code 1

    … wow so many errors here. ๐Ÿ˜€

  55. You know, I actually posted it. That part:

    Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++-4.2 failed with exit code 1

    is the error. When I said “so many errors here” I was referring to the overall project, not this build.

  56. Hello.

    I have few error about include (string.h etc…) when trying to compile my .hx file :
    I’m using haxe -main HaxeTest -cpp cpp -lib neash -lib nme –remap neko:cpp –remap flash:neash -D iphonesim

    And it’s saying things like /usr/lib/haxe/lib/hxcpp/2,05,1//include/hxcpp.h:6:20: error: string.h: No such file or directory

    I have string.h in my /usr/include… What should I do ?

    When I let XCode compile it for me, I have no .a in the cpp/ dir.

    1. Hi Idealprod,
      It seems strange indeed. Could you post the compile “gcc” command if you have not further luck.
      One thing it might be is a different SDK version. The standard hxcpp build system (int $HXCPP/build-tool/BuildCommon.xml) uses IPHONE_VER 2.2.1. If you do not have this installed on your system, this could explain the problem. Try changing this value to 3.0.0 and see if that fixes the problem. Alternatively, you may consider installing the older SDK – but only do this if the first options does not work.

      Hugh

    1. Hi Aurel300,
      I have been away from iPhone development for a little while now, but I will be picking it up again soon.
      I was hoping there might be a little more of an error message that will help me pinpoint the problem. In the build results panel, you can expand out the middle bit so you get 3 sections. In the middle panel, there should be some more messages – it would be helpful if you could post these.

      Hugh

  57. Well, it says the library Something.iphoneos.a (which I created sooner in haxe) is missing.

    And it is true. You sooner said I should delete the cpp directory, in which was the file located, and now, for some reason, the haxe compile script is not creating the library at all.

    How do I rebuild that library? (yes, I am very new to Xcode…)

  58. I’m trying that and I’ll give you the gcc call if it doesn’t work.

    Is there a way to compile the SDL lib for the 3.1 iphone OS ? I’d like to make haXe programs for the very last iPhone version.

    Thanks !

  59. Great !

    I’ve changed the iphone version to 3.0, and it’s working great.

    Can I target 3.1.2 with the SDL repos’ I downloaded ? What can I do if now ?

    Thanks !

  60. Hi Huge…

    I’ve been trying to get this working, and I ran into the hxObject.h issue. I changed it to hxcpp.h like advised. It then said it couldn’t find string.h, so I added “/usr/include” to my search for headers. This solved the missing “string.h” issues. But doing this, then got me another error:

    /usr/include/c++/4.0.0/cstdarg:57: error: ‘::va_list’ has not been declared

    I then tried a different to find an old SDK (no joy, yet!) – any news on making this stable?

    Cheers
    Simon

  61. This is the error I get…

    In file included from /usr/include/c++/4.2.1/tr1/cstdarg:37,
    from /usr/include/c++/4.2.1/tr1/stdarg.h:37,
    from /usr/include/stdarg.h:4,
    from /usr/include/wchar.h:111,
    from /usr/lib/haxe/lib/hxcpp/2,05,1/include/hxcpp.h:7,
    from /Users/simon/Documents/workspace/haxe/Haxe Test/main.cpp:1:
    /usr/include/c++/4.0.0/cstdarg:57: error: ‘::va_list’ has not been declared

  62. I’ve managed to get past that problem (am I going down the wrong path?), by adding

    #include

    in to the hxcpp.h above the “standard headers…”, which is great, except I’m now getting a lot of errors about the architecture. I will play around a bit to see what I can dig up.

  63. Hi Simon,
    I have had reports that version of gcc later than I have been using have “optimized” the cross-inclusion of headers, meaning they need to be explicitly included. I actually got a patch today, so I’ll merge it in soon. But you are on the right track!

    Hugh

  64. I’m using iPhone simulator for this (Version 3.x.x – they all do the same, I don’t have access to version 2.x.x)… I put a breakpoint on the __hxcpp_lib_main(); in main.cpp, which just coughs this back

    Current language: auto; currently c++
    Program received signal: โ€œEXC_BAD_ACCESSโ€.
    Cannot access memory at address 0x1
    Cannot access memory at address 0x1

    Which doesn’t reveal much for me…

  65. Hi Simon,
    The most helpful bit will be the call stack.
    It may be crashing before the lib_main gets called. In the debugger-console-window, you can type “where” to get the list.
    I use the simulator/sdk version 3.0.0.

    Hugh

  66. This is the stack when I type “where” in the console.

    #0 SDL_main (argc=1, argv=0x130f030) at /Users/simon/Documents/workspace/haxe/Haxe Test/main.cpp:21
    #1 0x0009fb41 in -[SDLUIKitDelegate applicationDidFinishLaunching:] ()
    #2 0x308f8ac3 in -[UIApplication _performInitializationWithURL:sourceBundleID:] ()
    #3 0x30901bf5 in -[UIApplication _runWithURL:sourceBundleID:] ()
    #4 0x308fef33 in -[UIApplication handleEvent:withNewEvent:] ()
    #5 0x308fad82 in -[UIApplication sendEvent:] ()
    #6 0x309013e1 in _UIApplicationHandleEvent ()
    #7 0x32046375 in PurpleEventCallback ()
    #8 0x30245560 in CFRunLoopRunSpecific ()
    #9 0x30244628 in CFRunLoopRunInMode ()
    #10 0x308f930d in -[UIApplication _run] ()
    #11 0x309021ee in UIApplicationMain ()
    #12 0x0009f9c1 in main ()
    Current language: auto; currently c++

  67. Hi again !

    It works great, but I have a few question.
    Is the whole flash API is handled ?
    Can I use libs for flash, like TweenerHX (doesn’t seem to work yet) ?
    Can I use the iPhone api, to handle the inclinometer, the accelerometer, compas, localisation… ?
    Can I make applications built for OS 3.0 working on a ipod on OS 3.1.2 ?

    Thanks !

    1. Hi,
      Good to see you got it working. in Answer to your questions:
      Is the whole flash API is handled ?
      No – I have only targeted those functions I thought would be needed to get a game off the ground.

      Can I use libs for flash, like TweenerHX (doesnโ€™t seem to work yet) ?
      If you have haxe source code, then you should have a chance. Neko does not support runtime properties, although the latest hxcpp does. This means on neko you can’t do: obj.x=20, you have to do obj.SetX(20).

      Can I use the iPhone api, to handle the inclinometer, the accelerometer, compas, localisationโ€ฆ ?
      The accellerometer is provided by the nme.Joystick class. The axes are the acceleration (usually gravity) resolved in the devices x,y,z directions. A have not done other APIs yet, although in the re-working I’m doing, I will probably add as many of these as I can.

      Can I make applications built for OS 3.0 working on a ipod on OS 3.1.2 ?
      Yes – I’m pretty sure that is how it’s done.

      Hugh

  68. Thanks for that answers !

    I actually have problem to have TweenerHX work, maybe I should rewrite it a bit…

    Do you have a website like the haxe.org/api where I can found which flash9 classes are handled ? It would be great.

    Is there a way I can port, or convert some classes I’m using in my haXe flash program so they work in haXe/iPhone ? For instance, I’m using flash.xml.XMLDocument, and it just can’t find it…

    Thanks.

  69. Hum… When I use gdb on iPhone device (real) i get many error messages which says “Bad font ; ARIAL.TTF”… How can I fix that ?

  70. Hi Idealprod,
    There is some code in NME that tries to locate the font on the system. It does

    if strcasecmp(inName.c_str(),”arial.ttf”))
    outFile = FONT_BASE “Arial.ttf”;

    Where FONT_BASE is “/System/Library/Fonts/Cache/” for the iphone (different for the simulator). Maybe this assumption is wrong?

    Also, you should not try to create a text field (eg, in a static initializer) before the call nme.Lib.Init, since the fonts will not have been initialized.

    If you have included the font in your project, you could try “./ARIAL.TTF” to force it to use the local font. If you do not set the font explicitly, you could try neash.text.TextField.mDefaultFont = “./ARIAL.TTF”.

    As for flash.XML, you have to use the haxe “Xml” class if you want to be cross-platform.

    Hope this helps,

    Hugh

  71. Hi, thanks! it works great but I have a couple of questions.

    I’ve been trying to load assets from external files:

    First, when I tried to load a PNG using:

    bitmap = new Bitmap(nme.display.BitmapData.Load(“myimage.png”));

    On the iPhone console I got an error:

    pnglib – CgBI: unknown critical chunk

    I fixed that adding a user defined setting in the project configuration, like this:

    User-Defined:
    IPHONE_OPTIMIZE_OPTIONS | -skip-PNGs

    now I’d like to embed or load a SWF file, create an instance and add it to the stage, is it possible?

    I tried:

    var data = new flash.utils.IDataInput(neko.io.File.read(“sweetswf.swf”,true));
    var swf = new neash.swf.SWF(data);

    but I’m getting this error:

    Error ZLib Error : -5

    and the app crashes.

    Has anyone successfully done this?

    Thanks!!

    1. Hi fluxa,
      I’m wondering if it is also a project setting and the swf file is getting somehow translated when it is placed on the device. Does the file run Ok from the desktop version of the code? I was also thinking that I have not thoroughly tested the swf code on the hxcpp target, mainly the neko target, so there may be an issue in the gzip code.

      Your error with the images looks like the selection code (which does something quite similar to rendering) interfering with the render cache.

      I’m currently working hard on a re-write of the nme code, but I will have a look at the swf/gzip code because that will be the same in the new version. The selection however will be a bit different, so I don’t want to spend too much time on it. If you can send me a little bit of source code, I’ll see if there is anything dumb I’m doing there. You can email me at “huge” at this site.

  72. Hi again,

    I have this simple example where I’m loading a png using the Loader class, add it to the stage with a enter frame listener which moves the displayobject around the stage.

    I’ve noticed if I make a mouse event (any, i.e: touch the screen) all the moving objects on the stage get distorted.

    some screenshots here: http://tinyurl.com/y9jfyhf

    I’m not sure but I think this is related with the GetObject method called in the DisplayObject class (neash.display.DisplayObject).

  73. Could someone tell me what I might have done wrong? HaxeTest was buil, but linking with c++ gives this error.
    Thanks

    Undefined symbols:
    “_start_thread”, referenced from:
    thread_create(_value*, _value*)in libstd.iphonesim.a(Thread.o)
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    “_start_thread”, referenced from:
    thread_create(_value*, _value*)in libstd.iphonesim.a(Thread.o)
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    Build failed (1 error)

  74. Hello Huge,

    I’ve recently got to this post, and started following the instructions right away. Unfortunately I didn’t manage to build properly.

    It took a bit of time, but I’ve read all the comments in this post.

    I had one of the errors(#include , which I changed to #include ), but I am stuck pretty at step 12, running the shell script.

    My original error log is posted here: http://pastebin.com/MyeMH7Sv.
    Then I finished reading all the posts and updated(well, reinstalled) haxe, as I had an older version.

    Here is my current setup:

    OSX 10.5.8, Simulator IPhoneOS 2.2.1, haXe Compiler 2.05(used to be Haxe Compiler 2.02),
    hxcpp 2,05,1 – neash 1,0,3 – nme 1,0,2 and I’ve exported the sdl off svn (revision 31) and linked the *.a files inside lib/iPhone

    with this updated setup I get different errors, all posted here: http://pastebin.com/MyeMH7Sv

    I tried with both if [“$CURRENT_ARCH”=”i386”] and if [“$CURRENT_ARCH”==”i386”] and didn’t make much difference, then I added tabs to like this if [ “$CURRENT_ARCH”=”i386” ]
    and got read of that, and figured out that spaces/tabs do matter, if is matched by fi, enclosing then and else, I think.

    I have no prior experience with bash, c++, just some actionscript 3.

    After the target script(CompileHaxe) got fixed, everything seemed to run (fan went crazy on the macbook for a bit), got some warning, the file got compiled, but it won’t run in the simulator(#
    [Session started at 2010-04-01 01:27:52 +0100.]
    #
    Failed to launch simulated application: Unknown error.).

    Here is the latest log: http://pastebin.com/n9Dm2YiK

    Where am I going wrong ?

  75. =) i can’t understand most of the things your saying but if your working on getting .swf and flash files and code to run on an iphone goodluck =) that would be wonderful

  76. Hi scorpion,
    I think you may have an old version of HXCPP, since the thread stuff has been moved from libstd.iphonesim.a. If you have the latest version, you may need to check that your project refers to the correct libraries.

    Hugh

  77. Hi George,
    It looks like you are missing the “HaxeTest.iphonesim.a” and the “HaxeTest.iphoneos.a” library files in your project. These should have been generated in when you ran the haxe compiler. ie, Check step 12.

    Hugh

  78. Thanks Huge,

    I’m all set now, I’ve just dragged the generated .a files(HaxeTest.iphonesim.a and HaxeTest.iphoneos.a) to the main project and recompiled, all good.

    Whenever I need to update, I wipe HaxeTest.iphonesim.a and compile twice(the first time the .a file gets generated again, but for some isn’t ready at the right time probably, because the app crashes in the simulator, the second time I compile, it’s all good)

    Thanks again!

  79. Hi Hugh!

    Thanks for the great work! I just got my first hxcpp project up and running on the iPhone.

    I’m now trying to load assets from a resource SWF into my application. (Or even an SWF itself, for starters.)

    I have googled all over the place and reread all the hxcpp and iPhone tutorials. I’ve tried everything I have encountered, but to no avail. Fluxa’s post didn’t work for me either.

    How can I load a MovieClip asset from a resource SWF into my application?

    Any pointers are greatly appreciated!

    Cheers,
    Timo

  80. All right, I think Hugh could make some updates to this post to share with us all things that you helped here on the comments. I think just the post don’t explain everything by itself to make it work.

    Now I’ve got 120 errors when link, I’ve got all latest libraries versions and I am using 3.1.3 sdk/simulator.

    cheers!

  81. it appears that the Error ZLib Error : -5 has something to do with how the swf has been compiled. Do you have any ideas? the Circle1.swf loads fine on to device and Sim but not some other SWFs I’ve tried.

  82. Hello, if any of you are like me and have just downloaded the new iOS 4 SDK and you can’t get this to go then after much trial and error the easiest method seems to be to use previous version – it seems as Hugh mentioned earlier that the gcc has been ‘optimised’ and will struggle to produce an iphoneos.a library. (At least I couldn’t get it it go anyway)

    here’s a link to iPhone SDK 3.2 Final with Xcode 3.2.2
    http://developer.apple.com/iphone/download.action?path=/iphone/iphone_sdk_3.2__final/xcode_3.2.2_and_iphone_sdk_3.2_final.dmg

    there’s more here:
    http://iphonesdkdev.blogspot.com/2010/04/old-versions-of-iphone-sdk.html

    don’t forget to edit BuildCommon.xml in /usr/lib/haxe/lib/hxcpp/2.05.x/build-tool and change IPHONE_VER to match the 3.0 SDK, tho if you’re upgrading you’ll probably still the 2.2.1 in there too!

    Good luck!

    1. Hi Alec,
      The nme version 2 stuff does not use SDL, so these errors should go away. I probably do need to recompile with the latest SDK though.

      Hugh

  83. Hi, this sounds like something nice to explore. But I get stuck at step 5.. I can’t seem to add “main.cpp” because I can’t find it in the NME project. I installed hxcpp and nme and neash with haxelib. What am I doing wrong, please ?

    1. Hi,
      On iphone and android, SDL is not used. There is some code so you can use windows without SDL. In fact, you can use the “external stage” to use the library without SDL – eg inside wxwindows. So if you are keen, it is pretty easy to side-step SDL.

  84. hey guys.
    I got a problem.My nme’s version is 3.4.4 , I can’t find Main.cpp under the folder that Huge mentioned in the fifth entry.Where can i get it.Who could send me a copy of it.Thanks .
    This is my email: qiao.jia@playfish.com

Leave a Reply

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