-
Notifications
You must be signed in to change notification settings - Fork 32
/
ecs_test.h
146 lines (127 loc) · 2.81 KB
/
ecs_test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifdef TEST_LUAECS
#include <stdio.h>
#include "luaecs.h"
#define COMPONENT_VECTOR2 1
#define TAG_MARK 2
#define COMPONENT_ID 3
#define COMPONENT_LUA 4
#define COMPONENT_USERDATA 1
struct vector2 {
float x;
float y;
};
struct id {
int v;
};
static int
ltest(lua_State *L) {
struct ecs_context *ctx = lua_touserdata(L, 1);
struct vector2 *v;
int i;
struct ecs_token token;
for (i = 0; (v = (struct vector2 *)entity_fetch(ctx, COMPONENT_VECTOR2, i, &token)); i++) {
printf("vector2 %d: x=%f y=%f\n", i, v->x, v->y);
struct id *id = (struct id *)entity_component(ctx, token, COMPONENT_ID);
if (id) {
printf("\tid = %d\n", id->v);
}
void *mark = entity_component(ctx, token, TAG_MARK);
if (mark) {
printf("\tMARK\n");
}
}
return 0;
}
static int
lsum(lua_State *L) {
struct ecs_context *ctx = lua_touserdata(L, 1);
struct vector2 *v;
int i;
float s = 0;
for (i = 0; (v = (struct vector2 *)entity_fetch(ctx, COMPONENT_VECTOR2, i, NULL)); i++) {
s += v->x + v->y;
}
lua_pushnumber(L, s);
return 1;
}
struct userdata_t {
unsigned char a;
void *b;
};
static int
ltestuserdata(lua_State *L) {
struct ecs_context *ctx = lua_touserdata(L, 1);
struct userdata_t *ud = entity_fetch(ctx, COMPONENT_USERDATA, 0, NULL);
ud->a = 1 - ud->a;
ud->b = ctx;
return 0;
}
static int
lgetlua(lua_State *L) {
struct ecs_context *ctx = lua_touserdata(L, 1);
int index = luaL_checkinteger(L, 2);
int t = entity_get_lua(ctx, COMPONENT_LUA, index-1, L);
if (t) {
return 1;
}
return 0;
}
static int
lsiblinglua(lua_State *L) {
struct ecs_context *ctx = lua_touserdata(L, 1);
int index = luaL_checkinteger(L, 2);
struct ecs_token token;
if (entity_fetch(ctx, TAG_MARK, index, &token) == NULL)
return luaL_error(L, "Invalid token");
int t = entity_component_lua(ctx, token, COMPONENT_LUA, L);
if (t) {
return 1;
}
return 0;
}
static int
lcache(lua_State *L) {
struct ecs_context *ctx = lua_touserdata(L, 1);
int index[3] = {
TAG_MARK,
COMPONENT_ID,
COMPONENT_EID,
};
struct ecs_cache *c = entity_cache_create(ctx, index, 3);
int n = entity_cache_sync(ctx, c);
int i;
lua_createtable(L, n, 0);
for (i=0;i<n;i++) {
int * v = (int *)entity_cache_fetch(ctx, c, i, COMPONENT_ID);
if (v) {
lua_pushinteger(L, *v);
} else {
lua_pushboolean(L, 0);
}
lua_rawseti(L, -2, i+1);
}
lua_createtable(L, n, 0);
for (i=0;i<n;i++) {
int64_t v = (int64_t)entity_cache_fetch(ctx, c, i, COMPONENT_EID);
lua_pushinteger(L, v);
lua_rawseti(L, -2, i+1);
}
entity_cache_release(ctx, c);
return 2;
}
LUAMOD_API int
luaopen_ecs_ctest(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{ "test", ltest },
{ "sum", lsum },
{ "testuserdata", ltestuserdata },
{ "getlua", lgetlua },
{ "siblinglua", lsiblinglua },
{ "cache", lcache },
{ NULL, NULL },
};
luaL_newlib(L, l);
return 1;
}
#endif