aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/scene/imesh.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/lua_api/scene/imesh.cpp')
-rw-r--r--src/client/lua_api/scene/imesh.cpp131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/client/lua_api/scene/imesh.cpp b/src/client/lua_api/scene/imesh.cpp
new file mode 100644
index 0000000..c2b6510
--- /dev/null
+++ b/src/client/lua_api/scene/imesh.cpp
@@ -0,0 +1,131 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <vector>
+#include <memory>
+#include <map>
+#include <functional>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+#include "../gameparts.hpp"
+#include "imesh.hpp"
+#include "igeneric.hpp"
+
+using namespace irr;
+using namespace scene;
+using namespace core;
+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");
+ return (LISceneNode*) ud;
+}
+
+/*
+static LISceneNode* checkismesh(lua_State* L){
+ return checkismesh(L,1);
+}
+*/
+
+//iscenecamera.new(Vector position, Vector lookat, parrent)
+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 );
+ 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;
+}
+
+/*mesh:setmaterial("string",layernum=0)*/
+static int setmaterial(lua_State* L){
+ LISceneNode* n = checkismesh(L,1);
+ u32 layernum = luaL_optint(L,3,0);
+ const char* matfile = luaL_optstring(L,2,"error.png");
+ printf("Setting material on a %s",n->type);
+ IVideoDriver* driver = device->getVideoDriver();
+ n->n->setMaterialTexture( 0, driver->getTexture(matfile) );
+ return 0;
+}
+
+static const luaL_reg imesh_f[] = {
+ {"new", newiscenemesh},
+// {"gethandeler", guigethandeler},
+// {"sethandeler", guisethandeler},
+ {0,0},
+};
+
+static const luaL_reg imesh_m[] = {
+ {"setMaterial", setmaterial},
+ {"getpos", iscenegetpos},
+ {"setpos", iscenesetpos},
+// {"settext", setiguitext},
+// {"remove", removeiguielement},
+ {0, 0},
+};
+
+int imesh_register(lua_State* L, IrrlichtDevice* d){
+
+ device = d;
+
+ printf("imesh registered\n");
+
+ luaL_newmetatable(L, "scene.imesh");
+
+ luaL_register(L,"imesh",imesh_f);
+
+ lua_pushstring(L,"__index");
+ lua_pushstring(L,"gethandeler");
+ lua_gettable(L,-3);
+ lua_settable(L,-4);
+
+ lua_pushstring(L,"__newindex");
+ lua_pushstring(L,"sethandeler");
+ lua_gettable(L,-3);
+ lua_settable(L,-4);
+
+ luaL_register(L, NULL, imesh_m);
+
+ return 1;
+}