aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/modules/graphics/je_lua_particle_system.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin-lua/modules/graphics/je_lua_particle_system.cpp')
-rw-r--r--src/libjin-lua/modules/graphics/je_lua_particle_system.cpp422
1 files changed, 422 insertions, 0 deletions
diff --git a/src/libjin-lua/modules/graphics/je_lua_particle_system.cpp b/src/libjin-lua/modules/graphics/je_lua_particle_system.cpp
new file mode 100644
index 0000000..0c42209
--- /dev/null
+++ b/src/libjin-lua/modules/graphics/je_lua_particle_system.cpp
@@ -0,0 +1,422 @@
+#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";
+
+ LUA_IMPLEMENT inline ParticleSystem* checkPS(lua_State* L)
+ {
+ LuaObject* luaObj = luax_checkobject(L, 1, Jin_Lua_ParticleSystem);
+ return luaObj->getObject<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)
+ {
+ ParticleSystem* ps = checkPS(L);
+ float dt = luax_checknumber(L, 2);
+ ps->update(dt);
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_render(lua_State* L)
+ {
+ ParticleSystem* ps = checkPS(L);
+ ps->render();
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_setPosition(lua_State* L)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* ps = checkPS(L);
+ bool b = luax_checkbool(L, 2);
+ //ps->pause(b);
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_clear(lua_State* L)
+ {
+ ParticleSystem* ps = checkPS(L);
+ //ps->clear();
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_setEmitRate(lua_State* L)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* ps = checkPS(L);
+ float ra = luax_checknumber(L, 2);
+ ps->setParticleRadialAccelaration(ra);
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_setParticleAngularSpeed(lua_State* L)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* 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);
+ ParticleSystem* ps = obj->getObject<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);
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_addParticleSprites(lua_State* L)
+ {
+ LuaObject* obj = luax_checkobject(L, 1, Jin_Lua_ParticleSystem);
+ ParticleSystem* ps = obj->getObject<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);
+ }
+ return 0;
+ }
+
+ // From 0
+ LUA_IMPLEMENT int l_removeParticleSprite(lua_State* L)
+ {
+ LuaObject* obj = luax_checkobject(L, 1, Jin_Lua_ParticleSystem);
+ ParticleSystem* ps = obj->getObject<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)
+ {
+ ParticleSystem* ps = checkPS(L);
+ bool enable = luax_checkbool(L, 2);
+ ps->enableParticleBlendAdditive(enable);
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_setParticleScale(lua_State* L)
+ {
+ ParticleSystem* ps = checkPS(L);
+ float scale = luax_checknumber(L, 2);
+ ps->setParticleScale(scale);
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_addParticleScalePoint(lua_State* L)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* ps = checkPS(L);
+ int i = luax_checkinteger(L, 2);
+ ps->removeParticleScalePoint(i);
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_setParticleColor(lua_State* L)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* ps = checkPS(L);
+ int i = luax_checkinteger(L, 2);
+ ps->removeParticleColorPoint(i);
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_setParticleTransparency(lua_State* L)
+ {
+ ParticleSystem* ps = checkPS(L);
+ float transparency = luax_checknumber(L, 2);
+ ps->setParticleTransparency(transparency);
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_addParticleTransparencyPoint(lua_State* L)
+ {
+ ParticleSystem* 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)
+ {
+ ParticleSystem* 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 \ No newline at end of file