如何配置tolua框架++

>> tolua++.h
tolua++.h ( 文件浏览 )
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Apr 2003
** $Id: tolua++.h -27 05:27:27Z jsalmon3 $
/* This c you can redistribute it and/or modify it.
** The software provided hereunder is on an &as is& basis, and
** the author has no obligation to provide maintenance, support, updates,
** enhancements, or modifications.
#ifndef TOLUA_H
#define TOLUA_H
#ifndef TOLUA_API
#define TOLUA_API extern
#define TOLUA_VERSION &tolua++-1.0.92&
#ifdef __cplusplus
extern &C& {
#define tolua_pushcppstring(x,y) tolua_pushstring(x,y.c_str())
#define tolua_iscppstring tolua_isstring
#define tolua_iscppstringarray tolua_isstringarray
#define tolua_pushfieldcppstring(L,lo,idx,s) tolua_pushfieldstring(L, lo, idx, s.c_str())
#define TEMPLATE_BIND(p)
#define TOLUA_TEMPLATE_BIND(p)
#define TOLUA_PROTECTED_DESTRUCTOR
#define TOLUA_PROPERTY_TYPE(p)
typedef int lua_O
#include &lua.h&
#include &lauxlib.h&
struct tolua_Error
const char*
typedef struct tolua_Error tolua_E
#define TOLUA_NOPEER LUA_REGISTRYINDEX /* for lua 5.1 */
TOLUA_API const char* tolua_typename (lua_State* L, int lo);
TOLUA_API void tolua_error (lua_State* L, char* msg, tolua_Error* err);
TOLUA_API int tolua_isnoobj (lua_State* L, int lo, tolua_Error* err);
TOLUA_API int tolua_isvalue (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isboolean (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isnumber (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isstring (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_istable (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isusertable (lua_State* L, int lo, const char* type, int def, tolua_Error* err);
TOLUA_API int tolua_isuserdata (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isusertype (lua_State* L, int lo, const char* type, int def, tolua_Error* err);
TOLUA_API int tolua_isvaluearray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isbooleanarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isnumberarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isstringarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_istablearray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isuserdataarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isusertypearray
(lua_State* L, int lo, const char* type, int dim, int def, tolua_Error* err);
TOLUA_API void tolua_open (lua_State* L);
TOLUA_API void* tolua_copy (lua_State* L, void* value, unsigned int size);
TOLUA_API int tolua_register_gc (lua_State* L, int lo);
TOLUA_API int tolua_default_collect (lua_State* tolua_S);
TOLUA_API void tolua_usertype (lua_State* L, char* type);
TOLUA_API void tolua_beginmodule (lua_State* L, char* name);
TOLUA_API void tolua_endmodule (lua_State* L);
TOLUA_API void tolua_module (lua_State* L, char* name, int hasvar);
TOLUA_API void tolua_class (lua_State* L, char* name, char* base);
TOLUA_API void tolua_cclass (lua_State* L, char* lname, char* name, char* base, lua_CFunction col);
TOLUA_API void tolua_function (lua_State* L, char* name, lua_CFunction func);
TOLUA_API void tolua_constant (lua_State* L, char* name, double value);
TOLUA_API void tolua_variable (lua_State* L, char* name, lua_CFunction get, lua_CFunction set);
TOLUA_API void tolua_array (lua_State* L,char* name, lua_CFunction get, lua_CFunction set);
/* TOLUA_API void tolua_set_call_event(lua_State* L, lua_CFunction func, char* type); */
/* TOLUA_API void tolua_addbase(lua_State* L, char* name, char* base); */
TOLUA_API void tolua_pushvalue (lua_State* L, int lo);
TOLUA_API void tolua_pushboolean (lua_State* L, int value);
TOLUA_API void tolua_pushnumber (lua_State* L, double value);
TOLUA_API void tolua_pushstring (lua_State* L, const char* value);
TOLUA_API void tolua_pushuserdata (lua_State* L, void* value);
TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type);
TOLUA_API void tolua_pushusertype_and_takeownership(lua_State* L, void* value, const char* type);
TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v);
TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v);
TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, double v);
TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v);
TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v);
TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type);
TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type);
TOLUA_API double tolua_tonumber (lua_State* L, int narg, double def);
TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def);
TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def);
TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def);
TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def);
TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def);
TOLUA_API double tolua_tofieldnumber (lua_State* L, int lo, int index, double def);
TOLUA_API const char* tolua_tofieldstring (lua_State* L, int lo, int index, const char* def);
TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def);
TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def);
TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def);
TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def);
TOLUA_API void tolua_dobuffer(lua_State* L, char* B, unsigned int size, const char* name);
TOLUA_API int class_gc_event (lua_State* L);
#ifdef __cplusplus
static inline const char* tolua_tocppstring (lua_State* L, int narg, const char* def) {
const char* s = tolua_tostring(L, narg, def);
return s?s:&&;
static inline const char* tolua_tofieldcppstring (lua_State* L, int lo, int index, const char* def) {
const char* s = tolua_tofieldstring(L, lo, index, def);
return s?s:&&;
#define tolua_tocppstring tolua_tostring
#define tolua_tofieldcppstring tolua_tofieldstring
TOLUA_API int tolua_fast_isa(lua_State *L, int mt_indexa, int mt_indexb, int super_index);
#ifdef __cplusplus
展开> <收缩
下载源码到电脑,阅读使用更方便
还剩0行未阅读,继续阅读 ▼
Sponsored links
源码文件列表
温馨提示: 点击源码文件名可预览文件内容哦 ^_^
42.00 B03-12-06 13:42
config.h.in1.49 kB11-26-06 16:27
195.63 kB03-01-07 01:13
7.52 kB03-01-07 01:12
1.05 kB10-12-06 22:49
doxygen-stratagus.cfg45.26 kB12-17-06 13:21
bg.po8.43 kB12-16-06 14:50
de.po7.99 kB12-16-06 14:50
fi.po7.86 kB12-16-06 14:50
fr.po7.67 kB12-16-06 14:50
pl.po7.81 kB12-16-06 14:50
stratagus.pot6.17 kB12-16-06 14:50
poweredby.png9.38 kB03-12-06 13:42
stratagus.ico2.19 kB03-12-06 13:42
COPYING17.90 kB03-12-06 13:42
85.52 kB03-05-07 14:34
9.46 kB03-01-07 01:12
2.73 kB03-01-07 01:12
19.73 kB03-12-06 13:42
coast_and_ground.png2.39 kB03-12-06 13:42
dark_coast.png2.31 kB03-12-06 13:42
dark_ground.png1.54 kB03-12-06 13:42
defect_human_wall.png7.43 kB03-12-06 13:42
defect_orc_wall.png7.54 kB03-12-06 13:42
destroyed_wall.png5.05 kB03-12-06 13:42
farm.png2.15 kB03-12-06 13:42
fog_of_war.png1.98 kB03-12-06 13:42
forest_and_ground.png3.58 kB03-12-06 13:42
human_wall.png1.93 kB03-12-06 13:42
light_and_dark_coast.png2.15 kB03-12-06 13:42
light_and_dark_ground.png2.38 kB03-12-06 13:42
light_and_dark_water.png2.77 kB03-12-06 13:42
light_coast.png2.33 kB03-12-06 13:42
light_ground.png2.06 kB03-12-06 13:42
orc_wall.png1.93 kB03-12-06 13:42
2.54 kB03-12-06 13:42
peasant.png22.18 kB03-12-06 13:42
rocks_and_coast.png3.73 kB03-12-06 13:42
stratagus.gimp2.97 kB03-12-06 13:42
summer.png35.49 kB03-12-06 13:42
summer_klein.png5.79 kB03-12-06 13:42
7.23 kB03-12-06 13:42
water_and_coast.png2.41 kB03-12-06 13:42
3.59 kB05-02-06 16:05
3.39 kB03-01-07 01:12
4.88 kB03-01-07 01:12
3.09 kB03-01-07 01:12
5.37 kB03-12-06 13:42
438.00 B03-12-06 13:42
18.83 kB10-28-06 21:19
34.59 kB11-18-06 23:02
1.36 kB03-12-06 13:42
35.79 kB10-28-06 21:19
20.17 kB11-18-06 23:02
25.12 kB10-28-06 21:19
692.00 B03-12-06 13:42
5.72 kB10-28-06 21:19
5.42 kB10-28-06 21:19
9.06 kB10-28-06 21:19
4.48 kB10-28-06 21:19
494.00 B03-12-06 13:42
9.04 kB10-28-06 21:19
7.15 kB10-28-06 21:19
14.67 kB11-05-06 16:25
38.27 kB10-28-06 21:19
34.02 kB10-28-06 21:19
9.60 kB03-04-07 17:12
913.00 B03-12-06 13:42
Rules.make.in960.00 B11-26-06 16:27
SConstruct7.40 kB03-01-07 01:12
21.21 kB01-09-07 00:05
15.34 kB01-07-07 21:28
6.46 kB03-12-06 13:42
15.44 kB01-12-07 21:25
3.69 kB11-05-06 16:25
7.42 kB11-05-06 20:26
7.04 kB11-08-06 22:59
4.24 kB11-08-06 23:39
8.16 kB03-12-06 13:42
3.77 kB11-19-06 22:28
22.47 kB11-18-06 23:02
3.56 kB03-12-06 13:42
7.12 kB01-13-07 17:08
2.14 kB06-13-06 19:33
8.12 kB03-05-07 14:34
6.35 kB11-19-06 22:28
11.21 kB10-12-06 22:49
6.00 kB11-19-06 22:28
32.60 kB01-13-07 17:43
Module.make1.89 kB03-12-06 13:42
30.39 kB12-17-06 13:21
15.19 kB12-16-06 14:50
15.36 kB11-18-06 23:02
11.00 kB01-07-07 21:28
2.97 kB03-12-06 13:42
16.54 kB03-18-06 18:14
31.98 kB11-19-06 22:28
Module.make1.60 kB03-12-06 13:42
35.76 kB01-07-07 21:28
1.43 kB03-12-06 13:42
Module.make1.48 kB03-12-06 13:42
56.99 kB11-19-06 22:28
2.92 kB03-12-06 13:42
18.45 kB03-12-06 13:42
Module.make1.52 kB03-12-06 13:42
3.07 kB11-19-06 01:07
18.60 kB01-14-07 19:15
5.35 kB12-22-06 13:18
Module.make1.52 kB09-24-06 15:35
9.85 kB02-21-07 18:43
19.08 kB03-04-07 11:53
4.38 kB03-12-06 13:42
5.76 kB03-12-06 13:42
4.68 kB03-12-06 13:42
4.62 kB03-12-06 13:42
15.84 kB11-23-06 22:28
4.51 kB11-24-06 21:25
6.04 kB03-12-06 13:42
9.93 kB09-16-06 18:54
3.80 kB03-12-06 13:42
5.05 kB03-12-06 13:42
10.78 kB03-12-06 13:42
5.12 kB03-12-06 13:42
4.54 kB03-12-06 13:42
4.07 kB03-12-06 13:42
5.65 kB03-12-06 13:42
5.07 kB03-12-06 13:42
6.83 kB03-12-06 13:42
5.16 kB03-12-06 13:42
6.76 kB03-12-06 13:42
8.93 kB11-23-06 22:28
5.66 kB03-12-06 13:42
11.42 kB03-12-06 13:42
4.03 kB03-12-06 13:42
7.91 kB10-12-06 22:49
6.76 kB03-12-06 13:42
9.13 kB03-12-06 13:42
7.00 kB03-12-06 13:42
5.22 kB03-12-06 13:42
8.34 kB11-24-06 19:03
5.14 kB03-12-06 13:42
5.35 kB09-16-06 18:54
4.60 kB03-12-06 13:42
5.78 kB03-12-06 13:42
7.81 kB03-12-06 13:42
3.86 kB03-12-06 13:42
5.15 kB03-12-06 13:42
6.65 kB03-12-06 13:42
4.66 kB03-12-06 13:42
5.55 kB09-17-06 13:11
9.07 kB06-13-06 19:33
24.56 kB09-16-06 18:54
6.52 kB09-16-06 18:54
6.15 kB09-16-06 18:54
7.66 kB03-12-06 13:42
9.00 kB09-16-06 18:54
4.32 kB03-12-06 13:42
5.31 kB03-12-06 13:42
6.01 kB09-16-06 18:54
6.90 kB09-16-06 18:54
14.93 kB03-12-06 13:42
9.03 kB09-16-06 18:54
8.92 kB09-16-06 18:54
6.58 kB09-16-06 18:54
9.16 kB03-12-06 13:42
3.77 kB03-12-06 13:42
6.23 kB11-24-06 19:03
4.18 kB03-12-06 13:42
Module.make3.63 kB05-02-06 16:05
4.49 kB03-12-06 13:42
5.27 kB03-12-06 13:42
3.79 kB03-12-06 13:42
18.29 kB10-12-06 22:49
8.66 kB03-12-06 13:42
14.03 kB03-12-06 13:42
19.47 kB01-10-07 13:01
9.76 kB09-16-06 18:54
7.73 kB09-16-06 18:54
12.42 kB03-12-06 13:42
20.78 kB10-29-06 16:03
4.96 kB03-12-06 13:42
6.25 kB03-12-06 13:42
8.48 kB01-12-07 16:24
9.97 kB09-16-06 18:54
38.58 kB03-12-06 13:42
12.87 kB09-16-06 18:54
17.55 kB11-26-06 16:27
10.95 kB11-26-06 16:27
14.84 kB03-13-06 00:18
8.21 kB06-13-06 19:33
3.86 kB10-28-06 21:19
3.77 kB01-05-07 17:11
6.33 kB11-19-06 01:54
6.00 kB11-19-06 00:50
6.65 kB11-19-06 00:50
4.69 kB11-19-06 01:39
4.76 kB10-12-06 22:49
6.47 kB12-09-06 00:15
4.82 kB11-19-06 01:07
15.38 kB11-19-06 01:39
2.37 kB03-12-06 13:42
4.21 kB11-19-06 01:54
16.93 kB01-12-07 18:34
2.69 kB03-12-06 13:42
2.38 kB12-09-06 00:15
2.41 kB03-12-06 13:42
19.51 kB11-19-06 13:02
Module.make2.03 kB10-12-06 22:49
3.37 kB11-19-06 22:28
6.47 kB03-12-06 13:42
10.08 kB03-01-07 01:12
8.54 kB11-19-06 02:07
6.10 kB11-19-06 02:07
4.52 kB11-10-06 13:41
18.28 kB11-19-06 13:02
1.97 kB10-12-06 22:49
9.59 kB11-19-06 01:54
1.94 kB03-12-06 13:42
4.35 kB11-18-06 23:02
6.44 kB11-19-06 22:28
5.16 kB11-19-06 22:28
14.41 kB11-19-06 22:28
10.66 kB04-09-07 01:51
10.55 kB11-20-06 00:33
2.33 kB05-02-06 16:05
3.79 kB03-12-06 13:42
24.15 kB12-09-06 00:15
39.31 kB01-05-07 17:11
3.50 kB11-19-06 13:02
34.78 kB11-19-06 22:28
4.00 kB11-19-06 01:07
7.32 kB01-09-07 14:06
3.73 kB11-26-06 16:27
14.68 kB11-19-06 22:28
2.85 kB03-12-06 13:42
9.78 kB12-16-06 14:50
18.17 kB01-12-07 16:24
10.76 kB07-08-06 22:37
22.19 kB01-12-07 18:34
5.94 kB05-02-06 16:05
4.87 kB10-12-06 22:49
10.03 kB03-12-06 13:42
20.95 kB10-12-06 22:49
Module.make1.61 kB03-12-06 13:42
14.06 kB11-22-06 11:32
25.72 kB11-20-06 00:33
3.05 kB11-20-06 00:33
49.21 kB01-14-07 18:10
18.29 kB11-19-06 02:07
7.57 kB11-19-06 22:28
Module.make1.54 kB03-12-06 13:42
55.78 kB03-05-07 14:34
35.99 kB11-26-06 16:27
22.79 kB01-06-07 22:23
Module.make1.52 kB03-12-06 13:42
7.36 kB12-22-06 20:51
3.52 kB03-12-06 13:42
6.49 kB11-18-06 23:02
Module.make1.59 kB05-05-06 21:48
3.75 kB10-12-06 22:49
10.85 kB11-08-06 23:39
9.64 kB11-19-06 22:28
14.88 kB11-19-06 22:28
4.39 kB11-19-06 22:28
19.25 kB11-19-06 22:28
5.44 kB11-19-06 13:02
9.04 kB03-12-06 13:42
45.00 B03-12-06 13:42
8.15 kB11-19-06 00:50
7.20 kB11-23-06 22:28
16.67 kB12-09-06 00:15
10.21 kB10-12-06 22:49
44.95 kB11-23-06 22:28
Module.make1.70 kB03-12-06 13:42
24.02 kB01-09-07 14:06
60.64 kB02-26-07 14:32
11.09 kB11-19-06 22:28
24.49 kB11-18-06 23:02
29.49 kB11-19-06 22:28
30.99 kB11-23-06 22:28
34.41 kB01-09-07 00:25
26.50 kB02-11-07 00:22
4.92 kB11-18-06 23:02
11.33 kB11-26-06 16:34
editor.pkg533.00 B09-04-06 14:41
font.pkg374.00 B12-09-06 00:15
game.pkg1.06 kB01-05-07 23:55
map.pkg208.00 B11-22-06 11:32
minimap.pkg122.00 B07-09-06 17:41
Module.make1.67 kB03-12-06 13:42
network.pkg1.36 kB03-05-07 14:34
player.pkg1.14 kB12-09-06 00:15
sound.pkg743.00 B11-19-06 23:54
stratagus.pkg1.93 kB02-21-07 18:43
6.65 kB02-27-07 14:59
543.09 kB03-05-07 14:34
13.44 kB02-27-07 14:59
684.00 B07-09-06 17:41
12.49 kB02-27-07 14:59
17.33 kB02-27-07 14:59
4.96 kB02-27-07 14:59
3.15 kB02-27-07 14:59
ui.pkg12.02 kB12-09-06 00:15
unit.pkg262.00 B12-09-06 00:15
unittype.pkg227.00 B12-09-06 00:15
upgrade.pkg167.00 B11-19-06 23:54
video.pkg703.00 B11-19-06 23:54
24.66 kB11-19-06 22:28
7.81 kB11-19-06 01:39
5.55 kB12-09-06 00:15
33.15 kB11-26-06 16:27
36.21 kB12-09-06 00:15
29.01 kB01-05-07 23:55
5.23 kB12-09-06 00:15
Module.make1.61 kB03-12-06 13:42
56.24 kB11-28-06 18:14
41.43 kB12-09-06 00:15
18.55 kB12-09-06 00:15
36.51 kB01-06-07 19:27
10.10 kB11-19-06 01:39
Module.make1.60 kB03-12-06 13:42
34.60 kB12-22-06 13:18
60.03 kB01-14-07 18:10
100.89 kB01-12-07 20:55
17.48 kB11-19-06 22:28
5.26 kB11-18-06 23:02
46.38 kB11-19-06 22:28
18.29 kB12-17-06 13:21
21.09 kB11-19-06 22:28
9.39 kB02-21-07 18:43
22.59 kB01-09-07 00:05
30.19 kB11-19-06 22:28
6.25 kB03-12-06 13:42
40.40 kB06-14-06 20:39
8.37 kB11-18-06 23:02
Module.make1.64 kB03-12-06 13:42
7.78 kB11-22-06 11:32
12.07 kB11-19-06 22:28
19.88 kB12-22-06 23:56
4.83 kB05-02-06 16:05
7.37 kB11-18-06 23:02
stratagus.sln887.00 B09-24-06 16:18
stratagus.vcproj91.52 kB01-06-07 13:41
Sponsored links
23 篇源代码 21 篇源代码 18 篇源代码 13 篇源代码 9 篇源代码
285 篇源代码 173 篇源代码 48 篇源代码 42 篇源代码 36 篇源代码
评价成功,多谢!
CodeForge积分(原CF币)全新升级,功能更强大,使用更便捷,不仅可以用来下载海量源代码马上还可兑换精美小礼品了
您的积分不足
支付宝优惠套餐快速获取 22 积分
10积分 / ¥100
22积分 / ¥200原价 ¥220 元
65积分 / ¥500原价 ¥650 元
支付宝订单完成后,积分将自动加入到您的账号。以下是优惠期的人民币价格,优惠期过后将恢复美元价格。
更多付款方式:、
您本次下载所消耗的积分将转交上传作者。
同一源码,30天内重复下载,只扣除一次积分。
鲁ICP备号-2
登录 CodeForge
还没有CodeForge账号?
Switch to the English version?
^_^"呃 ...
Sorry!这位大神很神秘,未开通博客呢,请浏览一下其他的吧tolua++快速入门
tolua++使用SCons进行编译,我们也可以使用自己喜欢的IDE新建一个控制台(Console)工程,然后将源码下bin目录中所有文件加入到工程中,直接编译即可;同样地,新建一个静态库(Static
Library)工程,将源码下lib目录中所有文件加入进入,编译即可。当然了,这些工程是需要lua库支持的,目前tolua++最新版本是1.0.93,支持lua
经过上面的步骤,在Windows系统上的话,你会得到tolua++.exe和tolua.lib,前者是一个辅助工具,用来将.pkg文件(下面将会讲到)转换成源代码,而后者则是我们需要在自己工程中加入的函数库。
下面开始正式使用tolua++。
一开始,我们并不需要特别注意,按照自己的需要来撰写完完全全的C++代码,假设我们有一个基于控制台的项目,名为mytest,设置好项目属性,包含必要的库(包括lua和tolua++),添加如下代码:
// file: UsingIt.h
#include &iostream&
class my_class
std::cout && “Hello World!”
好,下面撰写我们的.pkg文件:
// file: mylib.pkg
$pfile “UsingIt.h”
class my_class
my_class();
~my_class();
还记得我们的tolua++.exe吗?是时候派上用场了:
& tolua++ &n mylib &o
mylib.cpp mylib.pkg
这条命令的意思是:使用tolua++程序,从mylib.pkg生成源代码并输出到mylib.cpp,而-n参数则是用来指定包名称的(lua
中package name)。你会得到mylib.cpp,文件,其中包含一个很重要的函数:int
tolua_&package
name&_open(lua_State*)。"package name”就是用-n指定,这里就是
tolua_mylib_open(lua_State*),它的功能就是将你在.pkg文件中所撰写的类、函数接口等注册给lua,这个函数是需要你自己调用的。
回到我们的mytest项目,将上面得到的mylib.cpp文件加入到项目中,添加一个新文件Main.cpp:
// file: Main.cpp
#include &iostream&
#include &tulua++.h&
extern “C” {
#include &lua.h&
#include &luaxlib.h&
#include “UsingIt.h”
int main()
&&& lua_State* L
= lua_open();
luaL_openlibs();
tolua_mylib_open(L); // 打开mylib
luaL_dofile(L, “mytest.lua”); // 执行脚本文件
lua_close(L);
&&& return
mytest.lua脚本文件内容:
local my = my_class()
my:greet()
好了,编译并运行你的mytest程序吧,你将看到 Hello World! 出现在你的屏幕上!
就说到这里了,更深入的使用请参考tolua++的文档,有一点需要注意的是,tolua++并不支持导出虚函数类供lua代码派生实现。但是这一点并非不可克服,后面我们会讲到该如何解决。
在条件允许的情况下,我更愿意选择luabind,或许你也想看看luabind的使用方法,请看《》。
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。cocos2dx 2.x版本:简化提炼tolua++绑定自定义类到lua中使用 - 推酷
cocos2dx 2.x版本:简化提炼tolua++绑定自定义类到lua中使用
cocos2dx的3.x版本已经提供了更好地绑定方式,网上有很多相关的教程,这里给一个链接:
由于目前我们的项目使用的是2.x版本的,且已经开发了一半了,这时候不适合转3.x的版本,于是只能用2.x的老方法tolua++来实现绑定。这就有一个蛋疼的问题,太麻烦!每次都要写pkg、使用tolua++.exe解释器编译生成新的cpp,在其中添加自己的类的头文件引用,然后把这个新的cpp文件覆盖到liblua中以前的,再编译库,生成新的obj,才算完事。这里给出木头大神的文章,里面写的很清楚:
可是我不想每次都这样麻烦,我希望在自己的工程里添加绑定的接口,这样每次运行时只更新我自己的项目,就可以让lua识别我的类。初一想,那就把“LuaCocos2d.cpp”这个类放到项目中如何,这个想法完全可以的,即我们新的绑定就不再添加到“LuaCocos2d.cpp”这里了,而是单独生成一个新的、只有我们自己的的绑定类接口的一个类放到工程中。好了,废话不多说,看流程
1】首先在我们自己的项目中创建一个文件夹,专门存放对lua的绑定接口类以及相关文件:LuaAPI
2】创建一个pkg文件,这个文件我们只创建一个,作为基础调用,用来给tolua++使用的,我们待会会详细说明
3】将cocos2d-x-2.2.3\tools\tolua++目录下的三个文件拷贝到1】中创建的文件里,这是工具,用来生成用的,这个不能少
这些文件的作用,在相关绑定的文章里有详细介绍,这里稍微提一下:
① basic.lua里面规定了tolua++解释器在解析pkg文件时候的附加规则
build.sh脚本命令,用来启动tolua++解释器解析pkg生成我们需要的那个cpp文件。
这里需要改动,因为拷贝了一份在这个目录下,所以
都变了,这里要注意。
#!/bin/bash
# Invoked build.xml, overriding the lolua++ property
SCRIPT_DIR=$(cd &$(dirname &${BASH_SOURCE[0]}&)& && pwd)
TOLUA=`D:\cocos2d-x-2.2.3\projects\actgamelua\Classes\LuaAPI`
if [ -z &${TOLUA}& ]; then
TOLUA=`D:\cocos2d-x-2.2.3\projects\actgamelua\Classes\LuaAPI`
if [ -z &${TOLUA}& ]; then
echo &Unable to find tolua++ (or tolua++5.1) in your PATH.&
cd ${SCRIPT_DIR}
${TOLUA} -L basic.lua -o &api/LuaAPI.cpp& API.pkg
这里有个&api/LuaAPI.cpp&,这是我们指定的要生成的cpp文件,即生成LuaAPI.cpp放在“api/”目录下(在我们创建的LuaAPI文件夹下)
③ build.bat批处理程序,实际上就是执行build.sh里的设置,同样的,路径要改一下:
tolua++ -L basic.lua -o &api/LuaAPI.cpp& API.pkg
④tolua++.exe解释器,用来解析pkg生成cpp的
4】此时,工具已备齐,命令也写好了,接下来我们要定义一个类,这个类是类似于pkg的描述文件,只不过以后我们的绑定类都按照pkg的格式写在这个里边,而不是写好多的pkg,并添加到cocos2d.pkg中。
在api目录下创建一个头文件:PkgDiscription.h
#ifndef _TEST_PROPERTY_H__
#define _TEST_PROPERTY_H__
#include &cocos2d.h&
USING_NS_CC;
*下面&tolua_begin& and &tolua_end& 这两行注释一定不能少,因为它们是识别的开始和结束
// tolua_begin
//@pkg1_begin 第一个pkg描述文件开始
class MyProperty : public CCObject
MyProperty (void);
~MyProperty (void);
void setUniqueId(int id);
int getUniqueId();
void setRoleName(const char *name);
const char* getRoleName();
//@pkg1_end
//@pkg2_begin
//这里存放下一个自定义类的pkg描述
//@pkg2_end
// tolua_end
那么创建这个类有什么用呢:用来给tolua++去解析编译的,而tolua++只能编译pkg,所以我们要把这个类加到API.pkg中
typedef unsigned char
typedef int
typedef unsigned long
typedef unsigned short
typedef char*
typedef const char*
$cfile &api/PkgDiscription.h&
4】经过之前的四步,现在直接点击“build.bat”,就会根据我们写的“PkgDiscription.h”,生成我们要的“LuaAPI.cpp”文件了,是在api/目录下
这里面有我们在PkgDiscription.h中声明要绑定的函数接口。但是
发现有错误!!
原因是:在PkgDiscription.h中写的自定义类MyProperty并不是我们真正的自定类,它只是一个pkg描述,那么直接的解决办法是: 在LuaAPI.cpp中引入我们的头文件“MyProperty.h”
但是这样不好,每次生成的时候都需要再次引入头文件,麻烦,但是pkg提供了生成的时候就引入头文件的办法,就是在API.pkg中添加头文件
typedef unsigned char
typedef int
typedef unsigned long
typedef unsigned short
typedef char*
typedef const char*
$cfile &api/PkgDiscription.h&
$#include &MyProperty.h&
但是这样还不好,因为每有一个新的需要绑定的自定义类,我们都需要这API.pkg中引入,这样麻烦,那么我们就创建一个总的头文件类,专门放头文件:PkgHeadFiles.h
#ifndef __PKG_HEAD_FILES_H_
#define __PKG_HEAD_FILES_H_
#include &MyProperty.h&
//在这里引入我们工程中定义的类(准备给lua用的)
于是在API.pkg中
typedef unsigned char
typedef int
typedef unsigned long
typedef unsigned short
typedef char*
typedef const char*
$cfile &api/PkgDiscription.h&
$#include &PkgHeadFiles.h&
⑤到这里基本已经大功告成了,但是还有一步,就是我们自己生成的LuaAPI.cpp文件,是怎么给lua使用的呢,那么只需要知道引擎本身的LuaCocos2d.cpp是怎么给lua使用的就好了。
关键点在CCLuaStack中,看CCLuaStack.cpp文件中init()函数,修改init()函数
bool CCLuaStack::init(void)
tolua_CocoStudio_open(m_state);
//新加的一句代码
tolua_API_open(m_state);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
CCLuaObjcBridge::luaopen_luaoc(m_state);
return true;
直接添加会报错,说找不到这个函数,那么新加的tolua_API_open在哪里呢,其实是在我们生成的LuaAPI.cpp中,所以为了方便,我们写一个LuaAPI.h,里面之声明一下这个函数(单单是为了方便而已)
LuaAPI.h代码:
#ifndef __LUA_API_H_
#define __LUA_API_H_
#ifdef __cplusplus
extern &C& {
#include &tolua++.h&
#ifdef __cplusplus
TOLUA_API int tolua_API_open(lua_State* tolua_S);
然后在CCLuaStack中引入这个LuaAPI.h
#include &CCLuaStack.h&
#include &LuaAPI/api/LuaAPI.h& //这里的路径,由于我已经在liblua项目库目录中引入相关目录了,所以用的是相对路径
好了至此,我们的api目录下的文件如下:
当项目完成以后后两个头文件可以删除,现在它只是方便书写而已。
⑥这样,每次需要修改文件时,在“PkgHeadFiles.h”引入我们自定义的类,在“PkgDiscription.h”中添加pkg描述,
然后运行一下build.bat即可
。都是在我们自己的工程中操作的,上传svn。
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
没有分页内容
图片无法显示
视频无法显示
与原文不一致}

我要回帖

更多关于 tolua框架 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信