engine.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**************************************************************************/
  2. /* engine.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/string/string_name.h"
  32. #include "core/templates/hash_map.h"
  33. #include "core/templates/list.h"
  34. class Object;
  35. class Dictionary;
  36. template <typename T>
  37. class TypedArray;
  38. class Engine {
  39. public:
  40. struct Singleton {
  41. StringName name;
  42. Object *ptr = nullptr;
  43. StringName class_name; // Used for binding generation hinting.
  44. // Singleton scope flags.
  45. bool user_created = false;
  46. bool editor_only = false;
  47. Singleton(const StringName &p_name = StringName(), Object *p_ptr = nullptr, const StringName &p_class_name = StringName());
  48. };
  49. private:
  50. friend class Main;
  51. uint64_t frames_drawn = 0;
  52. uint32_t _frame_delay = 0;
  53. uint64_t _frame_ticks = 0;
  54. double _process_step = 0;
  55. int ips = 60;
  56. int user_ips = 60;
  57. double physics_jitter_fix = 0.5;
  58. double _fps = 1;
  59. int _max_fps = 0;
  60. int _audio_output_latency = 0;
  61. double _time_scale = 1.0;
  62. double _game_time_scale = 1.0;
  63. double _user_time_scale = 1.0;
  64. uint64_t _physics_frames = 0;
  65. int max_physics_steps_per_frame = 8;
  66. int max_user_physics_steps_per_frame = 8;
  67. double _physics_interpolation_fraction = 0.0f;
  68. bool abort_on_gpu_errors = false;
  69. bool use_validation_layers = false;
  70. bool generate_spirv_debug_info = false;
  71. bool extra_gpu_memory_tracking = false;
  72. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  73. bool accurate_breadcrumbs = false;
  74. #endif
  75. int32_t gpu_idx = -1;
  76. uint64_t _process_frames = 0;
  77. bool _in_physics = false;
  78. List<Singleton> singletons;
  79. HashMap<StringName, Object *> singleton_ptrs;
  80. bool editor_hint = false;
  81. bool project_manager_hint = false;
  82. bool extension_reloading = false;
  83. bool embedded_in_editor = false;
  84. bool recovery_mode_hint = false;
  85. bool _print_header = true;
  86. static inline Engine *singleton = nullptr;
  87. String write_movie_path;
  88. String shader_cache_path;
  89. static constexpr int SERVER_SYNC_FRAME_COUNT_WARNING = 5;
  90. int server_syncs = 0;
  91. bool frame_server_synced = false;
  92. bool freeze_time_scale = false;
  93. protected:
  94. void _update_time_scale();
  95. public:
  96. static Engine *get_singleton();
  97. virtual void set_physics_ticks_per_second(int p_ips);
  98. virtual int get_physics_ticks_per_second() const;
  99. virtual int get_user_physics_ticks_per_second() const;
  100. virtual void set_max_physics_steps_per_frame(int p_max_physics_steps);
  101. virtual int get_max_physics_steps_per_frame() const;
  102. virtual int get_user_max_physics_steps_per_frame() const;
  103. void set_physics_jitter_fix(double p_threshold);
  104. double get_physics_jitter_fix() const;
  105. virtual void set_max_fps(int p_fps);
  106. virtual int get_max_fps() const;
  107. virtual void set_audio_output_latency(int p_msec);
  108. virtual int get_audio_output_latency() const;
  109. virtual double get_frames_per_second() const { return _fps; }
  110. uint64_t get_frames_drawn();
  111. uint64_t get_physics_frames() const { return _physics_frames; }
  112. uint64_t get_process_frames() const { return _process_frames; }
  113. bool is_in_physics_frame() const { return _in_physics; }
  114. uint64_t get_frame_ticks() const { return _frame_ticks; }
  115. double get_process_step() const { return _process_step; }
  116. double get_physics_interpolation_fraction() const { return _physics_interpolation_fraction; }
  117. void set_time_scale(double p_scale);
  118. double get_time_scale() const;
  119. void set_user_time_scale(double p_scale);
  120. double get_effective_time_scale() const;
  121. double get_unfrozen_time_scale() const;
  122. void set_print_to_stdout(bool p_enabled);
  123. bool is_printing_to_stdout() const;
  124. void set_print_error_messages(bool p_enabled);
  125. bool is_printing_error_messages() const;
  126. void print_header(const String &p_string) const;
  127. void print_header_rich(const String &p_string) const;
  128. void set_frame_delay(uint32_t p_msec);
  129. uint32_t get_frame_delay() const;
  130. void add_singleton(const Singleton &p_singleton);
  131. void get_singletons(List<Singleton> *p_singletons);
  132. bool has_singleton(const StringName &p_name) const;
  133. Object *get_singleton_object(const StringName &p_name) const;
  134. void remove_singleton(const StringName &p_name);
  135. bool is_singleton_user_created(const StringName &p_name) const;
  136. bool is_singleton_editor_only(const StringName &p_name) const;
  137. #ifdef TOOLS_ENABLED
  138. _FORCE_INLINE_ void set_editor_hint(bool p_enabled) { editor_hint = p_enabled; }
  139. _FORCE_INLINE_ bool is_editor_hint() const { return editor_hint; }
  140. _FORCE_INLINE_ void set_project_manager_hint(bool p_enabled) { project_manager_hint = p_enabled; }
  141. _FORCE_INLINE_ bool is_project_manager_hint() const { return project_manager_hint; }
  142. _FORCE_INLINE_ void set_extension_reloading_enabled(bool p_enabled) { extension_reloading = p_enabled; }
  143. _FORCE_INLINE_ bool is_extension_reloading_enabled() const { return extension_reloading; }
  144. _FORCE_INLINE_ void set_recovery_mode_hint(bool p_enabled) { recovery_mode_hint = p_enabled; }
  145. _FORCE_INLINE_ bool is_recovery_mode_hint() const { return recovery_mode_hint; }
  146. #else
  147. _FORCE_INLINE_ void set_editor_hint(bool p_enabled) {}
  148. _FORCE_INLINE_ bool is_editor_hint() const { return false; }
  149. _FORCE_INLINE_ void set_project_manager_hint(bool p_enabled) {}
  150. _FORCE_INLINE_ bool is_project_manager_hint() const { return false; }
  151. _FORCE_INLINE_ void set_extension_reloading_enabled(bool p_enabled) {}
  152. _FORCE_INLINE_ bool is_extension_reloading_enabled() const { return false; }
  153. _FORCE_INLINE_ void set_recovery_mode_hint(bool p_enabled) {}
  154. _FORCE_INLINE_ bool is_recovery_mode_hint() const { return false; }
  155. #endif
  156. Dictionary get_version_info() const;
  157. Dictionary get_author_info() const;
  158. TypedArray<Dictionary> get_copyright_info() const;
  159. Dictionary get_donor_info() const;
  160. Dictionary get_license_info() const;
  161. String get_license_text() const;
  162. void set_write_movie_path(const String &p_path);
  163. String get_write_movie_path() const;
  164. String get_architecture_name() const;
  165. void set_shader_cache_path(const String &p_path);
  166. String get_shader_cache_path() const;
  167. bool is_abort_on_gpu_errors_enabled() const;
  168. bool is_validation_layers_enabled() const;
  169. bool is_generate_spirv_debug_info_enabled() const;
  170. bool is_extra_gpu_memory_tracking_enabled() const;
  171. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  172. bool is_accurate_breadcrumbs_enabled() const;
  173. #endif
  174. int32_t get_gpu_index() const;
  175. void increment_frames_drawn();
  176. bool notify_frame_server_synced();
  177. void set_freeze_time_scale(bool p_frozen);
  178. void set_embedded_in_editor(bool p_enabled);
  179. bool is_embedded_in_editor() const;
  180. Engine();
  181. virtual ~Engine();
  182. };