diff options
author | chai <chaifix@163.com> | 2018-08-20 11:51:32 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-20 11:51:32 +0800 |
commit | 65bafdc682db46f0f115374ad39f1fbc348832ac (patch) | |
tree | 7862548cdf01060e89a45c9817afc6e5d263acd7 /src/lua/modules/graphics/luaopen_JSL.cpp | |
parent | 9593ae6ecdfcfb876fa7953f25e19f0a97e1453a (diff) |
*update
Diffstat (limited to 'src/lua/modules/graphics/luaopen_JSL.cpp')
-rw-r--r-- | src/lua/modules/graphics/luaopen_JSL.cpp | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/lua/modules/graphics/luaopen_JSL.cpp b/src/lua/modules/graphics/luaopen_JSL.cpp new file mode 100644 index 0000000..c7a257a --- /dev/null +++ b/src/lua/modules/graphics/luaopen_JSL.cpp @@ -0,0 +1,145 @@ +#include "lua/modules/luax.h" +#include "lua/common/common.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::graphics; + + typedef Texture Image; + + static inline Ref<JSLProgram>& checkJSLProgram(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); + return proxy->getRef<JSLProgram>(); + } + + static enum VARIABLE_TYPE + { + INVALID = 0, + + NUMBER, + IMAGE, + CANVAS, + VEC2, + VEC3, + VEC4, + COLOR, + }; + + static VARIABLE_TYPE strToType(const char* str) + { + std::string s = std::string(str); + if (s == "number") return NUMBER; + else if (s == "Image") return IMAGE; + else if (s == "Canvas") return CANVAS; + else if (s == "vec2") return VEC2; + else if (s == "vec3") return VEC3; + else if (s == "vec4") return VEC4; + else if (s == "Color") return COLOR; + else return INVALID; + } + + /** + * Use send function send variables to JSL program. + */ + static int l_send(lua_State* L) + { + Ref<JSLProgram>& ref = checkJSLProgram(L); + // number Image Texel + const char* typestr = luax_checkstring(L, 2); + // variable name + const char* variable = luax_checkstring(L, 3); + if (typestr != nullptr) + { + int type = strToType(typestr); + switch (type) + { + case NUMBER: + { + float number = luax_checknumber(L, 4); + ref->sendFloat(variable, number); + break; + } + case IMAGE: + { + Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_IMAGE); + Ref<Image>& tex = proxy->getRef<Image>(); + ref->sendTexture(variable, tex.getObject()); + break; + } + case CANVAS: + { + Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_CANVAS); + Ref<Canvas>& canvas = proxy->getRef<Canvas>(); + ref->sendCanvas(variable, canvas.getObject()); + break; + } + case VEC2: + { + float x = luax_checknumber(L, 4); + float y = luax_checknumber(L, 5); + ref->sendVec2(variable, x, y); + break; + } + case VEC3: + { + float x = luax_checknumber(L, 4); + float y = luax_checknumber(L, 5); + float z = luax_checknumber(L, 6); + ref->sendVec3(variable, x, y, z); + break; + } + case VEC4: + { + float x = luax_checknumber(L, 4); + float y = luax_checknumber(L, 5); + float z = luax_checknumber(L, 6); + float w = luax_checknumber(L, 7); + ref->sendVec4(variable, x, y, z, w); + break; + } + case COLOR: + { + color col; + col.rgba.r = luax_checkinteger(L, 4); + col.rgba.g = luax_checkinteger(L, 5); + col.rgba.b = luax_checkinteger(L, 6); + col.rgba.a = luax_checkinteger(L, 7); + ref->sendColor(variable, &col); + break; + } + default: + return 0; + } + } + return 1; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); + proxy->release(); + return 0; + } + + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "send", l_send }, + { 0, 0 } + }; + + /** + * JSL program + */ + int luaopen_JSL(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_SHADER, f); + return 0; + } + +} +}
\ No newline at end of file |