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 ()
 
DLLEXPORT LeviathanApplication (Engine *engine)
 Version for tests with incomplete engine instance. More...
 
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 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...
 
bool ExternalEngineInstance = false
 
Engine_Engine
 
AppDefApplicationConfiguration = nullptr
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Static Protected Attributes

static LeviathanApplicationCurapp = NULL
 

Additional Inherited Members

- Public Types inherited from Leviathan::ThreadSafeGeneric< MutexType >
using LockT = typename LockTypeResolver< MutexType >::LType
 

Detailed Description

Base class for all leviathan programs.

Definition at line 16 of file Application.h.

Constructor & Destructor Documentation

◆ LeviathanApplication() [1/2]

DLLEXPORT LeviathanApplication::LeviathanApplication ( )

Definition at line 10 of file Application.cpp.

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

◆ LeviathanApplication() [2/2]

DLLEXPORT LeviathanApplication::LeviathanApplication ( Engine engine)

Version for tests with incomplete engine instance.

Definition at line 16 of file Application.cpp.

16  :
17  ExternalEngineInstance(true), _Engine(engine)
18 {
19  LEVIATHAN_ASSERT(_Engine, "no engine pointer given");
20  Curapp = this;
21 }
static LeviathanApplication * Curapp
Definition: Application.h:130
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:98

◆ ~LeviathanApplication()

DLLEXPORT LeviathanApplication::~LeviathanApplication ( )
virtual

Definition at line 23 of file Application.cpp.

24 {
25  // Release should have been called when exiting the main loop
28  _Engine = nullptr;
29  Curapp = nullptr;
30 }
static LeviathanApplication * Curapp
Definition: Application.h:130
#define SAFE_DELETE(x)
Definition: Define.h:147

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 113 of file Application.cpp.

113 {}

◆ _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 173 of file Application.cpp.

174 {
175 
176  _Engine->ClearTimers();
177 }
DLLEXPORT void ClearTimers()
Clears physical timers.
Definition: Engine.cpp:1251

◆ CustomizeEnginePostLoad()

DLLEXPORT void LeviathanApplication::CustomizeEnginePostLoad ( )
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 187 of file Application.cpp.

187 {}

◆ DummyGameConfigurationVariables()

DLLEXPORT void LeviathanApplication::DummyGameConfigurationVariables ( GameConfiguration configobj)
static

Definition at line 198 of file Application.cpp.

200 {}

◆ DummyGameKeyConfigVariables()

DLLEXPORT void LeviathanApplication::DummyGameKeyConfigVariables ( KeyConfiguration keyconfigobj)
static

Definition at line 202 of file Application.cpp.

204 {}

◆ EnginePreShutdown()

DLLEXPORT void LeviathanApplication::EnginePreShutdown ( )
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 189 of file Application.cpp.

189 {}

◆ ForceRelease()

DLLEXPORT void LeviathanApplication::ForceRelease ( )

Used to immediately terminate the program.

Note
Should be only called if initialization fails

Definition at line 92 of file Application.cpp.

93 {
94  GUARD_LOCK();
95  ShouldQuit = true;
96  Quit = true;
97 
98  if(_Engine) {
99  // The prelease does some which requires a tick //
100  _Engine->PreRelease();
101  _Engine->Tick();
102  _Engine->Release(true);
103  }
104 
106 }
DLLEXPORT void PreRelease()
Sets objects ready to be released.
Definition: Engine.cpp:473
DLLEXPORT void Tick()
Definition: Engine.cpp:836
#define SAFE_DELETE(x)
Definition: Define.h:147
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
DLLEXPORT void Release(bool forced=false)
Definition: Engine.cpp:536

◆ Get()

DLLEXPORT LeviathanApplication * LeviathanApplication::Get ( )
static

Definition at line 32 of file Application.cpp.

33 {
34  return Curapp;
35 }
static LeviathanApplication * Curapp
Definition: Application.h:130

◆ GetDefinition()

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

Definition at line 60 of file Application.h.

61  {
63  }

◆ GetEngine()

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

Definition at line 55 of file Application.h.

56  {
57  return _Engine;
58  }

◆ 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 191 of file Application.cpp.

192 {
193 
194  return nullptr;
195 }

◆ GetProgramNetType()

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

◆ Initialize()

DLLEXPORT bool LeviathanApplication::Initialize ( AppDef configuration)
virtual

Definition at line 39 of file Application.cpp.

