From 33e6b9627e6a46d388d46f2c5b4d15ba7e9f9904 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Wed, 18 Oct 2017 21:34:55 -0400 Subject: Started refactoring * Finished a basic server * Changed from ZMQ to nanomsg (basically the same though) * Moved some repeated code from the client/server directories into "shared" * Edited makefile to reflect new dependencies --- src/client/main.cpp | 222 +++++++++++++++------------------------------------- 1 file changed, 65 insertions(+), 157 deletions(-) (limited to 'src/client/main.cpp') diff --git a/src/client/main.cpp b/src/client/main.cpp index 52de76f..763c9f3 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -7,6 +7,8 @@ extern "C" { } #include +#include + #include #include @@ -14,139 +16,65 @@ extern "C" { #include "menuhandeler.hpp" #include "lua_api/load_gui.hpp" #include "lua_api/load_game.hpp" -#include "lua_api/load_core.hpp" +#include "lua_api/load_scene.hpp" #include "lua_api/load_phys.hpp" #include "callbackhandeler.hpp" +#include "../shared/lua_api/common.h" +#include "../shared/lua_api/load_net.hpp" +#include "../shared/phys/physcommon.hpp" + using namespace irr; using namespace core; using namespace scene; using namespace video; using namespace io; using namespace gui; +using namespace std::chrono; -btDiscreteDynamicsWorld* World; -core::list Objects; - -void loadLLibs(lua_State* L){ - luaopen_base(L); - luaopen_table(L); - luaopen_io(L); - luaopen_string(L); - luaopen_math(L); -} +//btDiscreteDynamicsWorld* World; +//irr::core::list Objects; lua_State* L; IrrlichtDevice* device; void loadIrrLibs(lua_State* L, IrrlichtDevice* device){ - printf("Loading guifuncs...\n"); - load_guifuncs(L); - load_gamefuncs(L); - load_corefuncs(L); - load_physfuncs(L); + printf("Loading guifuncs...\n"); + load_guifuncs(L); + load_gamefuncs(L); + load_scenefuncs(L); + load_physfuncs(L); } static int GetRandInt(int TMax) { return rand() % TMax; } +void RemoveISceneNode(btRigidBody* rb){ + ISceneNode *Node = static_cast(rb->getUserPointer()); + Node->remove(); +} +/* // Removes all objects from the world -void ClearObjects() { +void ClearObjects(btDiscreteDynamicsWorld* wr, core::list objs, void(*f)(btRigidBody*)) { - for(list::Iterator Iterator = Objects.begin(); Iterator != Objects.end(); ++Iterator) { + for(list::Iterator Iterator = objs.begin(); Iterator != objs.end(); ++Iterator) { btRigidBody *Object = *Iterator; - printf("Found an object to clean:%p\n",Object); - + if(f){ + (*f)(Object); + } // Delete irrlicht node ISceneNode *Node = static_cast(Object->getUserPointer()); - printf("got node\n"); Node->remove(); - printf("removed node\n"); + // Remove the object from the world - World->removeRigidBody(Object); - printf("removed rigidbody\n"); - printf("right before delete, object is %p\n",Object); + wr->removeRigidBody(Object); delete Object; - printf("deleted object\n"); } - - Objects.clear(); + objs.clear(); } +*/ -// Create a box rigid body -// void CreateBox(ISceneManager* irrScene, const btVector3 &TPosition, const core::vector3df &TScale, btScalar TMass) { -// -// // Create an Irrlicht cube -// scene::ISceneNode *Node = irrScene->addCubeSceneNode(1.0f); -// Node->setScale(TScale); -// -// Node->setMaterialFlag(video::EMF_LIGHTING, 1); -// Node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true); -// //Node->setMaterialTexture(0, irrDriver->getTexture("rust0.jpg")); -// -// // Set the initial position of the object -// btTransform Transform; -// Transform.setIdentity(); -// Transform.setOrigin(TPosition); -// -// // Give it a default MotionState -// btDefaultMotionState *MotionState = new btDefaultMotionState(Transform); -// -// // Create the shape -// btVector3 HalfExtents(TScale.X * 0.5f, TScale.Y * 0.5f, TScale.Z * 0.5f); -// btCollisionShape *Shape = new btBoxShape(HalfExtents); -// -// // Add mass -// btVector3 LocalInertia; -// Shape->calculateLocalInertia(TMass, LocalInertia); -// -// // Create the rigid body object -// btRigidBody *RigidBody = new btRigidBody(TMass, MotionState, Shape, LocalInertia); -// -// // Store a pointer to the irrlicht node so we can update it later -// RigidBody->setUserPointer((void *)(Node)); -// -// // Add it to the world -// World->addRigidBody(RigidBody); -// Objects.push_back(RigidBody); -// } -// -// // Create a sphere rigid body -// void CreateSphere(ISceneManager* irrScene, const btVector3 &TPosition, btScalar TRadius, btScalar TMass) { -// -// // Create an Irrlicht sphere -// scene::ISceneNode *Node = irrScene->addSphereSceneNode(TRadius, 32); -// Node->setMaterialFlag(video::EMF_LIGHTING, 1); -// Node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true); -// //Node->setMaterialTexture(0, irrDriver->getTexture("ice0.jpg")); -// -// // Set the initial position of the object -// btTransform Transform; -// Transform.setIdentity(); -// Transform.setOrigin(TPosition); -// -// // Give it a default MotionState -// btDefaultMotionState *MotionState = new btDefaultMotionState(Transform); -// -// // Create the shape -// btCollisionShape *Shape = new btSphereShape(TRadius); -// -// // Add mass -// btVector3 LocalInertia; -// Shape->calculateLocalInertia(TMass, LocalInertia); -// -// // Create the rigid body object -// btRigidBody *RigidBody = new btRigidBody(TMass, MotionState, Shape, LocalInertia); -// -// // Store a pointer to the irrlicht node so we can update it later -// RigidBody->setUserPointer((void *)(Node)); -// -// // Add it to the world -// World->addRigidBody(RigidBody); -// Objects.push_back(RigidBody); -// } -// -// // Converts a quaternion to an euler angle +// Converts a quaternion to an euler angle void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler) { btScalar W = TQuat.getW(); btScalar X = TQuat.getX(); @@ -163,19 +91,9 @@ void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler) { TEuler *= core::RADTODEG; } -// 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. -void UpdatePhysics(u32 TDeltaTime) { - - World->stepSimulation(TDeltaTime * 0.002f, 60); - - btRigidBody *TObject; - // Relay the object's orientation to irrlicht - for(core::list::Iterator it = Objects.begin(); it != Objects.end(); ++it) { - +void UpdateElement(btRigidBody* TObject){ //UpdateRender(*Iterator); - scene::ISceneNode *Node = static_cast((*it)->getUserPointer()); - TObject = *it; + scene::ISceneNode *Node = static_cast((TObject)->getUserPointer()); // Set position btVector3 Point = TObject->getCenterOfMassPosition(); @@ -185,20 +103,25 @@ void UpdatePhysics(u32 TDeltaTime) { btVector3 EulerRotation; QuaternionToEuler(TObject->getOrientation(), EulerRotation); Node->setRotation(core::vector3df(EulerRotation[0], EulerRotation[1], EulerRotation[2])); - - } } +/* +// 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*)) { -// Creates a base box -void CreateStartScene(ISceneManager* smgr) { + World->stepSimulation(TDeltaTime * 0.02f, 60); - ClearObjects(); - //CreateBox(smgr,btVector3(0.0f, 0.0f, 0.0f), core::vector3df(10.0f, 0.5f, 10.0f), 0.0f); + // Relay the object's orientation to irrlicht + for(core::list::Iterator it = Objects.begin(); it != Objects.end(); ++it) { + (*f)(*it); + } } +*/ int main(int argc, char *argv[]){ - printf("Brok[en]gine"); + printf("Brok[en]gine Client"); // Initialize bullet + /* btBroadphaseInterface *BroadPhase = new btAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000)); printf("Broadphase\n"); btDefaultCollisionConfiguration *CollisionConfiguration = new btDefaultCollisionConfiguration(); @@ -209,6 +132,9 @@ int main(int argc, char *argv[]){ printf("Solver\n"); World = new btDiscreteDynamicsWorld(Dispatcher, BroadPhase, Solver, CollisionConfiguration); printf("Physics world init ok.\n"); + */ + phys_genesis(); + //Create a new lua state, this gets shared everywhere lua_State *state = luaL_newstate(); L = state; @@ -220,6 +146,7 @@ int main(int argc, char *argv[]){ return 1; //Loads libraries for interfaceing with irrlicht loadIrrLibs(state,device); + loadNetLibs(state); printf("Loadded irr libs...\n"); //Sets the global event handeler GlobalEventReceiver ger = GlobalEventReceiver(device); @@ -240,44 +167,22 @@ int main(int argc, char *argv[]){ IGUIEnvironment* guienv = device->getGUIEnvironment(); ITimer* irrTimer = device->getTimer(); - device->setWindowCaption(L"Bork[en]gine Demo"); - - /* - CreateBox(smgr, - btVector3( - GetRandInt(10) - 5.0f, 7.0f, - GetRandInt(10) - 5.0f), - core::vector3df( - GetRandInt(3) + 0.5f, - GetRandInt(3) + 0.5f, - GetRandInt(3) + 0.5f), - 1.0f); - CreateBox(smgr, - btVector3(0,0,0), - core::vector3df(10,10,10), - 0.0f); - CreateSphere(smgr, - btVector3( - GetRandInt(10) - 5.0f, 7.0f, - GetRandInt(10) - 5.0f), - GetRandInt(5) / 5.0f + 0.2f, 1.0f); - */ - - //guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!", - // rect(10,10,260,22), true); - printf("Abbout to add camera\n"); - //smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0)); - //smgr->addCameraSceneNodeMaya(); + device->setWindowCaption(L"Bork[en]gine Client"); printf("Everything registered, about to start running device!\n"); - u32 TimeStamp = irrTimer->getTime(), DeltaTime = 0; + //u32 TimeStamp = irrTimer->getTime(), DeltaTime = 0; + //high_resolution_clock::time_point t1 = high_resolution_clock::now(); while(device->run()){ - if(device->isWindowActive()){ - DeltaTime = irrTimer->getTime() - TimeStamp; - TimeStamp = irrTimer->getTime(); - UpdatePhysics(DeltaTime); + gameloop_net(L); + gameloop_phys(UpdateElement); + if(device->isWindowActive()){ + //high_resolution_clock::time_point now = high_resolution_clock::now(); + //duration delta = now-t1; + //double steps = delta.count() * 100; + //UpdatePhysics(steps,UpdateElement); + //t1 = now; driver->beginScene(true, true, SColor(255,100,101,140)); @@ -294,10 +199,12 @@ int main(int argc, char *argv[]){ lua_call(state,0,0); lua_pop(state,2); } - printf("Claoseing lua state...\n"); + printf("Closeing lua state...\n"); //lua_close(state); - printf("clearing objects...\n"); - ClearObjects(); //Clearing objects must be done after we droped the device. + //printf("clearing objects...\n"); + //ClearObjects(World,Objects,RemoveISceneNode); //Clearing objects must be done after we droped the device. + phys_shutdown(RemoveISceneNode); + /* printf("cleared objects\n"); delete BroadPhase; printf("deleted broadphase\n"); @@ -310,6 +217,7 @@ int main(int argc, char *argv[]){ delete World; //Muah ha ha printf("deleted world\n"); + */ device->drop(); printf("droped device\n"); -- cgit v1.2.3-70-g09d2