aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/modules/graphics/je_lua_mesh.cpp
blob: fc42096a131cb6f357913d4cd7e9688486632e7f (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
#include "common/je_lua_object.h"
#include "common/je_lua_common.h"
#include "libjin/jin.h"
#include "je_lua_mesh.h"
#include "je_lua_texture.h"

using namespace JinEngine::Math;
using namespace JinEngine::Graphics;

namespace JinEngine
{
    namespace Lua
    {

        const char* Jin_Lua_Mesh = "Mesh";

        LUA_IMPLEMENT inline Mesh* checkMesh(lua_State* L)
        {
            LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Mesh);
            return luaObj->getObject<Mesh>();
        }

        LUA_IMPLEMENT int l_getBound(lua_State* L)
        {
            Mesh* mesh = checkMesh(L);
            BBox bbox = mesh->getBound();
            luax_pushnumber(L, bbox.l);
            luax_pushnumber(L, bbox.r);
            luax_pushnumber(L, bbox.t);
            luax_pushnumber(L, bbox.b);
            return 4;
        }

        LUA_IMPLEMENT int l_setGraphic(lua_State* L)
        {
            LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Mesh);
            Mesh* mesh = luaObj->getObject<Mesh>();
            luaObj->removeDependency((int)MeshDependency::DEP_GRAPHIC);
            LuaObject* obj = (LuaObject*)luax_checktype(L, 2, Jin_Lua_Texture);
            mesh->setGraphic(obj->getObject<Texture>());
            luaObj->setDependency((int)MeshDependency::DEP_GRAPHIC, obj);
            return 0;
        }

        // x, y, u, v, color_table
        LUA_IMPLEMENT int l_pushVertex(lua_State* L)
        {
            Mesh* mesh = checkMesh(L);
            float x = luax_checknumber(L, 2);
            float y = luax_checknumber(L, 3);
            float u = luax_checknumber(L, 4);
            float v = luax_checknumber(L, 5);
            if (!luax_istable(L, 6))
            {
                luax_typerror(L, 6, "color table");
                return 1;
            }
            Color c;
            c.r = luax_rawgetnumberthenpop(L, -1, 1);
            c.g = luax_rawgetnumberthenpop(L, -1, 2);
            c.b = luax_rawgetnumberthenpop(L, -1, 3);
            c.a = luax_rawgetnumberthenpop(L, -1, 4);
            mesh->pushVertex(x, y, u, v, c);
            return 0;
        }

        LUA_IMPLEMENT int l_gc(lua_State* L)
        {
            LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Mesh);
            luaObj->release();
            return 0;
        }

        LUA_EXPORT void luaopen_Mesh(lua_State* L)
        {
            luaL_Reg methods[] = {
                { "__gc",       l_gc         },
                { "getBound",   l_getBound   },
                { "setGraphic", l_setGraphic },
                { "pushVertex", l_pushVertex },
                { 0,            0            }
            };
            luax_newtype(L, Jin_Lua_Mesh, methods);
        }

    } // namespace Lua
} // namespace JinEngine