守望者--AIR技术交流

标题: Adobe FlasCC中 C++端与Flash端的函数参数(如数组和ByteArray)... [打印本页]

作者: 破晓    时间: 2015-4-7 09:47
标题: Adobe FlasCC中 C++端与Flash端的函数参数(如数组和ByteArray)...
本帖最后由 破晓 于 2015-8-26 09:15 编辑

节选自:Using C++ code forpath finding in your ActionScript game


如下为C++端接口,as3api.cpp:

  1. <font color="#000"><font face="Arial">/*
  2. ** ADOBE SYSTEMS INCORPORATED
  3. ** Copyright 2012 Adobe Systems Incorporated
  4. ** All Rights Reserved.
  5. **
  6. ** NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the
  7. ** terms of the Adobe license agreement accompanying it.  If you have received this file from a
  8. ** source other than Adobe, then your use, modification, or distribution of it requires the prior
  9. ** written permission of Adobe.
  10. */
  11. #include "pathfinder.h"
  12. #include "AS3/AS3.h"

  13. // First we mark the function declaration with a GCC attribute specifying the
  14. // AS3 signature

  15. ////** flascc_createPather **////
  16. void flascc_createPather() __attribute__((used,
  17.         annotate("as3sig:public function flascc_createPather():int"),
  18.         annotate("as3package:com.renaun.flascc_interface")));

  19. void flascc_createPather()
  20. {
  21.     // Create an instance of the Class
  22.     Pather *result;
  23.     result = (Pather *)new Pather();
  24.    
  25.     // Create int for the pointer into memory for this object instance
  26.     AS3_DeclareVar(asresult, int);
  27.     AS3_CopyScalarToVar(asresult, result);
  28.    
  29.     // return the pointer value of this object in memory
  30.     AS3_ReturnAS3Var(asresult);
  31. }

  32. ////** flascc_deletePather **////
  33. void flascc_deletePather() __attribute__((used,
  34.       annotate("as3sig:public function flascc_deletePather(self):void"),
  35.       annotate("as3package:com.renaun.flascc_interface")));

  36. void flascc_deletePather()
  37. {
  38.     Pather *arg1 = (Pather *) 0 ;
  39.     AS3_GetScalarFromVar(arg1, self);
  40.     delete arg1;
  41.     AS3_ReturnAS3Var(undefined);
  42. }


  43. ////** flascc_setMap **////
  44. void flascc_setMap() __attribute__((used,
  45.      annotate("as3sig:public function flascc_setMap(self, buffer:int, colSize:int, rowSize:int):void"),
  46.      annotate("as3package:com.renaun.flascc_interface")));

  47. void flascc_setMap()
  48. {
  49.     Pather *arg1 = (Pather *) 0 ;
  50.     unsigned char *arg2 = (unsigned char *) 0 ;
  51.     int arg3;
  52.     int arg4;
  53.     // convert arguments
  54.     AS3_GetScalarFromVar(arg1, self);
  55.     AS3_GetScalarFromVar(arg2, buffer);
  56.     AS3_GetScalarFromVar(arg3, colSize);
  57.     AS3_GetScalarFromVar(arg4, rowSize);
  58.     // apply to object
  59.     (arg1)->setMap((unsigned char *)arg2, arg3, arg4);
  60.     // return void
  61.     AS3_ReturnAS3Var(undefined);
  62. }



  63. ////** flascc_getPath **////
  64. void flascc_getPath() __attribute__((used,
  65.       annotate("as3sig:public function flascc_getPath(self, sx:int, sy:int, nx:int, ny:int):int"),
  66.       annotate("as3package:com.renaun.flascc_interface")));

  67. void flascc_getPath()
  68. {
  69.     Pather *arg1 = (Pather *) 0 ;
  70.     int arg2;
  71.     int arg3;
  72.     int arg4;
  73.     int arg5;
  74.     int* result;
  75.     // convert arguments
  76.     AS3_GetScalarFromVar(arg1, self);
  77.     AS3_GetScalarFromVar(arg2, sx);
  78.     AS3_GetScalarFromVar(arg3, sy);
  79.     AS3_GetScalarFromVar(arg4, nx);
  80.     AS3_GetScalarFromVar(arg5, ny);
  81.     // apply to object
  82.     result = (arg1)->getPath(arg2, arg3, arg4, arg5);
  83.     // return Number
  84.     AS3_DeclareVar(asresult, int);
  85.     AS3_CopyScalarToVar(asresult, result);
  86.     AS3_ReturnAS3Var(asresult);
  87. }
  88. </font></font>
复制代码


如下为AS3接口,详细给出了C++与Flash传递ByteArray及Array的步骤:


  1. <font color="#000"><font face="Arial">package com.renaun
  2. {
  3. import com.renaun.flascc_interface.*;

  4. import com.renaun.flascc.CModule;
  5. import com.renaun.flascc.ram;

  6. import flash.utils.ByteArray;

  7. public class PathFinder
  8. {
  9.         public function PathFinder(map:ByteArray, colSize:int, rowSize:int):void
  10.         {
  11.                 objectPtr = flascc_createPather();
  12.                 mapColSize = colSize;
  13.                 mapRowSize = rowSize;
  14.                 if (map.length != colSize * rowSize)
  15.                         throw new Error("Map size doesn't equal col * row size");
  16.                 // Setup bytes to be written to Flascc memory
  17.                 map.position = 0;
  18.                 mapByteArrayPtr = CModule.malloc(map.length);
  19.                 CModule.writeBytes(mapByteArrayPtr, map.length, map);
  20.                 flascc_setMap(objectPtr, mapByteArrayPtr, colSize, rowSize);
  21.         }
  22.         
  23.         public var mapColSize:int;
  24.         public var mapRowSize:int;
  25.         private var objectPtr:int;
  26.         public var mapByteArrayPtr:int;
  27.         
  28.         public function getMap():ByteArray
  29.         {
  30.                 var mapBytes:ByteArray = new ByteArray();
  31.                 ram.position = mapByteArrayPtr;
  32.                 ram.readBytes(mapBytes, 0, mapColSize * mapRowSize);
  33.                 return mapBytes;
  34.         }
  35.         
  36.         public function getPath(sx:int, sy:int, nx:int, ny:int):Array
  37.         {
  38.                 var pathPtr:int = flascc_getPath(objectPtr, sx, sy, nx, ny);
  39.                
  40.                 ram.position = pathPtr;
  41.                 var result:int = ram.readShort();
  42.                 var size:int = ram.readShort();
  43.                 var pathXYs:Array = [];
  44.                 if (size > 0)
  45.                 {
  46.                         ram.readShort();
  47.                         ram.readShort();
  48.                         for (var j:int = 1; j < size; j++)
  49.                         {
  50.                                 pathXYs.push(ram.readShort());
  51.                                 pathXYs.push(ram.readShort());
  52.                         }
  53.                 }
  54.                 //CModule.free(pathPtr);
  55.                 return pathXYs;
  56.         }
  57.         
  58.         public function destroy():void
  59.         {
  60.                 CModule.free(mapByteArrayPtr);
  61.                 flascc_deletePather(objectPtr);
  62.         }
  63.         
  64. }
  65. }</font></font>
复制代码


源码:


[attach]1299[/attach]





本文来自:http://blog.csdn.net/xsolver/article/details/16839111







欢迎光临 守望者--AIR技术交流 (http://www.airmyth.com/)