aboutsummaryrefslogtreecommitdiff
path: root/src/client/main.cpp
blob: 4e284c509cf596771ccefb16ad6b56629013aa50 (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
#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 "callbackhandeler.hpp"

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

void loadLLibs(lua_State* L){
  luaopen_base(L);
  luaopen_table(L);
  luaopen_io(L);
  luaopen_string(L);
  luaopen_math(L);
}

void loadIrrLibs(lua_State* L, IrrlichtDevice* device){
  printf("Loading guifuncs...\n");
  load_guifuncs(L,device);
  load_gamefuncs(L,device);
}

int main(int argc, char *argv[]){
  //Create a new lua state, this gets shared everywhere
  lua_State *state = luaL_newstate();
  //Load the lua libraries
  loadLLibs(state);
  //Defined in initdevice.cpp, creates the irrlicht device
  IrrlichtDevice *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");
  }



  device->setWindowCaption(L"Bork[en]gine Demo");

  //Load some menu
  loadMenu("Some menu",device);

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

  guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
		rect<s32>(10,10,260,22), true);

	smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));

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

  while(device->run()){
    driver->beginScene(true, true, SColor(255,100,101,140));

    smgr->drawAll();
    guienv->drawAll();

    driver->endScene();
    lua_getglobal(state,"GAME");
    lua_getfield(state,-1,"tick");
    if(!lua_isnil(state,-1))
      lua_call(state,0,0);
  }
  lua_close(state);
  return 0;
}