aboutsummaryrefslogtreecommitdiff
path: root/src/lua/graphics/luaopen_JSL.cpp
blob: d1d3bacae8fc8ae7d157f7ef2d68c6147fc6abb2 (plain)
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
#include "libs/luax/luax.h"
#include "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;
    }
}
}