diff options
Diffstat (limited to 'src/client/lua_api/scene/imesh.cpp')
| -rw-r--r-- | src/client/lua_api/scene/imesh.cpp | 162 |
1 files changed, 93 insertions, 69 deletions
diff --git a/src/client/lua_api/scene/imesh.cpp b/src/client/lua_api/scene/imesh.cpp index 1a1c28b..3f3ae24 100644 --- a/src/client/lua_api/scene/imesh.cpp +++ b/src/client/lua_api/scene/imesh.cpp @@ -14,6 +14,7 @@ extern "C" { #include "../gameparts.hpp" #include "imesh.hpp" #include "igeneric.hpp" +#include "../../../shared/lua_api/common.h" using namespace irr; using namespace scene; @@ -22,7 +23,6 @@ using namespace video; extern IrrlichtDevice* device; - static LISceneNode* checkismesh(lua_State* L, int index){ void* ud = luaL_checkudata(L,index,"scene.imesh"); luaL_argcheck(L,ud != NULL, index, "'scene.imesh' expected"); @@ -35,83 +35,107 @@ static LISceneNode* checkismesh(lua_State* L){ } */ -static int newiscenemesh(lua_State* L){ - printf("Createing mesh!\n"); - int nargs = lua_gettop(L); - if(nargs != 1){ - printf("Incorrect # of args to create a mesh!"); - } - //The model for the mesh - const char* modelpath = luaL_optstring(L,1,"error"); - - //Create the mesh - ISceneManager* smgr = device->getSceneManager(); - IAnimatedMesh* amesh = smgr->getMesh(modelpath); - IAnimatedMeshSceneNode* mesh = smgr->addAnimatedMeshSceneNode( amesh ); - mesh->setMaterialFlag(EMF_GOURAUD_SHADING,true); - printf("Registered the mesh!\n"); - - //Register it's callback - //registerguicallback(llabel,EGET_ELEMENT_HOVERED,iguilabelevent); - - //Create it's lua representation - LISceneNode* lmesh = (LISceneNode*)lua_newuserdata(L, sizeof(LISceneNode)); - int tref = luaL_ref(L,LUA_REGISTRYINDEX); - //iguielements[lcam] = tref; - lua_rawgeti(L,LUA_REGISTRYINDEX,tref);//Put it back on the stack since luaL_ref pops the object. - - //Set it's metatable - luaL_getmetatable(L, "scene.imesh"); - lua_setmetatable(L, -2); - - //Create the struct - lmesh->n = mesh; - lmesh->funcmap = hashmap_new(); - lmesh->type = "imesh"; - - //Free up anything made in this function - //free(label); - - //Put it on top and return it - lua_rawgeti(L,LUA_REGISTRYINDEX,tref); - return 1; +//{} :: scene.newmesh("/path/to/model") +static int newiscenemesh(lua_State* L){//"path/to" + + printf("Createing mesh!\n"); + int nargs = lua_gettop(L); + if(nargs != 1){ + lua_pushfstring(L,"scene.newmesh got %d arguments, expecting 1",nargs); + lua_error(L); + } + //The model for the mesh + const char* modelpath = luaL_optstring(L,1,"error");//"path/to" + lua_pop(L,1);// + + //Create the mesh + ISceneManager* smgr = device->getSceneManager(); + IAnimatedMesh* amesh = smgr->getMesh(modelpath); + IAnimatedMeshSceneNode* mesh = smgr->addAnimatedMeshSceneNode( amesh ); + mesh->setMaterialFlag(EMF_GOURAUD_SHADING,true); + printf("Registered the mesh!\n"); + + lua_newtable(L);//{} + luaL_getmetatable(L,"scene.imesh");//{},scene.imesh + lua_setmetatable(L,-2);//{} + + lua_pushlightuserdata(L,mesh);//{},ud_mesh + lua_setfield(L,-2,"node");//{} + + return 1; } -static const luaL_reg imesh_f[] = { - {"new", newiscenemesh}, -// {"gethandeler", guigethandeler}, -// {"sethandeler", guisethandeler}, - {0,0}, -}; +// ud_node :: ({v3 size}, {v3 origin}) +void makenewiscenecube(lua_State* L){ + double x,y,z; + popvector3d(L,&x,&y,&z);//{v3 size} + double sx,sy,sz; + popvector3d(L,&sx,&sy,&sz);//{} + IMeshSceneNode* n = device->getSceneManager()->addCubeSceneNode(10,0,-1,core::vector3df(x,y,z),core::vector3df(0,0,0),core::vector3df(sx,sy,sz)); + lua_pushlightuserdata(L,n); +} -static const luaL_reg imesh_m[] = { - {"setMaterial", iscenesetmaterial}, - {"getpos", iscenegetpos}, - {"setpos", iscenesetpos}, -// {"settext", setiguitext}, -// {"remove", removeiguielement}, - {0, 0}, -}; +// {} :: scene.newcube({v3 size}, {v3 origin}) +int newiscenecube(lua_State* L){//{v3 size}, {v3 origin} + makenewiscenecube(L);//ud_node + ISceneNode* n = (ISceneNode*)lua_touserdata(L,-1);//ud_node + lua_pop(L,1);// + lua_newtable(L);//{} + luaL_getmetatable(L,"scene.icube");//{},scene.icube + lua_setmetatable(L,-2);//{} -int imesh_register(lua_State* L, IrrlichtDevice* d){ + lua_pushlightuserdata(L,n);//{},ud_mesh + lua_setfield(L,-2,"node");//{} - device = d; + return 1; +} - luaL_newmetatable(L, "scene.imesh"); +// self:setMaterial("path/to/material") +int iscenesetmaterial(lua_State* L){//self,"path/to" + ISceneNode* node = (IMeshSceneNode*)lua_touserdata(L,-2); + const char* s = lua_tostring(L,-1); + //ISceneNode* i = toiscenenode(L,1)->n; + //const char* s = luaL_optstring(L,2,"error.png"); + //printf("Setting material to %s",s); - luaL_register(L,"imesh",imesh_f); + IVideoDriver* driver = device->getVideoDriver(); + node->setMaterialTexture(0, driver->getTexture(s)); - lua_pushstring(L,"__index"); - lua_pushstring(L,"gethandeler"); - lua_gettable(L,-3); - lua_settable(L,-4); + lua_pop(L,2); + return 0; +} - lua_pushstring(L,"__newindex"); - lua_pushstring(L,"sethandeler"); - lua_gettable(L,-3); - lua_settable(L,-4); +static const luaL_reg imesh_m[] = { + {"setMaterial", iscenesetmaterial}, + {"getpos", iscenegetpos}, + {"setpos", iscenesetpos}, + // {"remove", removeiguielement}, + {0, 0}, +}; + +static const luaL_reg icube_m[] = { + {0,0}, +}; - luaL_register(L, NULL, imesh_m); +void imesh_register(lua_State* L){ + + luaL_newmetatable(L, "scene.imesh");//scene.icamera + lua_newtable(L);//scene.icamera,{} + luaL_register(L,NULL,imesh_m);//scene.icamera,{} + lua_setfield(L,-2,"__index");//scene.icamera + lua_pop(L,1);// + + luaL_newmetatable(L,"scene.icube");//scene.icube + lua_newtable(L);//scene.icube, {} + luaL_register(L,NULL,imesh_m);//scene.icube,{} + luaL_register(L,NULL,icube_m);//scene.icube,{} + lua_setfield(L,-2,"__index");//scene.icube + lua_pop(L,1);// + + lua_getglobal(L,"scene"); + lua_pushcfunction(L,newiscenemesh); + lua_setfield(L,-2,"newmesh"); + lua_pushcfunction(L,newiscenecube); + lua_setfield(L,-2,"newcube"); - return 1; } |
