Implement as3 and C ++ Interaction Based on alchemy (1) c ++ return array to as3

Source: Internet
Author: User

Implement as3 and C ++ Interaction Based on alchemy (1) c ++ return array to as3
  • Author: Liu Da-poechant
  • Email: zhongchao.ustc@gmail.com
  • Blog: blog.csnd.net/poechang
  • Date: March 26Th, 2012
0 use of alchemy

Please refer to the Adobe website, which is not described here.

1 C ++ code 1.1 interface functions
// Input as3_val test (void * data, as3_val ARGs) as a parameter in as3 Array {// create an array int array_size = 3; int array_c [array_size]; array_c [0] = 1; array_c [1] = 2; array_c [2] = 3; // obtain as3 array as3_val asarray = NULL from the parameter; as3_arrayvalue (ARGs, "as3valtype", & asarray); // obtain the push method of array as3_val push_function = as3_gets (asarray, "push "); // call the preceding Method for (INT I = 0; I <array_size; ++ I) {// You must create an array object and then The ray object is pushed to the result array !!! As3_val prior = as3_array ("inttype", array_c [I]); as3_call (push_function, asarray, int_to_push); as3_release (int_to_push);} reverse (push_function); Return asarray ;}
1.2 encapsulation and exposure
int main(){    AS3_Val testFun = AS3_Function(NULL, test);    AS3_Val _API = AS3_Object("test:AS3ValType", testFun);    AS3_Release(testFun);    AS3_LibInit(_API);    return 0;}  
2. use alchemy to compile C ++ into SWC
g++ test.cpp -swc -O3 -o test.swc

This process is slow.

3 as3 code
Package {import cmodule. a. clibinit; import flash. display. sprite; public class Test2 extends sprite {Private Static Var Lib: Object = NULL; Public Function Test2 (): void {var Loader: clibinit = new clibinit (); var Lib: object = loader. init (); var oldarray: array = new array (); // call the C ++ function var array: array = Lib. test (oldarray); For (var I: Int = 0; I <array. length; ++ I) {trace (array [I]) ;}}}
4. Run the as3 Program

Add the test. SWC generated above to the as3 project and add the path. Then run. My results in Flash buider are as follows:

123 [SWF] test2.swf-after decompression, It is 1,574,377 bytes [unload SWF] test2.swf

-

For more information, see "LIU Da · poechant's csdn blog": blog.csdn.net/poechant

-

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.