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 float RunSingleUpdate ()
 Runs a single engine update cycle (tick + render) More...
 
virtual DLLEXPORT bool PassCommandLine (int argcount, char *args[])
 
virtual DLLEXPORT void Tick (float elapsed)
 
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)
 
static DLLEXPORT std::vector< std::string > CommandLineStringSplitter (const char *str, std::vector< char * > &argcharstrings, bool addprogramname=true)
 Splits a single string command line into arguments. More...
 

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
 
bool PreferSleepOverLoopAccuracy = false
 
- 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:138
The main class of the Leviathan Game Engine.
Definition: Engine.h:37

◆ 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:138
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:102

◆ ~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:138
#define SAFE_DELETE(x)
Definition: Define.h:151

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

196 {}

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

183 {
184  _Engine->ClearTimers();
185 }
DLLEXPORT void ClearTimers()
Clears physical timers.
Definition: Engine.cpp:1255

◆ CommandLineStringSplitter()

DLLEXPORT std::vector< std::string > LeviathanApplication::CommandLineStringSplitter ( const char *  str,
std::vector< char * > &  argcharstrings,
bool  addprogramname = true 
)
static

Splits a single string command line into arguments.

Definition at line 268 of file Application.cpp.

270 {
271  StringIterator itr(std::make_unique<UTF8PointerDataIterator>(str, str + std::strlen(str)));
272 
273  std::vector<std::string> args;
274 
275  if(addprogramname) {
276  // TODO: actually detect this somehow
277  args.push_back("LeviathanApplication");
278  }
279 
280  while(!itr.IsOutOfBounds()) {
281  auto current =
282  itr.GetNextCharacterSequence<std::string>(UNNORMALCHARACTER_TYPE_WHITESPACE);
283 
284  if(current && current->size() > 0) {
285  if(current->at(0) == '\'' || current->at(0) == '\"') {
286  current = StringIterator(current.get())
287  .GetStringInQuotes<std::string>(QUOTETYPE_BOTH);
288  }
289 
290  args.push_back(*current);
291  }
292  }
293 
294  argcharstrings.resize(args.size());
295  for(size_t i = 0; i < args.size(); ++i) {
296  argcharstrings[i] = const_cast<char*>(args[i].c_str());
297  }
298  argcharstrings.push_back(nullptr);
299 
300  return args;
301 }
Iterator class for getting parts of a string.

◆ CustomizeEnginePostLoad()

DLLEXPORT void LeviathanApplication::CustomizeEnginePostLoad ( )
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 198 of file Application.cpp.

198 {}

◆ DummyGameConfigurationVariables()

DLLEXPORT void LeviathanApplication::DummyGameConfigurationVariables ( GameConfiguration configobj)
static

Definition at line 208 of file Application.cpp.

210 {}

◆ DummyGameKeyConfigVariables()

DLLEXPORT void LeviathanApplication::DummyGameKeyConfigVariables ( KeyConfiguration keyconfigobj)
static

Definition at line 212 of file Application.cpp.

214 {}

◆ EnginePreShutdown()

DLLEXPORT void LeviathanApplication::EnginePreShutdown ( )
virtual

Reimplemented in Pong::PongMasterServer.

Definition at line 200 of file Application.cpp.

200 {}

◆ 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->Update();
102  _Engine->Release(true);
103  }
104 
106 }
DLLEXPORT void PreRelease()
Sets objects ready to be released.
Definition: Engine.cpp:459
DLLEXPORT float Update()
Runs an update cycle (tick and rendering if needed)
Definition: Engine.cpp:822
#define SAFE_DELETE(x)
Definition: Define.h:151
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
DLLEXPORT void Release(bool forced=false)
Definition: Engine.cpp:522

◆ 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:138

◆ GetDefinition()

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

Definition at line 62 of file Application.h.

63  {
65  }

◆ GetEngine()

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

Definition at line 57 of file Application.h.

58  {
59  return _Engine;
60  }

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

203 {
204  return nullptr;
205 }

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

188 {
189  return true;
190 }

◆ MarkAsClosing()

DLLEXPORT void LeviathanApplication::MarkAsClosing ( )

Thread safely marks the game to close sometime.

The closing should happen in around 2 updates

Definition at line 216 of file Application.cpp.

217 {
218  QuitSometime = true;
219 }
bool QuitSometime
This can be quickly set anywhere to quit sometime in the future.
Definition: Application.h:125

◆ 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:1293

◆ PreFirstTick()

DLLEXPORT void LeviathanApplication::PreFirstTick ( )
virtual

