Leviathan  0.8.0.0
Leviathan game engine
Leviathan::LeviathanApplication Class Referenceabstract

Base class for all leviathan programs. More...

#include <Application.h>

+ Inheritance diagram for Leviathan::LeviathanApplication:

Public Member Functions

DLLEXPORT LeviathanApplication ()
 
virtual DLLEXPORT ~LeviathanApplication ()
 
virtual DLLEXPORT bool Initialize (AppDef *configuration)
 
DLLEXPORT void ForceRelease ()
 Used to immediately terminate the program. More...
 
virtual DLLEXPORT void StartRelease ()
 Safely releases the Application //. More...
 
DLLEXPORT void MarkAsClosing ()
 Thread safely marks the game to close sometime. More...
 
virtual DLLEXPORT int RunMessageLoop ()
 
virtual DLLEXPORT void Render ()
 
virtual DLLEXPORT bool PassCommandLine (int argcount, char *args[])
 
virtual DLLEXPORT void Tick (int mspassed)
 
virtual DLLEXPORT void PreFirstTick ()
 
DLLEXPORT bool Quitting ()
 
DLLEXPORT EngineGetEngine ()
 
DLLEXPORT AppDefGetDefinition ()
 
DLLEXPORT void ClearTimers ()
 Resets all time sensitive timers. More...
 
virtual DLLEXPORT bool InitLoadCustomScriptTypes (asIScriptEngine *engine)
 
virtual DLLEXPORT void RegisterApplicationPhysicalMaterials (PhysicsMaterialManager *manager)
 
virtual DLLEXPORT void CustomizeEnginePostLoad ()
 
virtual DLLEXPORT void EnginePreShutdown ()
 
virtual DLLEXPORT std::shared_ptr< GameWorldGetGameWorld (int id)
 Used to query a world for specific id. More...
 
virtual DLLEXPORT NETWORKED_TYPE GetProgramNetType () const =0
 
- Public Member Functions inherited from Leviathan::ThreadSafeGeneric< MutexType >
DLLEXPORT ThreadSafeGeneric ()
 
DLLEXPORT ~ThreadSafeGeneric ()
 
FORCE_INLINE void VerifyLock (RecursiveLock &guard) const
 
FORCE_INLINE void VerifyLock (Lock &lockit) const
 

Static Public Member Functions

static DLLEXPORT LeviathanApplicationGet ()
 
static DLLEXPORT void DummyGameConfigurationVariables (GameConfiguration *configobj)
 
static DLLEXPORT void DummyGameKeyConfigVariables (KeyConfiguration *keyconfigobj)
 
static DLLEXPORT void StartServerProcess (const std::string &processname, const std::string &commandline)
 

Protected Member Functions

virtual DLLEXPORT void Release ()
 Performs the final steps in the release process. More...
 
virtual DLLEXPORT void _InternalInit ()
 
virtual NetworkInterface_GetApplicationPacketHandler ()=0
 Called in Initialize to get the derived packet handler type. More...
 
virtual void _ShutdownApplicationPacketHandler ()=0
 

Protected Attributes

bool Quit = false
 
bool ShouldQuit = false
 
bool QuitSometime = false
 This can be quickly set anywhere to quit sometime in the future. More...
 
Engine_Engine
 
AppDefApplicationConfiguration = nullptr
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Static Protected Attributes

static LeviathanApplicationCurapp = NULL
 

Detailed Description

Base class for all leviathan programs.

Definition at line 16 of file Application.h.

Constructor & Destructor Documentation

◆ LeviathanApplication()

DLLEXPORT LeviathanApplication::LeviathanApplication ( )

Definition at line 11 of file Application.cpp.

11  :
12  _Engine(new Engine(this))
13 {
14  Curapp = this;
15 }
static LeviathanApplication * Curapp
Definition: Application.h:116
The main class of the Leviathan Game Engine.
Definition: Engine.h:24

◆ ~LeviathanApplication()

DLLEXPORT LeviathanApplication::~LeviathanApplication ( )
virtual

Definition at line 17 of file Application.cpp.

17  {
18 
19  // Release should have been called when exiting the main loop
21  Curapp = nullptr;
22 }
static LeviathanApplication * Curapp
Definition: Application.h:116
#define SAFE_DELETE(x)
Definition: Define.h:116

Member Function Documentation

◆ _GetApplicationPacketHandler()

virtual NetworkInterface* Leviathan::LeviathanApplication::_GetApplicationPacketHandler ( )
protectedpure virtual

