Creating AS3 code for C++ classes converted with Alchemy (a là Box2D)

531 views Asked by At

So I've a collection of C++ classes which I now have converting fine with Alchemy to a swc file and can call the exposed functions fine from my AS3 code.

What I'd really like to do is recreate stuff like Box2D's b2Vec.as class,

public class b2Vec2 extends b2Base {

    public function b2Vec2(p:int) {
        _ptr = p;
    }

    public function get v2():V2 {
        return new V2(x, y);
    }

    public function set v2(v:V2):void {
        x = v.x;
        y = v.y;
    }

    public function get x():Number { return mem._mrf(_ptr + 0); }
    public function set x(v:Number):void { mem._mwf(_ptr + 0, v); }
    public function get y():Number { return mem._mrf(_ptr + 4); }
    public function set y(v:Number):void { mem._mwf(_ptr + 4, v); }

}

This is a simple example, but shows what I'd like to do. On the C side of things, b2Vec2 is a struct,

/// A 2D column vector.
struct b2Vec2
{
    /// Default constructor does nothing (for performance).
    b2Vec2() {}

    ....

    float32 x, y;
};

So, for this struct, it's easy to calculate that the first variable of a b2Vec2 object is a float, which will be the value in x and can be read via Alchemy's MemUser classes _mrf (read a fload from a point in memory) with _mrf(pointerAddress) and you can read in the second float with _mrf(pointerAddress + 4).

My question is, if you're not a C++ expert (me), is there any way to get the definition of a class, as in the addresses of all the variables within and what they are? So, for the b2Vec2 one, I'd imaging it'd be something like, float32 x 0 float34 y 4 ...

The reason I'm asking is because one of the classes in particular has loads of variables and to try and get each and every one's information so I can access it directly from the AS3 code would be lots of work and I'm going to assume I'll introduce plenty of human error to it.

2

There are 2 answers

0
seaders On BEST ANSWER

I actually got a good enough answer from the WCK guy, Jesse Sternberg. He got most of his AS3 classes that are paired with their C++ counterparts are done automatically during the build process in his own 'probe' folder. In there, there's one Box2D.c class that defines a load of templates at the top, then in the main function, probes classes for member variables and that prints out the relevant AS3 code for accessing them from that side.

It's a little bit funky to get used to, but certainly very handy and much better than doing it manually!

0
Guy Sirton On

The very general C++ answer is no. During run-time you can not discover what are the member variables of a particular struct.

How can I add reflection to a C++ application?

I don't know what the Alchemy compiled code looks like though. You can build some tool/preprocessor to generate the C++ classes or structs from some other representation and then that tool can also generate your AS3 accessors - that would be a way of eliminating the user error and some of the work. It might be tricky because figuring out the address of a member variable is not always that simple.

If you can call member functions in the C++ class why don't you move your get/set functions there and call those?