GameEngine.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : The game engine for editor
  9. #pragma once
  10. #if !defined(Q_MOC_RUN)
  11. #include <AzCore/Outcome/Outcome.h>
  12. #include "LogFile.h"
  13. #include "Util/ModalWindowDismisser.h"
  14. #endif
  15. class CStartupLogoDialog;
  16. struct IInitializeUIInfo;
  17. #include <AzCore/Interface/Interface.h>
  18. #include <AzCore/Math/Vector3.h>
  19. #include <AzCore/Module/DynamicModuleHandle.h>
  20. class ThreadedOnErrorHandler : public QObject
  21. {
  22. Q_OBJECT
  23. public:
  24. explicit ThreadedOnErrorHandler(ISystemUserCallback* callback);
  25. ~ThreadedOnErrorHandler();
  26. public slots:
  27. bool OnError(const char* error);
  28. private:
  29. ISystemUserCallback* m_userCallback;
  30. };
  31. AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  32. //! This class serves as a high-level wrapper for CryEngine game.
  33. class SANDBOX_API CGameEngine
  34. : public IEditorNotifyListener
  35. {
  36. AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  37. public:
  38. CGameEngine();
  39. ~CGameEngine(void);
  40. //! Initialize System.
  41. //! @return successful outcome if initialization succeeded. or failed outcome with error message.
  42. AZ::Outcome<void, AZStd::string> Init(
  43. bool bPreviewMode,
  44. bool bTestMode,
  45. const char* sCmdLine,
  46. IInitializeUIInfo* logo,
  47. HWND hwndForInputSystem);
  48. //! Initialize game.
  49. //! @return true if initialization succeeded, false otherwise
  50. bool InitGame(const char* sGameDLL);
  51. //! Load new level into 3d engine.
  52. //! Also load AI triangulation for this level.
  53. bool LoadLevel(
  54. bool bDeleteAIGraph,
  55. bool bReleaseResources);
  56. //!* Reload level if it was already loaded.
  57. bool ReloadLevel();
  58. //! Request to switch In/Out of game mode on next update.
  59. //! The switch will happen when no sub systems are currently being updated.
  60. //! @param inGame When true editor switch to game mode.
  61. void RequestSetGameMode(bool inGame);
  62. //! Switch In/Out of AI and Physics simulation mode.
  63. //! @param enabled When true editor switch to simulation mode.
  64. void SetSimulationMode(bool enabled, bool bOnlyPhysics = false);
  65. //! Get current simulation mode.
  66. bool GetSimulationMode() const { return m_bSimulationMode; };
  67. //! Returns true if level is loaded.
  68. bool IsLevelLoaded() const { return m_bLevelLoaded; };
  69. //! Assign new level path name.
  70. void SetLevelPath(const QString& path);
  71. //! Return name of currently loaded level.
  72. const QString& GetLevelName() const { return m_levelName; };
  73. //! Return extension of currently loaded level.
  74. const QString& GetLevelExtension() const { return m_levelExtension; };
  75. //! Get fully specified level path.
  76. const QString& GetLevelPath() const { return m_levelPath; };
  77. //! Query if engine is in game mode.
  78. bool IsInGameMode() const { return m_bInGameMode; };
  79. //! Force level loaded variable to true.
  80. void SetLevelLoaded(bool bLoaded) { m_bLevelLoaded = bLoaded; }
  81. //! Force level just created variable to true.
  82. void SetLevelCreated(bool bJustCreated) { m_bJustCreated = bJustCreated; }
  83. //! Query ISystem interface.
  84. ISystem* GetSystem() { return m_pISystem; };
  85. //! Set player position in game.
  86. //! @param bEyePos If set then given position is position of player eyes.
  87. void SetPlayerViewMatrix(const Matrix34& tm, bool bEyePos = true);
  88. //! When set, player in game will be every frame synchronized with editor camera.
  89. void SyncPlayerPosition(bool bEnable);
  90. bool IsSyncPlayerPosition() const { return m_bSyncPlayerPosition; };
  91. //! Set game's current Mod name.
  92. void SetCurrentMOD(const char* sMod);
  93. //! Returns game's current Mod name.
  94. QString GetCurrentMOD() const;
  95. //! Called every frame.
  96. void Update();
  97. virtual void OnEditorNotifyEvent(EEditorNotifyEvent event);
  98. void OnAreaModified(const AABB& modifiedArea);
  99. void ExecuteQueuedEvents();
  100. //! mutex used by other threads to lock up the PAK modification,
  101. //! so only one thread can modify the PAK at once
  102. static AZStd::recursive_mutex& GetPakModifyMutex()
  103. {
  104. //! mutex used to halt copy process while the export to game
  105. //! or other pak operation is done in the main thread
  106. static AZStd::recursive_mutex s_pakModifyMutex;
  107. return s_pakModifyMutex;
  108. }
  109. private:
  110. void SetGameMode(bool inGame);
  111. void SwitchToInGame();
  112. void SwitchToInEditor();
  113. static void HandleQuitRequest(IConsoleCmdArgs*);
  114. CLogFile m_logFile;
  115. QString m_levelName;
  116. QString m_levelExtension;
  117. QString m_levelPath;
  118. QString m_MOD;
  119. bool m_bLevelLoaded;
  120. bool m_bInGameMode;
  121. bool m_bSimulationMode;
  122. bool m_bSyncPlayerPosition;
  123. bool m_bJustCreated;
  124. bool m_bIgnoreUpdates;
  125. ISystem* m_pISystem;
  126. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  127. Matrix34 m_playerViewTM;
  128. struct SSystemUserCallback* m_pSystemUserCallback;
  129. AZStd::unique_ptr<AZ::DynamicModuleHandle> m_hSystemHandle;
  130. enum EPendingGameMode
  131. {
  132. ePGM_NotPending,
  133. ePGM_SwitchToInGame,
  134. ePGM_SwitchToInEditor,
  135. };
  136. EPendingGameMode m_ePendingGameMode;
  137. AZStd::unique_ptr<class ModalWindowDismisser> m_modalWindowDismisser;
  138. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  139. };