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 // Maybe this header could be hidden with a Pimpl
11 #include "TimeIncludes.h"
12 
13 #include <functional>
14 #include <inttypes.h>
15 #include <list>
16 #include <memory>
17 #include <mutex>
18 #include <thread>
19 #include <vector>
20 
21 
22 namespace Leviathan {
23 
24 namespace Editor {
25 class Editor;
26 class Importer;
27 } // namespace Editor
28 
29 class GameModuleLoader;
30 
37 class Engine : public ThreadSafe {
38 
39  friend Window;
40  friend GUI::GuiManager;
41  friend GameWorld;
42  friend LeviathanApplication;
43 
44 public:
47 
48  DLLEXPORT bool Init(
49  AppDef* definition, NETWORKED_TYPE ntype, NetworkInterface* packethandler);
50 
52  DLLEXPORT void Release(bool forced = false);
53 
56  DLLEXPORT void PreRelease();
57 
60  DLLEXPORT inline bool HasPreReleaseBeenDone() const
61  {
62  return PreReleaseDone;
63  }
64 
68 
69  // //! \brief Returns the number of tick that was last simulated
70  // DLLEXPORT int GetCurrentTick() const;
71 
73  DLLEXPORT void MessagePump();
74 
78  DLLEXPORT float Update();
79 
81  DLLEXPORT void PreFirstTick();
82 
84 
86  DLLEXPORT void ClearTimers();
87 
89  DLLEXPORT void MarkQuit();
90 
91  // ------------------------------------ //
92  // Threading support
93 
102  DLLEXPORT void Invoke(const std::function<void()>& function);
103 
105  DLLEXPORT void RunOnMainThread(const std::function<void()>& function);
106 
108  DLLEXPORT bool IsOnMainThread() const;
109 
111  DLLEXPORT inline void AssertIfNotMainThread() const
112  {
114  IsOnMainThread(), "Function not called on main thread (AssertIfNotMainThread)");
115  }
116 
117 
118  // ------------------------------------ //
123  DLLEXPORT bool PassCommandLine(int argcount, char* args[]);
124 
136  DLLEXPORT std::shared_ptr<GameWorld> CreateWorld(Window* owningwindow, int worldtype,
137  const std::shared_ptr<PhysicsMaterialManager>& physicsMaterials,
138  const WorldNetworkSettings& networking, int overrideid = -1);
139 
146  DLLEXPORT void DestroyWorld(const std::shared_ptr<GameWorld>& world);
147 
152 
159  DLLEXPORT bool CloseWindow(Window* window);
160 
163  {
164  return GraphicalEntity1;
165  }
166 
168  DLLEXPORT void ReportClosedWindow(Window* windowentity);
169 
171  DLLEXPORT bool IsValidWindow(Window* window) const;
172 
173  DLLEXPORT void SaveScreenShot();
174 
175  // ------------------------------------ //
178  DLLEXPORT void OpenEditorWindow(Window* useexistingwindow = nullptr);
179 
183 
184  // ------------------------------------ //
186  {
187  return Graph;
188  }
189 
190  inline bool IsInGraphicalMode() const
191  {
192  return !NoGui;
193  }
194 
196  {
197  return MainEvents;
198  }
200  {
201  return RenderTimer;
202  }
204  {
205  return MainConsole;
206  }
208  {
209  return MainFileHandler;
210  }
212  {
213  return Define;
214  }
216  {
217  return Owner;
218  }
220  {
221  return _NetworkHandler;
222  }
224  {
225  return _ThreadingManager;
226  }
228  {
229  return Sound;
230  }
232  {
234  }
236  {
237  return _RemoteConsole;
238  }
239  inline Random* GetRandom()
240  {
241  return MainRandom;
242  }
244  {
245  return _GameModuleLoader.get();
246  }
248  {
249  return MainScript;
250  }
251 
253 
254 #ifdef LEVIATHAN_USES_LEAP
255  inline LeapManager* GetLeapManager()
256  {
257  return LeapData;
258  }
259 #endif
260 
261  inline bool GetNoGui()
262  {
263  return NoGui;
264  }
265 
266  // Command line settings can only be set before initializing //
267  inline void SetNoGUI()
268  {
269  NoGui = true;
270  }
271 
272  // Static access //
273  DLLEXPORT static Engine* GetEngine();
274  DLLEXPORT static Engine* Get();
275 
276 protected:
277  DLLEXPORT void Tick(float elapsed);
278  DLLEXPORT void RenderFrame(float elapsed);
279 
281  DLLEXPORT void ProcessInvokes();
282 
283  // after load function //
284  void PostLoad();
285 
289 
294 
296  bool _ReceiveConsoleInput(const std::string& command);
297 
300 
301 protected:
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 
346 
348  int FrameLimit = -1;
349  int FrameCount = 0;
350  int TickCount = 0;
351  int TickTime = 0;
352 
354  bool PreReleaseDone = false;
355 
358  bool PreReleaseWaiting = false;
359 
360  // Engine settings //
362  bool NoGui = false;
363  bool NoLeap = false;
364  bool NoSTDInput = false;
365 
369  bool IsClient = false;
370 
371  // Marks that the Engine has already done prerelease //
372  bool PreReleaseCompleted = false;
373 
374 
376  std::list<std::function<void()>> InvokeQueue;
377 
378  // Stores the command line before running it //
380  std::vector<std::unique_ptr<std::string>> PassedCommands;
381 
383  std::vector<std::unique_ptr<std::string>> QueuedConsoleCommands;
384 
386  std::vector<std::unique_ptr<Editor::Importer>> QueuedImports;
387 
389 };
390 
391 } // namespace Leviathan
DLLEXPORT bool IsOnMainThread() const
Returns true if called on the main thread.
Definition: Engine.cpp:91
void _NotifyThreadsRegisterOgre()
Definition: Engine.cpp:1266
ScriptExecutor * MainScript
Definition: Engine.h:313
GameModuleLoader * GetGameModuleLoader()
Definition: Engine.h:243
DLLEXPORT void MarkQuit()
Marks the owning application to quit.
Definition: Engine.cpp:1163
DLLEXPORT void PreRelease()
Sets objects ready to be released.
Definition: Engine.cpp:460
LeviathanApplication * GetOwningApplication()
Definition: Engine.h:215
NetworkHandler * GetNetworkHandler()
Definition: Engine.h:219
DLLEXPORT ~Engine()
Definition: Engine.cpp:71
DLLEXPORT void ClearTimers()
Clears physical timers.
Definition: Engine.cpp:1256
DLLEXPORT void Invoke(const std::function< void()> &function)
Runs function on the main thread before the next tick.
Definition: Engine.cpp:1169
Allows the inherited object to be locked.
Definition: ThreadSafe.h:137
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:15
WantedClockType::time_point TimePoint
Definition: TimeIncludes.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 SecondDuration GetTimeSinceLastTick() const
Calculates how long has elapsed since the last tick.
Definition: Engine.cpp:1275
std::vector< std::unique_ptr< Editor::Importer > > QueuedImports
Queued importers from command line parsing.
Definition: Engine.h:386
DLLEXPORT bool HasPreReleaseBeenDone() const
Checks if PreRelease is done and Release can be called.
Definition: Engine.h:60
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:1526
OutOfMemoryHandler * OutOMemory
Definition: Engine.h:317
DLLEXPORT void RenderFrame(float elapsed)
Definition: Engine.cpp:983
DLLEXPORT void SaveScreenShot()
Definition: Engine.cpp:1023
Handles ScriptModule creation and AngelScript code execution.
DLLEXPORT void ProcessInvokes()
Handles InvokeQueue.
Definition: Engine.cpp:1175
std::recursive_mutex RecursiveMutex
Definition: ThreadSafe.h:17
DLLEXPORT Window * GetWindowEntity()
Returns the main window.
Definition: Engine.h:162
void SetNoGUI()
Definition: Engine.h:267
bool _ReceiveConsoleInput(const std::string &command)
Console input comes through this.
Definition: Engine.cpp:1550
DLLEXPORT void OpenEditorWindow(Window *useexistingwindow=nullptr)
Opens an Editor::Editor window.
Definition: Engine.cpp:1133
Window * GraphicalEntity1
Definition: Engine.h:307
DLLEXPORT bool Init(AppDef *definition, NETWORKED_TYPE ntype, NetworkInterface *packethandler)
Definition: Engine.cpp:96
std::unique_ptr< ConsoleInput > _ConsoleInput
Definition: Engine.h:323
The main class of the Leviathan Game Engine.
Definition: Engine.h:37
bool PreReleaseWaiting
Definition: Engine.h:358
GUI controller for a Window.
Definition: GuiManager.h:20
std::vector< std::unique_ptr< std::string > > PassedCommands
Definition: Engine.h:380
ScriptConsole * GetScriptConsole()
Definition: Engine.h:203
DLLEXPORT void ReportClosedWindow(Window *windowentity)
Removes an closed window from the engine.
Definition: Engine.cpp:1108
TimePoint LastTickTime
Time when last ticked, used to track elapsed time.
Definition: Engine.h:345
DLLEXPORT void PreFirstTick()
Called by Application before the first Update call.
Definition: Engine.cpp:971
DLLEXPORT Engine(LeviathanApplication *owner)
Definition: Engine.cpp:63
DLLEXPORT void Tick(float elapsed)
Definition: Engine.cpp:895
DLLEXPORT int GetWindowOpenCount()
Definition: Engine.cpp:1033
bool GetNoGui()
Definition: Engine.h:261
DLLEXPORT bool IsValidWindow(Window *window) const
Definition: Engine.cpp:1054
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:1152
SoundDevice * GetSoundDevice()
Definition: Engine.h:227
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:1190
std::vector< std::unique_ptr< Editor::Editor > > OpenedEditors
Definition: Engine.h:325
Graphics * GetGraphics()
Definition: Engine.h:185
RemoteConsole * _RemoteConsole
Definition: Engine.h:320
DLLEXPORT Window * GetWindowFromSDLID(uint32_t sdlid)
Definition: Engine.cpp:806
EventHandler * GetEventHandler()
Definition: Engine.h:195
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:100
SoundDevice * Sound
Definition: Engine.h:310
DLLEXPORT void MessagePump()
Processes queued messages from SDL and CEF.
Definition: Engine.cpp:620
std::vector< std::unique_ptr< std::string > > QueuedConsoleCommands
Stores console commands that came from the command line.
Definition: Engine.h:383
RemoteConsole * GetRemoteConsole()
Definition: Engine.h:235
std::mutex NetworkHandlerLock
Mutex that is locked while NetworkHandler is used.
Definition: Engine.h:342
ResourceRefreshHandler * _ResourceRefreshHandler
Definition: Engine.h:321
std::list< std::function< void()> > InvokeQueue
Definition: Engine.h:376
AppDef * GetDefinition()
Definition: Engine.h:211
FileSystem * MainFileHandler
Definition: Engine.h:315
DLLEXPORT bool CloseWindow(Window *window)
Closes a window.
Definition: Engine.cpp:1094
std::mutex GameWorldsLock
Mutex that is locked when changing the worlds.
Definition: Engine.h:339
DLLEXPORT Window * OpenNewWindow()
Opens a new window.
Definition: Engine.cpp:1069
static DLLEXPORT Engine * instance
Definition: Engine.h:388
#define DLLEXPORT
Definition: Include.h:84
FileSystem * GetFileSystem()
Definition: Engine.h:207
RecursiveMutex InvokeLock
Definition: Engine.h:375
Random * MainRandom
Definition: Engine.h:316
bool PreReleaseCompleted
Definition: Engine.h:372
static DLLEXPORT Engine * GetEngine()
Definition: Engine.cpp:81
bool IsInGraphicalMode() const
Definition: Engine.h:190
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:86
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
DLLEXPORT float Update()
Runs an update cycle (tick and rendering if needed)
Definition: Engine.cpp:823
Class used to handle remote server commands and receiving messages.
Definition: RemoteConsole.h:45
ResourceRefreshHandler * GetResourceRefreshHandler()
Definition: Engine.h:231
unsigned int uint32_t
Definition: core.h:40
int FrameLimit
Max FPS.
Definition: Engine.h:348
DLLEXPORT void ExecuteCommandLine()
Definition: Engine.cpp:1438
Graphics * Graph
Definition: Engine.h:305
Handles everything related to connections.
Random * GetRandom()
Definition: Engine.h:239
RenderingStatistics * GetRenderingStatistics()
Definition: Engine.h:199
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:1294
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:1202
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 void DestroyWorld(const std::shared_ptr< GameWorld > &world)
Releases a GameWorld.
Definition: Engine.cpp:1234
ScriptExecutor * GetScriptExecutor()
Definition: Engine.h:247
DLLEXPORT void AssertIfNotMainThread() const
Asserts if not called on the main thread.
Definition: Engine.h:111
std::chrono::duration< float, std::ratio< 1 > > SecondDuration
Definition: TimeIncludes.h:16
ThreadingManager * GetThreadingManager()
Definition: Engine.h:223
std::unique_ptr< GameModuleLoader > _GameModuleLoader
Definition: Engine.h:324
bool IsClient
Set to true when initialized as a client.
Definition: Engine.h:369
Represents a world that contains entities.
Definition: GameWorld.h:57
DLLEXPORT void Release(bool forced=false)
Definition: Engine.cpp:523
std::vector< Window * > AdditionalGraphicalEntities
Definition: Engine.h:308
Manages loading GameModule objects to make sure that each is loaded only once.
EventHandler * MainEvents
Definition: Engine.h:312
LeviathanApplication * Owner
Definition: Engine.h:333