aboutsummaryrefslogtreecommitdiff
path: root/src/script/graphics/luaopen_JSL.cpp
blob: d5db363f1169bc1f026907d7397881912453031a (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "3rdparty/luax/luax.h"
#include "libjin/jin.h"
#include "../luaopen_types.h"

namespace jin
{
namespace lua
{

    using namespace render;  

    static inline JSLProgram* checkJSLProgram(lua_State* L)
    {
        Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL);
        if(proxy != nullptr)
            return (JSLProgram*)proxy->object;
        return nullptr; 
    }

    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)
    {
        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:
            {
                Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE);
                Image* img = (Image*)proxy->object;
                jsl->sendImage(variable, img);
                break;
            }
            case CANVAS:
            {
                Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE);
                Canvas* canvas = (Canvas*)proxy->object;
                jsl->sendCanvas(variable, canvas);
                break;
            }
            case VEC2:
            {
                float x = luax_checknumber(L, 4);
                float y = luax_checknumber(L, 5);
                jsl->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);
                jsl->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);
                jsl->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);
                jsl->sendColor(variable, &col);
                break;
            }
            default:
                return 0;
            }
        }
        return 1; 
    }

    static int l_gc(lua_State* L)
    {
        Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL);
        JSLProgram* jsl = (JSLProgram*)proxy->object;
        delete jsl;
        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, TYPE_JSL, f);
        return 0;
    }

}
}