-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoreImpl.cpp
172 lines (151 loc) · 4.2 KB
/
CoreImpl.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
#include "jar/core/CoreImpl.hpp"
#include "jar/core/Logger.hpp"
#include "jar/core/CLArguments.hpp"
#include "jar/core/Lua.hpp"
#include "jar/core/FileSystem.hpp"
#include "jar/core/Component.hpp"
#include <jar/luabind/Core.hpp>
#include <luabind/luabind.hpp>
namespace jar {
CoreImpl::CoreImpl() :
mCLArguments(0),
mLua(0),
mPhysFSInitialized(false)
{
//ctor
}
CoreImpl::~CoreImpl()
{
//dtor
}
const bool CoreImpl::Init(int argc, char** argv, const std::string& rootPath)
{
mRootPath = rootPath;
//init Command Line Arguments
mCLArguments = new CLArguments(argc, argv);
if(!mCLArguments)
{
Logger::GetDefaultLogger().Error("Could not allocate memory!");
Deinit();
return false;
}
//init Lua
mLua = new Lua();
if(!mLua)
{
Logger::GetDefaultLogger().Error("Could not allocate memory!");
Deinit();
return false;
}
if(!mLua->Init())
{
Logger::GetDefaultLogger().Error("Could not init Lua: "+mLua->GetLastError());
Deinit();
return false;
}
if(!mLua->OpenBaseLibrary() || !mLua->OpenMathLibrary() || !mLua->OpenStringLibrary() || !mLua->OpenTableLibrary() || !mLua->OpenPackageLibrary())
{
Logger::GetDefaultLogger().Error("Could not open Lua libraries: "+mLua->GetLastError());
Deinit();
return false;
}
if(!mLua->OpenDebugLibrary())
{
Logger::GetDefaultLogger().Error("Could not open Lua debug library: "+mLua->GetLastError());
Deinit();
return false;
}
#ifdef DEBUG
luabind::globals(mLua->GetState())["DEBUG"] = true;
#endif
Logger::GetDefaultLogger().Info("Initialized Lua", 1);
jar::BindCore(mLua->GetState());
Logger::GetDefaultLogger().Info("Exposed Core System to Lua", 1);
//init physicsFS
assert(argc > 0);
if(!fs::Init(argv[0]))
{
Logger::GetDefaultLogger().Error("Cannot initialize filesystem: " + fs::GetLastError());
Deinit();
return false;
}
if(!fs::SetWriteDir("./"))
{
Logger::GetDefaultLogger().Error("Cannot set write directory: " + fs::GetLastError());
Deinit();
return false;
}
mPhysFSInitialized = true;
Logger::GetDefaultLogger().Info("Initialized PhysicsFS", 1);
Logger::GetDefaultLogger().Info("Core Systems successfully initialized!", 1);
for(std::set<Component*>::iterator it = mComponents.begin(); it != mComponents.end(); ++it)
{
if(!(*it)->Init())
{
// init failed, so let's deinitialize everything
Deinit();
return false;
}
}
return true;
}
const bool CoreImpl::Deinit()
{
bool failed = false;
for(std::set<Component*>::iterator it = mComponents.begin(); it != mComponents.end(); ++it)
{
if(!(*it)->Deinit())
{
failed = true;
}
}
if(mPhysFSInitialized)
{
if(!fs::Deinit())
{
//now PhysicsFS is "probably badly screwed up".
Logger::GetDefaultLogger().Error(std::string(fs::GetLastError()));
failed = true;
}
mPhysFSInitialized = false;
}
if(mLua)
{
if(!mLua->Deinit())
{
Logger::GetDefaultLogger().Error("Could not deinitialize Lua: " + mLua->GetLastError());
failed = true;
}
delete mLua;
}
if(mCLArguments)
{
delete mCLArguments;
}
Logger::GetDefaultLogger().Info("Core Systems successfully deinitialized!", 1);
return !failed;
}
void CoreImpl::Update(const TimeType deltaT)
{
for(std::set<Updatable*>::iterator it = mUpdatables.begin(); it != mUpdatables.end(); ++it)
{
(*it)->Update(deltaT);
}
}
void CoreImpl::RegisterComponent(Component* c)
{
mComponents.insert(c);
}
void CoreImpl::UnregisterComponent(Component* c)
{
mComponents.erase(c);
}
void CoreImpl::RegisterUpdatable(Updatable* u)
{
mUpdatables.insert(u);
}
void CoreImpl::UnregisterUpdatable(Updatable* u)
{
mUpdatables.erase(u);
}
} // namespace jar