Wwx2014 Talk – Hxcpp magic

Just got back from the haxe conference, wwx2014, after having a great time. Plenty of fantastic people to talk to.

The talk consists of some slides, with a demo at the end. To run the demo, you need to run the binary version on either mac or windows, but the slides can be viewed with flash.

Or, you can build it yourself for you preferred platform and explore the source code with:

haxelib install nme
haxelib install gm2d
haxelib run nme demo gm2d 10-wwx2014 cpp

Edit – must mention that you might need an dev version of haxe to build the demo.

9 Replies to “Wwx2014 Talk – Hxcpp magic”

  1. Hi,
    You need a more recent version of haxe. There has not been an official release of haxe since wwx, so you will need to get hold of an overnight build/package, or build it from source.

  2. Dear Huge,

    It *seems* I was using the either the latest haxe from the haxe git OR the the latest haxe from automated builds.

    And still, I wasn’t able to build the sample “demo gm2d 10-wwx2014” you put above, there were plenty of errors on LINUX. I was only able to build the trivial “trace(“Hello world”)” sample on linux c++.

    (I need to verify my above statement though.)

    However, you don’t mention Linux as one of possible targets, at the above posting.

    I am currently considering studying the ocaml and c++ backend codebase, …though I am not sure I will actually study, as my desire is not that steady at the time. With the purpose of developing c++ support on linux target…

  3. Hi Huge,

    My system is Linux:

    $ lsb_release -dc
    Description: Ubuntu 14.04.1 LTS
    Codename: trusty
    $ uname -a
    Linux ssd-ubuntu14-04 3.13.0-33-generic #58-Ubuntu SMP Tue Jul 29 16:47:17 UTC 2014 i686 i686 i686 GNU/Linux

    I fetched the latest haxe from git according to instructions on http://haxe.org/documentation/introduction/building-haxe.html , built it, installed. Tried to build the “demo gm2d 10-wwx2014” sample, build failed. The complete log is at http://paste.lisp.org/display/143394 .

  4. Hi Huge

    I want to use hxcpp magic to retrieve float or int values from a (nme/openfl) ndll. It is C++ for desktop target. Usually, it can be done using “get” functions and returning them (through CFFI), or, as alternative, using a callback event. However, I would like to access them for reading natively, similar to using “untyped __global__”. This is because I want to retrieve this values very often. I appreciate any help on how to do this.

    1. Hi
      You can pass a native pointer from your ndll to your cpp code using the data member of alloc_abstract. This could be a pointer to static, or some member data.
      You can then access this pointer using the cpp.Pointer<Float> stucture. Then using “.ref” should give you 100% fast access to the pointer data. If it is member data, you will need to be careful to manage the lifetime of the data, because cpp.Pointer will not hold a GC reference to anything.

  5. Thanks for your answer. What is the “kind” in the first argument of alloc_abstract ? Do I need to make a class, or structure, or the it can be a “plain” pointer to float?

  6. ‘kind’ is just an id you can use to tell one type of abstract from another. Use alloc_kind to get one. In NME, I alloc one kind and use it for everything.
    alloc_abstract is usually used for managing external data with GC. Typically, you would also use ‘val_gc’ which adds a finalizer, which gets called when there are no more GC references to the abstract. You would normally ‘free’ the data in this callback. But you do not have to if it is a pointer to some static memory that does not need freeing. So your data can be a pointer to float if you like.
    You can access the data in cpp via the cpp.Pointer.fromHandle, passing the the abstract returned from your ndll. Here you can supply the ‘kind’ name if you like (which comes if you use ‘kind_share’ instread of ‘alloc_kind’) and hxcpp will check the type – but you can ignore this if you like. Since the cpp.Pointer class takes a template-type, the returned data will be cast to this type, and when you use “.ref”, you have access to your data. Keep in mind that hxcpp ‘Float’ are usually “double”, not “float”, so you may need to use cpp.Float32, or just use doubles in you ndll.

  7. Thanks I got it working now. Had to update to latest Haxe and hxcpp. I am getting the same value using “.ref” and “.value”. I was using floats in cpp so your comment for using “double”s was very useful.
    What I do is getting a cpp.Pointer in my constructor and just use it in the “update” function without having to call my ndll again which is great.

Leave a Reply

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