From bced528a07a2ff1591455e4c4228ec18f8e0532d Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Mon, 10 Apr 2017 19:55:02 -0400 Subject: Added getpos and setpos functions for scene nodes --- src/client/callbackhandeler.cpp | 43 +++++++---- src/client/config.hpp | 2 +- src/client/initdevice.cpp | 5 +- src/client/lua_api/gameparts.hpp | 1 + src/client/lua_api/gui/iguigeneric.cpp | 27 +++++++ src/client/lua_api/gui/iguigeneric.hpp | 11 +++ src/client/lua_api/gui/iguilabel.cpp | 3 +- src/client/lua_api/load_core.cpp | 31 +++----- src/client/lua_api/load_core.hpp | 4 +- src/client/lua_api/load_gui.cpp | 4 +- src/client/lua_api/scene/icamera.cpp | 72 ++++++++++-------- src/client/lua_api/scene/igeneric.cpp | 70 ++++++++++++++++++ src/client/lua_api/scene/igeneric.hpp | 15 ++++ src/client/lua_api/scene/imesh.cpp | 131 +++++++++++++++++++++++++++++++++ src/client/lua_api/scene/imesh.hpp | 11 +++ src/client/main.cpp | 6 ++ 16 files changed, 362 insertions(+), 74 deletions(-) create mode 100644 src/client/lua_api/gui/iguigeneric.cpp create mode 100644 src/client/lua_api/gui/iguigeneric.hpp create mode 100644 src/client/lua_api/scene/igeneric.cpp create mode 100644 src/client/lua_api/scene/igeneric.hpp create mode 100644 src/client/lua_api/scene/imesh.cpp create mode 100644 src/client/lua_api/scene/imesh.hpp (limited to 'src') diff --git a/src/client/callbackhandeler.cpp b/src/client/callbackhandeler.cpp index f39f4ca..5e5e407 100644 --- a/src/client/callbackhandeler.cpp +++ b/src/client/callbackhandeler.cpp @@ -15,6 +15,8 @@ using namespace irr; using namespace gui; using namespace std; +extern lua_State* L; + std::map > guifuncs; //IrrlichtDevice* device; @@ -31,24 +33,33 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ EEVENT_TYPE type = e.EventType; switch (type){ case EET_GUI_EVENT:{ - IGUIElement* caller = e.GUIEvent.Caller; - EGUI_EVENT_TYPE get = e.GUIEvent.EventType; - printf("detected gui event...\n"); - if ( - guifuncs.find(caller) != guifuncs.end() && - guifuncs[caller].find(get) != guifuncs[caller].end()){ - guifuncs[caller][get](e); - printf("sucessfully called a gui event\n"); - return true; - } - return false; - break; + IGUIElement* caller = e.GUIEvent.Caller; + EGUI_EVENT_TYPE get = e.GUIEvent.EventType; + printf("detected gui event...\n"); + bool callerregistered = guifuncs.find(caller) != guifuncs.end(); + bool callerhasfunc = guifuncs[caller].find(get) != guifuncs[caller].end(); + if (callerregistered && callerhasfunc){ + guifuncs[caller][get](e); + printf("sucessfully called a gui event\n"); + return true; + } + return false; + break; } - case EET_MOUSE_INPUT_EVENT:{ - - } + case EET_MOUSE_INPUT_EVENT:{ + SEvent::SMouseInput se = e.MouseInput; + //printf("X: %d Y: %d\n",se.X, se.Y); + + lua_getglobal(L,"GAME"); + lua_getfield(L,-1,"onMouseMove"); + if(!lua_isnil(L,-1)){ + lua_pushnumber(L,se.X); + lua_pushnumber(L,se.Y); + lua_call(L,2,0); + } + } default: - printf("Called an unknown event\n"); + //printf("Called an unknown event\n"); return false; } } diff --git a/src/client/config.hpp b/src/client/config.hpp index 8b13789..888804e 100644 --- a/src/client/config.hpp +++ b/src/client/config.hpp @@ -1 +1 @@ - +/*This file might contain config suff in the future...*/ diff --git a/src/client/initdevice.cpp b/src/client/initdevice.cpp index 814dd67..e288863 100644 --- a/src/client/initdevice.cpp +++ b/src/client/initdevice.cpp @@ -1,4 +1,7 @@ - +/* + This files runes a lua file that contains settings information, and creates an irrlicht device + with those settings. +*/ extern "C" { #include #include diff --git a/src/client/lua_api/gameparts.hpp b/src/client/lua_api/gameparts.hpp index e6f02a1..2938ee2 100644 --- a/src/client/lua_api/gameparts.hpp +++ b/src/client/lua_api/gameparts.hpp @@ -1,3 +1,4 @@ +#include "../util/hashmap.h" extern "C" { #include #include diff --git a/src/client/lua_api/gui/iguigeneric.cpp b/src/client/lua_api/gui/iguigeneric.cpp new file mode 100644 index 0000000..7f39fdc --- /dev/null +++ b/src/client/lua_api/gui/iguigeneric.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include +#include +extern "C" { + #include + #include + #include +} +#include +#include "../guiparts.hpp" +#include "iguielement.hpp" +#include "../../callbackhandeler.hpp" +#include "../../util/hashmap.h" + +using namespace irr; +using namespace gui; + +extern IrrlichtDevice* device; + +int iguigeneric_register(lua_State* L, IrrlichtDevice* d){ + + return 0; +} diff --git a/src/client/lua_api/gui/iguigeneric.hpp b/src/client/lua_api/gui/iguigeneric.hpp new file mode 100644 index 0000000..287fb4e --- /dev/null +++ b/src/client/lua_api/gui/iguigeneric.hpp @@ -0,0 +1,11 @@ +#include +#include +#include +extern "C" { + #include + #include + #include +} +#include + +int iguigeneric_register(lua_State* L, irr::IrrlichtDevice* d); diff --git a/src/client/lua_api/gui/iguilabel.cpp b/src/client/lua_api/gui/iguilabel.cpp index da720f9..3754ac6 100644 --- a/src/client/lua_api/gui/iguilabel.cpp +++ b/src/client/lua_api/gui/iguilabel.cpp @@ -61,11 +61,10 @@ static int newiguilabel(lua_State* L){ //Create the button IGUIEnvironment* env = guidevice->getGUIEnvironment(); - //printf("Createing button with data %d %d %d %d, %p, %d, %s, %s\n", startx,starty,endx,endy,guielements[parent],gui_elenum,button_label,button_tooltip); //Create the label IGUIStaticText* llabel = (IGUIStaticText*) env->addStaticText(label,core::rect(startx,starty,endx,endy),false,false, guielements[parent], gui_elenum++, false); printf("Created the button\n"); - + //Register it's callback registerguicallback(llabel,EGET_ELEMENT_HOVERED,iguilabelevent); diff --git a/src/client/lua_api/load_core.cpp b/src/client/lua_api/load_core.cpp index 910115f..2109fd5 100644 --- a/src/client/lua_api/load_core.cpp +++ b/src/client/lua_api/load_core.cpp @@ -3,30 +3,21 @@ #include #include extern "C" { - #include - #include - #include + #include + #include + #include } #include +#include "scene/icamera.hpp" +#include "scene/imesh.hpp" -int makevector(lua_State* L){ - lua_newtable(L); - - lua_pushstring("x"); - lua_optint(L,1,0); - lua_settable(L,-2); - - lua_pushstring("y"); - lua_optint(L,2,0); - lua_settable(L,-2); - - lua_pushstring("z"); - lua_optint(L,3,0); - lua_settable(L,-2); - -} +using namespace irr; void load_corefuncs(lua_State* L, IrrlichtDevice* d){ + icamera_register(L,d); + imesh_register(L,d); + lua_pop(L, 1); + lua_newtable(L); - lua_setglobal(L,"GAME"); + lua_setglobal(L,"scene"); } diff --git a/src/client/lua_api/load_core.hpp b/src/client/lua_api/load_core.hpp index 641ca08..6280484 100644 --- a/src/client/lua_api/load_core.hpp +++ b/src/client/lua_api/load_core.hpp @@ -1,5 +1,5 @@ -#ifndef __H_loadgame -#define __H_loadgame +#ifndef __H_loadcore +#define __H_loadcore #include #include #include diff --git a/src/client/lua_api/load_gui.cpp b/src/client/lua_api/load_gui.cpp index ed7faac..49c40e4 100644 --- a/src/client/lua_api/load_gui.cpp +++ b/src/client/lua_api/load_gui.cpp @@ -11,6 +11,7 @@ extern "C" { #include "gui/iguibutton.hpp" #include "gui/iguilabel.hpp" +#include "gui/iguigeneric.hpp" #include "../callbackhandeler.hpp" #include "guiparts.hpp" @@ -33,11 +34,10 @@ void load_guifuncs(lua_State* L, IrrlichtDevice* d){ guidevice = d; gui_elenum = 0; guielements[0] = NULL; - printf("About to initalize guielements vector\n"); - printf("Done initalizeing guilements vector\n"); iguibutton_register(L,d); iguilabel_register(L,d); + iguigeneric_register(L,d); lua_pop(L, 1); lua_newtable(L); diff --git a/src/client/lua_api/scene/icamera.cpp b/src/client/lua_api/scene/icamera.cpp index 2dd5a67..ce54ef2 100644 --- a/src/client/lua_api/scene/icamera.cpp +++ b/src/client/lua_api/scene/icamera.cpp @@ -13,11 +13,13 @@ extern "C" { #include #include "../gameparts.hpp" #include "icamera.hpp" +#include "igeneric.hpp" using namespace irr; using namespace scene; +using namespace core; -//IrrlichtDevice* guidevice; +extern IrrlichtDevice* device; static LISceneNode* checkiscenecamera(lua_State* L, int index){ void* ud = luaL_checkudata(L,index,"scene.icamera"); @@ -36,8 +38,10 @@ static int newiscenecamera(lua_State* L){ //The position of the camera int startx = luaL_optint(L,1,0); int starty = luaL_optint(L,2,0); - int endx = luaL_optint(L,3,startx+100); - int endy = luaL_optint(L,4,starty+100); + int startz = luaL_optint(L,3,0); + int endx = luaL_optint(L,4,startx+100); + int endy = luaL_optint(L,5,starty+100); + int endz = luaL_optint(L,6,startz+100); //Label and tooltip wchar_t* label; @@ -48,65 +52,73 @@ static int newiscenecamera(lua_State* L){ printf("Got the string option\n"); //If the element has a parrent - int parent = 0; + int parent = -1; if(nargs >= 6){ - parent = luaL_optint(L,6,0); + parent = luaL_optint(L,7,0); printf("got the parrent\n"); } - //Create the button - IGUIEnvironment* env = guidevice->getGUIEnvironment(); + //Create the camera + ISceneManager* smgr = device->getSceneManager(); + //IGUIEnvironment* env = device->getGUIEnvironment(); //printf("Createing button with data %d %d %d %d, %p, %d, %s, %s\n", startx,starty,endx,endy,guielements[parent],gui_elenum,button_label,button_tooltip); //Create the label - IGUIStaticText* llabel = (IGUIStaticText*) env->addStaticText(label,core::rect(startx,starty,endx,endy),false,false, guielements[parent], gui_elenum++, false); - printf("Created the button\n"); + //IGUIStaticText* llabel = (IGUIStaticText*) env->addStaticText(label,core::rect(startx,starty,endx,endy),false,false, guielements[parent], gui_elenum++, false); + ICameraSceneNode* cam = smgr->addCameraSceneNode(0, vector3df(startx,starty,startz), vector3df(endx,endy,endz)); + printf("Registered the camera!\n"); //Register it's callback - registerguicallback(llabel,EGET_ELEMENT_HOVERED,iguilabelevent); + //registerguicallback(llabel,EGET_ELEMENT_HOVERED,iguilabelevent); //Create it's lua representation - LIGUIElement* ltext = (LIGUIElement*)lua_newuserdata(L, sizeof(LIGUIElement)); + LISceneNode* lcam = (LISceneNode*)lua_newuserdata(L, sizeof(LISceneNode)); int tref = luaL_ref(L,LUA_REGISTRYINDEX); - iguielements[llabel] = tref; - lua_rawgeti(L,LUA_REGISTRYINDEX,tref);//Put it back on the stack since luaL_ref pops the object. + //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, "gui.iguilabel"); + luaL_getmetatable(L, "scene.iscenecamera"); lua_setmetatable(L, -2); //Create the struct - ltext->e = llabel; - ltext->funcmap = hashmap_new(); - ltext->type = "iguilabel"; + lcam->n = cam; + lcam->funcmap = hashmap_new(); + lcam->type = "iscenecamera"; //Free up anything made in this function - free(label); + //free(label); //Put it on top and return it lua_rawgeti(L,LUA_REGISTRYINDEX,tref); return 1; } -static const luaL_reg iguilabel_f[] = { - {"new", newiguilabel}, - {"gethandeler", guigethandeler}, - {"sethandeler", guisethandeler}, +static const luaL_reg icamera_f[] = { + {"new", newiscenecamera}, +// {"gethandeler", guigethandeler}, +// {"sethandeler", guisethandeler}, {0,0}, }; -static const luaL_reg iguilabel_m[] = { - {"move", moveiguielement}, - {"settext", setiguitext}, - {"remove", removeiguielement}, +static const luaL_reg icamera_m[] = { + {"getpos", iscenegetpos}, + {"setpos", iscenesetpos}, +// {"move", moveiguielement}, +// {"settext", setiguitext}, +// {"remove", removeiguielement}, {0, 0}, }; -int iguilabel_register(lua_State* L, IrrlichtDevice* d){ +int icamera_register(lua_State* L, IrrlichtDevice* d){ - luaL_newmetatable(L, "gui.iguilabel"); + device = d; - luaL_register(L,"iguilabel",iguilabel_f); + printf("icamera registered\n"); + + luaL_newmetatable(L, "scene.icamera"); + + luaL_register(L,"icamera",icamera_f); lua_pushstring(L,"__index"); lua_pushstring(L,"gethandeler"); @@ -118,7 +130,7 @@ int iguilabel_register(lua_State* L, IrrlichtDevice* d){ lua_gettable(L,-3); lua_settable(L,-4); - luaL_register(L, NULL, iguilabel_m); + luaL_register(L, NULL, icamera_m); return 1; } diff --git a/src/client/lua_api/scene/igeneric.cpp b/src/client/lua_api/scene/igeneric.cpp new file mode 100644 index 0000000..51aaf66 --- /dev/null +++ b/src/client/lua_api/scene/igeneric.cpp @@ -0,0 +1,70 @@ +/*This file defines some things that all igui stuff can do*/ +extern "C" { + #include + #include + #include +} +#include +#include "igeneric.hpp" +#include "../gameparts.hpp" + +using namespace irr; +using namespace core; +using namespace scene; + +static LISceneNode* toiscenenode(lua_State* L, int index){ + LISceneNode* ret = (LISceneNode*)lua_touserdata(L,index); + if(ret == NULL) + luaL_typerror(L,index,"LISceneNode"); + return ret; +} + +int iscenegetpos(lua_State* L){ + ISceneNode* i = toiscenenode(L,1)->n; + vector3df pos = i->getAbsolutePosition(); + + lua_createtable(L,3,0); + + lua_pushnumber(L,1); + lua_pushnumber(L,pos.X); + lua_settable(L,-3); + + lua_pushnumber(L,2); + lua_pushnumber(L,pos.Y); + lua_settable(L,-3); + + lua_pushnumber(L,3); + lua_pushnumber(L,pos.Z); + lua_settable(L,-3); + + return 1; +} +int iscenesetpos(lua_State* L){ + + ISceneNode* i = toiscenenode(L,1)->n; + + lua_pushnumber(L,1); + lua_gettable(L,-2); + f32 x = (f32)lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushnumber(L,2); + lua_gettable(L,-2); + f32 y = (f32)lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushnumber(L,3); + lua_gettable(L,-2); + f32 z = (f32)lua_tonumber(L,-1); + lua_pop(L,1); + + printf("Trying to set pos of %p to %f %f %f",i,x,y,z); + + i->setPosition(vector3df(x,y,z)); + i->updateAbsolutePosition(); + vector3df pos = i->getAbsolutePosition(); + printf("After setting pos, new pos is %f %f %f",pos.X,pos.Y,pos.Z); + + + return 0; +} diff --git a/src/client/lua_api/scene/igeneric.hpp b/src/client/lua_api/scene/igeneric.hpp new file mode 100644 index 0000000..848709b --- /dev/null +++ b/src/client/lua_api/scene/igeneric.hpp @@ -0,0 +1,15 @@ +#ifndef __H_iscenegeneric +#define __H_iscenegeneric +#include +#include +extern "C" { + #include + #include + #include +} +#include + +int iscenegetpos(lua_State* L); +int iscenesetpos(lua_State* L); + +#endif 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 +#include +#include +#include +#include +#include +extern "C" { + #include + #include + #include +} +#include +#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; +} diff --git a/src/client/lua_api/scene/imesh.hpp b/src/client/lua_api/scene/imesh.hpp new file mode 100644 index 0000000..bd33c07 --- /dev/null +++ b/src/client/lua_api/scene/imesh.hpp @@ -0,0 +1,11 @@ + +#include +#include +extern "C" { + #include + #include + #include +} +#include + +int imesh_register(lua_State* L, irr::IrrlichtDevice* d); diff --git a/src/client/main.cpp b/src/client/main.cpp index 4e284c5..71a51c6 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -14,6 +14,7 @@ extern "C" { #include "menuhandeler.hpp" #include "lua_api/load_gui.hpp" #include "lua_api/load_game.hpp" +#include "lua_api/load_core.hpp" #include "callbackhandeler.hpp" using namespace irr; @@ -31,15 +32,19 @@ void loadLLibs(lua_State* L){ luaopen_math(L); } +lua_State* L; + void loadIrrLibs(lua_State* L, IrrlichtDevice* device){ printf("Loading guifuncs...\n"); load_guifuncs(L,device); load_gamefuncs(L,device); + load_corefuncs(L,device); } int main(int argc, char *argv[]){ //Create a new lua state, this gets shared everywhere lua_State *state = luaL_newstate(); + L = state; //Load the lua libraries loadLLibs(state); //Defined in initdevice.cpp, creates the irrlicht device @@ -89,5 +94,6 @@ int main(int argc, char *argv[]){ lua_call(state,0,0); } lua_close(state); + device->drop(); return 0; } -- cgit v1.2.3-70-g09d2