Called in Initialize to get the derived packet handler type.

Implemented in Pong::PongMasterServer.

◆ _InternalInit()

DLLEXPORT void LeviathanApplication::_InternalInit ( )
protectedvirtual

called just before returning from initialization, and can be used setting start time etc.

Definition at line 100 of file Application.cpp.

100  {
101 
102 }

◆ _ShutdownApplicationPacketHandler()

virtual void Leviathan::LeviathanApplication::_ShutdownApplicationPacketHandler ( )
protectedpure virtual

Called to destroy the custom packet handler. This is called just before the engine is released and the main loop exited

Implemented in Pong::PongMasterServer.

◆ ClearTimers()

DLLEXPORT void LeviathanApplication::ClearTimers ( )

Resets all time sensitive timers.

Call this after loading resources to avoid long simulate times

Definition at line 162 of file Application.cpp.

162  {
163 
164  _Engine->ClearTimers();
165 }
DLLEXPORT void ClearTimers()
Clears physical timers.
Definition: Engine.cpp:1185

◆ CustomizeEnginePostLoad()

DLLEXPORT void LeviathanApplication::CustomizeEnginePostLoad ( )
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 182 of file Application.cpp.

182  {
183 
184 }

◆ DummyGameConfigurationVariables()

DLLEXPORT void LeviathanApplication::DummyGameConfigurationVariables ( GameConfiguration configobj)
static

Definition at line 196 of file Application.cpp.

198 {
199 
200 }

◆ DummyGameKeyConfigVariables()

DLLEXPORT void LeviathanApplication::DummyGameKeyConfigVariables ( KeyConfiguration keyconfigobj)
static

Definition at line 202 of file Application.cpp.

204 {
205 
206 }

◆ EnginePreShutdown()

DLLEXPORT void LeviathanApplication::EnginePreShutdown ( )
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 186 of file Application.cpp.

186  {
187 
188 }

◆ ForceRelease()

DLLEXPORT void LeviathanApplication::ForceRelease ( )

Used to immediately terminate the program.

Note
Should be only called if initialization fails

Definition at line 81 of file Application.cpp.

81  {
82  GUARD_LOCK();
83  ShouldQuit = true;
84  Quit = true;
85 
86  if(_Engine){
87  // The prelease does some which requires a tick //
89  _Engine->Tick();
90  _Engine->Release(true);
91  }
92 
94 }
DLLEXPORT void PreRelease()
Sets objects ready to be released.
Definition: Engine.cpp:482
DLLEXPORT void Tick()
Definition: Engine.cpp:841
#define SAFE_DELETE(x)
Definition: Define.h:116
#define GUARD_LOCK()
Definition: ThreadSafe.h:91
DLLEXPORT void Release(bool forced=false)
Definition: Engine.cpp:541

◆ Get()

DLLEXPORT LeviathanApplication * LeviathanApplication::Get ( )
static

Definition at line 24 of file Application.cpp.

24  {
25 
26  return Curapp;
27 }
static LeviathanApplication * Curapp
Definition: Application.h:116

◆ GetDefinition()

DLLEXPORT AppDef* Leviathan::LeviathanApplication::GetDefinition ( )
inline

Definition at line 48 of file Application.h.

◆ GetEngine()

DLLEXPORT Engine* Leviathan::LeviathanApplication::GetEngine ( )
inline

Definition at line 47 of file Application.h.

47 { return _Engine;};

◆ GetGameWorld()

DLLEXPORT std::shared_ptr< GameWorld > LeviathanApplication::GetGameWorld ( int  id)
virtual

Used to query a world for specific id.

This is called when the world holder couldn't find a world with the id

Definition at line 190 of file Application.cpp.

190  {
191 
192  return nullptr;
193 }

◆ GetProgramNetType()

virtual DLLEXPORT NETWORKED_TYPE Leviathan::LeviathanApplication::GetProgramNetType ( ) const
pure virtual

◆ Initialize()

DLLEXPORT bool LeviathanApplication::Initialize ( AppDef configuration)
virtual

Definition at line 31 of file Application.cpp.

31  {
32  GUARD_LOCK();
33 
34  // Store configuration //
35  ApplicationConfiguration = configuration;
36 
37  // Init engine //
40  return false;
41 
42  _InternalInit();
43  return true;
44 }
virtual NetworkInterface * _GetApplicationPacketHandler()=0
Called in Initialize to get the derived packet handler type.
virtual DLLEXPORT NETWORKED_TYPE GetProgramNetType() const =0
virtual DLLEXPORT void _InternalInit()
DLLEXPORT bool Init(AppDef *definition, NETWORKED_TYPE ntype, NetworkInterface *packethandler)
Definition: Engine.cpp:92
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ InitLoadCustomScriptTypes()

