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-2019 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 class Importer;
24 } // namespace Editor
25 
26 class GameModuleLoader;
27 
34 class Engine : public ThreadSafe {
35 
36  friend Window;
37  friend GUI::GuiManager;
38  friend GameWorld;
39  friend LeviathanApplication;
40 
41 public:
44 
45  DLLEXPORT bool Init(
46  AppDef* definition, NETWORKED_TYPE ntype, NetworkInterface* packethandler);
47 
49  DLLEXPORT void Release(bool forced = false);
50 
53  DLLEXPORT void PreRelease();
54 
57  DLLEXPORT inline bool HasPreReleaseBeenDone() const
58  {
59  return PreReleaseDone;
60  }
61 
64  DLLEXPORT int64_t GetTimeSinceLastTick() const;
65 
67  DLLEXPORT int GetCurrentTick() const;
68 
70  DLLEXPORT void MessagePump();
71 
72 
73  DLLEXPORT void Tick();
74  DLLEXPORT void RenderFrame();
75  DLLEXPORT void PreFirstTick();
76 
78 
80  DLLEXPORT void ClearTimers();
81 
83  DLLEXPORT void MarkQuit();
84 
85  // ------------------------------------ //
86  // Threading support
87 
96  DLLEXPORT void Invoke(const std::function<void()>& function);
97 
99  DLLEXPORT void RunOnMainThread(const std::function<void()>& function);
100 
102  DLLEXPORT bool IsOnMainThread() const;
103 
105  DLLEXPORT inline void AssertIfNotMainThread() const
106  {
108  IsOnMainThread(), "Function not called on main thread (AssertIfNotMainThread)");
109  }
110 
111 
112  // ------------------------------------ //
117  DLLEXPORT bool PassCommandLine(int argcount, char* args[]);
118 
130  DLLEXPORT std::shared_ptr<GameWorld> CreateWorld(Window* owningwindow, int worldtype,
131  const std::shared_ptr<PhysicsMaterialManager>& physicsMaterials,
132  const WorldNetworkSettings& networking, int overrideid = -1);
133 
140  DLLEXPORT void DestroyWorld(const std::shared_ptr<GameWorld>& world);
141 
146 
153  DLLEXPORT bool CloseWindow(Window* window);
154 
157  {
158  return GraphicalEntity1;
159  }
160 
162  DLLEXPORT void ReportClosedWindow(Window* windowentity);
163 
165  DLLEXPORT bool IsValidWindow(Window* window) const;
166 
167  DLLEXPORT void SaveScreenShot();
168 
169  // ------------------------------------ //
172  DLLEXPORT void OpenEditorWindow(Window* useexistingwindow = nullptr);
173 
177 
178  // ------------------------------------ //
180  {
181  return Graph;
182  }
183 
184  inline bool IsInGraphicalMode() const
185  {
186  return !NoGui;
187  }
188 
190  {
191  return MainEvents;
192  }
194  {
195  return RenderTimer;
196  }
198  {
199  return MainConsole;
200  }
202  {
203  return MainFileHandler;
204  }
206  {
207  return Define;
208  }
210  {
211  return Owner;
212  }
214  {
215  return _NetworkHandler;
216  }
218  {
219  return _ThreadingManager;
220  }
222  {
223  return Sound;
224  }
226  {
228  }
230  {
231  return _RemoteConsole;
232  }
233  inline Random* GetRandom()
234  {
235  return MainRandom;
236  }
238  {
239  return _GameModuleLoader.get();
240  }
242  {
243  return MainScript;
244  }
245 
247 
248 #ifdef LEVIATHAN_USES_LEAP
249  inline LeapManager* GetLeapManager()
250  {
251  return LeapData;
252  }
253 #endif
254 
255  inline bool GetNoGui()
256  {
257  return NoGui;
258  }
259 
260  // Command line settings can only be set before initializing //
261  inline void SetNoGUI()
262  {
263 
264  NoGui = true;
265  }
266 
267  // Static access //
268  DLLEXPORT static Engine* GetEngine();
269  DLLEXPORT static Engine* Get();
270 
271 protected:
272  // after load function //
273  void PostLoad();
274 
278 
282 
289  void _AdjustTickClock(int amount, bool absolute = true);
290 
293  void _AdjustTickNumber(int tickamount, bool absolute);
294 
296  DLLEXPORT void ProcessInvokes();
297 
299  bool _ReceiveConsoleInput(const std::string& command);
300 
303 
304  // ------------------------------------ //
305  AppDef* Define = nullptr;
306 
308  Graphics* Graph = nullptr;
309 
311  std::vector<Window*> AdditionalGraphicalEntities;
312 
313  SoundDevice* Sound = nullptr;
314  DataStore* Mainstore = nullptr;
319  Random* MainRandom = nullptr;
325 
326  std::unique_ptr<ConsoleInput> _ConsoleInput;
327  std::unique_ptr<GameModuleLoader> _GameModuleLoader;
328  std::vector<std::unique_ptr<Editor::Editor>> OpenedEditors;
329 
330 #ifdef LEVIATHAN_USES_LEAP
331  LeapManager* LeapData = nullptr;
332 #endif
333 
334 
337 
339  std::vector<std::shared_ptr<GameWorld>> GameWorlds;
340 
342  std::mutex GameWorldsLock;
343 
345  std::mutex NetworkHandlerLock;
346 
347  // data //
348  int64_t LastTickTime;
349 
350  int TimePassed = 0;
351  int FrameLimit = 0;
352  int TickCount = 0;
353  int TickTime = 0;
354  int FrameCount = 0;
355 
357  bool PreReleaseDone = false;
358 
361  bool PreReleaseWaiting = false;
362 
363  // Engine settings //
364  bool NoGui = false;
365  bool NoLeap = false;
366  bool NoSTDInput = false;
367 
371  bool IsClient = false;
372 
373  // Marks that the Engine has already done prerelease //
374  bool PreReleaseCompleted = false;
375 
376 
377  // Invoke store //
379  std::list<std::function<void()>> InvokeQueue;
380 
381  // Stores the command line before running it //
383  std::vector<std::unique_ptr<std::string>> PassedCommands;
384 
386  std::vector<std::unique_ptr<std::string>> QueuedConsoleCommands;
387 
389  std::vector<std::unique_ptr<Editor::Importer>> QueuedImports;
390 
392 };
393 
394 } // namespace Leviathan
DLLEXPORT bool IsOnMainThread() const
Returns true if called on the main thread.
Definition: Engine.cpp:90
void _NotifyThreadsRegisterOgre()
Definition: Engine.cpp:1257
ScriptExecutor * MainScript
Definition: Engine.h:316
GameModuleLoader * GetGameModuleLoader()
Definition: Engine.h:237
DLLEXPORT void MarkQuit()
Marks the owning application to quit.
Definition: Engine.cpp:1156
DLLEXPORT void PreRelease()
Sets objects ready to be released.
Definition: Engine.cpp:473
LeviathanApplication * GetOwningApplication()
Definition: Engine.h:209
NetworkHandler * GetNetworkHandler()
Definition: Engine.h:213
DLLEXPORT ~Engine()
Definition: Engine.cpp:70
DLLEXPORT void ClearTimers()
Clears physical timers.
Definition: Engine.cpp:1249
DLLEXPORT void Invoke(const std::function< void()> &function)
Runs function on the main thread before the next tick.
Definition: Engine.cpp:1162
Allows the inherited object to be locked.
Definition: ThreadSafe.h:137
RenderingStatistics * RenderTimer
Definition: Engine.h:307
AppDef * Define
Definition: Engine.h:305
Random number generator based on Mersenne Twister.
Definition: Random.h:15
Manages loading the audio library and provides some helpers.
Definition: SoundDevice.h:15
DataStore * Mainstore
Definition: Engine.h:314
Allows object to register for events that can be fired from anywhere.
Definition: EventHandler.h:15
std::vector< std::unique_ptr< Editor::Importer > > QueuedImports
Queued importers from command line parsing.
Definition: Engine.h:389
DLLEXPORT bool HasPreReleaseBeenDone() const
Checks if PreRelease is done and Release can be called.
Definition: Engine.h:57
IDFactory * IDDefaultInstance
Definition: Engine.h:335
NetworkHandler * _NetworkHandler
Definition: Engine.h:321
ScriptConsole * MainConsole
Definition: Engine.h:317
Class for indexing and searching game data directory.
Definition: FileSystem.h:65
void _RunQueuedConsoleCommands()
Runs all commands in QueuedConsoleCommands.
Definition: Engine.cpp:1571
OutOfMemoryHandler * OutOMemory
Definition: Engine.h:320
DLLEXPORT void SaveScreenShot()
Definition: Engine.cpp:1016
Handles ScriptModule creation and AngelScript code execution.
DLLEXPORT void ProcessInvokes()
Handles InvokeQueue.
Definition: Engine.cpp:1168
std::recursive_mutex RecursiveMutex
Definition: ThreadSafe.h:17
DLLEXPORT Window * GetWindowEntity()
Returns the main window.
Definition: Engine.h:156
void SetNoGUI()
Definition: Engine.h:261
bool _ReceiveConsoleInput(const std::string &command)
Console input comes through this.
Definition: Engine.cpp:1595
DLLEXPORT void OpenEditorWindow(Window *useexistingwindow=nullptr)
Opens an Editor::Editor window.
Definition: Engine.cpp:1126
void _AdjustTickNumber(int tickamount, bool absolute)
Sets the tick number to a specified value.
Definition: Engine.cpp:1311
Window * GraphicalEntity1
Definition: Engine.h:310
DLLEXPORT bool Init(AppDef *definition, NETWORKED_TYPE ntype, NetworkInterface *packethandler)
Definition: Engine.cpp:95
std::unique_ptr< ConsoleInput > _ConsoleInput
Definition: Engine.h:326
The main class of the Leviathan Game Engine.
Definition: Engine.h:34
bool PreReleaseWaiting
Definition: Engine.h:361
GUI controller for a Window.
Definition: GuiManager.h:20
std::vector< std::unique_ptr< std::string > > PassedCommands
Definition: Engine.h:383
ScriptConsole * GetScriptConsole()
Definition: Engine.h:197
DLLEXPORT void ReportClosedWindow(Window *windowentity)
Removes an closed window from the engine.
Definition: Engine.cpp:1101
DLLEXPORT void PreFirstTick()
Definition: Engine.cpp:942
DLLEXPORT Engine(LeviathanApplication *owner)
Definition: Engine.cpp:62
void _AdjustTickClock(int amount, bool absolute=true)
Sets the tick clock to a certain value.
Definition: Engine.cpp:1278
DLLEXPORT int GetWindowOpenCount()
Definition: Engine.cpp:1026
bool GetNoGui()
Definition: Engine.h:255
DLLEXPORT bool IsValidWindow(Window *window) const
Definition: Engine.cpp:1047
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:1145
SoundDevice * GetSoundDevice()
Definition: Engine.h:221
Allows various resource loaders to get notified when the file on disk changes.
ThreadingManager * _ThreadingManager
Definition: Engine.h:322
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:1183
std::vector< std::unique_ptr< Editor::Editor > > OpenedEditors
Definition: Engine.h:328
Graphics * GetGraphics()
Definition: Engine.h:179
RemoteConsole * _RemoteConsole
Definition: Engine.h:323
DLLEXPORT Window * GetWindowFromSDLID(uint32_t sdlid)
Definition: Engine.cpp:819
EventHandler * GetEventHandler()
Definition: Engine.h:189
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:98
SoundDevice * Sound
Definition: Engine.h:313
DLLEXPORT void MessagePump()
Processes queued messages from Ogre, SDL and input.
Definition: Engine.cpp:633
std::vector< std::unique_ptr< std::string > > QueuedConsoleCommands
Stores console commands that came from the command line.
Definition: Engine.h:386
RemoteConsole * GetRemoteConsole()
Definition: Engine.h:229
std::mutex NetworkHandlerLock
Mutex that is locked while NetworkHandler is used.
Definition: Engine.h:345
ResourceRefreshHandler * _ResourceRefreshHandler
Definition: Engine.h:324
DLLEXPORT void RenderFrame()
Definition: Engine.cpp:954
DLLEXPORT int64_t GetTimeSinceLastTick() const
Calculates how long has elapsed since the last tick.
Definition: Engine.cpp:1266
std::list< std::function< void()> > InvokeQueue
Definition: Engine.h:379
AppDef * GetDefinition()
Definition: Engine.h:205
FileSystem * MainFileHandler
Definition: Engine.h:318
DLLEXPORT bool CloseWindow(Window *window)
Closes a window.
Definition: Engine.cpp:1087
std::mutex GameWorldsLock
Mutex that is locked when changing the worlds.
Definition: Engine.h:342
int64_t LastTickTime
Definition: Engine.h:348
DLLEXPORT void Tick()
Definition: Engine.cpp:836
DLLEXPORT Window * OpenNewWindow()
Opens a new window.
Definition: Engine.cpp:1062
static DLLEXPORT Engine * instance
Definition: Engine.h:391
#define DLLEXPORT
Definition: Include.h:84
FileSystem * GetFileSystem()
Definition: Engine.h:201
RecursiveMutex InvokeLock
Definition: Engine.h:378
Random * MainRandom
Definition: Engine.h:319
bool PreReleaseCompleted
Definition: Engine.h:374
static DLLEXPORT Engine * GetEngine()
Definition: Engine.cpp:80
bool IsInGraphicalMode() const
Definition: Engine.h:184
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85
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:225
unsigned int uint32_t
Definition: core.h:40
DLLEXPORT void ExecuteCommandLine()
Definition: Engine.cpp:1483
Graphics * Graph
Definition: Engine.h:308
Handles everything related to connections.
Random * GetRandom()
Definition: Engine.h:233
RenderingStatistics * GetRenderingStatistics()
Definition: Engine.h:193
bool PreReleaseDone
Set when PreRelease is called and Tick has happened.
Definition: Engine.h:357
DLLEXPORT bool PassCommandLine(int argcount, char *args[])
Definition: Engine.cpp:1340
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:1195
std::vector< std::shared_ptr< GameWorld > > GameWorlds
List of current worlds.
Definition: Engine.h:339
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:1272
DLLEXPORT void DestroyWorld(const std::shared_ptr< GameWorld > &world)
Releases a GameWorld.
Definition: Engine.cpp:1227
ScriptExecutor * GetScriptExecutor()
Definition: Engine.h:241
DLLEXPORT void AssertIfNotMainThread() const
Asserts if not called on the main thread.
Definition: Engine.h:105
ThreadingManager * GetThreadingManager()
Definition: Engine.h:217
std::unique_ptr< GameModuleLoader > _GameModuleLoader
Definition: Engine.h:327
bool IsClient
Set to true when initialized as a client.
Definition: Engine.h:371
Represents a world that contains entities.
Definition: GameWorld.h:94
DLLEXPORT void Release(bool forced=false)
Definition: Engine.cpp:536
std::vector< Window * > AdditionalGraphicalEntities
Definition: Engine.h:311
Manages loading GameModule objects to make sure that each is loaded only once.
EventHandler * MainEvents
Definition: Engine.h:315
LeviathanApplication * Owner
Definition: Engine.h:336