aboutsummaryrefslogtreecommitdiff
path: root/src/client/main.cpp
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-07-04 15:15:48 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2017-07-04 15:15:48 -0400
commitb98dbac4ed2f755ce71bd9be17f26a3f86c1e3cc (patch)
treec9d7755ca9a308706c4dc1b6fd1eaa690d8ec46e /src/client/main.cpp
parentbced528a07a2ff1591455e4c4228ec18f8e0532d (diff)
downloadbrokengine-b98dbac4ed2f755ce71bd9be17f26a3f86c1e3cc.tar.gz
brokengine-b98dbac4ed2f755ce71bd9be17f26a3f86c1e3cc.tar.bz2
brokengine-b98dbac4ed2f755ce71bd9be17f26a3f86c1e3cc.zip
Lots of changes
* client no longer crashes when closed * started work on adding physics types * some bugfixes to core lua api
Diffstat (limited to 'src/client/main.cpp')
-rw-r--r--src/client/main.cpp233
1 files changed, 220 insertions, 13 deletions
diff --git a/src/client/main.cpp b/src/client/main.cpp
index 71a51c6..65dd99c 100644
--- a/src/client/main.cpp
+++ b/src/client/main.cpp
@@ -15,6 +15,7 @@ extern "C" {
#include "lua_api/load_gui.hpp"
#include "lua_api/load_game.hpp"
#include "lua_api/load_core.hpp"
+#include "lua_api/load_phys.hpp"
#include "callbackhandeler.hpp"
using namespace irr;
@@ -24,6 +25,9 @@ using namespace video;
using namespace io;
using namespace gui;
+btDiscreteDynamicsWorld* World;
+core::list<btRigidBody*> Objects;
+
void loadLLibs(lua_State* L){
luaopen_base(L);
luaopen_table(L);
@@ -33,22 +37,175 @@ void loadLLibs(lua_State* L){
}
lua_State* L;
+IrrlichtDevice* device;
void loadIrrLibs(lua_State* L, IrrlichtDevice* device){
printf("Loading guifuncs...\n");
- load_guifuncs(L,device);
- load_gamefuncs(L,device);
- load_corefuncs(L,device);
+ load_guifuncs(L);
+ load_gamefuncs(L);
+ load_corefuncs(L);
+ load_physfuncs(L);
+}
+
+static int GetRandInt(int TMax) { return rand() % TMax; }
+
+// Removes all objects from the world
+void ClearObjects() {
+
+ for(list<btRigidBody *>::Iterator Iterator = Objects.begin(); Iterator != Objects.end(); ++Iterator) {
+ btRigidBody *Object = *Iterator;
+
+ // Delete irrlicht node
+ ISceneNode *Node = static_cast<ISceneNode *>(Object->getUserPointer());
+ Node->remove();
+
+ // Remove the object from the world
+ World->removeRigidBody(Object);
+
+ delete Object;
+ }
+
+ Objects.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
+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;
+}
+
+// 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.001f, 60);
+
+ btRigidBody *TObject;
+ // Relay the object's orientation to irrlicht
+ for(core::list<btRigidBody *>::Iterator it = Objects.begin(); it != Objects.end(); ++it) {
+
+ //UpdateRender(*Iterator);
+ scene::ISceneNode *Node = static_cast<scene::ISceneNode *>((*it)->getUserPointer());
+ TObject = *it;
+
+ // 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]));
+
+ }
+}
+
+// Creates a base box
+void CreateStartScene(ISceneManager* smgr) {
+
+ ClearObjects();
+ CreateBox(smgr,btVector3(0.0f, 0.0f, 0.0f), core::vector3df(10.0f, 0.5f, 10.0f), 0.0f);
}
int main(int argc, char *argv[]){
+ // Initialize bullet
+ btBroadphaseInterface *BroadPhase = new btAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000));
+ btDefaultCollisionConfiguration *CollisionConfiguration = new btDefaultCollisionConfiguration();
+ btCollisionDispatcher *Dispatcher = new btCollisionDispatcher(CollisionConfiguration);
+ btSequentialImpulseConstraintSolver *Solver = new btSequentialImpulseConstraintSolver();
+ World = new btDiscreteDynamicsWorld(Dispatcher, BroadPhase, Solver, CollisionConfiguration);
+
//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
- IrrlichtDevice *device = spawnIrrDevice(state);
+ device = spawnIrrDevice(state);
if (!device)
return 1;
//Loads libraries for interfaceing with irrlicht
@@ -63,9 +220,7 @@ int main(int argc, char *argv[]){
printf("Failed to open lua file:../data/guitest.lua\n");
}
-
-
- device->setWindowCaption(L"Bork[en]gine Demo");
+ //Load some bullet physics stuff
//Load some menu
loadMenu("Some menu",device);
@@ -73,6 +228,30 @@ int main(int argc, char *argv[]){
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
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<s32>(10,10,260,22), true);
@@ -81,19 +260,47 @@ int main(int argc, char *argv[]){
printf("Everything registered, about to start running device!\n");
+ u32 TimeStamp = irrTimer->getTime(), DeltaTime = 0;
while(device->run()){
- driver->beginScene(true, true, SColor(255,100,101,140));
-
- smgr->drawAll();
- guienv->drawAll();
+ if(device->isWindowActive()){
+ DeltaTime = irrTimer->getTime() - TimeStamp;
+ TimeStamp = irrTimer->getTime();
- driver->endScene();
+ UpdatePhysics(DeltaTime);
+
+ 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_close(state);
+ printf("Claoseing lua state...\n");
+ //lua_close(state);
+ printf("clearing objects...\n");
+ ClearObjects();
+ 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;
}