#include #include extern "C" { #include #include #include } #include #include #include #include #include "initdevice.hpp" #include "menuhandeler.hpp" #include "lua_api/load_gui.hpp" #include "lua_api/load_game.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; //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_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(btDiscreteDynamicsWorld* wr, core::list objs, void(*f)(btRigidBody*)) { for(list::Iterator Iterator = objs.begin(); Iterator != objs.end(); ++Iterator) { btRigidBody *Object = *Iterator; if(f){ (*f)(Object); } // Delete irrlicht node ISceneNode *Node = static_cast(Object->getUserPointer()); Node->remove(); // Remove the object from the world wr->removeRigidBody(Object); delete Object; } objs.clear(); } */ // Converts a quaternion to an euler angle void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler) { btScalar W = TQuat.getW(); btScalar X = TQuat.getX(); btScalar Y = TQuat.getY(); btScalar Z = TQuat.getZ(); float WSquared = W * W; float XSquared = X * X; float YSquared = Y * Y; float ZSquared = Z * Z; TEuler.setX(atan2f(2.0f * (Y * Z + X * W), -XSquared - YSquared + ZSquared + WSquared)); TEuler.setY(asinf(-2.0f * (X * Z - Y * W))); TEuler.setZ(atan2f(2.0f * (X * Y + Z * W), XSquared - YSquared - ZSquared + WSquared)); TEuler *= core::RADTODEG; } void UpdateElement(btRigidBody* TObject){ //UpdateRender(*Iterator); scene::ISceneNode *Node = static_cast((TObject)->getUserPointer()); // Set position btVector3 Point = TObject->getCenterOfMassPosition(); Node->setPosition(core::vector3df((f32)Point[0], (f32)Point[1], (f32)Point[2])); // Set rotation 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*)) { World->stepSimulation(TDeltaTime * 0.02f, 60); // 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 Client"); // Initialize bullet /* btBroadphaseInterface *BroadPhase = new btAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000)); printf("Broadphase\n"); btDefaultCollisionConfiguration *CollisionConfiguration = new btDefaultCollisionConfiguration(); printf("Collision config\n"); btCollisionDispatcher *Dispatcher = new btCollisionDispatcher(CollisionConfiguration); printf("Dispatcher\n"); btSequentialImpulseConstraintSolver *Solver = new btSequentialImpulseConstraintSolver(); 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; //Load the lua libraries loadLLibs(state); //Defined in initdevice.cpp, creates the irrlicht device device = spawnIrrDevice(state); if (!device) 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); device->setEventReceiver(&ger); int iErr = luaL_dofile(state,"../data/guitest.lua"); if(iErr != 0){ lua_error(state); printf("Failed to open lua file:../data/guitest.lua\n"); } //Load some bullet physics stuff //Load some menu loadMenu("Some menu",device); IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IGUIEnvironment* guienv = device->getGUIEnvironment(); ITimer* irrTimer = device->getTimer(); device->setWindowCaption(L"Bork[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()){ 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)); smgr->drawAll(); guienv->drawAll(); driver->endScene(); }else{ device->yield(); } lua_getglobal(state,"GAME"); lua_getfield(state,-1,"tick"); if(!lua_isnil(state,-1)) lua_call(state,0,0); 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); /* printf("cleared objects\n"); delete BroadPhase; printf("deleted broadphase\n"); delete CollisionConfiguration; printf("deleted collision config\n"); delete Dispatcher; printf("Deleted dispatcher\n"); delete Solver; printf("deleted solver\n"); delete World; //Muah ha ha printf("deleted world\n"); */ device->drop(); printf("droped device\n"); return 0; }