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
 

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

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

◆ LeviathanApplication() [2/2]

DLLEXPORT LeviathanApplication::LeviathanApplication ( Engine engine)

Version for tests with incomplete engine instance.

Definition at line 17 of file Application.cpp.

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

◆ ~LeviathanApplication()

DLLEXPORT LeviathanApplication::~LeviathanApplication ( )
virtual

Definition at line 24 of file Application.cpp.

25 {
26  // Release should have been called when exiting the main loop
29  _Engine = nullptr;
30  Curapp = nullptr;
31 }
static LeviathanApplication * Curapp
Definition: Application.h:130
#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 114 of file Application.cpp.

114 {}

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

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

◆ CustomizeEnginePostLoad()

DLLEXPORT void LeviathanApplication::CustomizeEnginePostLoad ( )
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 188 of file Application.cpp.

188 {}

◆ DummyGameConfigurationVariables()

DLLEXPORT void LeviathanApplication::DummyGameConfigurationVariables ( GameConfiguration configobj)
static

Definition at line 199 of file Application.cpp.

201 {}

◆ DummyGameKeyConfigVariables()

DLLEXPORT void LeviathanApplication::DummyGameKeyConfigVariables ( KeyConfiguration keyconfigobj)
static

Definition at line 203 of file Application.cpp.

205 {}

◆ EnginePreShutdown()

DLLEXPORT void LeviathanApplication::EnginePreShutdown ( )
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 190 of file Application.cpp.

190 {}

◆ ForceRelease()

DLLEXPORT void LeviathanApplication::ForceRelease ( )

Used to immediately terminate the program.

Note
Should be only called if initialization fails

Definition at line 93 of file Application.cpp.

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

◆ Get()

DLLEXPORT LeviathanApplication * LeviathanApplication::Get ( )
static

Definition at line 33 of file Application.cpp.

34 {
35  return Curapp;
36 }
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 192 of file Application.cpp.

193 {
194 
195  return nullptr;
196 }

◆ GetProgramNetType()

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

◆ Initialize()

DLLEXPORT bool LeviathanApplication::Initialize ( AppDef configuration)
virtual

Definition at line 40 of file Application.cpp.

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

◆ InitLoadCustomScriptTypes()

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

Reimplemented in Pong::PongMasterServer.

Definition at line 180 of file Application.cpp.

181 {
182 
183  return true;
184 }

◆ 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 207 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:121

◆ PassCommandLine()

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

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

Definition at line 109 of file Application.cpp.

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

◆ PreFirstTick()

DLLEXPORT void LeviathanApplication::PreFirstTick ( )
virtual

Definition at line 121 of file Application.cpp.

121 {}

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

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

◆ Render()

DLLEXPORT void LeviathanApplication::Render ( )
virtual

Definition at line 116 of file Application.cpp.

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

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

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

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

◆ 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, NULL,
232  NULL, &processstart, &startedinfo)) {
233  // Failed to start the process
234  Logger::Get()->Error("Failed to start the server process, error code: " +
235  Convert::ToString(GetLastError()));
236  return;
237  }
238 
239  // Close our handles //
240  CloseHandle(startedinfo.hThread);
241  DEBUG_BREAK;
242  // ServerProcessHandle = startedinfo.hProcess;
243 
244 
245 #else
246  // Popen should work //
247 
248  // Actually fork might be simpler //
249  if(fork() == 0) {
250  // We are now in the child process //
251 
252  execl(processname.c_str(), commandline.c_str(), (char*)NULL);
253  }
254 
255 
256 #endif // _WIN32
257 }
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 186 of file Application.cpp.

186 {}

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: