aboutsummaryrefslogtreecommitdiff
path: root/src/client/main.cpp
blob: fbe707b742b1bafa0379bfa6be18c8cf57d8699c (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
#include <stdio.h>
#include <stdlib.h>
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}
#include <irrlicht.h>

#include <chrono>

#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_scene.hpp"
#include "lua_api/load_phys.hpp"
#include "lua_api/load_video.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<btRigidBody*> Objects;

lua_State* L;
IrrlichtDevice* device;
IVideoDriver* driver;
void loadIrrLibs(lua_State* L, IrrlichtDevice* device){
	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){
	ISceneNode *Node = static_cast<ISceneNode*>(rb->getUserPointer());
	if(Node)
		Node->remove();
}

// 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){
	
	if(TObject->getUserPointer() != NULL){
		//UpdateRender(*Iterator);
		scene::ISceneNode *Node = static_cast<scene::ISceneNode *>((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]));
	}
	
}

SColor background = SColor(255,100,101,140);

//setbackgroundcolor(r,g,b)
int setbackgroundcolor(lua_State* L){
	long r,g,b;
	popvector3i(L,&r,&g,&b);
	background = SColor(255,r,g,b);
	return 0;
}

int main(int argc, char *argv[]){
	printf("Brok[en]gine Client");
	// Initialize bullet
	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/init.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);

	driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();

	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();
	lua_getglobal(L,"GAME");
	lua_pushcfunction(L,setbackgroundcolor);
	lua_setfield(L,-2,"setbackgroundcolor");
	lua_pop(L,1);
	while(device->run()){
		gameloop_net(L);
		gameloop_phys(UpdateElement);
		if(device->isWindowActive()){
			//high_resolution_clock::time_point now = high_resolution_clock::now();
			//duration<double> delta = now-t1;
			//double steps = delta.count() * 100;
			//UpdatePhysics(steps,UpdateElement);
			//t1 = now;
			driver->beginScene(true, true, background);
			smgr->drawAll();
			guienv->drawAll();
			driver->endScene();
		}else{
			device->yield();
		}
		lua_getglobal(state,"GAME");//{}
		lua_getfield(state,-1,"tick");//{},function_tick()
		if(!lua_isnil(state,-1)){
			lua_call(state,0,0);
			lua_pop(state,1);
		}else{
			lua_pop(state,2);
		}
		//lua_pop(state,2);
	}
	//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();

	return 0;
}