1 | // Copyright (C) 2016-2016 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #include "nv/lua/lua_gfx.hh"
|
---|
8 |
|
---|
9 | #include "nv/base/capi.hh"
|
---|
10 | #include "nv/lua/lua_raw.hh"
|
---|
11 | #include "nv/lua/lua_templates.hh"
|
---|
12 |
|
---|
13 | using nv::lua::detail::is_color_mask;
|
---|
14 | using nv::lua::detail::to_color_mask;
|
---|
15 | using nv::lua::detail::to_pcolor_mask;
|
---|
16 | using nv::lua::detail::push_color_mask;
|
---|
17 |
|
---|
18 | using nv::lua::detail::is_scissor_test;
|
---|
19 | using nv::lua::detail::to_scissor_test;
|
---|
20 | using nv::lua::detail::to_pscissor_test;
|
---|
21 | using nv::lua::detail::push_scissor_test;
|
---|
22 |
|
---|
23 | using nv::lua::detail::is_clear_state;
|
---|
24 | using nv::lua::detail::to_clear_state;
|
---|
25 | using nv::lua::detail::to_pclear_state;
|
---|
26 | using nv::lua::detail::push_clear_state;
|
---|
27 |
|
---|
28 | static int nlua_color_mask_tostring( lua_State * L )
|
---|
29 | {
|
---|
30 | nv::color_mask* m = to_pcolor_mask( L, 1 );
|
---|
31 | lua_pushfstring( L, "color_mask(%s%s%s%s)",
|
---|
32 | m->red ? "R" : "-",
|
---|
33 | m->red ? "G" : "-",
|
---|
34 | m->red ? "B" : "-",
|
---|
35 | m->red ? "A" : "-"
|
---|
36 | );
|
---|
37 | return 1;
|
---|
38 | }
|
---|
39 |
|
---|
40 | static int nlua_color_mask_index( lua_State * L )
|
---|
41 | {
|
---|
42 | nv::color_mask* m = to_pcolor_mask( L, 1 );
|
---|
43 | size_t len = 0;
|
---|
44 | const char * key = lua_tolstring( L, 2, &len );
|
---|
45 | if ( len > 0 )
|
---|
46 | {
|
---|
47 | char k = key[0];
|
---|
48 | if ( k == 'r' && ( len == 1 || nv::nvstrcmp( key, "red" ) == 0 ) )
|
---|
49 | {
|
---|
50 | lua_pushboolean( L, m->red ); return 1;
|
---|
51 | }
|
---|
52 | if ( k == 'g' && ( len == 1 || nv::nvstrcmp( key, "green" ) == 0 ) )
|
---|
53 | {
|
---|
54 | lua_pushboolean( L, m->green ); return 1;
|
---|
55 | }
|
---|
56 | if ( k == 'b' && ( len == 1 || nv::nvstrcmp( key, "blue" ) == 0 ) )
|
---|
57 | {
|
---|
58 | lua_pushboolean( L, m->blue ); return 1;
|
---|
59 | }
|
---|
60 | if ( k == 'a' && ( len == 1 || nv::nvstrcmp( key, "alpha" ) == 0 ) )
|
---|
61 | {
|
---|
62 | lua_pushboolean( L, m->alpha ); return 1;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | luaL_getmetafield( L, 1, "__functions" );
|
---|
67 | lua_pushvalue( L, 2 );
|
---|
68 | lua_rawget( L, -2 );
|
---|
69 | return 1;
|
---|
70 | }
|
---|
71 |
|
---|
72 | static int nlua_color_mask_newindex( lua_State * L )
|
---|
73 | {
|
---|
74 | nv::color_mask* m = to_pcolor_mask( L, 1 );
|
---|
75 | size_t len = 0;
|
---|
76 | const char * key = lua_tolstring( L, 2, &len );
|
---|
77 | if ( len > 0 )
|
---|
78 | {
|
---|
79 | char k = key[0];
|
---|
80 | if ( k == 'r' && ( len == 1 || nv::nvstrcmp( key, "red" ) == 0 ) )
|
---|
81 | {
|
---|
82 | m->red = lua_toboolean( L, 3 ) != 0; return 0;
|
---|
83 | }
|
---|
84 | if ( k == 'g' && ( len == 1 || nv::nvstrcmp( key, "green" ) == 0 ) )
|
---|
85 | {
|
---|
86 | m->green = lua_toboolean( L, 3 ) != 0; return 0;
|
---|
87 | }
|
---|
88 | if ( k == 'b' && ( len == 1 || nv::nvstrcmp( key, "blue" ) == 0 ) )
|
---|
89 | {
|
---|
90 | m->blue = lua_toboolean( L, 3 ) != 0; return 0;
|
---|
91 | }
|
---|
92 | if ( k == 'a' && ( len == 1 || nv::nvstrcmp( key, "alpha" ) == 0 ) )
|
---|
93 | {
|
---|
94 | m->alpha = lua_toboolean( L, 3 ) != 0; return 0;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | luaL_error( L, "color_mask : unknown index - %s", lua_tostring( L, 2 ) );
|
---|
98 | return 0;
|
---|
99 | }
|
---|
100 |
|
---|
101 | static int luaopen_color_mask( lua_State * L )
|
---|
102 | {
|
---|
103 | struct constructor
|
---|
104 | {
|
---|
105 | static inline nv::color_mask construct( lua_State* L, int index )
|
---|
106 | {
|
---|
107 | int args = lua_gettop( L ) - index;
|
---|
108 | bool r = args >= 0 ? lua_toboolean( L, index + 0 ) != 0 : true;
|
---|
109 | bool g = args >= 1 ? lua_toboolean( L, index + 1 ) != 0 : true;
|
---|
110 | bool b = args >= 2 ? lua_toboolean( L, index + 2 ) != 0 : true;
|
---|
111 | bool a = args >= 3 ? lua_toboolean( L, index + 3 ) != 0 : true;
|
---|
112 | return nv::color_mask( r, g, b, a );
|
---|
113 | }
|
---|
114 | };
|
---|
115 |
|
---|
116 | NV_LUA_STACK_ASSERT( L, 1 );
|
---|
117 | static const struct luaL_Reg nlua_color_mask_sf[] = {
|
---|
118 | { "new", nv::lua::detail::new_impl<nv::color_mask, constructor> },
|
---|
119 | { NULL, NULL }
|
---|
120 | };
|
---|
121 |
|
---|
122 | static const struct luaL_Reg nlua_color_mask_f[] = {
|
---|
123 | { "clone", nv::lua::detail::clone_impl<nv::color_mask> },
|
---|
124 | { "tostring", nlua_color_mask_tostring },
|
---|
125 | { NULL, NULL }
|
---|
126 | };
|
---|
127 |
|
---|
128 | static const struct luaL_Reg nlua_color_mask_sm[] = {
|
---|
129 | { "__call", nv::lua::detail::call_impl<nv::color_mask, constructor> },
|
---|
130 | { NULL, NULL }
|
---|
131 | };
|
---|
132 |
|
---|
133 | static const struct luaL_Reg nlua_color_mask_m[] = {
|
---|
134 | { "__eq", nv::lua::detail::eq_impl<nv::color_mask> },
|
---|
135 | { "__index", nlua_color_mask_index },
|
---|
136 | { "__newindex", nlua_color_mask_newindex },
|
---|
137 | { "__tostring", nlua_color_mask_tostring },
|
---|
138 | { NULL, NULL }
|
---|
139 | };
|
---|
140 |
|
---|
141 | luaL_newmetatable( L, nv::lua::pass_traits<nv::color_mask>::metatable() );
|
---|
142 | nlua_register( L, nlua_color_mask_m, -1 );
|
---|
143 | lua_createtable( L, 0, 0 );
|
---|
144 | nlua_register( L, nlua_color_mask_f, -1 );
|
---|
145 | lua_setfield( L, -2, "__functions" );
|
---|
146 | lua_pop( L, 1 );
|
---|
147 |
|
---|
148 | lua_createtable( L, 0, 0 );
|
---|
149 | nlua_register( L, nlua_color_mask_sf, -1 );
|
---|
150 | lua_createtable( L, 0, 0 );
|
---|
151 | nlua_register( L, nlua_color_mask_sm, -1 );
|
---|
152 | lua_setmetatable( L, -2 );
|
---|
153 |
|
---|
154 | // nv::lua::detail::push_vec( L, T() );
|
---|
155 | // lua_setfield( L, -2, "ZERO" );
|
---|
156 | // nv::lua::detail::push_vec( L, nlua_vec_constructor<T, sizeof( T ) / sizeof( typename T::value_type )>::unit() );
|
---|
157 | // lua_setfield( L, -2, "UNIT" );
|
---|
158 |
|
---|
159 | return 1;
|
---|
160 | }
|
---|
161 |
|
---|
162 | static int luaopen_scissor_test( lua_State * L )
|
---|
163 | {
|
---|
164 | NV_LUA_STACK_ASSERT( L, 1 );
|
---|
165 | return 1;
|
---|
166 | }
|
---|
167 |
|
---|
168 | static int luaopen_clear_state( lua_State * L )
|
---|
169 | {
|
---|
170 | NV_LUA_STACK_ASSERT( L, 1 );
|
---|
171 | return 1;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | void nv::lua::register_gfx( lua_State* L )
|
---|
176 | {
|
---|
177 | int stack = lua_gettop( L );
|
---|
178 | nlua_requiref( L, "color_mask", luaopen_color_mask, 1 );
|
---|
179 | nlua_requiref( L, "scissor_test", luaopen_scissor_test, 1 );
|
---|
180 | nlua_requiref( L, "clear_state", luaopen_clear_state, 1 );
|
---|
181 | lua_settop( L, stack );
|
---|
182 | }
|
---|