Leviathan  0.8.0.0
Leviathan game engine
Engine.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2018 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "Common/ThreadSafe.h"
9 
10 #include <functional>
11 #include <inttypes.h>
12 #include <list>
13 #include <memory>
14 #include <mutex>
15 #include <thread>
16 #include <vector>
17 
18 
19 namespace Leviathan {
20 
21 namespace Editor {
22 class Editor;
23 }
24 
25 class GameModuleLoader;
26 
33 class Engine : public ThreadSafe {
34 
35  friend Window;
36  friend GUI::GuiManager;
37  friend GameWorld;
38  friend LeviathanApplication;
39 
40 public:
43 
44  DLLEXPORT bool Init(
45  AppDef* definition, NETWORKED_TYPE ntype, NetworkInterface* packethandler);
46 
48  DLLEXPORT void Release(bool forced = false);
49 
52  DLLEXPORT void PreRelease();
53 
56  DLLEXPORT inline bool HasPreReleaseBeenDone() const
57  {
58  return PreReleaseDone;
59  }
60 
63  DLLEXPORT int64_t GetTimeSinceLastTick() const;
64 
66  DLLEXPORT int GetCurrentTick() const;
67 
69  DLLEXPORT void MessagePump();
70 
71 
72  DLLEXPORT void Tick();
73  DLLEXPORT void RenderFrame();
74  DLLEXPORT void PreFirstTick();
75 
77 
79  DLLEXPORT void ClearTimers();
80 
82  DLLEXPORT void MarkQuit();
83 
84  // ------------------------------------ //
85  // Threading support
86 
95  DLLEXPORT void Invoke(const std::function<void()>& function);
96 
98  DLLEXPORT void RunOnMainThread(const std::function<void()>& function);
99 
101  DLLEXPORT bool IsOnMainThread() const;
102 
104  DLLEXPORT inline void AssertIfNotMainThread() const
105  {
106  LEVIATHAN_ASSERT(
107  IsOnMainThread(), "Function not called on main thread (AssertIfNotMainThread)");
108  }
109 
110 
111  // ------------------------------------ //
116  DLLEXPORT bool PassCommandLine(int argcount, char* args[]);
117 
129  DLLEXPORT std::shared_ptr<GameWorld> CreateWorld(Window* owningwindow, int worldtype,
130  const std::shared_ptr<PhysicsMaterialManager>& physicsMaterials,
131  const WorldNetworkSettings& networking, int overrideid = -1);
132 
139  DLLEXPORT void DestroyWorld(const std::shared_ptr<GameWorld>& world);
140 
145 
152  DLLEXPORT bool CloseWindow(Window* window);
153 
156  {
157  return GraphicalEntity1;
158  }
159 
161  DLLEXPORT void ReportClosedWindow(Window* windowentity);
162 
164  DLLEXPORT bool IsValidWindow(Window* window) const;
165 
166  DLLEXPORT void SaveScreenShot();
167 
168  // ------------------------------------ //
171  DLLEXPORT void OpenEditorWindow(Window* useexistingwindow = nullptr);
172 
176 
177  // ------------------------------------ //
179  {
180  return Graph;
181  }
183  {
184  return MainEvents;
185  }
187  {
188  return RenderTimer;
189  }
191  {
192  return MainConsole;
193  }
195  {
196  return MainFileHandler;
197  }
199  {
200  return Define;
201  }
203  {
204  return Owner;
205  }
207  {
208  return _NetworkHandler;
209  }
211  {
212  return _ThreadingManager;
213  }
215  {
216  return Sound;
217  }
219  {
221  }
223  {
224  return _RemoteConsole;
225  }
226  inline Random* GetRandom()
227  {
228  return MainRandom;
229  }
231  {
232  return _GameModuleLoader.get();
233  }
235  {
236  return MainScript;
237  }
238 
240 
241 #ifdef LEVIATHAN_USES_LEAP
242  inline LeapManager* GetLeapManager()
243  {
244  return LeapData;
245  }
246 #endif
247 
248  inline bool GetNoGui()
249  {
250  return NoGui;
251  }
252 
253  // Command line settings can only be set before initializing //
254  inline void SetNoGUI()
255  {
256 
257  NoGui = true;
258  }
259 
260  // Static access //
261  DLLEXPORT static Engine* GetEngine();
262  DLLEXPORT static Engine* Get();
263 
264 protected:
265  // after load function //
266  void PostLoad();
267 
271 
275 
282  void _AdjustTickClock(int amount, bool absolute = true);
283 
286  void _AdjustTickNumber(int tickamount, bool absolute);
287 
289  DLLEXPORT void ProcessInvokes();
290 
292  bool _ReceiveConsoleInput(const std::string& command);
293 
296 
298  bool ParseSingleCommand(
299  StringIterator& itr, int& argindex, const int argcount, char* args[]);
300 
301  // ------------------------------------ //
302  AppDef* Define = nullptr;
303 
305  Graphics* Graph = nullptr;
306 
308  std::vector<Window*> AdditionalGraphicalEntities;
309 
310  SoundDevice* Sound = nullptr;
311  DataStore* Mainstore = nullptr;
316  Random* MainRandom = nullptr;
322 
323  std::unique_ptr<ConsoleInput> _ConsoleInput;
324  std::unique_ptr<GameModuleLoader> _GameModuleLoader;
325  std::vector<std::unique_ptr<Editor::Editor>> OpenedEditors;
326 
327 #ifdef LEVIATHAN_USES_LEAP
328  LeapManager* LeapData = nullptr;
329 #endif
330 
331 
334 
336  std::vector<std::shared_ptr<GameWorld>> GameWorlds;
337 
339  std::mutex GameWorldsLock;
340 
342  std::mutex NetworkHandlerLock;
343 
344  // data //
345  int64_t LastTickTime;
346 
347  int TimePassed = 0;
348  int FrameLimit = 0;
349  int TickCount = 0;
350  int TickTime = 0;
351  int FrameCount = 0;
352 
354  bool PreReleaseDone = false;
355 
358  bool PreReleaseWaiting = false;
359 
360  // Engine settings //
361  bool NoGui = false;
362  bool NoLeap = false;
363  bool NoSTDInput = false;
364 
368  bool IsClient = false;
369 
370  // Marks that the Engine has already done prerelease //
371  bool PreReleaseCompleted = false;
372 
373 
374  // Invoke store //
376  std::list<std::function<void()>> InvokeQueue;
377 
378  // Stores the command line before running it //
379  std::vector<std::unique_ptr<std::string>> PassedCommands;
380 
382  std::vector<std::unique_ptr<std::string>> QueuedConsoleCommands;
383 
385 };
386 
387 } // namespace Leviathan
DLLEXPORT bool IsOnMainThread() const
Returns true if called on the main thread.
Definition: Engine.cpp:89
void _NotifyThreadsRegisterOgre()
Definition: Engine.cpp:1235
ScriptExecutor * MainScript
Definition: Engine.h:313
GameModuleLoader * GetGameModuleLoader()
Definition: Engine.h:230
DLLEXPORT void MarkQuit()
Marks the owning application to quit.
Definition: Engine.cpp:1132
DLLEXPORT void PreRelease()
Sets objects ready to be released.
Definition: Engine.cpp:446
LeviathanApplication * GetOwningApplication()
Definition: Engine.h:202
NetworkHandler * GetNetworkHandler()
Definition: Engine.h:206
DLLEXPORT ~Engine()
Definition: Engine.cpp:69
DLLEXPORT void ClearTimers()
Clears physical timers.
Definition: Engine.cpp:1227
DLLEXPORT void Invoke(const std::function< void()> &function)
Runs function on the main thread before the next tick.
Definition: Engine.cpp:1138
Allows the inherited object to be locked.
Definition: ThreadSafe.h:123
RenderingStatistics * RenderTimer
Definition: Engine.h:304
AppDef * Define
Definition: Engine.h:302
Random number generator based on Mersenne Twister.
Definition: Random.h:15
Manages loading the audio library and provides some helpers.
Definition: SoundDevice.h:20
DataStore * Mainstore
Definition: Engine.h:311
Allows object to register for events that can be fired from anywhere.
Definition: EventHandler.h:15
DLLEXPORT bool HasPreReleaseBeenDone() const
Checks if PreRelease is done and Release can be called.
Definition: Engine.h:56
IDFactory * IDDefaultInstance
Definition: Engine.h:332
NetworkHandler * _NetworkHandler
Definition: Engine.h:318
ScriptConsole * MainConsole
Definition: Engine.h:314
Class for indexing and searching game data directory.
Definition: FileSystem.h:65
void _RunQueuedConsoleCommands()
Runs all commands in QueuedConsoleCommands.
Definition: Engine.cpp:1545
OutOfMemoryHandler * OutOMemory
Definition: Engine.h:317
DLLEXPORT void SaveScreenShot()
Definition: Engine.cpp:990
Handles ScriptModule creation and AngelScript code execution.
DLLEXPORT void ProcessInvokes()
Handles InvokeQueue.
Definition: Engine.cpp:1145
std::recursive_mutex RecursiveMutex
Definition: ThreadSafe.h:15
DLLEXPORT Window * GetWindowEntity()
Returns the main window.
Definition: Engine.h:155
void SetNoGUI()
Definition: Engine.h:254
bool _ReceiveConsoleInput(const std::string &command)
Console input comes through this.
Definition: Engine.cpp:1570
DLLEXPORT void OpenEditorWindow(Window *useexistingwindow=nullptr)
Opens an Editor::Editor window.
Definition: Engine.cpp:1102
void _AdjustTickNumber(int tickamount, bool absolute)
Sets the tick number to a specified value.
Definition: Engine.cpp:1289
Window * GraphicalEntity1
Definition: Engine.h:307
DLLEXPORT bool Init(AppDef *definition, NETWORKED_TYPE ntype, NetworkInterface *packethandler)
Definition: Engine.cpp:94
std::unique_ptr< ConsoleInput > _ConsoleInput
Definition: Engine.h:323
The main class of the Leviathan Game Engine.
Definition: Engine.h:33
bool PreReleaseWaiting
Definition: Engine.h:358
GUI controller for a Window.
Definition: GuiManager.h:18
std::vector< std::unique_ptr< std::string > > PassedCommands
Definition: Engine.h:379
ScriptConsole * GetScriptConsole()
Definition: Engine.h:190
DLLEXPORT void ReportClosedWindow(Window *windowentity)
Removes an closed window from the engine.
Definition: Engine.cpp:1077
DLLEXPORT void PreFirstTick()
Definition: Engine.cpp:915
DLLEXPORT Engine(LeviathanApplication *owner)
Definition: Engine.cpp:61
void _AdjustTickClock(int amount, bool absolute=true)
Sets the tick clock to a certain value.
Definition: Engine.cpp:1256
DLLEXPORT int GetWindowOpenCount()
Definition: Engine.cpp:1000
bool GetNoGui()
Definition: Engine.h:248
DLLEXPORT bool IsValidWindow(Window *window) const
Definition: Engine.cpp:1021
Iterator class for getting parts of a string.
Manages delayed execution of functions through use of QueuedTask and subclasses.
DLLEXPORT void FocusOrOpenEditor()
Focuses the first editor or opens an editor if none are open.
Definition: Engine.cpp:1121
SoundDevice * GetSoundDevice()
Definition: Engine.h:214
Allows various resource loaders to get notified when the file on disk changes.
ThreadingManager * _ThreadingManager
Definition: Engine.h:319
NETWORKED_TYPE
Type of networked application.
Definition: CommonNetwork.h:22
DLLEXPORT void RunOnMainThread(const std::function< void()> &function)
Runs the function now if on the main thread otherwise calls Invoke.
Definition: Engine.cpp:1161
std::vector< std::unique_ptr< Editor::Editor > > OpenedEditors
Definition: Engine.h:325
Graphics * GetGraphics()
Definition: Engine.h:178
RemoteConsole * _RemoteConsole
Definition: Engine.h:320
DLLEXPORT Window * GetWindowFromSDLID(uint32_t sdlid)
Definition: Engine.cpp:794
EventHandler * GetEventHandler()
Definition: Engine.h:182
SoundDevice * Sound
Definition: Engine.h:310
DLLEXPORT void MessagePump()
Processes queued messages from Ogre, SDL and input.
Definition: Engine.cpp:606
std::vector< std::unique_ptr< std::string > > QueuedConsoleCommands
Stores console commands that came from the command line.
Definition: Engine.h:382
RemoteConsole * GetRemoteConsole()
Definition: Engine.h:222
std::mutex NetworkHandlerLock
Mutex that is locked while NetworkHandler is used.
Definition: Engine.h:342
ResourceRefreshHandler * _ResourceRefreshHandler
Definition: Engine.h:321
DLLEXPORT void RenderFrame()
Definition: Engine.cpp:928
DLLEXPORT int64_t GetTimeSinceLastTick() const
Calculates how long has elapsed since the last tick.
Definition: Engine.cpp:1244
std::list< std::function< void()> > InvokeQueue
Definition: Engine.h:376
AppDef * GetDefinition()
Definition: Engine.h:198
FileSystem * MainFileHandler
Definition: Engine.h:315
DLLEXPORT bool CloseWindow(Window *window)
Closes a window.
Definition: Engine.cpp:1063
std::mutex GameWorldsLock
Mutex that is locked when changing the worlds.
Definition: Engine.h:339
int64_t LastTickTime
Definition: Engine.h:345
DLLEXPORT void Tick()
Definition: Engine.cpp:811
DLLEXPORT Window * OpenNewWindow()
Opens a new window.
Definition: Engine.cpp:1036
static DLLEXPORT Engine * instance
Definition: Engine.h:384
#define DLLEXPORT
Definition: Include.h:115
FileSystem * GetFileSystem()
Definition: Engine.h:194
RecursiveMutex InvokeLock
Definition: Engine.h:375
Random * MainRandom
Definition: Engine.h:316
bool PreReleaseCompleted
Definition: Engine.h:371
static DLLEXPORT Engine * GetEngine()
Definition: Engine.cpp:79
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:84
Base class for all leviathan programs.
Definition: Application.h:16
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
Class used to handle remote server commands and receiving messages.
Definition: RemoteConsole.h:45
ResourceRefreshHandler * GetResourceRefreshHandler()
Definition: Engine.h:218
unsigned int uint32_t
Definition: core.h:40
DLLEXPORT void ExecuteCommandLine()
Definition: Engine.cpp:1457
Graphics * Graph
Definition: Engine.h:305
Handles everything related to connections.
Random * GetRandom()
Definition: Engine.h:226
RenderingStatistics * GetRenderingStatistics()
Definition: Engine.h:186
bool PreReleaseDone
Set when PreRelease is called and Tick has happened.
Definition: Engine.h:354
DLLEXPORT bool PassCommandLine(int argcount, char *args[])
Definition: Engine.cpp:1420
DLLEXPORT std::shared_ptr< GameWorld > CreateWorld(Window *owningwindow, int worldtype, const std::shared_ptr< PhysicsMaterialManager > &physicsMaterials, const WorldNetworkSettings &networking, int overrideid=-1)
Creates a GameWorld for placing entities into.
Definition: Engine.cpp:1173
std::vector< std::shared_ptr< GameWorld > > GameWorlds
List of current worlds.
Definition: Engine.h:336
Class that encapsulates common networking functionality that is required by all networked programs...
DLLEXPORT int GetCurrentTick() const
Returns the number of tick that was last simulated.
Definition: Engine.cpp:1250
DLLEXPORT void DestroyWorld(const std::shared_ptr< GameWorld > &world)
Releases a GameWorld.
Definition: Engine.cpp:1205
ScriptExecutor * GetScriptExecutor()
Definition: Engine.h:234
DLLEXPORT void AssertIfNotMainThread() const
Asserts if not called on the main thread.
Definition: Engine.h:104
ThreadingManager * GetThreadingManager()
Definition: Engine.h:210
std::unique_ptr< GameModuleLoader > _GameModuleLoader
Definition: Engine.h:324
bool IsClient
Set to true when initialized as a client.
Definition: Engine.h:368
Represents a world that contains entities.
Definition: GameWorld.h:99
DLLEXPORT void Release(bool forced=false)
Definition: Engine.cpp:509
std::vector< Window * > AdditionalGraphicalEntities
Definition: Engine.h:308
Manages loading GameModule objects to make sure that each is loaded only once.
bool ParseSingleCommand(StringIterator &itr, int &argindex, const int argcount, char *args[])
Helper for PassCommandLine.
Definition: Engine.cpp:1319
EventHandler * MainEvents
Definition: Engine.h:312
LeviathanApplication * Owner
Definition: Engine.h:333