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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
#include <vector>
#include "common/je_lua_object.h"
#include "common/je_lua_common.h"
#include "libjin/jin.h"
#include "je_lua_sprite.h"
#include "je_lua_particle_system.h"
using namespace std;
using namespace JinEngine::Math;
using namespace JinEngine::Graphics;
using namespace JinEngine::Graphics::Particles;
namespace JinEngine
{
namespace Lua
{
const char* Jin_Lua_ParticleSystem = "ParticleSystem";
typedef Shared<ParticleSystem>& SharedParticleSystem;
LUA_IMPLEMENT inline SharedParticleSystem checkPS(lua_State* L)
{
LuaObject* luaObj = luax_checkobject(L, 1, Jin_Lua_ParticleSystem);
return luaObj->getShared<ParticleSystem>();
}
LUA_IMPLEMENT int l_gc(lua_State* L)
{
LuaObject* luaObj = luax_checkobject(L, 1, Jin_Lua_ParticleSystem);
luaObj->release();
return 0;
}
LUA_IMPLEMENT int l_update(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
float dt = luax_checknumber(L, 2);
ps->update(dt);
return 0;
}
LUA_IMPLEMENT int l_render(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
ps->render();
return 0;
}
LUA_IMPLEMENT int l_setPosition(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
float x = luax_checknumber(L, 2);
float y = luax_checknumber(L, 3);
ps->setPosition(x, y);
return 0;
}
LUA_IMPLEMENT int l_setScale(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
float sx = luax_checknumber(L, 2);
float sy = luax_checknumber(L, 3);
//ps->setScale(sx, sy);
return 0;
}
LUA_IMPLEMENT int l_pause(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
bool b = luax_checkbool(L, 2);
//ps->pause(b);
return 0;
}
LUA_IMPLEMENT int l_clear(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
//ps->clear();
return 0;
}
LUA_IMPLEMENT int l_setEmitRate(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
if (luax_gettop(L) >= 3)
{
float floor = luax_checknumber(L, 2);
float ceil = luax_checknumber(L, 3);
ps->setEmitRate(floor, ceil);
}
else if (luax_gettop(L) >= 2)
{
float n = luax_checknumber(L, 2);
ps->setEmitRate(n);
}
return 0;
}
LUA_IMPLEMENT int l_setEmitForce(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
if (luax_gettop(L) >= 3)
{
float floor = luax_checknumber(L, 2);
float ceil = luax_checknumber(L, 3);
ps->setEmitForce(floor, ceil);
}
else if (luax_gettop(L) >= 2)
{
float n = luax_checknumber(L, 2);
ps->setEmitForce(n);
}
return 0;
}
LUA_IMPLEMENT int l_setEmitDirection(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
if (luax_gettop(L) >= 3)
{
float floor = luax_checknumber(L, 2);
float ceil = luax_checknumber(L, 3);
ps->setEmitDirection(floor, ceil);
}
else if (luax_gettop(L) >= 2)
{
float n = luax_checknumber(L, 2);
ps->setEmitDirection(n);
}
return 0;
}
LUA_IMPLEMENT int l_setEmitPosition(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
if (luax_gettop(L) >= 3)
{
if (!luax_istable(L, 2))
{
luax_typerror(L, 2, "table");
return 1;
}
if (!luax_istable(L, 3))
{
luax_typerror(L, 3, "table");
return 1;
}
Vector2<float> floor, ceil;
floor.x = luax_rawgetnumber(L, 2, 1);
floor.y = luax_rawgetnumber(L, 2, 2);
ceil.x = luax_rawgetnumber(L, 3, 1);
ceil.y = luax_rawgetnumber(L, 3, 2);
ps->setEmitPosition(floor, ceil);
}
else if (luax_gettop(L) >= 2)
{
if (!luax_istable(L, 2))
{
luax_typerror(L, 2, "table");
return 1;
}
Vector2<float> pos;
pos.x = luax_rawgetnumber(L, 2, 1);
pos.y = luax_rawgetnumber(L, 2, 2);
ps->setEmitPosition(pos);
}
return 0;
}
LUA_IMPLEMENT int l_setParticleLife(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
if (luax_gettop(L) >= 3)
{
float floor = luax_checknumber(L, 2);
float ceil = luax_checknumber(L, 3);
ps->setParticleLife(floor, ceil);
}
else if (luax_gettop(L) >= 2)
{
float n = luax_checknumber(L, 2);
ps->setParticleLife(n);
}
return 0;
}
LUA_IMPLEMENT int l_setParticleLinearAccelaration(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
if (!luax_istable(L, 2))
{
luax_typerror(L, 2, "table");
return 1;
}
Vector2<float> ac;
ac.x = luax_rawgetnumber(L, 2, 1);
ac.y = luax_rawgetnumber(L, 2, 2);
ps->setParticleLinearAccelaration(ac);
return 0;
}
LUA_IMPLEMENT int l_setParticleRadialAccelaration(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
float ra = luax_checknumber(L, 2);
ps->setParticleRadialAccelaration(ra);
return 0;
}
LUA_IMPLEMENT int l_setParticleAngularSpeed(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
if (luax_gettop(L) >= 3)
{
float floor = luax_checknumber(L, 2);
float ceil = luax_checknumber(L, 3);
ps->setParticleAngularSpeed(floor, ceil);
}
else if (luax_gettop(L) >= 2)
{
float n = luax_checknumber(L, 2);
ps->setParticleAngularSpeed(n);
}
return 0;
}
LUA_IMPLEMENT int l_setParticleSpritesMode(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
int n = luax_checkinteger(L, 2);
SpriteMode mode = static_cast<SpriteMode>(n);
ps->setParticleSpritesMode(mode);
return 0;
}
LUA_IMPLEMENT int l_addParticleSprite(lua_State* L)
{
LuaObject* obj = luax_checkobject(L, 1, Jin_Lua_ParticleSystem);
SharedParticleSystem ps = obj->getShared<ParticleSystem>();
LuaObject* objSpr = luax_checkobject(L, 2, Jin_Lua_Sprite);
Sprite* spr = objSpr->getObject<Sprite>();
ps->addParticleSprite(spr);
int depn = (*obj).getDependenciesCount();
(*obj).setDependency((int)ParticleSystemDependency::DEP_SPRITES + depn, objSpr->getShared());
return 0;
}
LUA_IMPLEMENT int l_addParticleSprites(lua_State* L)
{
LuaObject* obj = luax_checkobject(L, 1, Jin_Lua_ParticleSystem);
SharedParticleSystem ps = obj->getShared<ParticleSystem>();
if (!luax_istable(L, 2))
{
luax_typerror(L, 2, "sprites table");
return 1;
}
int n = luax_tableidxlen(L, 2);
int depn = (*obj).getDependenciesCount();
for (int i = 1; i <= n; ++i)
{
luax_rawgeti(L, 2, i);
LuaObject* objSpr = luax_checkobject(L, -1, Jin_Lua_Sprite);
luax_pop(L, 1);
Sprite* spr = objSpr->getObject<Sprite>();
ps->addParticleSprite(spr);
(*obj).setDependency((int)ParticleSystemDependency::DEP_SPRITES + depn + i - 1, objSpr->getShared());
}
return 0;
}
// From 0
LUA_IMPLEMENT int l_removeParticleSprite(lua_State* L)
{
LuaObject* obj = luax_checkobject(L, 1, Jin_Lua_ParticleSystem);
SharedParticleSystem ps = obj->getShared<ParticleSystem>();
int n = luax_checkinteger(L, 2);
ps->removeParticleSprite(n);
(*obj).removeDependency((int)ParticleSystemDependency::DEP_SPRITES + n);
return 0;
}
LUA_IMPLEMENT int l_enableParticleBlendAdditive(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
bool enable = luax_checkbool(L, 2);
ps->enableParticleBlendAdditive(enable);
return 0;
}
LUA_IMPLEMENT int l_setParticleScale(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
float scale = luax_checknumber(L, 2);
ps->setParticleScale(scale);
return 0;
}
LUA_IMPLEMENT int l_addParticleScalePoint(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
float scale = luax_checknumber(L, 2);
float t = luax_checknumber(L, 3);
ps->addParticleScalePoint(scale, t);
return 0;
}
LUA_IMPLEMENT int l_removeParticleScalePoint(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
int i = luax_checkinteger(L, 2);
ps->removeParticleScalePoint(i);
return 0;
}
LUA_IMPLEMENT int l_setParticleColor(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
if (!luax_istable(L, 2))
{
luax_typerror(L, 2, "color table");
return 1;
}
Color c;
c.r = luax_rawgetnumber(L, 2, 1);
c.g = luax_rawgetnumber(L, 2, 2);
c.b = luax_rawgetnumber(L, 2, 3);
c.a = luax_rawgetnumber(L, 2, 4);
ps->setParticleColor(c);
return 0;
}
LUA_IMPLEMENT int l_addParticleColorPoint(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
if (!luax_istable(L, 2))
{
luax_typerror(L, 2, "color table");
return 1;
}
Color c;
c.r = luax_rawgetnumber(L, 2, 1);
c.g = luax_rawgetnumber(L, 2, 2);
c.b = luax_rawgetnumber(L, 2, 3);
c.a = luax_rawgetnumber(L, 2, 4);
float t = luax_checknumber(L, 3);
ps->addParticleColorPoint(c, t);
return 0;
}
LUA_IMPLEMENT int l_removeParticleColorPoint(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
int i = luax_checkinteger(L, 2);
ps->removeParticleColorPoint(i);
return 0;
}
LUA_IMPLEMENT int l_setParticleTransparency(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
float transparency = luax_checknumber(L, 2);
ps->setParticleTransparency(transparency);
return 0;
}
LUA_IMPLEMENT int l_addParticleTransparencyPoint(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
float transparency = luax_checknumber(L, 2);
float t = luax_checknumber(L, 3);
ps->addParticleTransparencyPoint(transparency, t);
return 0;
}
LUA_IMPLEMENT int l_removeParticleTransparencyPoint(lua_State* L)
{
SharedParticleSystem ps = checkPS(L);
int i = luax_checkinteger(L, 2);
ps->removeParticleTransparencyPoint(i);
return 0;
}
LUA_EXPORT void luaopen_ParticleSystem(lua_State* L)
{
luaL_Reg methods[] = {
{ "__gc", l_gc },
{ "update", l_update },
{ "render", l_render },
{ "setPosition", l_setPosition },
{ "setScale", l_setScale },
{ "pause", l_pause },
{ "clear", l_clear },
{ "setEmitRate", l_setEmitRate },
{ "setEmitForce", l_setEmitForce },
{ "setEmitDirection", l_setEmitDirection },
{ "setEmitPosition", l_setEmitPosition },
{ "setParticleLife", l_setParticleLife },
{ "setParticleLinearAccelaration", l_setParticleLinearAccelaration },
{ "setParticleRadialAccelaration", l_setParticleRadialAccelaration },
{ "setParticleAngularSpeed", l_setParticleAngularSpeed },
{ "setParticleSpritesMode", l_setParticleSpritesMode },
{ "addParticleSprite", l_addParticleSprite },
{ "addParticleSprites", l_addParticleSprites },
{ "removeParticleSprite", l_removeParticleSprite },
{ "enableParticleBlendAdditive", l_enableParticleBlendAdditive },
{ "setParticleScale", l_setParticleScale },
{ "addParticleScalePoint", l_addParticleScalePoint },
{ "removeParticleScalePoint", l_removeParticleScalePoint },
{ "setParticleColor", l_setParticleColor },
{ "addParticleColorPoint", l_addParticleColorPoint },
{ "removeParticleColorPoint", l_removeParticleColorPoint },
{ "setParticleTransparency", l_setParticleTransparency },
{ "addParticleTransparencyPoint", l_addParticleTransparencyPoint },
{ "removeParticleTransparencyPoint", l_removeParticleTransparencyPoint },
{ 0, 0 }
};
luax_newtype(L, Jin_Lua_ParticleSystem, methods);
}
} // namespace Lua
} // namespace JinEngine
|