DLLEXPORT bool LeviathanApplication::InitLoadCustomScriptTypes ( asIScriptEngine *  engine)
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 167 of file Application.cpp.

167  {
168 
169  return true;
170 }

◆ MarkAsClosing()

DLLEXPORT void LeviathanApplication::MarkAsClosing ( )

Thread safely marks the game to close sometime.

The closing should happen in around 2 ticks (100ms)

Definition at line 208 of file Application.cpp.

208  {
209  QuitSometime = true;
210 }
bool QuitSometime
This can be quickly set anywhere to quit sometime in the future.
Definition: Application.h:110

◆ PassCommandLine()

DLLEXPORT bool LeviathanApplication::PassCommandLine ( int  argcount,
char *  args[] 
)
virtual

Reimplemented in Leviathan::MasterServerApplication, and Leviathan::ServerApplication.

Definition at line 96 of file Application.cpp.

96  {
97  return _Engine->PassCommandLine(argcount, args);
98 }
DLLEXPORT bool PassCommandLine(int argcount, char *args[])
Definition: Engine.cpp:1378

◆ PreFirstTick()

DLLEXPORT void LeviathanApplication::PreFirstTick ( )
virtual

Definition at line 108 of file Application.cpp.

108  {
109 
110 }

◆ Quitting()

DLLEXPORT bool Leviathan::LeviathanApplication::Quitting ( )
inline

Definition at line 46 of file Application.h.

46 { return Quit; };

◆ RegisterApplicationPhysicalMaterials()

DLLEXPORT void LeviathanApplication::RegisterApplicationPhysicalMaterials ( PhysicsMaterialManager manager)
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 172 of file Application.cpp.

174 {
175 
176 }

◆ Release()

DLLEXPORT void LeviathanApplication::Release ( )
protectedvirtual

Performs the final steps in the release process.

Warning
This should not be called directly

Definition at line 46 of file Application.cpp.

46  {
47  {
48  GUARD_LOCK();
49  // set as quitting //
50  Quit = true;
51 
52  // Nothing else to do if no engine //
53  if(!_Engine)
54  return;
55 
56  // Shutdown the packet handler
57  // PreRelease should have been done at this point and the NetworkHandler
58  // should have been released so this can no longer be in use
60  }
61 
62  // This avoids deadlocking //
63  _Engine->Release();
64 
65  {
66  GUARD_LOCK();
67  // Delete the already released engine //
68  delete _Engine;
69  _Engine = NULL;
70  }
71 }
virtual void _ShutdownApplicationPacketHandler()=0
#define GUARD_LOCK()
Definition: ThreadSafe.h:91
DLLEXPORT void Release(bool forced=false)
Definition: Engine.cpp:541

◆ Render()

DLLEXPORT void LeviathanApplication::Render ( )
virtual

Definition at line 104 of file Application.cpp.

104  {
105  _Engine->RenderFrame();
106 }
DLLEXPORT void RenderFrame()
Definition: Engine.cpp:958

◆ RunMessageLoop()

DLLEXPORT int LeviathanApplication::RunMessageLoop ( )
virtual

TODO: make this wait happen only if tick wasn't actually and no frame was rendered

Definition at line 112 of file Application.cpp.

