/
main.cpp
574 lines (512 loc) · 16.6 KB
/
main.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#include <GL/glew.h>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <cmath>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <lua.hpp>
#include "globals.hpp"
#include "Graphics/GraphicsUtils.hpp"
#include "Graphics/model.hpp"
#include "InputManager.hpp"
#include "ResourceManager.hpp"
#include "Graphics/RenderManager.hpp"
#include "Graphics/Sprite.hpp"
#include "GUI/Console.hpp"
#include "Lua/luabinding.hpp"
#include "Lua/GameObjectBinding.hpp"
#include "Networking/server.hpp"
#include "Lua/SettingsBinding.hpp"
using namespace std;
Console *global_con;
InputManager *im;
ResourceManager resman;
RenderManager rendman;
GuiManager *gui;
SoundManager *soundman;
ENetPeer *serverPeer;
ENetHost *client;
sf::RenderWindow *gwindow;
vector<string> packetList;
ShaderProgram *debugprg;
GLuint debugvbo;
int width, height;
const char *serverDelete = \
"function __serverDelete(id)\n"
"for k,v in pairs(serverObjects) do\n"
"if v:getID() == id then\n"
"v:remove()\n"
"table.remove(serverObjects, k)\n"
"return\n"
"end\n"
"end\n"
"end\n"
"function __addServerObject(obj)\n"
"table.insert(serverObjects, obj)\n"
"return #serverObjects\n"
"end\n"
"function __getServerObject(id)\n"
"for k,v in pairs(serverObjects) do\n"
"if v:getID() == id then\n"
"return v\n"
"end\n"
"end\n"
"return nil\n"
"end\n";
int stringToInt(string input){
int out;
stringstream ss;
ss << input;
ss >> out;
return out;
}
float stringToFloat(string input){
float out;
stringstream ss;
ss << input;
ss >> out;
return out;
}
string intToString(int input){
stringstream out;
out << input;
return out.str();
}
string floatToString(float input){
stringstream out;
out << input;
return out.str();
}
int main(int argc, char *argv[]){
srand(time(NULL));
//Set Default settings
width = 800;
height = 600;
settings.vsync = true;
settings.fullscreen = false;
settings.maxFPS = 0;
settings.AA = 1;
settings.width = width;
settings.height = height;
//Create lua vm and generate bindings
lua_State *l = luaL_newstate();
luaL_openlibs(l);
bindFunctions(l);
//Load server delete function
if(luaL_dostring(l,serverDelete)){
cerr << lua_tostring(l, -1) << endl;
return EXIT_FAILURE;
}
//Load settings from lua
if(luaL_dofile(l,"./data/scripts/settings.lua")){
cerr << lua_tostring(l, -1) << endl;
return EXIT_FAILURE;
}
//Load enet for networking
if(enet_initialize() != 0){
cerr << "Could not initalize ENet" << endl;
return EXIT_FAILURE;
}
atexit(enet_deinitialize);
ENetEvent enetEvent;
//Create sfml window with opengl context
gwindow = NULL;
setSettings();
sf::Event event;
InputManager ime(gwindow);
im = &ime;
Console con(l,sf::Vector2f(0,0),
sf::Color(50,50,50),sf::Color::White);
global_con = &con;
GuiManager guie(im);
guie.add(&con);
gui = &guie;
//Create sound manager
SoundManager m;
soundman = &m;
GLenum glewStatus = glewInit();
if(glewStatus != GLEW_OK){
cerr << "Error: " << glewGetErrorString(glewStatus) << endl;
return EXIT_FAILURE;
}
if(!GLEW_ARB_uniform_buffer_object){
cerr << "UBOs not supported!" << endl;
return EXIT_FAILURE;
}
if(!GLEW_ARB_texture_cube_map_array){
cerr << "Cubemap arrays not supported!" << endl;
return EXIT_FAILURE;
}
if(GLEW_ARB_debug_output){
}else{
cout << "Warning: ARB_debug_output unsupported" << endl;
}
if(GLEW_VERSION_3_0 == false){
cerr << "OpenGL 3.0 not supported!" << endl;
return EXIT_FAILURE;
}
bool programsGood = true;
ShaderProgram skyprg("./data/shaders/skyvertex.glsl",
"./data/shaders/skyfragment.glsl");
if(!skyprg.good)
cerr << "Bad skybox shader program. No skybox will be rendered." << endl;
debugprg = new ShaderProgram("./data/shaders/debuglinesv.glsl","./data/shaders/debuglinesf.glsl");
glGenBuffers(1,&debugvbo);
if(!debugprg->good)
cerr << "Bad physics debug shader program. No collision debug info will be rendered." << endl;
//Load the skybox
rendman.skybox.setModel(resman.loadModel("skybox.iqm"));
ShaderProgram prg("./data/shaders/vertex.glsl",
"./data/shaders/fragment.glsl");
if(!prg.good){
programsGood = false;
cerr << "Bad main shader program. Execution cannot continue." << endl;
}
ShaderProgram depthPrg("./data/shaders/depthv.glsl",
"./data/shaders/depthf.glsl");
if(!depthPrg.good){
programsGood = false;
cerr << "Bad depth shader program. Execution cannot continue." << endl;
}
ShaderProgram deferredPrg("./data/shaders/deferredv.glsl","./data/shaders/deferredf.glsl");
if(!deferredPrg.good){
programsGood = false;
cerr << "Bad deferred shader program. Execution cannot continue." << endl;
}
ShaderProgram sprg("./data/shaders/spritev.glsl","./data/shaders/spritef.glsl");
if(!sprg.good){
programsGood = false;
cerr << "Bad deferred shader program. Execution cannot continue." << endl;
}
spriteprg = &sprg;
if(!programsGood){
return EXIT_FAILURE;
}
//Load main lua file and then call init function
lua_pushnumber(l, width);
lua_setglobal(l, "width");
lua_pushnumber(l, height);
lua_setglobal(l, "height");
lua_newtable(l);
lua_setglobal(l, "serverObjects");
int status = luaL_dofile(l, "./data/scripts/main.lua");
if(status){
cerr << "Could not load file " << lua_tostring(l,-1) << endl;
return EXIT_FAILURE;
}
glGenFramebuffersEXT(1,&rendman.framebuffer);
glGenRenderbuffersEXT(1,&rendman.renderbuffer);
glGenBuffers(1, &rendman.ubo);
glBindBufferARB(GL_UNIFORM_BUFFER,rendman.ubo);
glBufferDataARB(GL_UNIFORM_BUFFER,sizeof(glm::mat4)*MAX_LIGHTS+sizeof(glm::vec4)*(1+MAX_LIGHTS*6),NULL,GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER,0,rendman.ubo);
glBindBufferARB(GL_UNIFORM_BUFFER,0);
glGenTextures(1,&rendman.normalTex);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D,rendman.normalTex);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenTextures(1,&rendman.depthCubemaps);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY_ARB, rendman.depthCubemaps);
glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY_ARB,0,GL_DEPTH_COMPONENT16,512,512,MAX_LIGHTS*6,0,GL_DEPTH_COMPONENT,GL_FLOAT,NULL);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB, GL_TEXTURE_COMPARE_FUNC, GL_GEQUAL);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY_ARB, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glBindRenderbufferEXT(GL_RENDERBUFFER,rendman.renderbuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER,GL_DEPTH_COMPONENT24,width,height);
glBindRenderbufferEXT(GL_RENDERBUFFER,0);
glUseProgram(prg.getID());
glUniform1i(prg.getUniform("shadowCubes"), 1);
glUniform1i(prg.getUniform("normalTex"), 2);
glUniformBlockBinding(prg.getID(),glGetUniformBlockIndex(prg.getID(), "Light"),0);
//Create projection matrix for main render
glm::mat4 projection = glm::perspective(45.0f, 1.0f*width/height, 0.1f, 1000.0f);
glUseProgram(skyprg.getID());
glUniformMatrix4fv(skyprg.getUniform("projection"),1,GL_FALSE,glm::value_ptr(projection));
glUseProgram(debugprg->getID());
glUniformMatrix4fv(debugprg->getUniform("projection"),1,GL_FALSE,glm::value_ptr(projection));
lua_getglobal(l, "init");
status = lua_pcall(l,0,0,0);
if(status){
cout << lua_tostring(l, -1) << endl;
global_con->out.println(lua_tostring(l, -1));
return EXIT_FAILURE;
}
//setfunction for server thread
sf::Thread sThread(&serverMain);
serverThread = &sThread;
//Set stuff for main game loop
sf::Clock dtTimer;
dtTimer.restart();
sf::Time dt;
int majv;
int minv;
glGetIntegerv(GL_MAJOR_VERSION, &majv);
glGetIntegerv(GL_MINOR_VERSION, &minv);
cout << "GL Version: "<< majv << "." << minv << endl;
gwindow->setActive(true);
char *pstr = new char[65536];
while(gwindow->isOpen()){
while(gwindow->pollEvent(event)){
if(event.type == sf::Event::Closed){
gwindow->close();
}
if(event.type == sf::Event::Resized){
width = event.size.width;
height = event.size.height;
glViewport(0,0,width,height);
sf::View view = gwindow->getView();
view.setSize(width,height);
view.setCenter(width/2, height/2);
gwindow->setView(view);
lua_pushnumber(l, width);
lua_setglobal(l, "width");
lua_pushnumber(l, height);
lua_setglobal(l, "height");
}
if(event.type == sf::Event::GainedFocus){
im->setFocus(true);
}
if(event.type == sf::Event::LostFocus){
im->setFocus(false);
}
if(event.type == sf::Event::TextEntered){
if((event.text.unicode == '\b') ||
(event.text.unicode == '\t') ||
(event.text.unicode >= 32 && event.text.unicode <= 126))
im->addInput(sf::String(event.text.unicode));
}
if(event.type == sf::Event::KeyPressed){
//Toggle console is user hits f1
if(event.key.code == sf::Keyboard::F1){
im->getString();
con.visible = !con.visible;
con.updates = !con.updates;
im->setGuiMousePos(sf::Vector2i(width/2,height/2));
} else if(im->isGuiLocked() == false){
lua_getglobal(l, "onKeyDown");
lua_pushnumber(l, event.key.code);
if(lua_pcall(l,1,0,0)){
cout << lua_tostring(l, -1) << endl;
global_con->out.println(lua_tostring(l, -1));
}
}
}
if(event.type == sf::Event::KeyReleased){
if(im->isGuiLocked() == false){
lua_getglobal(l, "onKeyRelease");
lua_pushnumber(l, event.key.code);
if(lua_pcall(l,1,0,0)){
cout << lua_tostring(l, -1) << endl;
global_con->out.println(lua_tostring(l, -1));
}
}
}
}
//Update sounds
soundman->update();
//Handle input packets and send buffered packets
if(client != NULL){
while(enet_host_service(client, &enetEvent, 0) >0){
if(enetEvent.type == ENET_EVENT_TYPE_RECEIVE){
//handle packets here
memcpy(pstr,enetEvent.packet->data,enetEvent.packet->dataLength);
pstr[enetEvent.packet->dataLength] = 0;
string input = pstr;
vector<string> pdata = breakString(input);
if(pdata[0] == "create"){
//Push the object to lua so
//lua side code can control it if it wants to
string modelName = pdata[1];
Model *mod = resman.loadModel(modelName);
if(mod == NULL){
cerr << "Could not create model " << modelName
<< endl;
continue;
}
//lua_getglobal(l, "serverObjects");
lua_getglobal(l, "__addServerObject");
GameObject *out = new (lua_newuserdata(l, sizeof(GameObject))) GameObject;
out->setModel(mod);
out->magic = GOMAGIC;
out->tag = pdata[2];
out->id = stringToInt(pdata[3]);
rendman.drawList.push_back(out);
//Push gameobject onto global serverObjects table to keep track of it
luaL_getmetatable(l, "MetaGO");
lua_setmetatable(l, -2);
//lua_rawseti(l, -2, out->id);
if(lua_pcall(l,1,1,0)){
cout << lua_tostring(l, -1) << endl;
global_con->out.println(lua_tostring(l, -1));
}
int index = l_toNumber(l,-1);
//push gameobject to lua createObject so a game script can handle it
lua_getglobal(l,"serverObjects");
lua_getglobal(l, "createObject"); //push createObject
lua_rawgeti(l, -2, index); // get serverObjects[id]
if(lua_pcall(l,1,0,0)){ //call createObjects(serverObjects[id])
cout << lua_tostring(l, -1) << endl;
global_con->out.println(lua_tostring(l, -1));
}
}
else if(pdata[0] == "move"){
uint32_t id = stringToInt(pdata[1]);
GameObject *obj = rendman.getId(id);
if(obj == NULL){
cerr << "Could not find object " << id << endl;
continue;
}
obj->position.x = stringToFloat(pdata[2]);
obj->position.y = stringToFloat(pdata[3]);
obj->position.z = stringToFloat(pdata[4]);
}
else if(pdata[0] == "rotate"){
uint32_t id = stringToInt(pdata[1]);
GameObject *obj = rendman.getId(id);
if(obj == NULL){
cerr << "Could not find object " << id << endl;
continue;
}
obj->rotation.x = stringToFloat(pdata[2]);
obj->rotation.y = stringToFloat(pdata[3]);
obj->rotation.z = stringToFloat(pdata[4]);
obj->rot.setX(stringToFloat(pdata[5]));
obj->rot.setY(stringToFloat(pdata[6]));
obj->rot.setZ(stringToFloat(pdata[7]));
obj->rot.setW(stringToFloat(pdata[8]));
obj->updateLookat();
}
else if(pdata[0] == "scale"){
uint32_t id = stringToInt(pdata[1]);
GameObject *obj = rendman.getId(id);
if(obj == NULL){
cerr << "Could not find object " << id << endl;
continue;
}
obj->scale.x = stringToFloat(pdata[2]);
obj->scale.y = stringToFloat(pdata[3]);
obj->scale.z = stringToFloat(pdata[4]);
}else if(pdata[0] == "delete") {
uint32_t id = stringToInt(pdata[1]);
GameObject *obj = rendman.getId(id);
if(obj != NULL){
rendman.remove(obj);
lua_getglobal(l,"__serverDelete");
lua_pushnumber(l,obj->id);
if(lua_pcall(l,1,0,0)){
cout << lua_tostring(l, -1) << endl;
global_con->out.println(lua_tostring(l, -1));
}
}
}else if(pdata[0] == "visible") {
uint32_t id = stringToInt(pdata[1]);
GameObject *obj = rendman.getId(id);
obj->visible = stringToInt(pdata[2]);
}else {
lua_getglobal(l,"onReceivePacket");
l_pushStringVector(l,pdata);
if(lua_pcall(l,1,0,0)){
string error = lua_tostring(l,-1);
cout << error << endl;
global_con->out.print(error);
}
}
enet_packet_destroy(enetEvent.packet);
}else if(enetEvent.type == ENET_EVENT_TYPE_DISCONNECT){
//handle server disconnect here
serverPeer = NULL;
enet_host_destroy(client);
client = NULL;
lua_getglobal(l, "onServerDisconnect");
if(lua_pcall(l,0,0,0)){
string error = lua_tostring(l,-1);
cout << error << endl;
global_con->out.print(error);
}
int index=1;
lua_getglobal(l,"serverObjects");
lua_pushvalue(l,-1);
lua_pushnil(l);
while(lua_next(l,-2) != 0){
GameObject *obj=l_toGO(l,-1);
rendman.remove(obj);
lua_pushnil(l);
lua_rawseti(l,-4,index);
index++;
lua_pop(l,1);
}
break;
}
}
//send buffered packets
for(int i=0;i<packetList.size();i++){
const char *cstr = packetList[i].c_str();
ENetPacket *packet = enet_packet_create(
cstr,
packetList[i].length(),
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(serverPeer, 0, packet);
enet_host_flush(client);
}
packetList.clear();
}
//Check if gui is locked and show cursor
if(im->isGuiLocked()){
gwindow->setMouseCursorVisible(true);
} else {
gwindow->setMouseCursorVisible(false);
}
//call lua update
lua_getglobal(l,"update");
lua_pushnumber(l,dt.asSeconds());
if(lua_pcall(l,1,0,0)){
cout << lua_tostring(l, -1) << endl;
global_con->out.println(lua_tostring(l, -1));
}
gui->update();
for(int i=0;i<rendman.drawList.size();i++){
if(rendman.drawList[i]->animate){
rendman.drawList[i]->aTime += dt.asSeconds();
rendman.drawList[i]->model->animate(rendman.drawList[i]->currentAnimation,
rendman.drawList[i]->aTime,&rendman.drawList[i]->outframe);
}
}
//Do all drawing here
glEnable(GL_TEXTURE_CUBE_MAP_ARB);
glUseProgram(depthPrg.getID());
glBindFramebufferEXT(GL_FRAMEBUFFER,rendman.framebuffer);
for(int i=0;i<rendman.lights.size();i++)
rendman.renderDepth(&depthPrg,i);
glUseProgram(deferredPrg.getID());
glUniformMatrix4fv(deferredPrg.getUniform("projection"),1,GL_FALSE,glm::value_ptr(projection));
rendman.renderDeferred(&deferredPrg);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(prg.getID());
glUniformMatrix4fv(prg.getUniform("projection"),1,GL_FALSE,glm::value_ptr(projection));
glUniformMatrix4fv(prg.getUniform("pointProj"),1,GL_FALSE,glm::value_ptr(PLight::pointProjection));
rendman.render(&prg,&skyprg,dt.asSeconds());
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
rendman.renderSprites(spriteprg,dt.asSeconds());
//Do sfml drawing here
gui->draw(gwindow);
gwindow->display();
dt = dtTimer.restart();
}
delete gwindow;
delete[] pstr;
enet_host_destroy(client);
serverRunning = false;
serverThread->wait();
}