Definition at line 194 of file Application.cpp.

194 {}

◆ Quitting()

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

Definition at line 52 of file Application.h.

53  {
54  return Quit;
55  }

◆ 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:522

◆ RunMessageLoop()

DLLEXPORT int LeviathanApplication::RunMessageLoop ( )
virtual

Definition at line 120 of file Application.cpp.

121 {
123  PreFirstTick();
124 
125  // For reporting wait failures //
126  int FailCount = 0;
127 
128  // Run until engine prerelease is done.
129  // We need to have done a proper run after calling StartRelease
130  while(!_Engine->HasPreReleaseBeenDone()) {
131  // Store this //
132  bool canprocess = _Engine->GetWindowOpenCount() != 0;
133 
134  _Engine->MessagePump();
135 
136  // Set as quitting //
137  if((!canprocess || QuitSometime) && !ShouldQuit) {
138  Logger::Get()->Info("Application: starting real close");
139  StartRelease();
140  }
141 
142  // Engine tick and render
143  const float waitTime = RunSingleUpdate();
144 
145  if(ShouldQuit || Quit) {
146  continue;
147  }
148 
149  if(waitTime > 0) {
150 
151  if(waitTime >= 0.002f || PreferSleepOverLoopAccuracy) {
152  try {
153  std::this_thread::sleep_for(SecondDuration(waitTime));
154  } catch(...) {
155  FailCount++;
156  }
157  } else {
158  // Busy wait the duration
159  const auto start = Time::GetCurrentTimePoint();
160 
161  while(std::chrono::duration_cast<SecondDuration>(
162  Time::GetCurrentTimePoint() - start)
163  .count() < waitTime) {
164  }
165  }
166  }
167  }
168 
169  // Report problems //
170  if(FailCount) {
171  if(Logger::Get())
172  Logger::Get()->Error(
173  "Application main loop sleep fails: " + std::to_string(FailCount));
174  }
175 
176  // always release before quitting to avoid tons of memory leaks //
177  Release();
178 
179  return 0;
180 }
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:60
virtual DLLEXPORT void PreFirstTick()
DLLEXPORT void PreFirstTick()
Called by Application before the first Update call.
Definition: Engine.cpp:970
DLLEXPORT int GetWindowOpenCount()
Definition: Engine.cpp:1032
virtual DLLEXPORT void Release()
Performs the final steps in the release process.
Definition: Application.cpp:55
static DLLEXPORT TimePoint GetCurrentTimePoint()
DLLEXPORT void MessagePump()
Processes queued messages from SDL and CEF.
Definition: Engine.cpp:619
virtual DLLEXPORT void StartRelease()
Safely releases the Application //.
Definition: Application.cpp:83
virtual DLLEXPORT float RunSingleUpdate()
Runs a single engine update cycle (tick + render)
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177
bool QuitSometime
This can be quickly set anywhere to quit sometime in the future.
Definition: Application.h:125
std::chrono::duration< float, std::ratio< 1 > > SecondDuration
Definition: TimeIncludes.h:16

◆ RunSingleUpdate()

DLLEXPORT float LeviathanApplication::RunSingleUpdate ( )
virtual

Runs a single engine update cycle (tick + render)

Returns
The number of seconds to sleep before next update

Definition at line 115 of file Application.cpp.

116 {
117  return _Engine->Update();
118 }
DLLEXPORT float Update()
Runs an update cycle (tick and rendering if needed)
Definition: Engine.cpp:822

◆ 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:459
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ StartServerProcess()

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

Definition at line 221 of file Application.cpp.

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

Reimplemented in Pong::PongMasterServer.

Definition at line 192 of file Application.cpp.

192 {}

Member Data Documentation

◆ _Engine

Engine* Leviathan::LeviathanApplication::_Engine
protected

Definition at line 130 of file Application.h.

◆ ApplicationConfiguration

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

Definition at line 131 of file Application.h.

◆ Curapp

LeviathanApplication * LeviathanApplication::Curapp = NULL
staticprotected

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

◆ PreferSleepOverLoopAccuracy

bool Leviathan::LeviathanApplication::PreferSleepOverLoopAccuracy = false
protected

If true always sleep the specified amount of time between loops, instead of busy waiting. This reduces the loop accuracy but can save power

Definition at line 135 of file Application.h.

◆ Quit

bool Leviathan::LeviathanApplication::Quit = false
protected

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

◆ ShouldQuit

bool Leviathan::LeviathanApplication::ShouldQuit = false
protected

Definition at line 122 of file Application.h.


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