112  {
113  // This is almost at tick so call this outside the loop for performance //
115  PreFirstTick();
116 
117  // For reporting wait failures //
118  int FailCount = 0;
119 
120  while(!_Engine->HasPreReleaseBeenDone()){
121  // Store this //
122  bool canprocess = _Engine->GetWindowOpenCount() != 0;
123 
124  _Engine->MessagePump();
125 
126  // Set as quitting //
127  if((!canprocess || QuitSometime) && !ShouldQuit){
128  Logger::Get()->Info("Application: starting real close");
129  StartRelease();
130  }
131 
132  // engine tick //
133  _Engine->Tick();
134 
135  if(ShouldQuit || Quit){
136  // We need to have done a proper run after calling StartRelease //
137  continue;
138  }
139 
140  Render();
141 
142  // We could potentially wait here //
145  try{
146  std::this_thread::sleep_for(std::chrono::milliseconds(1));
147  } catch(...){
148  FailCount++;
149  }
150  }
151 
152  // Report problems //
153  if(FailCount)
154  std::cout << "Application main loop sleep fails: " << FailCount << std::endl;
155 
156  // always release before quitting to avoid tons of memory leaks //
157  Release();
158 
159  return 0;
160 }
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
DLLEXPORT bool HasPreReleaseBeenDone() const
Checks if PreRelease is done and Release can be called.
Definition: Engine.h:47
virtual DLLEXPORT void PreFirstTick()
DLLEXPORT void PreFirstTick()
Definition: Engine.cpp:945
DLLEXPORT int GetWindowOpenCount()
Definition: Engine.cpp:1030
virtual DLLEXPORT void Release()
Performs the final steps in the release process.
Definition: Application.cpp:46
DLLEXPORT void MessagePump()
Processes queued messages from Ogre, SDL and input.
Definition: Engine.cpp:642
virtual DLLEXPORT void StartRelease()
Safely releases the Application //.
Definition: Application.cpp:73
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void Tick()
Definition: Engine.cpp:841
bool QuitSometime
This can be quickly set anywhere to quit sometime in the future.
Definition: Application.h:110
virtual DLLEXPORT void Render()

◆ StartRelease()

DLLEXPORT void LeviathanApplication::StartRelease ( )
virtual

Safely releases the Application //.

Note
This should be used instead of Release

Definition at line 73 of file Application.cpp.

73  {
74  GUARD_LOCK();
75  ShouldQuit = true;
76 
77  // Tell Engine to expect a Release soon //
79 }
DLLEXPORT void PreRelease()
Sets objects ready to be released.
Definition: Engine.cpp:482
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ StartServerProcess()

DLLEXPORT void LeviathanApplication::StartServerProcess ( const std::string &  processname,
const std::string &  commandline 
)
static

Definition at line 212 of file Application.cpp.

214 {
215 
216 #ifdef _WIN32
217  // Create needed info //
218  STARTUPINFOA processstart;
219  PROCESS_INFORMATION startedinfo;
220 
221  ZeroMemory(&processstart, sizeof(STARTUPINFOA));
222  ZeroMemory(&startedinfo, sizeof(PROCESS_INFORMATION));
223 
224  processstart.cb = sizeof(STARTUPINFOA);
225  //processstart.dwFlags = STARTF_FORCEOFFFEEDBACK;
226  //processstart.wShowWindow = SW_SHOWMINIMIZED;
227 
228  std::string finalstart = "\""+processname+"\" "+commandline;
229 
230  // Use windows process creation //
231  if(!CreateProcessA(NULL, const_cast<char*>(finalstart.c_str()), NULL, NULL, FALSE, 0,
232  NULL, NULL, &processstart,
233  &startedinfo))
234  {
235  // Failed to start the process
236  Logger::Get()->Error("Failed to start the server process, error code: "+
237  Convert::ToString(GetLastError()));
238  return;
239  }
240 
241  // Close our handles //
242  CloseHandle(startedinfo.hThread);
243  DEBUG_BREAK;
244  //ServerProcessHandle = startedinfo.hProcess;
245 
246 
247 #else
248  // Popen should work //
249 
250  // Actually fork might be simpler //
251  if(fork() == 0){
252  // We are now in the child process //
253 
254  execl(processname.c_str(), commandline.c_str(), (char*) NULL);
255  }
256 
257 
258 #endif // _WIN32
259 
260 }
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177

◆ Tick()

DLLEXPORT void LeviathanApplication::Tick ( int  mspassed)
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 178 of file Application.cpp.

178  {
179 
180 }

Member Data Documentation

◆ _Engine

Engine* Leviathan::LeviathanApplication::_Engine
protected

Definition at line 112 of file Application.h.

◆ ApplicationConfiguration

AppDef* Leviathan::LeviathanApplication::ApplicationConfiguration = nullptr
protected

Definition at line 113 of file Application.h.

◆ Curapp

LeviathanApplication * LeviathanApplication::Curapp = NULL
staticprotected

Definition at line 116 of file Application.h.

◆ Quit

bool Leviathan::LeviathanApplication::Quit = false
protected

Definition at line 106 of file Application.h.

◆ QuitSometime

bool Leviathan::LeviathanApplication::QuitSometime = false
protected

This can be quickly set anywhere to quit sometime in the future.

Definition at line 110 of file Application.h.

◆ ShouldQuit

bool Leviathan::LeviathanApplication::ShouldQuit = false
protected

Definition at line 107 of file Application.h.


The documentation for this class was generated from the following files: