Changeset 177


Ignore:
Timestamp:
07/27/13 16:31:34 (12 years ago)
Author:
epyon
Message:
  • common - added narrow_cast (to be expanded)
  • lua/glm - added random coord constructor and UNIT/ZERO constants for each vector class
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/common.hh

    r171 r177  
    195195}
    196196
     197template <typename T, typename U>
     198T narrow_cast(const U& a)
     199{
     200        return static_cast<T>(a & T(0xFFFFFFFFFFFFFFFF) );
     201}
     202
    197203#endif // NV_COMMON_HH
  • trunk/src/lua/lua_glm.cc

    r174 r177  
    99#include "nv/lua/lua_raw.hh"
    1010#include "nv/string.hh"
     11#include "nv/random.hh"
    1112
    1213static size_t nlua_swizzel_lookup[256];
     
    2425template < typename T, size_t k >
    2526struct nlua_vec_constructor {
     27        static inline T unit() { return T(); }
    2628        static inline T construct( lua_State*, int ) {
    2729                return T();
     
    3032
    3133template < typename T > struct nlua_vec_constructor< T, 1 > {
     34        static inline T unit() { return T( 1 ); }
    3235        static inline T construct( lua_State* L, int index ) {
    3336                return T( lua_tonumber( L, index ) );
     
    3639
    3740template < typename T > struct nlua_vec_constructor< T, 2 > {
     41        static inline T unit() { return T( 1, 1 ); }
    3842        static inline T construct( lua_State* L, int index ) {
    3943                if ( lua_type( L, index ) == LUA_TUSERDATA )
     
    4549
    4650template < typename T > struct nlua_vec_constructor< T, 3 > {
     51        static inline T unit() { return T( 1, 1, 1 ); }
    4752        static inline T construct( lua_State* L, int index ) {
    4853                typedef glm::detail::tvec2<typename T::value_type> vec2;
     
    6570
    6671template < typename T > struct nlua_vec_constructor< T, 4 > {
     72        static inline T unit() { return T( 1, 1, 1, 1 ); }
    6773        static inline T construct( lua_State* L, int index ) {
    6874                typedef glm::detail::tvec2<typename T::value_type> vec2;
     
    113119
    114120template< typename T >
     121int nlua_vec_random( lua_State* L )
     122{
     123        nlua_push_vec<T>( L, nv::random::get().range( nlua_to_vec<T>( L, 1 ), nlua_to_vec<T>( L, 2 ) ) );
     124        return 1;
     125}
     126
     127template< typename T >
    115128int nlua_vec_clone( lua_State* L )
    116129{
     
    118131        return 1;
    119132}
    120 
    121133
    122134template< typename T >
     
    291303        static const struct luaL_Reg nlua_vec_sf [] = {
    292304                { "new",            nlua_vec_new<T> },
     305                { "random",         nlua_vec_random<T> },
    293306                {NULL, NULL}
    294307        };
     
    297310                { "clone",          nlua_vec_clone<T> },
    298311                { "get",            nlua_vec_get<T> },
     312                { "tostring",       nlua_vec_tostring<T> },
    299313                {NULL, NULL}
    300314        };
     
    330344        nlua_register( L, nlua_vec_sm, -1 );
    331345        lua_setmetatable( L, -2 );
     346
     347        nlua_push_vec( L, T() );
     348        lua_setfield( L, -2, "ZERO" );
     349        nlua_push_vec( L, nlua_vec_constructor<T,sizeof( T ) / sizeof( typename T::value_type )>::unit() );
     350        lua_setfield( L, -2, "UNIT" );
    332351        return 1;
    333352}
Note: See TracChangeset for help on using the changeset viewer.