aboutsummaryrefslogtreecommitdiff
path: root/src/client/main.cpp
blob: 52de76f962f44e84c91e2dc91d12dbc42e426612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <stdio.h>
#include <stdlib.h>
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}
#include <irrlicht.h>

#include <btBulletDynamicsCommon.h>
#include <cstdlib>

#include "initdevice.hpp"
#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_phys.hpp"
#include "callbackhandeler.hpp"

using namespace irr;
using namespace core;
using namespace scene;
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);
  luaopen_io(L);
  luaopen_string(L);
  luaopen_math(L);
}

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);
}

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;

        printf("Found an object to clean:%p\n",Object);

		// Delete irrlicht node
		ISceneNode *Node = static_cast<ISceneNode *>(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);
		delete Object;
        printf("deleted object\n");
	}

	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.002f, 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[]){
    printf("Brok[en]gine");
    // 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");
  //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);
  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 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);
    printf("Abbout to add camera\n");
    //smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
    //smgr->addCameraSceneNodeMaya();

  printf("Everything registered, about to start running device!\n");

  u32 TimeStamp = irrTimer->getTime(), DeltaTime = 0;
  while(device->run()){
    if(device->isWindowActive()){
        DeltaTime = irrTimer->getTime() - TimeStamp;
        TimeStamp = irrTimer->getTime();

        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_pop(state,2);
  }
  printf("Claoseing lua state...\n");
  //lua_close(state);
  printf("clearing objects...\n");
  ClearObjects(); //Clearing objects must be done after we droped the device.
  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;
}