From 2831e232b886c5e3b0791ea5192f9e5194e6abf3 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Fri, 9 Mar 2018 23:55:49 -0500 Subject: Added IGUIImages Added the ability to display itextures on the gui --- Makefile | 66 +++++++++++++++++----- src/client/callbackhandeler.cpp | 13 ++++- src/client/lua_api/gui/iguiimage.cpp | 100 +++++++++++++++++++++++++++++++++ src/client/lua_api/gui/iguiimage.hpp | 12 ++++ src/client/lua_api/gui/simple.cpp | 55 ++++++++++++++++++ src/client/lua_api/gui/simple.hpp | 0 src/client/lua_api/load_gui.cpp | 2 + src/client/lua_api/load_video.cpp | 21 +++++++ src/client/lua_api/load_video.hpp | 14 +++++ src/client/lua_api/phys/cbphysbox.cpp | 54 ++++++++++++++---- src/client/lua_api/scene/icamera.cpp | 65 +++++++-------------- src/client/lua_api/scene/igeneric.cpp | 2 +- src/client/lua_api/scene/imesh.cpp | 2 +- src/client/lua_api/video/iimage.cpp | 81 ++++++++++++++++++++++++++ src/client/lua_api/video/iimage.hpp | 8 +++ src/client/lua_api/video/itexture.cpp | 41 ++++++++++++++ src/client/lua_api/video/itexture.hpp | 8 +++ src/client/lua_api/video/smaterial.cpp | 51 +++++++++++++++++ src/client/lua_api/video/smaterial.hpp | 8 +++ src/client/main.cpp | 31 +++++----- src/server/main.cpp | 13 +++++ src/shared/lua_api/common.c | 25 +++++++++ src/shared/lua_api/common.h | 1 + src/shared/lua_api/load_phys.cpp | 8 ++- src/shared/lua_api/load_phys.hpp | 7 --- src/shared/lua_api/phys/bgame.cpp | 13 +++++ src/shared/lua_api/phys/bphysbox.cpp | 21 +++++-- src/shared/phys/physcommon.cpp | 8 ++- src/test/test_phys.cpp | 72 ++++++++++++++++++++++++ src/test/test_streams.cpp | 75 +++++++++++++++++++++++++ src/test_bullet.cpp | 72 ++++++++++++++++++++++++ 31 files changed, 844 insertions(+), 105 deletions(-) create mode 100644 src/client/lua_api/gui/iguiimage.cpp create mode 100644 src/client/lua_api/gui/iguiimage.hpp create mode 100644 src/client/lua_api/gui/simple.cpp create mode 100644 src/client/lua_api/gui/simple.hpp create mode 100644 src/client/lua_api/load_video.cpp create mode 100644 src/client/lua_api/load_video.hpp create mode 100644 src/client/lua_api/video/iimage.cpp create mode 100644 src/client/lua_api/video/iimage.hpp create mode 100644 src/client/lua_api/video/itexture.cpp create mode 100644 src/client/lua_api/video/itexture.hpp create mode 100644 src/client/lua_api/video/smaterial.cpp create mode 100644 src/client/lua_api/video/smaterial.hpp create mode 100644 src/shared/lua_api/phys/bgame.cpp create mode 100644 src/test/test_phys.cpp create mode 100644 src/test/test_streams.cpp create mode 100644 src/test_bullet.cpp diff --git a/Makefile b/Makefile index 29b436d..9440bd8 100644 --- a/Makefile +++ b/Makefile @@ -16,9 +16,9 @@ TEST_SRC=src/test/ MASTER_EXE=borkmaster.exe +#Change these on linux CC=g++ MKDIR=mkdir -#Change this on linux RM=rm -f ECHO=printf @@ -30,21 +30,31 @@ INANDIR=./lib/nanomsg/nanomsg-1.0.0/src INCLUDES=-I$(ILUADIR) -I$(IIRRDIR) -I$(IBULDIR) -I$(INANDIR) #library directories -LLUADIR=./lib/luajit -LIRRDIR=./lib/irrlicht -LBULDIR=./lib/bullet -LNANDIR=./lib/nanomsg +LLUADIR=lib/luajit +LIRRDIR=lib/irrlicht +LBULDIR=lib/bullet +LNANDIR=lib/nanomsg LIBRARIES=-L$(LLUADIR) -L$(LIRRDIR) -L$(LBULDIR) -L$(LNANDIR) -DYNAMIC_LIBS = -llua -lnanomsg.dll +#Libraries +LIBLUA=$(LLUADIR)/liblua.a +LIBIRR=$(LIRRDIR)/libIrrlicht.a +LIBBCO=$(LBULDIR)/libBulletCollision.a +LIBBDY=$(LBULDIR)/libBulletDynamics.a +LIBBLM=$(LBULDIR)/libLinearMath.a +LIBNAN=$(LNANDIR)/libnanomsg.dll.a + +DLLIRR=$(LIRRDIR)/Irrlicht.dll + +DYNAMIC_LIBS = -llua -lnanomsg.dll -lIrrlicht #order matters! -STATIC_LIBS = -lBulletDynamics -lBulletCollision -lLinearMath -lIrrlicht +STATIC_LIBS = -lBulletDynamics -lBulletCollision -lLinearMath LIBS=-Bstatic $(STATIC_LIBS) -Bdynamic $(DYNAMIC_LIBS) -SLIBS=$(LBULDIR)/libBulletDynamics.a $(LBULDIR)/libBulletCollision.a $(LBULDIR)/libLinearMath.a $(LLUADIR)/liblua.a $(LIRRDIR)/libIrrlicht.a $(LNANDIR)/libnanomsg.dll.a $(LIRRDIR)/Irrlicht.dll +SLIBS=$(LIBLUA) $(LIBIRR) $(LIBBDY) $(LIBBCO) $(LIBBLM) $(LIBNAN) $(DLLIRR) #can't compile with -std=c++11 because irrlicht will complain -CFLAGS=-Wall -g $(INCLUDES) $(LIBRARIES) +CFLAGS=-Wall -g -static $(INCLUDES) LDFLAGS=-mwindows all : $(CLIENT_PATH) $(SERVER_PATH) @@ -52,8 +62,8 @@ client : $(CLIENT_PATH) server : $(SERVER_PATH) #Compile the client -$(CLIENT_PATH) : $(CLIENT_BUILD)main.o $(CLIENT_BUILD)initdevice.o $(CLIENT_BUILD)menuhandeler.o $(CLIENT_BUILD)load_gui.o $(CLIENT_BUILD)callbackhandeler.o $(CLIENT_BUILD)load_game.o $(CLIENT_BUILD)load_scene.o $(CLIENT_BUILD)load_phys.o $(CLIENT_BUILD)common.o $(CLIENT_BUILD)load_net.o $(CLIENT_BUILD)physcommon.o $(SLIBS) - $(CC) -static -o $(CLIENT_PATH) $(CFLAGS) $(LIBS) $(CLIENT_BUILD)*.o $(SLIBS) +$(CLIENT_PATH) : $(CLIENT_BUILD)main.o $(CLIENT_BUILD)initdevice.o $(CLIENT_BUILD)menuhandeler.o $(CLIENT_BUILD)load_gui.o $(CLIENT_BUILD)callbackhandeler.o $(CLIENT_BUILD)load_game.o $(CLIENT_BUILD)load_scene.o $(CLIENT_BUILD)load_phys.o $(CLIENT_BUILD)common.o $(CLIENT_BUILD)load_net.o $(CLIENT_BUILD)physcommon.o $(SLIBS) $(CLIENT_BUILD)load_video.o + $(CC) -o $(CLIENT_PATH) -static $(CLIENT_BUILD)*.o $(LIBRARIES) $(DYNAMIC_LIBS) $(STATIC_LIBS) $(SLIBS) $(CLIENT_BUILD)main.o : $(CLIENT_SRC)main.cpp $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)main.o $(CLIENT_SRC)main.cpp @@ -79,15 +89,30 @@ $(CLIENT_BUILD)menuhandeler.o : $(CLIENT_SRC)menuhandeler.cpp $(CLIENT_SRC)menuh $(CLIENT_BUILD)load_game.o : $(CLIENT_SRC)lua_api/load_game.cpp $(CLIENT_SRC)lua_api/load_game.hpp $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)load_game.o $(CLIENT_SRC)lua_api/load_game.cpp -$(CLIENT_BUILD)load_gui.o : $(CLIENT_BUILD)iguibutton.o $(CLIENT_BUILD)iguilabel.o $(CLIENT_BUILD)iguielement.o $(CLIENT_BUILD)iguigeneric.o $(CLIENT_BUILD)iguiwindow.o $(CLIENT_BUILD)iguiskin.o $(CLIENT_BUILD)iguicheckbox.o $(CLIENT_BUILD)common.o +$(CLIENT_BUILD)load_gui.o : $(CLIENT_BUILD)iguibutton.o $(CLIENT_BUILD)iguilabel.o $(CLIENT_BUILD)iguielement.o $(CLIENT_BUILD)iguigeneric.o $(CLIENT_BUILD)iguiwindow.o $(CLIENT_BUILD)iguiskin.o $(CLIENT_BUILD)iguicheckbox.o $(CLIENT_BUILD)common.o $(CLIENT_BUILD)iguiimage.o $(CLIENT_SRC)lua_api/load_gui.cpp $(CLIENT_SRC)lua_api/load_gui.hpp $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)load_gui.o $(CLIENT_SRC)lua_api/load_gui.cpp +$(CLIENT_BUILD)iguiimage.o : $(CLIENT_SRC)lua_api/gui/iguiimage.cpp $(CLIENT_SRC)lua_api/gui/iguiimage.hpp + $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)iguiimage.o $(CLIENT_SRC)lua_api/gui/iguiimage.cpp + $(CLIENT_BUILD)iguiwindow.o : $(CLIENT_BUILD)iguiutil.o $(CLIENT_SRC)lua_api/gui/iguiwindow.cpp $(CLIENT_SRC)lua_api/gui/iguiwindow.hpp $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)iguiwindow.o $(CLIENT_SRC)lua_api/gui/iguiwindow.cpp $(CLIENT_BUILD)iguiutil.o : $(CLIENT_SRC)lua_api/gui/iguiutil.cpp $(CLIENT_SRC)lua_api/gui/iguiutil.hpp $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)iguiutil.o $(CLIENT_SRC)lua_api/gui/iguiutil.cpp +$(CLIENT_BUILD)load_video.o : $(CLIENT_SRC)lua_api/load_video.cpp $(CLIENT_SRC)lua_api/load_video.hpp $(CLIENT_BUILD)smaterial.o $(CLIENT_BUILD)itexture.o $(CLIENT_BUILD)iimage.o + $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)load_video.o $(CLIENT_SRC)lua_api/load_video.cpp + +$(CLIENT_BUILD)smaterial.o : $(CLIENT_SRC)lua_api/video/smaterial.cpp $(CLIENT_SRC)lua_api/video/smaterial.hpp + $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)smaterial.o $(CLIENT_SRC)lua_api/video/smaterial.cpp + +$(CLIENT_BUILD)itexture.o : $(CLIENT_SRC)lua_api/video/itexture.cpp $(CLIENT_SRC)lua_api/video/itexture.hpp + $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)itexture.o $(CLIENT_SRC)lua_api/video/itexture.cpp + +$(CLIENT_BUILD)iimage.o : $(CLIENT_SRC)lua_api/video/iimage.cpp $(CLIENT_SRC)lua_api/video/iimage.hpp + $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)iimage.o $(CLIENT_SRC)lua_api/video/iimage.cpp + $(CLIENT_BUILD)load_phys.o : $(CLIENT_BUILD)cbphysbox.o $(CLIENT_BUILD)bphysmodel.o $(CC) $(CFLAGS) -c -o $(CLIENT_BUILD)load_phys.o $(CLIENT_SRC)lua_api/load_phys.cpp @@ -142,7 +167,7 @@ $(CLIENT_BUILD)hashmap.o : $(CLIENT_SRC)util/hashmap.c $(CLIENT_SRC)util/hashmap #end compile client #compile server -$(SERVER_PATH) : $(SERVER_BUILD)main.o $(SERVER_BUILD)hashmap.o $(SERVER_BUILD)common.o $(SERVER_BUILD)physcommon.o $(SERVER_BUILD)load_net.o +$(SERVER_PATH) : $(SERVER_BUILD)main.o $(SERVER_BUILD)hashmap.o $(SERVER_BUILD)common.o $(SERVER_BUILD)physcommon.o $(SERVER_BUILD)load_net.o $(SERVER_BUILD)load_phys.o $(CC) -static -o $(SERVER_PATH) $(CFLAGS) $(SERVER_BUILD)*.o $(SLIBS) $(SERVER_BUILD)main.o : $(SERVER_SRC)main.cpp $(SERVER_SRC)testclient.cpp @@ -157,21 +182,34 @@ $(SERVER_BUILD)hashmap.o : $(SHARED_SRC)util/hashmap.c $(SHARED_SRC)util/hashmap $(SERVER_BUILD)common.o : $(SHARED_SRC)lua_api/common.c $(SHARED_SRC)lua_api/common.h $(CC) $(CFLAGS) -c -o $(SERVER_BUILD)common.o $(SHARED_SRC)lua_api/common.c +# Net $(SERVER_BUILD)load_net.o : $(SHARED_SRC)lua_api/load_net.cpp $(SHARED_SRC)lua_api/load_net.hpp $(SERVER_BUILD)stream.o $(CC) $(CFLAGS) -c -o $(SERVER_BUILD)load_net.o $(SHARED_SRC)lua_api/load_net.cpp $(SERVER_BUILD)stream.o : $(SHARED_SRC)lua_api/stream.cpp $(SHARED_SRC)lua_api/stream.hpp $(CC) $(CFLAGS) -c -o $(SERVER_BUILD)stream.o $(SHARED_SRC)lua_api/stream.cpp +# Phys +$(SERVER_BUILD)load_phys.o: $(SHARED_SRC)lua_api/load_phys.cpp $(SHARED_SRC)lua_api/load_phys.hpp $(SERVER_BUILD)bphysbox.o + $(CC) $(CFLAGS) -c -o $(SERVER_BUILD)load_phys.o $(SHARED_SRC)lua_api/load_phys.cpp + +$(SERVER_BUILD)bphysbox.o : $(SHARED_SRC)lua_api/phys/bphysbox.cpp $(SHARED_SRC)lua_api/phys/bphysbox.hpp + $(CC) $(CFLAGS) -c -o $(SERVER_BUILD)bphysbox.o $(SHARED_SRC)lua_api/phys/bphysbox.cpp + #end compile server #compile & run tests -test: $(TEST_PATH)test_stream +test: $(TEST_PATH)test_stream $(TEST_PATH)test_phys @$(ECHO) "Testing stream : " @./$(TEST_PATH)test_stream + @$(ECHO) "Testing physics: " + @./$(TEST_PATH)test_phys $(TEST_PATH)test_stream : $(TEST_SRC)test_streams.cpp $(SHARED_SRC)/lua_api/stream.cpp $(SHARED_SRC)/lua_api/stream.hpp @$(CC) $(CFLAGS) -o $(TEST_PATH)test_stream $(TEST_SRC)test_streams.cpp $(SHARED_SRC)/lua_api/stream.cpp $(SHARED_SRC)/lua_api/stream.hpp + +$(TEST_PATH)test_phys : $(TEST_SRC)test_phys.cpp + $(CC) $(CFLAGS) -o $(TEST_PATH)test_phys $(TEST_SRC)test_phys.cpp $(SLIBS) #end tests clean : diff --git a/src/client/callbackhandeler.cpp b/src/client/callbackhandeler.cpp index c548624..cf24cd2 100644 --- a/src/client/callbackhandeler.cpp +++ b/src/client/callbackhandeler.cpp @@ -31,6 +31,7 @@ GlobalEventReceiver::GlobalEventReceiver(IrrlichtDevice* d){ } bool GlobalEventReceiver::OnEvent(const SEvent& e){ EEVENT_TYPE type = e.EventType; + //printf("Onevent called:%d\n",(int)type); switch (type){ case EET_GUI_EVENT:{ IGUIElement* caller = e.GUIEvent.Caller; @@ -48,12 +49,15 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ SEvent::SMouseInput se = e.MouseInput; //printf("X: %d Y: %d\n",se.X, se.Y); - lua_getglobal(L,"GAME"); - lua_getfield(L,-1,"onMouseMove"); + lua_getglobal(L,"GAME");//{} + lua_getfield(L,-1,"onMouseMove");//{},onMouseMove() if(!lua_isnil(L,-1)){ lua_pushnumber(L,se.X); lua_pushnumber(L,se.Y); lua_call(L,2,0); + lua_pop(L,1); + }else{ + lua_pop(L,2); } break; } @@ -68,11 +72,14 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ lua_pushboolean(L,se.Control); lua_pushboolean(L,se.Shift); lua_call(L,4,0); + lua_pop(L,1); + }else{ + lua_pop(L,2); } break; } default: - //printf("Called an unknown event\n"); + printf("Called an unknown event\n"); return false; } } diff --git a/src/client/lua_api/gui/iguiimage.cpp b/src/client/lua_api/gui/iguiimage.cpp new file mode 100644 index 0000000..c349c3e --- /dev/null +++ b/src/client/lua_api/gui/iguiimage.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +extern "C" { + #include + #include + #include +} +#include +#include "../guiparts.hpp" +#include "iguielement.hpp" +#include "iguiwindow.hpp" +#include "iguiutil.hpp" +#include "../../callbackhandeler.hpp" +#include "../../../shared/lua_api/common.h" + +using namespace irr; +using namespace gui; + +extern IrrlichtDevice* device; +extern IGUIEnvironment* env; +//IrrlichtDevice* guidevice; + + +//static bool iguiimageevent(irr::SEvent e){ + //IGUIElement* caller = (IGUIElement*)e.GUIEvent.Caller; + //int ref = iguielements[caller]; + //EGUI_EVENT_TYPE etype = e.GUIEvent.EventType; + //printf("Detected image event\n"); + //if(etype == EGET_ELEMENT_CLOSED){ + //lua_rawgeti(tL,LUA_REGISTRYINDEX,ref); + //printf("getting raw, the thing on the top of stack is a %s\n",luaL_typename(tL,-1)); + //LIGUIElement* tbut = checkiguiwindow(tL,-1); + //int hashmapresponse; + //char* hashkey = (char*)"onclose"; + //int terror = hashmap_get(tbut->funcmap,hashkey,(void**)&hashmapresponse); + //if(terror == MAP_OK){ //Only call if we actually have that function. + //printf("Looks like we have an onclose function, calling!\n"); + //lua_rawgeti(tL,LUA_REGISTRYINDEX,hashmapresponse); //push the function + //lua_rawgeti(tL,LUA_REGISTRYINDEX,ref); //push the referance to iguielement + //lua_call(tL,1,1); + ////int b = lua_isnoneornil(tL,1); + //int a = lua_toboolean(tL,-1); + //printf("a:%d\n",a); + //return a; + //} + //} + //printf("Oh no! an iguiimage generated an event!"); + //return false; +//} + +//new({startx,starty},alpha,itexture) +static int newiguiimage(lua_State* L){ + printf("Creating iguiimage\n"); + + video::ITexture* tex = (video::ITexture*)lua_touserdata(L,-1); + lua_pop(L,1); + + bool usealpha = lua_toboolean(L,-1); + lua_pop(L,1); + + long sx,sy; + popvector2i(L,&sx,&sy); + + IGUIEnvironment* env = device->getGUIEnvironment(); + IGUIImage* img = env->addImage(tex,core::position2d(sx,sy),usealpha,0,-1,L""); + img->setImage(tex); + + lua_pushlightuserdata(L,img);//ud_iguiimg + luaL_getmetatable(L,"iguiimage");//ud_iguiimg,{m_iguiimg} + lua_setmetatable(L,-1);//ud_iguiimg + + return 1; +} + +static const luaL_reg iguiimage_m[] = { + //{"move", moveiguielement}, + //{"settext", setiguitext}, + //{"remove", removeiguielement}, + {0, 0}, +}; + +void iguiimage_register(lua_State* L){ + printf("Loading iguiimage\n"); + luaL_newmetatable(L,"iguiimage");//{m_iguiimg} + lua_newtable(L);//{m_iguiimg},{} + luaL_register(L,NULL,iguiimage_m);//{m_iguiimg},{iguiimg_m} + printf("About to set field\n"); + lua_setfield(L,-2,"__index");//{m_iguiimg} + lua_pop(L,1);// + printf("Got half way\n"); + lua_getglobal(L,"gui");//{gui} + lua_pushcfunction(L,newiguiimage);//{gui},newimg() + lua_setfield(L,-2,"newiguiimage");//{gui} + lua_pop(L,1);// + printf("Finished loading iguiimage\n"); +} diff --git a/src/client/lua_api/gui/iguiimage.hpp b/src/client/lua_api/gui/iguiimage.hpp new file mode 100644 index 0000000..7210ff0 --- /dev/null +++ b/src/client/lua_api/gui/iguiimage.hpp @@ -0,0 +1,12 @@ + + +#include +#include +extern "C" { + #include + #include + #include +} +#include + +int iguiimage_register(lua_State* L); diff --git a/src/client/lua_api/gui/simple.cpp b/src/client/lua_api/gui/simple.cpp new file mode 100644 index 0000000..8790069 --- /dev/null +++ b/src/client/lua_api/gui/simple.cpp @@ -0,0 +1,55 @@ +#include +extern "C" { + #include + #include + #include +} + +// {{startx, starty},{endx,endy}} +lua_torecti(lua_State* L, int number, int* sx, int* sy, int* ex, int* ey){ + lua_pushnumber(L,1);// ...,{},...,1 + lua_gettable(L,number);// ...,{{sx,sy},{ex,ey}},...,{sx,sy} + + lua_pushnumber(L,1);// ...,{{sx,sy},{ex,ey}},...,{sx,sy},1 + lua_gettable(L,-2);// ...,{{sx,sy},{ex,ey}},...,{sx,sy},sx + *sx = lua_tonumber(L,-1);// ...,{{sx,sy},{ex,ey}},...,{sx,sy},sx + + lua_pop(1);// ...,{{sx,sy},{ex,ey}},...,{sx,sy} + + lua_pushnumber(L,2);// ...,{{sx,sy},{ex,ey}},...,{sx,sy},2 + lua_gettable(L,-2);// ...,{{sx,sy},{ex,ey}},...,{sx,sy},sy + *sy = lua_tonumber(L,-1); + + lua_pop(2);// ...,{{sx,sy},{ex,ey}},... + + + lua_pushnumber(L,2);// ...,{{sx,sy},{ex,ey}},...,2 + lua_gettable(L,number);// ...,{{sx,sy},{ex,ey}},...,{ex,ey} + lua_pushnumber(L,1);// ...,{{sx,sy},{ex,ey}},...,{ex,ey},1 + + lua_gettable(L,-2);// ...,{{sx,sy},{ex,ey}},...,{ex,ey},ex + *ex = lua_tonumber(L,-1);// ...,{{sx,sy},{ex,ey}},...,{ex,ey},ex + lua_pop(L,1);// ...,{{sx,sy},{ex,ey}},...,{ex,ey} + + lua_pushnumber(L,2);// ...,{{sx,sy},{ex,ey}},...,{ex,ey},2 + lua_gettable(L,-2);// ...,{{sx,sy},{ex,ey}},...,{ex,ey},ey + *ey = lua_tonumber(L,-1);// ...,{{sx,sy},{ex,ey}},...,{ex,ey},ey + + lua_pop(L,2);// ...,{{sx,sy},{ex,ey}},... +} + +int addIGUIButton(lua_State* L){ + IGUIEnvironment env = lua_touserdata(L,1); + IGUIElement parent = lua_touserdata(L,2); + s32 id = lua_tonumber(L,3); + s32 sx,sy,ex,ey; + lua_torecti(L,4,&sx,&sy,&ex,&ey); + core::rect rect = core::rect(sx,sy,ex,ey); + IGUIElement* button = env->addButton(rect,parent,id, + +} + +void add_gui(lua_State* L){ + +} + diff --git a/src/client/lua_api/gui/simple.hpp b/src/client/lua_api/gui/simple.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/client/lua_api/load_gui.cpp b/src/client/lua_api/load_gui.cpp index f8e8dc0..86c95cc 100644 --- a/src/client/lua_api/load_gui.cpp +++ b/src/client/lua_api/load_gui.cpp @@ -15,6 +15,7 @@ extern "C" { #include "gui/iguiwindow.hpp" #include "gui/iguiskin.hpp" #include "gui/iguicheckbox.hpp" +#include "gui/iguiimage.hpp" #include "../callbackhandeler.hpp" #include "guiparts.hpp" @@ -52,6 +53,7 @@ void load_guifuncs(lua_State* L){ lua_setglobal(L,"gui"); iguicheckbox_register(L); iguiwindow_register(L,device); + iguiimage_register(L); lua_pushcfunction(L,screenwidth); lua_setglobal(L,"scrw"); diff --git a/src/client/lua_api/load_video.cpp b/src/client/lua_api/load_video.cpp new file mode 100644 index 0000000..8662aa0 --- /dev/null +++ b/src/client/lua_api/load_video.cpp @@ -0,0 +1,21 @@ + +extern "C" { + #include + #include + #include +} +#include +#include "video/smaterial.hpp" +#include "video/itexture.hpp" +#include "video/iimage.hpp" + +void load_videofuncs(lua_State* L){ + printf("Loading video libraries...\n"); + lua_newtable(L);//{} + lua_setglobal(L,"video");// + + smaterial_register(L); + itexture_register(L); + iimage_register(L); + +} diff --git a/src/client/lua_api/load_video.hpp b/src/client/lua_api/load_video.hpp new file mode 100644 index 0000000..c80129e --- /dev/null +++ b/src/client/lua_api/load_video.hpp @@ -0,0 +1,14 @@ +#ifndef __H_loadvideo +#define __H_loadvideo +#include +#include +#include +extern "C" { + #include + #include + #include +} +#include + +void load_videofuncs(lua_State* L); +#endif diff --git a/src/client/lua_api/phys/cbphysbox.cpp b/src/client/lua_api/phys/cbphysbox.cpp index 029e6ab..6cbd749 100644 --- a/src/client/lua_api/phys/cbphysbox.cpp +++ b/src/client/lua_api/phys/cbphysbox.cpp @@ -61,6 +61,8 @@ static int newcbphysbox(lua_State* L){// ISceneNode* n = (ISceneNode*)lua_touserdata(L,-1);//ud_iscenenode lua_pop(L,1); + r->setUserPointer(n); + lua_newtable(L);//{} lua_pushlightuserdata(L,r);//{},ud_rigidbody lua_setfield(L,-2,"rigidbody");//{} @@ -75,36 +77,68 @@ static int newcbphysbox(lua_State* L){// //bphysbox:setpos({v3 pos}) int cbphyssetpos(lua_State* L){//{rigidbody=ud_btRigidbody,node=ud_iscenenode},{v3 pos} - printf("calling cbphysbox setpos\n"); + //printf("calling cbphysbox setpos\n"); double x,y,z; popvector3d(L,&x,&y,&z);//{rigidbody=ud_btRigidbody,node=ud_iscenenode} - printf("Getting rigidbody\n"); + //printf("Getting rigidbody\n"); lua_getfield(L,-1,"rigidbody");//{rigidbody=ud_btRigidbody,node=ud_iscenenode},ud_btRigidbody btRigidBody* r = (btRigidBody*)lua_touserdata(L,-1);//{rigidbody=ud_btRigidbody,node=ud_iscenenode},ud_btRigidbody - printf("Got rigidbody, it was %p\n",r); + //printf("Got rigidbody, it was %p\n",r); lua_pop(L,1);//{rigidbody=ud_btRigidbody,node=ud_iscenenode} lua_getfield(L,-1,"node");//{rigidbody=ud_btRigidbody,node=ud_iscenenode},ud_iscenenode ISceneNode* i = (ISceneNode*)lua_touserdata(L,-1);//{btRigidBody=ud_btRigidbody,node=ud_iscenenode},ud_iscenenode - printf("Got node, it was %p\n",i); + //printf("Got node, it was %p\n",i); lua_pop(L,2);// - btTransform bt; + btTransform bt = btTransform(btQuaternion(0,0,0,1), btVector3(x,y,z)); btMotionState* ms = r->getMotionState(); - ms->getWorldTransform(bt); - bt.setOrigin(btVector3(x,y,z)); ms->setWorldTransform(bt); - r->activate(); + r->setWorldTransform(bt); + + r->activate(true); - i->setPosition(vector3df(x,y,z)); - i->updateAbsolutePosition(); + //i->setPosition(vector3df(x,y,z)); + //i->updateAbsolutePosition(); return 0; } +//bphysbox:getpos() +int cbphysgetpos(lua_State* L){//{rigidbody=ud_btRigidbody,node=ud_iscenenode} + //printf("cphysgetpos called, stack size is %d\n",lua_gettop(L)); + lua_getfield(L,-1,"rigidbody");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody + btRigidBody* r = (btRigidBody*) lua_touserdata(L,-1); + lua_pop(L,2); + btTransform bt = r->getCenterOfMassTransform(); + btVector3 p = bt.getOrigin(); + pushvector3d(L,p.x(),p.y(),p.z()); + return 1; +} + +int cbphysgetgravity(lua_State* L){ + lua_getfield(L,-1,"rigidbody");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody + btRigidBody* r = (btRigidBody*) lua_touserdata(L,-1); + lua_pop(L,2); + btVector3 p = r->getGravity(); + pushvector3d(L,p.x(),p.y(),p.z()); + return 1; +} + +int cbphysapplygravity(lua_State* L){ + lua_getfield(L,-1,"rigidbody");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody + btRigidBody* r = (btRigidBody*) lua_touserdata(L,-1); + lua_pop(L,2); + r->applyGravity(); + return 0; +} + static const luaL_reg cbphysbox_m[] = { {"setcpos", cbphyssetpos},//overload + {"getcpos", cbphysgetpos}, + {"getgravity", cbphysgetgravity}, + {"applygravity",cbphysapplygravity}, // {"delete", delbphysbox},//client side delete needs to delete the visual representation {0, 0}, }; diff --git a/src/client/lua_api/scene/icamera.cpp b/src/client/lua_api/scene/icamera.cpp index 3c5e2b0..ec9469d 100644 --- a/src/client/lua_api/scene/icamera.cpp +++ b/src/client/lua_api/scene/icamera.cpp @@ -14,6 +14,7 @@ extern "C" { #include "../gameparts.hpp" #include "icamera.hpp" #include "igeneric.hpp" +#include "../../../shared/lua_api/common.h" using namespace irr; using namespace scene; @@ -97,60 +98,33 @@ static int newiscenefpscamera(lua_State* L){// static int newiscenecamera(lua_State* L){ printf("Createing camera!\n"); int nargs = lua_gettop(L); + double px,py,pz; + double lx,ly,lz; + + int parent = lua_tonumber(L,-1); + lua_pop(L,1); //The position of the camera - int startx = luaL_optint(L,1,0); - int starty = luaL_optint(L,2,0); - 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; - const char* labelopt = luaL_optstring(L,5,"Label"); - int bls = strlen(labelopt); - label = (wchar_t*)malloc(sizeof(wchar_t)*(bls)); - mbstowcs(label,labelopt,bls); - printf("Got the string option\n"); - + popvector3d(L,&px,&py,&pz); + printf("position of camera was %f,%f,%f\n",px,py,pz); + popvector3d(L,&lx,&ly,&lz); + printf("lookat of camera was %f,%f,%f\n",lx,ly,lz); + //If the element has a parrent - if(nargs >= 6){ - printf("got the parrent\n"); + if(nargs >= 3){ + printf("got the parrent\n"); } //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); - ICameraSceneNode* cam = smgr->addCameraSceneNode(0, vector3df(startx,starty,startz), vector3df(endx,endy,endz)); + ICameraSceneNode* cam = smgr->addCameraSceneNode(0, vector3df(px,py,pz), vector3df(lx,ly,lz)); printf("Registered the camera!\n"); + lua_newtable(L); + lua_pushlightuserdata(L,cam); + lua_setfield(L,-2,"node"); + luaL_getmetatable(L,"scene.icamera"); + lua_setmetatable(L,-2); - //Register it's callback - //registerguicallback(llabel,EGET_ELEMENT_HOVERED,iguilabelevent); - - //Create it's lua representation - LISceneNode* lcam = (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.iscenecamera"); - lua_setmetatable(L, -2); - - //Create the struct - lcam->n = cam; - lcam->funcmap = hashmap_new(); - lcam->type = "iscenecamera"; - - //Free up anything made in this function - //free(label); - - //Put it on top and return it - lua_rawgeti(L,LUA_REGISTRYINDEX,tref); return 1; } @@ -167,6 +141,7 @@ static const luaL_reg imayacamera_m[] = { }; static const luaL_reg ifpscamera_m[] = { + {"getpos", iscenegetpos}, {0,0}, }; diff --git a/src/client/lua_api/scene/igeneric.cpp b/src/client/lua_api/scene/igeneric.cpp index 0d061d2..8280008 100644 --- a/src/client/lua_api/scene/igeneric.cpp +++ b/src/client/lua_api/scene/igeneric.cpp @@ -43,7 +43,7 @@ int iscenesetpos(lua_State* L){//{node=ud_IMeshSceneNode},{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); + //printf("After setting pos, new pos is %f %f %f",pos.X,pos.Y,pos.Z); lua_pop(L,2);// return 0; } diff --git a/src/client/lua_api/scene/imesh.cpp b/src/client/lua_api/scene/imesh.cpp index 3f3ae24..f391431 100644 --- a/src/client/lua_api/scene/imesh.cpp +++ b/src/client/lua_api/scene/imesh.cpp @@ -71,7 +71,7 @@ void makenewiscenecube(lua_State* L){ 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)); + IMeshSceneNode* n = device->getSceneManager()->addCubeSceneNode(1,0,-1,core::vector3df(x,y,z),core::vector3df(0,0,0),core::vector3df(sx,sy,sz)); lua_pushlightuserdata(L,n); } diff --git a/src/client/lua_api/video/iimage.cpp b/src/client/lua_api/video/iimage.cpp new file mode 100644 index 0000000..b832783 --- /dev/null +++ b/src/client/lua_api/video/iimage.cpp @@ -0,0 +1,81 @@ + +#include "iimage.hpp" +#include + +#include "../../../shared/lua_api/common.h" + +using namespace irr; +using namespace video; + +extern IrrlichtDevice* device; +extern IVideoDriver* driver; + +//newiimage(ECOLOR_FORMAT,{width,height}) +int newiimage(lua_State* L){ + printf("Attempting to create new iimage\n"); + long w,h; + popvector2i(L,&w,&h); + int format = lua_tonumber(L,-1); + lua_pop(L,1); + printf("All data collected, creating...\n"); + IImage* img = driver->createImage((irr::video::ECOLOR_FORMAT)format,irr::core::dimension2d(w,h)); + lua_pushlightuserdata(L,img); + luaL_getmetatable(L,"iimage"); + lua_setmetatable(L,-2); + printf("Everything sets up, returning\n"); + return 1; +} + +//setPixel({x,y},{r,g,b,a},bool_shouldblend) +int setiimagepixel(lua_State* L){ + printf("Setpixel called\n"); + bool sb = lua_toboolean(L,-1);//{ud_iimage},{x,y},{r,g,b,a},bool_shouldblend + lua_pop(L,1);//{ud_iimage},{x,y},{r,g,b,a} + long r,g,b,a; + popvector4i(L,&r,&g,&b,&a);//{ud_iimage},{x,y} + long x,y; + popvector2i(L,&x,&y);//{ud_iimage} + IImage* img = (IImage*)lua_touserdata(L,-1);//{ud_iimage} + img->setPixel(x,y,SColor(a,r,g,b),sb); + lua_pop(L,1); + return 0; +} + +static const luaL_reg iimage_m[] = { + {"setPixel", setiimagepixel}, + {0,0}, +}; + +#define set_const(l,x) lua_pushstring(l,#x);lua_pushinteger(l,x);lua_settable(l,-3); + +void iimage_register(lua_State* L){ + printf("Registering iimage...\n"); + driver = device->getVideoDriver(); + lua_getglobal(L,"video");//{} + + set_const(L,ECF_A1R5G5B5); + set_const(L,ECF_R5G6B5); + set_const(L,ECF_A8R8G8B8); + set_const(L,ECF_R16F); + set_const(L,ECF_G16R16F); + set_const(L,ECF_A16B16G16R16F); + set_const(L,ECF_R32F); + set_const(L,ECF_G32R32F); + set_const(L,ECF_A32B32G32R32F); + set_const(L,ECF_UNKNOWN); + + lua_pushcfunction(L,newiimage);//{},newiimage() + lua_setfield(L,-2,"newiimage");//{} + lua_pop(L,1);// + + luaL_newmetatable(L,"iimage");//{m_iimage} + + lua_newtable(L);//{m_iimage},{} + luaL_register(L,NULL,iimage_m);//{m_iimage},{iimage} + + lua_setfield(L,-2,"__index");//{m_iimage} + + lua_pop(L,1);// + + printf("registered iimage!\n"); +} diff --git a/src/client/lua_api/video/iimage.hpp b/src/client/lua_api/video/iimage.hpp new file mode 100644 index 0000000..d5890e4 --- /dev/null +++ b/src/client/lua_api/video/iimage.hpp @@ -0,0 +1,8 @@ + +extern "C" { + #include + #include + #include +} + +void iimage_register(lua_State* L); diff --git a/src/client/lua_api/video/itexture.cpp b/src/client/lua_api/video/itexture.cpp new file mode 100644 index 0000000..a7f4652 --- /dev/null +++ b/src/client/lua_api/video/itexture.cpp @@ -0,0 +1,41 @@ + +extern "C" { + #include + #include + #include +} +#include +#include "itexture.hpp" +#include "iimage.hpp" +#include "../../../shared/lua_api/common.h" + +using namespace irr; +using namespace video; + +extern IrrlichtDevice* device; +extern IVideoDriver* driver; +//newtexture(string name,IImage* image) +int newitexture(lua_State* L){ + IImage* im = (IImage*) lua_touserdata(L,-1); + lua_pop(L,1); + const char* name = lua_tostring(L,-1); + lua_pop(L,1); + + ITexture* tex = driver->addTexture(name,im); + if(!tex){ + lua_pushstring(L,"Failed to create texture!"); + lua_error(L); + } + + lua_pushlightuserdata(L,tex); + + return 1; +} + + + +void itexture_register(lua_State* L){ + lua_getglobal(L,"video"); + lua_pushcfunction(L,newitexture); + lua_setfield(L,-2,"newtexture"); +} diff --git a/src/client/lua_api/video/itexture.hpp b/src/client/lua_api/video/itexture.hpp new file mode 100644 index 0000000..35d7174 --- /dev/null +++ b/src/client/lua_api/video/itexture.hpp @@ -0,0 +1,8 @@ + +extern "C" { + #include + #include + #include +} + +void itexture_register(lua_State* L); diff --git a/src/client/lua_api/video/smaterial.cpp b/src/client/lua_api/video/smaterial.cpp new file mode 100644 index 0000000..e5b9eff --- /dev/null +++ b/src/client/lua_api/video/smaterial.cpp @@ -0,0 +1,51 @@ + +extern "C" { + #include + #include + #include +} +#include +#include "smaterial.hpp" + +using namespace irr::video; + +/*This probably needs a _gc metamethod*/ +int newsmaterial(lua_State* L){ + SMaterial* mat = new SMaterial(); + lua_pushlightuserdata(L,mat); + + return 0; +} + +#define set_const(l,x) lua_pushstring(l,#x);lua_pushinteger(l,x);lua_settable(l,-3); + +void smaterial_register(lua_State* L){ + //Add globals dealing with material flags + + lua_getglobal(L,"video");//{} + + set_const(L,EMF_WIREFRAME); + set_const(L,EMF_POINTCLOUD); + set_const(L,EMF_GOURAUD_SHADING); + set_const(L,EMF_LIGHTING); + set_const(L,EMF_ZBUFFER); + set_const(L,EMF_ZWRITE_ENABLE); + set_const(L,EMF_BACK_FACE_CULLING); + set_const(L,EMF_FRONT_FACE_CULLING); + set_const(L,EMF_BILINEAR_FILTER); + set_const(L,EMF_TRILINEAR_FILTER); + set_const(L,EMF_ANISOTROPIC_FILTER); + set_const(L,EMF_FOG_ENABLE); + set_const(L,EMF_NORMALIZE_NORMALS); + set_const(L,EMF_TEXTURE_WRAP); + set_const(L,EMF_ANTI_ALIASING); + set_const(L,EMF_COLOR_MASK); + set_const(L,EMF_COLOR_MATERIAL); + set_const(L,EMF_USE_MIP_MAPS); + set_const(L,EMF_BLEND_OPERATION); + set_const(L,EMF_POLYGON_OFFSET); + + lua_pushcfunction(L,newsmaterial);//{},newsmaterial + lua_setfield(L,-2,"newsmaterial"); + +} diff --git a/src/client/lua_api/video/smaterial.hpp b/src/client/lua_api/video/smaterial.hpp new file mode 100644 index 0000000..7457d69 --- /dev/null +++ b/src/client/lua_api/video/smaterial.hpp @@ -0,0 +1,8 @@ + +extern "C" { + #include + #include + #include +} + +void smaterial_register(lua_State* L); diff --git a/src/client/main.cpp b/src/client/main.cpp index 2d30c70..54aafc0 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -18,6 +18,7 @@ extern "C" { #include "lua_api/load_game.hpp" #include "lua_api/load_scene.hpp" #include "lua_api/load_phys.hpp" +#include "lua_api/load_video.hpp" #include "callbackhandeler.hpp" #include "../shared/lua_api/common.h" @@ -37,13 +38,23 @@ using namespace std::chrono; lua_State* L; IrrlichtDevice* device; - +IVideoDriver* driver; void loadIrrLibs(lua_State* L, IrrlichtDevice* device){ - printf("Loading guifuncs...\n"); + printf("Loading guifuncs..."); load_guifuncs(L); + printf("[OK]\n"); + printf("Loading gamefuncs..."); load_gamefuncs(L); + printf("[OK]\n"); + printf("Loading scenefuncs..."); load_scenefuncs(L); + printf("[OK]\n"); + printf("Loading physfuncs..."); load_physfuncs(L); + printf("[OK]\n"); + printf("Loading videofuncs..."); + load_videofuncs(L); + printf("[OK]\n"); } void RemoveISceneNode(btRigidBody* rb){ @@ -70,6 +81,7 @@ void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler) { } void UpdateElement(btRigidBody* TObject){ + if(TObject->getUserPointer() != NULL){ //UpdateRender(*Iterator); scene::ISceneNode *Node = static_cast((TObject)->getUserPointer()); @@ -83,6 +95,7 @@ void UpdateElement(btRigidBody* TObject){ QuaternionToEuler(TObject->getOrientation(), EulerRotation); Node->setRotation(core::vector3df(EulerRotation[0], EulerRotation[1], EulerRotation[2])); } + } int main(int argc, char *argv[]){ @@ -117,20 +130,18 @@ int main(int argc, char *argv[]){ //Load some menu loadMenu("Some menu",device); - IVideoDriver* driver = device->getVideoDriver(); + driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IGUIEnvironment* guienv = device->getGUIEnvironment(); - device->setWindowCaption(L"Bork[en]gine Client"); + device->setWindowCaption(L"Brok[en]gine Client"); printf("Everything registered, about to start running device!\n"); //u32 TimeStamp = irrTimer->getTime(), DeltaTime = 0; //high_resolution_clock::time_point t1 = high_resolution_clock::now(); while(device->run()){ - printf("Game loop:\n"); gameloop_net(L); - printf("Update phys:\n"); gameloop_phys(UpdateElement); if(device->isWindowActive()){ //high_resolution_clock::time_point now = high_resolution_clock::now(); @@ -138,35 +149,29 @@ int main(int argc, char *argv[]){ //double steps = delta.count() * 100; //UpdatePhysics(steps,UpdateElement); //t1 = now; - printf("beginscene\n"); driver->beginScene(true, true, SColor(255,100,101,140)); - printf("scene drawAll\n"); smgr->drawAll(); - printf("gui drawAll\n"); guienv->drawAll(); - printf("scene end\n"); driver->endScene(); }else{ device->yield(); } lua_getglobal(state,"GAME");//{} lua_getfield(state,-1,"tick");//{},function_tick() - printf("Tick\n"); if(!lua_isnil(state,-1)){ lua_call(state,0,0); lua_pop(state,1); }else{ lua_pop(state,2); } + //lua_pop(state,2); } - printf("Closeing lua state...\n"); //lua_close(state); //printf("clearing objects...\n"); //ClearObjects(World,Objects,RemoveISceneNode); //Clearing objects must be done after we droped the device. phys_shutdown(RemoveISceneNode); device->drop(); - printf("droped device\n"); return 0; } diff --git a/src/server/main.cpp b/src/server/main.cpp index 17fd535..94e0dbd 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -23,6 +23,7 @@ extern "C" { #include "../shared/lua_api/common.h" #include "../shared/lua_api/load_net.hpp" +#include "../shared/lua_api/load_phys.hpp" #include "../shared/phys/physcommon.hpp" using namespace std; @@ -38,9 +39,13 @@ int main (){ printf("Brok[en]gine Server\n"); L = luaL_newstate(); + lua_newtable(L); + lua_setglobal(L,"GAME"); + phys_genesis(); loadLLibs(L); loadNetLibs(L); + loadPhysLibs(L); int iErr = luaL_dofile(L,"../data/init.lua"); if(iErr != 0){ lua_error(L); @@ -49,6 +54,14 @@ int main (){ do{ gameloop(); std::this_thread::yield(); + lua_getglobal(L,"GAME");//{} + lua_getfield(L,-1,"tick");//{},function_tick() + if(!lua_isnil(L,-1)){ + lua_call(L,0,0); + lua_pop(L,1); + }else{ + lua_pop(L,2); + } }while(true); phys_shutdown(NULL); diff --git a/src/shared/lua_api/common.c b/src/shared/lua_api/common.c index 50eb850..9a8baca 100644 --- a/src/shared/lua_api/common.c +++ b/src/shared/lua_api/common.c @@ -86,6 +86,31 @@ int pushvector2i(lua_State* L, long a, long b){ return 1; } +int popvector4i(lua_State* L,long* a,long* b,long* c, long* d){ + lua_pushinteger(L,1);//{v4},1 + lua_gettable(L,-2);//{v4},v4[1] + *a = lua_tointeger(L,-1);//{v4},v4[1] + lua_pop(L,1);//{v4} + + lua_pushinteger(L,2);//{v4},2 + lua_gettable(L,-2);//{v4},v4[2] + *b = lua_tointeger(L,-1);//{v4},v4[2] + lua_pop(L,1);//{v4} + + lua_pushinteger(L,3);//{v4},3 + lua_gettable(L,-2);//{v4},v4[3] + *c = lua_tointeger(L,-1);//{v4},v4[3] + lua_pop(L,1);//{v4} + + lua_pushinteger(L,4);//{v4},3 + lua_gettable(L,-2);//{v4},v4[3] + *d = lua_tointeger(L,-1);//{v4},v4[3] + lua_pop(L,1);//{v4} + + lua_pop(L,1);// + return 0; +} + int popvector3i(lua_State* L,long* a,long* b,long* c){//{v3} lua_pushinteger(L,1);//{v3},1 lua_gettable(L,-2);//{v3},v3[1] diff --git a/src/shared/lua_api/common.h b/src/shared/lua_api/common.h index 9260706..c2e067d 100644 --- a/src/shared/lua_api/common.h +++ b/src/shared/lua_api/common.h @@ -11,6 +11,7 @@ int pushvector3i(lua_State*,long,long,long); int pushvector3d(lua_State*,double,double,double); int pushvector2i(lua_State*,long,long); +int popvector4i(lua_State*,long*,long*,long*,long*); int popvector3i(lua_State*,long*,long*,long*); int popvector3d(lua_State*,double*,double*,double*); int popvector2i(lua_State*,long*,long*); diff --git a/src/shared/lua_api/load_phys.cpp b/src/shared/lua_api/load_phys.cpp index c91396c..14a27ea 100644 --- a/src/shared/lua_api/load_phys.cpp +++ b/src/shared/lua_api/load_phys.cpp @@ -1,6 +1,8 @@ -#include "load_phys.hpp" - +#include "./load_phys.hpp" +#include "./phys/bphysbox.hpp" void loadPhysLibs(lua_State* L){ - + lua_newtable(L);//{} + lua_setglobal(L,"phys"); + bphysbox_register(L); } diff --git a/src/shared/lua_api/load_phys.hpp b/src/shared/lua_api/load_phys.hpp index 5e05642..6f9e9a9 100644 --- a/src/shared/lua_api/load_phys.hpp +++ b/src/shared/lua_api/load_phys.hpp @@ -3,12 +3,5 @@ extern "C" { #include #include } -#include "load_core.hpp" - -struct EntityPhysics : EntityMass{ - btTransform* transform; - btRigidBody* rigidbody; - btCollisionShape* shape; -}EntityPhysics; void loadPhysLibs(lua_State* L); diff --git a/src/shared/lua_api/phys/bgame.cpp b/src/shared/lua_api/phys/bgame.cpp new file mode 100644 index 0000000..b755787 --- /dev/null +++ b/src/shared/lua_api/phys/bgame.cpp @@ -0,0 +1,13 @@ +extern "C" { + #include + #include + #include +} + +#include + +extern btDiscreteDynamicsWorld* World; + +void registergravity(lua_State* L){ + +} diff --git a/src/shared/lua_api/phys/bphysbox.cpp b/src/shared/lua_api/phys/bphysbox.cpp index 3036cf9..78f1c45 100644 --- a/src/shared/lua_api/phys/bphysbox.cpp +++ b/src/shared/lua_api/phys/bphysbox.cpp @@ -45,25 +45,34 @@ void makenewbphysbox(lua_State* L){ btVector3 pos = btVector3(px,py,pz); // Set the initial position of the object - btTransform transform; - transform.setIdentity(); - transform.setOrigin(pos); + btTransform transform = btTransform(btQuaternion(0,0,0,1),pos); + //transform.setIdentity(); + //transform.setOrigin(pos); // Give it a default MotionState btDefaultMotionState* motionstate = new btDefaultMotionState(transform); - + if(!motionstate){ + printf("No motionstate\n"); + } // Create the shape btCollisionShape* shape = new btBoxShape(vshape); + if(!shape){ + printf("no shape\n"); + } // Add mass - btVector3 localinertia; + btVector3 localinertia = btVector3(0,0,0); shape->calculateLocalInertia(mass, localinertia); // Create the rigid body object btRigidBody* rigidbody = new btRigidBody(mass, motionstate, shape, localinertia); + if(!rigidbody){ + printf("No rigidbody\n"); + } // Add it to the world World->addRigidBody(rigidbody); + printf("Added rigid body to world: %p\n",World); Objects.push_back(rigidbody); lua_pushlightuserdata(L,rigidbody);//ud_rigidbody @@ -114,6 +123,7 @@ static int bphyssetpos(lua_State *L){//self,{v3 pos} bt.setOrigin(to); ms->setWorldTransform(bt); i->activate(); + lua_pop(L,1);// return 0; } @@ -139,6 +149,7 @@ static const luaL_reg bphysbox_m[] = { }; void bphysbox_register(lua_State* L){// + printf("Registered bphysbox\n"); luaL_newmetatable(L, "phys.physbox");//{phys.physbox} lua_newtable(L);//{phys.physbox},{} diff --git a/src/shared/phys/physcommon.cpp b/src/shared/phys/physcommon.cpp index c15274d..4f506bb 100644 --- a/src/shared/phys/physcommon.cpp +++ b/src/shared/phys/physcommon.cpp @@ -32,7 +32,7 @@ btCollisionDispatcher* Dispatcher; btSequentialImpulseConstraintSolver* Solver; void phys_genesis(){ - BroadPhase = new btAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000)); + BroadPhase = new btAxisSweep3(btVector3(-1000,-1000,-1000),btVector3(1000,1000,1000)); printf("Broadphase\n"); CollisionConfiguration = new btDefaultCollisionConfiguration(); printf("Collision config\n"); @@ -42,6 +42,8 @@ void phys_genesis(){ printf("Solver\n"); World = new btDiscreteDynamicsWorld(Dispatcher, BroadPhase, Solver, CollisionConfiguration); printf("Physics world init ok.\n"); + World->setGravity(btVector3(0,-10,0)); + printf("Created physics world: %p\n",World); } void phys_shutdown(void(*f)(btRigidBody*)){ @@ -65,7 +67,7 @@ void phys_shutdown(void(*f)(btRigidBody*)){ // Runs the physics simulation. // - TDeltaTime tells the simulation how much time has passed since the last frame so the simulation can run independently of the frame rate. Optionally pass in an argument that will be called on every rigidbody in the world void UpdatePhysics(double TDeltaTime, void(*f)(btRigidBody*)) { - World->stepSimulation(TDeltaTime * 0.02f, 60); + World->stepSimulation(TDeltaTime * 0.2f, 60); if(f){ // Relay the object's orientation to irrlicht for(std::list::iterator it = Objects.begin(); it != Objects.end(); ++it) { @@ -78,7 +80,7 @@ high_resolution_clock::time_point t1 = high_resolution_clock::now(); void gameloop_phys(void(*f)(btRigidBody*)){ high_resolution_clock::time_point now = high_resolution_clock::now(); duration delta = now-t1; - double steps = delta.count() * 100; + double steps = delta.count() * 10; UpdatePhysics(steps,f); t1 = now; } diff --git a/src/test/test_phys.cpp b/src/test/test_phys.cpp new file mode 100644 index 0000000..d56b031 --- /dev/null +++ b/src/test/test_phys.cpp @@ -0,0 +1,72 @@ +#include + +#include + +int main (void) +{ + + btBroadphaseInterface* broadphase = new btDbvtBroadphase(); + + btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); + btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); + + btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver; + + btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration); + + dynamicsWorld->setGravity(btVector3(0, -10, 0)); + + + btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1); + + btCollisionShape* fallShape = new btSphereShape(1); + + + btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -1, 0))); + btRigidBody::btRigidBodyConstructionInfo + groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0)); + btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI); + dynamicsWorld->addRigidBody(groundRigidBody); + + + btDefaultMotionState* fallMotionState = + new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 50, 0))); + btScalar mass = 1; + btVector3 fallInertia(0, 0, 0); + fallShape->calculateLocalInertia(mass, fallInertia); + btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, fallInertia); + btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI); + dynamicsWorld->addRigidBody(fallRigidBody); + + + for (int i = 0; i < 300; i++) { + dynamicsWorld->stepSimulation(1 / 60.f, 10); + + btTransform trans; + fallRigidBody->getMotionState()->getWorldTransform(trans); + + std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl; + } + + dynamicsWorld->removeRigidBody(fallRigidBody); + delete fallRigidBody->getMotionState(); + delete fallRigidBody; + + dynamicsWorld->removeRigidBody(groundRigidBody); + delete groundRigidBody->getMotionState(); + delete groundRigidBody; + + + delete fallShape; + + delete groundShape; + + + delete dynamicsWorld; + delete solver; + delete collisionConfiguration; + delete dispatcher; + delete broadphase; + + return 0; +} \ No newline at end of file diff --git a/src/test/test_streams.cpp b/src/test/test_streams.cpp new file mode 100644 index 0000000..cbcbcac --- /dev/null +++ b/src/test/test_streams.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include + +#include "../shared/lua_api/stream.hpp" + +int main(){ + //Simple + struct stream* s = stream_create(); + stream_writeInt(s,5); + assert(stream_readInt(s) == 5); + stream_free(s); + + //Little more complex + s = stream_create(); + stream_writeInt(s,1); + stream_writeInt(s,2); + stream_writeInt(s,3); + stream_writeInt(s,4); + assert(stream_readInt(s) == 1); + assert(stream_readInt(s) == 2); + assert(stream_readInt(s) == 3); + assert(stream_readInt(s) == 4); + stream_free(s); + + //Make sure we're not leaking memory + //Uncomment this section, and check htop or windows task manager + /* + time_t t; + time(&t); + srand((unsigned) t); + int i,j; + for(i = 0; i < 100000; i++){ + s = stream_create(); + for(j = 0; j < 10000; j++){ + stream_writeInt(s,rand()); + } + stream_free(s); + } + */ + + //Read/write data + s = stream_create(); + char* str = (char*)"One two three four!\n"; + int slen = strlen(str); + stream_writeData(s,str,slen); + char out[slen+1]; + stream_readData(s,slen,out); + out[slen] = '\0'; + assert(strcmp(out,str) == 0); + stream_free(s); + + //Read/write string + s = stream_create(); + stream_writeString(s,str,slen); + char* rstr = stream_readString(s); + assert(strcmp(rstr,str) == 0); + stream_free(s); + + //Make sure the string reads it's null character + s = stream_create(); + stream_writeString(s,str,slen); + stream_writeInt(s,5); + stream_print(s); + stream_readString(s); + stream_print(s); + int i = stream_readInt(s); + assert(i == 5); + stream_free(s); + + printf("OK!\n"); + return 0; +} diff --git a/src/test_bullet.cpp b/src/test_bullet.cpp new file mode 100644 index 0000000..fa17f6a --- /dev/null +++ b/src/test_bullet.cpp @@ -0,0 +1,72 @@ +#include + +#include + +int main (void) +{ + + btBroadphaseInterface* broadphase = new btDbvtBroadphase(); + + btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); + btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); + + btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver; + + btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration); + + dynamicsWorld->setGravity(btVector3(0, -10, 0)); + + + btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1); + + btCollisionShape* fallShape = new btSphereShape(1); + + + btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -1, 0))); + btRigidBody::btRigidBodyConstructionInfo + groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0)); + btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI); + dynamicsWorld->addRigidBody(groundRigidBody); + + + btDefaultMotionState* fallMotionState = + new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 50, 0))); + btScalar mass = 1; + btVector3 fallInertia(0, 0, 0); + fallShape->calculateLocalInertia(mass, fallInertia); + btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, fallInertia); + btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI); + dynamicsWorld->addRigidBody(fallRigidBody); + + + for (int i = 0; i < 300; i++) { + dynamicsWorld->stepSimulation(1 / 60.f, 10); + + btTransform trans; + fallRigidBody->getMotionState()->getWorldTransform(trans); + + std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl; + } + + dynamicsWorld->removeRigidBody(fallRigidBody); + delete fallRigidBody->getMotionState(); + delete fallRigidBody; + + dynamicsWorld->removeRigidBody(groundRigidBody); + delete groundRigidBody->getMotionState(); + delete groundRigidBody; + + + delete fallShape; + + delete groundShape; + + + delete dynamicsWorld; + delete solver; + delete collisionConfiguration; + delete dispatcher; + delete broadphase; + + return 0; +} -- cgit v1.2.3-70-g09d2