From 9edf2ba9fe8524976d2f298767fff0149e8c0d41 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 17 May 2018 19:12:18 +0800 Subject: change file tree --- src/script/graphics/luaopen_JSL.cpp | 88 +++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/script/graphics/luaopen_JSL.cpp (limited to 'src/script/graphics/luaopen_JSL.cpp') diff --git a/src/script/graphics/luaopen_JSL.cpp b/src/script/graphics/luaopen_JSL.cpp new file mode 100644 index 0000000..c7b4e5e --- /dev/null +++ b/src/script/graphics/luaopen_JSL.cpp @@ -0,0 +1,88 @@ +#include "3rdparty/luax/luax.h" +#include "libjin/render/jsl.h" +#include "../luaopen_types.h" +namespace jin +{ +namespace lua +{ + + using namespace render; + + static inline JSLProgram* checkJSLProgram(lua_State* L) + { + return (JSLProgram*)luax_checktype(L, 1, TYPE_JSL); + } + + static enum VARIABLE_TYPE + { + INVALID = 0, + NUMBER , + IMAGE , + TEXEL + }; + + 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 == "Texel") return TEXEL; + else return INVALID; + } + + /** + * Use send function send variables to JSL program. + */ + static int l_send(lua_State* L) + { + JSLProgram* jsl = 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); + jsl->sendFloat(variable, number); + break; + } + case IMAGE: + { + Image* img = (Image*)luax_checktype(L, 4, TYPE_IMAGE); + jsl->sendImage(variable, img); + break; + } + case TEXEL: + break; + } + } + return 1; + } + + static int l_gc(lua_State* L) + { + + return 0; + } + + static const luaL_Reg f[] = { + {"send", l_send}, + {"__gc", l_gc}, + {0, 0} + }; + + /** + * JSL program + */ + int luaopen_JSL(lua_State* L) + { + luax_newtype(L, TYPE_JSL, f); + return 0; + } +} +} \ No newline at end of file -- cgit v1.1-26-g67d0