40 {
41  GUARD_LOCK();
42 
43  // Store configuration //
44  ApplicationConfiguration = configuration;
45 
46  // Init engine //
47  if(!_Engine->Init(
49  return false;
50 
51  _InternalInit();
52  return true;
53 }
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:95
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ InitLoadCustomScriptTypes()

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

Reimplemented in Pong::PongMasterServer.

Definition at line 179 of file Application.cpp.

180 {
181 
182  return true;
183 }

◆ 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 206 of file Application.cpp.

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

◆ PassCommandLine()

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

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

Definition at line 108 of file Application.cpp.

109 {
110  return _Engine->PassCommandLine(argcount, args);
111 }
DLLEXPORT bool PassCommandLine(int argcount, char *args[])
Definition: Engine.cpp:1342

◆ PreFirstTick()

DLLEXPORT void LeviathanApplication::PreFirstTick ( )
virtual

Definition at line 120 of file Application.cpp.

120 {}

◆ Quitting()

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

Definition at line 50 of file Application.h.

51  {
52  return Quit;
53  }

◆ Release()

DLLEXPORT void LeviathanApplication::Release ( )
protectedvirtual

Performs the final steps in the release process.

Warning
This should not be called directly

Definition at line 55 of file Application.cpp.

56 {
57  {
58  GUARD_LOCK();
59  // set as quitting //
60  Quit = true;
61 
62  // Nothing else to do if no engine //
63  if(!_Engine)
64  return;
65 
66  // Shutdown the packet handler
67  // PreRelease should have been done at this point and the NetworkHandler
68  // should have been released so this can no longer be in use
70  }
71 
72  // This avoids deadlocking //
73  _Engine->Release();
74 
75  {
76  GUARD_LOCK();
77  // Delete the already released engine //
78  delete _Engine;
79  _Engine = NULL;
80  }
81 }
virtual void _ShutdownApplicationPacketHandler()=0
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
DLLEXPORT void Release(bool forced=false)
Definition: Engine.cpp:536

◆ Render()

DLLEXPORT void LeviathanApplication::Render ( )
virtual

Definition at line 115 of file Application.cpp.

116 {
117  _Engine->RenderFrame();
118 }
DLLEXPORT void RenderFrame()
Definition: Engine.cpp:954

◆ 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 122 of file Application.cpp.

123 {
124  // This is almost at tick so call this outside the loop for performance //
126  PreFirstTick();
127 
128  // For reporting wait failures //
129  int FailCount = 0;
130 
131  while(!_Engine->HasPreReleaseBeenDone()) {
132  // Store this //
133  bool canprocess = _Engine->GetWindowOpenCount() != 0;
134 
135  _Engine->MessagePump();
136 
137  // Set as quitting //
138  if((!canprocess || QuitSometime) && !ShouldQuit) {
139  Logger::Get()->Info("Application: starting real close");
140  StartRelease();
141  }
142 
143  // engine tick //
144  _Engine->Tick();
145 
146  if(ShouldQuit || Quit) {
147  // We need to have done a proper run after calling StartRelease //
148  continue;
149  }
150 
151  Render();
152 
153  // We could potentially wait here //
156  try {
157  std::this_thread::sleep_for(std::chrono::milliseconds(1));
158  } catch(...) {
159  FailCount++;
160  }
161  }
162 
163  // Report problems //
164  if(FailCount)
165  std::cout << "Application main loop sleep fails: " << FailCount << std::endl;
166 
167  // always release before quitting to avoid tons of memory leaks //
168  Release();
169 
170  return 0;
171 }
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:57
virtual DLLEXPORT void PreFirstTick()
DLLEXPORT void PreFirstTick()
Definition: Engine.cpp:942
DLLEXPORT int GetWindowOpenCount()
Definition: Engine.cpp:1026
virtual DLLEXPORT void Release()
Performs the final steps in the release process.
Definition: Application.cpp:55
DLLEXPORT void MessagePump()
Processes queued messages from Ogre, SDL and input.
Definition: Engine.cpp:633
virtual DLLEXPORT void StartRelease()
Safely releases the Application //.
Definition: Application.cpp:83
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void Tick()
Definition: Engine.cpp:836
bool QuitSometime
This can be quickly set anywhere to quit sometime in the future.
Definition: Application.h:121
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 83 of file Application.cpp.

84 {
85  GUARD_LOCK();
86  ShouldQuit = true;
87 
88  // Tell Engine to expect a Release soon //
90 }
DLLEXPORT void PreRelease()
Sets objects ready to be released.
Definition: Engine.cpp:473
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ StartServerProcess()

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

Definition at line 211 of file Application.cpp.

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

185 {}

Member Data Documentation

◆ _Engine

Engine* Leviathan::LeviathanApplication::_Engine
protected

Definition at line 126 of file Application.h.

◆ ApplicationConfiguration

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

Definition at line 127 of file Application.h.

◆ Curapp

LeviathanApplication * LeviathanApplication::Curapp = NULL
staticprotected

Definition at line 130 of file Application.h.

◆ ExternalEngineInstance

bool Leviathan::LeviathanApplication::ExternalEngineInstance = false
protected

If true then this was given the Engine instance from somewhere else (most likely PartialEngine)

Definition at line 125 of file Application.h.

◆ Quit

bool Leviathan::LeviathanApplication::Quit = false
protected

Definition at line 117 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 121 of file Application.h.

◆ ShouldQuit

bool Leviathan::LeviathanApplication::ShouldQuit = false
protected

Definition at line 118 of file Application.h.


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