update
[linguofeng.github.com.git] / _posts / 2013-02-20-cocos2d-x-toluaxx.textile
blob82f3a816b2b0d500db616f7dcaea952a63d81ddf
1 ---
2 layout: post
3 title: Cocos2d-x之使用tolua++生成lua对象
4 description: 以CCScale9Sprite为例
5 categories: [archive]
6 tags: [cocos2d-x]
7 ---
9 <section>
10     <div class="page-header">
11         <h3>一、创建CCScale9Sprite.pkg</h3>
12     </div>
13 <div class="lgf-command">
14 1.保留枚举类型
15 2.删除CC_DLL,改用多继承
16 3.删除inline内联关键字
17 4.删除public等访问限定词
18 5.删除成员变量
19 6.保留static关键字
20 7.删除非public的函数
21 </div>
22 <pre class="lgf-command-description prettyprint">
23 class CCScale9Sprite : public CCNode
25     void setPreferredSize(CCSize size);
26     static CCScale9Sprite* create(const char *pszFileName);
28 </pre>
29 </section>
31 <section>
32     <div class="page-header">
33         <h3>二、修改Cocos2d.pkg</h3>
34     </div>
35 <pre class="prettyprint">
36 $#include "LuaCocos2d.h"
38 $pfile "CCScale9Sprite.pkg"
39 </pre>
40 </section>
42 <section>
43     <div class="page-header">
44         <h3>三、生成CCScale9Sprite.cpp</h3>
45     </div>
46 <div class="lgf-command">
47 执行 @./tolua++ -tCocos2d -o CCScale9Sprite.cpp Cocos2d.pkg@
48 把生成的内容拷贝到LuaCocos2d.cpp,注意位置
49 </div>
50 <pre class="lgf-command-description prettyprint">
51 // static void tolua_reg_types (lua_State* tolua_S)
52 tolua_usertype(tolua_S,"CCScale9Sprite");
54 /* method: setPreferredSize of class  CCScale9Sprite */
55 #ifndef TOLUA_DISABLE_tolua_Cocos2d_CCScale9Sprite_setPreferredSize00
56 static int tolua_Cocos2d_CCScale9Sprite_setPreferredSize00(lua_State* tolua_S)
58 #ifndef TOLUA_RELEASE
59  tolua_Error tolua_err;
60  if (
61      !tolua_isusertype(tolua_S,1,"CCScale9Sprite",0,&tolua_err) ||
62      (tolua_isvaluenil(tolua_S,2,&tolua_err) || !tolua_isusertype(tolua_S,2,"CCSize",0,&tolua_err)) ||
63      !tolua_isnoobj(tolua_S,3,&tolua_err)
64  )
65   goto tolua_lerror;
66  else
67 #endif
68  {
69   CCScale9Sprite* self = (CCScale9Sprite*)  tolua_tousertype(tolua_S,1,0);
70   CCSize size = *((CCSize*)  tolua_tousertype(tolua_S,2,0));
71 #ifndef TOLUA_RELEASE
72   if (!self) tolua_error(tolua_S,"invalid 'self' in function 'setPreferredSize'", NULL);
73 #endif
74   {
75    self->setPreferredSize(size);
76   }
77  }
78  return 0;
79 #ifndef TOLUA_RELEASE
80  tolua_lerror:
81  tolua_error(tolua_S,"#ferror in function 'setPreferredSize'.",&tolua_err);
82  return 0;
83 #endif
85 #endif //#ifndef TOLUA_DISABLE
87 /* method: create of class  CCScale9Sprite */
88 #ifndef TOLUA_DISABLE_tolua_Cocos2d_CCScale9Sprite_create00
89 static int tolua_Cocos2d_CCScale9Sprite_create00(lua_State* tolua_S)
91 #ifndef TOLUA_RELEASE
92  tolua_Error tolua_err;
93  if (
94      !tolua_isusertable(tolua_S,1,"CCScale9Sprite",0,&tolua_err) ||
95      !tolua_isstring(tolua_S,2,0,&tolua_err) ||
96      !tolua_isnoobj(tolua_S,3,&tolua_err)
97  )
98   goto tolua_lerror;
99  else
100 #endif
102   const char* pszFileName = ((const char*)  tolua_tostring(tolua_S,2,0));
103   {
104    CCScale9Sprite* tolua_ret = (CCScale9Sprite*)  CCScale9Sprite::create(pszFileName);
105     tolua_pushusertype(tolua_S,(void*)tolua_ret,"CCScale9Sprite");
106   }
108  return 1;
109 #ifndef TOLUA_RELEASE
110  tolua_lerror:
111  tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err);
112  return 0;
113 #endif
115 #endif //#ifndef TOLUA_DISABLE
117 // TOLUA_API int tolua_Cocos2d_open (lua_State* tolua_S)
118   tolua_cclass(tolua_S,"CCScale9Sprite","CCScale9Sprite","CCNode",NULL);
119   tolua_beginmodule(tolua_S,"CCScale9Sprite");
120    tolua_function(tolua_S,"setPreferredSize",tolua_Cocos2d_CCScale9Sprite_setPreferredSize00);
121    tolua_function(tolua_S,"create",tolua_Cocos2d_CCScale9Sprite_create00);
122   tolua_endmodule(tolua_S);
123 </pre>
124 </section>
126 <section>
127     <div class="page-header">
128         <h3>四、修改LuaCocos2d.h,因为CCScale9Sprite对象在Cocos2d-x的扩展包中</h3>
129     </div>
130 <pre class="prettyprint">
131 #include "cocos-ext.h"
132 using namespace cocos2d::extension;
133 </pre>
134 </section>
136 <section>
137     <div class="page-header">
138         <h3>五、使用</h3>
139     </div>
140 <pre class="prettyprint">
141 local sprite = CCScale9Sprite:create("image.png")
142 sprite:setPreferredSize(CCSizeMake(100, 100))
143 </pre>
144 </section>