Search code examples
adobeswcalchemy

flash alchemy: passing array of floats


Doesn't work:

             var b:ByteArray = new ByteArray();
             b.writeFloat(-50.000000);
             b.position = 0;


             cpp.processFloat(b,b.bytesAvailable);

CPP:

static AS3_Val processFloat(void* self, AS3_Val args){
unsigned iTestSize;
AS3_Val sOrigFile = AS3_Undefined();

AS3_ArrayValue( args, "AS3ValType, IntType", &sOrigFile, &iTestSize );

float * sFile = (float *)malloc(sizeof(float) * (iTestSize + 1));

int res = AS3_ByteArray_readBytes(sFile, sOrigFile, iTestSize);

fprintf( stderr, "** processFloat() size: %i sFile: %.03f, res:%i\n", iTestSize, sFile, res );

fprintf ouput: ** processFloat size: 4 sFile: 0.000, res:4

What's wrong? How can I pass array of floats to swc from flash?


Solution

  • Your problem is byte-ordering (more details here). You need to set the ByteArray to Little Endian:

    var b:ByteArray = new ByteArray();
    b.endian= Endian.LITTLE_ENDIAN;
    b.writeFloat(-50.000000);
    b.position = 0;
    
    cpp.processFloat(b,b.bytesAvailable);
    

    Also, your iTestSize is confused. You pass in bytesAvailable but then you multiply it by sizeof(float) (and add 1??).