From 65bafdc682db46f0f115374ad39f1fbc348832ac Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 20 Aug 2018 11:51:32 +0800 Subject: *update --- src/lua/modules/graphics/luaopen_JSL.cpp | 145 +++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 src/lua/modules/graphics/luaopen_JSL.cpp (limited to 'src/lua/modules/graphics/luaopen_JSL.cpp') 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& checkJSLProgram(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); + return proxy->getRef(); + } + + 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& 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& tex = proxy->getRef(); + ref->sendTexture(variable, tex.getObject()); + break; + } + case CANVAS: + { + Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_CANVAS); + Ref& canvas = proxy->getRef(); + 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 -- cgit v1.1-26-g67d0