engine.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*************************************************************************/
  2. /* engine.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "engine.h"
  31. #include "core/authors.gen.h"
  32. #include "core/donors.gen.h"
  33. #include "core/license.gen.h"
  34. #include "core/version.h"
  35. #include "core/version_hash.gen.h"
  36. void Engine::set_iterations_per_second(int p_ips) {
  37. ERR_FAIL_COND_MSG(p_ips <= 0, "Engine iterations per second must be greater than 0.");
  38. ips = p_ips;
  39. }
  40. int Engine::get_iterations_per_second() const {
  41. return ips;
  42. }
  43. void Engine::set_physics_jitter_fix(float p_threshold) {
  44. if (p_threshold < 0) {
  45. p_threshold = 0;
  46. }
  47. physics_jitter_fix = p_threshold;
  48. }
  49. float Engine::get_physics_jitter_fix() const {
  50. return physics_jitter_fix;
  51. }
  52. void Engine::set_target_fps(int p_fps) {
  53. _target_fps = p_fps > 0 ? p_fps : 0;
  54. }
  55. int Engine::get_target_fps() const {
  56. return _target_fps;
  57. }
  58. uint64_t Engine::get_frames_drawn() {
  59. return frames_drawn;
  60. }
  61. void Engine::set_frame_delay(uint32_t p_msec) {
  62. _frame_delay = p_msec;
  63. }
  64. uint32_t Engine::get_frame_delay() const {
  65. return _frame_delay;
  66. }
  67. void Engine::set_time_scale(float p_scale) {
  68. _time_scale = p_scale;
  69. }
  70. float Engine::get_time_scale() const {
  71. return _time_scale;
  72. }
  73. void Engine::set_portals_active(bool p_active) {
  74. _portals_active = p_active;
  75. }
  76. Dictionary Engine::get_version_info() const {
  77. Dictionary dict;
  78. dict["major"] = VERSION_MAJOR;
  79. dict["minor"] = VERSION_MINOR;
  80. dict["patch"] = VERSION_PATCH;
  81. dict["hex"] = VERSION_HEX;
  82. dict["status"] = VERSION_STATUS;
  83. dict["build"] = VERSION_BUILD;
  84. dict["year"] = VERSION_YEAR;
  85. String hash = VERSION_HASH;
  86. dict["hash"] = hash.length() == 0 ? String("unknown") : hash;
  87. String stringver = String(dict["major"]) + "." + String(dict["minor"]);
  88. if ((int)dict["patch"] != 0) {
  89. stringver += "." + String(dict["patch"]);
  90. }
  91. stringver += "-" + String(dict["status"]) + " (" + String(dict["build"]) + ")";
  92. dict["string"] = stringver;
  93. return dict;
  94. }
  95. static Array array_from_info(const char *const *info_list) {
  96. Array arr;
  97. for (int i = 0; info_list[i] != nullptr; i++) {
  98. arr.push_back(String::utf8(info_list[i]));
  99. }
  100. return arr;
  101. }
  102. static Array array_from_info_count(const char *const *info_list, int info_count) {
  103. Array arr;
  104. for (int i = 0; i < info_count; i++) {
  105. arr.push_back(String::utf8(info_list[i]));
  106. }
  107. return arr;
  108. }
  109. Dictionary Engine::get_author_info() const {
  110. Dictionary dict;
  111. dict["lead_developers"] = array_from_info(AUTHORS_LEAD_DEVELOPERS);
  112. dict["project_managers"] = array_from_info(AUTHORS_PROJECT_MANAGERS);
  113. dict["founders"] = array_from_info(AUTHORS_FOUNDERS);
  114. dict["developers"] = array_from_info(AUTHORS_DEVELOPERS);
  115. return dict;
  116. }
  117. Array Engine::get_copyright_info() const {
  118. Array components;
  119. for (int component_index = 0; component_index < COPYRIGHT_INFO_COUNT; component_index++) {
  120. const ComponentCopyright &cp_info = COPYRIGHT_INFO[component_index];
  121. Dictionary component_dict;
  122. component_dict["name"] = String::utf8(cp_info.name);
  123. Array parts;
  124. for (int i = 0; i < cp_info.part_count; i++) {
  125. const ComponentCopyrightPart &cp_part = cp_info.parts[i];
  126. Dictionary part_dict;
  127. part_dict["files"] = array_from_info_count(cp_part.files, cp_part.file_count);
  128. part_dict["copyright"] = array_from_info_count(cp_part.copyright_statements, cp_part.copyright_count);
  129. part_dict["license"] = String::utf8(cp_part.license);
  130. parts.push_back(part_dict);
  131. }
  132. component_dict["parts"] = parts;
  133. components.push_back(component_dict);
  134. }
  135. return components;
  136. }
  137. Dictionary Engine::get_donor_info() const {
  138. Dictionary donors;
  139. donors["platinum_sponsors"] = array_from_info(DONORS_SPONSOR_PLATINUM);
  140. donors["gold_sponsors"] = array_from_info(DONORS_SPONSOR_GOLD);
  141. donors["silver_sponsors"] = array_from_info(DONORS_SPONSOR_SILVER);
  142. donors["bronze_sponsors"] = array_from_info(DONORS_SPONSOR_BRONZE);
  143. donors["mini_sponsors"] = array_from_info(DONORS_SPONSOR_MINI);
  144. donors["gold_donors"] = array_from_info(DONORS_GOLD);
  145. donors["silver_donors"] = array_from_info(DONORS_SILVER);
  146. donors["bronze_donors"] = array_from_info(DONORS_BRONZE);
  147. return donors;
  148. }
  149. Dictionary Engine::get_license_info() const {
  150. Dictionary licenses;
  151. for (int i = 0; i < LICENSE_COUNT; i++) {
  152. licenses[LICENSE_NAMES[i]] = LICENSE_BODIES[i];
  153. }
  154. return licenses;
  155. }
  156. String Engine::get_license_text() const {
  157. return String(GODOT_LICENSE_TEXT);
  158. }
  159. void Engine::set_print_error_messages(bool p_enabled) {
  160. _print_error_enabled = p_enabled;
  161. }
  162. bool Engine::is_printing_error_messages() const {
  163. return _print_error_enabled;
  164. }
  165. void Engine::add_singleton(const Singleton &p_singleton) {
  166. singletons.push_back(p_singleton);
  167. singleton_ptrs[p_singleton.name] = p_singleton.ptr;
  168. }
  169. Object *Engine::get_singleton_object(const String &p_name) const {
  170. const Map<StringName, Object *>::Element *E = singleton_ptrs.find(p_name);
  171. ERR_FAIL_COND_V_MSG(!E, nullptr, "Failed to retrieve non-existent singleton '" + p_name + "'.");
  172. return E->get();
  173. };
  174. bool Engine::has_singleton(const String &p_name) const {
  175. return singleton_ptrs.has(p_name);
  176. };
  177. void Engine::get_singletons(List<Singleton> *p_singletons) {
  178. for (List<Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  179. p_singletons->push_back(E->get());
  180. }
  181. }
  182. Engine *Engine::singleton = nullptr;
  183. Engine *Engine::get_singleton() {
  184. return singleton;
  185. }
  186. Engine::Engine() {
  187. singleton = this;
  188. frames_drawn = 0;
  189. ips = 60;
  190. physics_jitter_fix = 0.5;
  191. _physics_interpolation_fraction = 0.0f;
  192. _frame_delay = 0;
  193. _fps = 1;
  194. _target_fps = 0;
  195. _time_scale = 1.0;
  196. _gpu_pixel_snap = false;
  197. _physics_frames = 0;
  198. _idle_frames = 0;
  199. _in_physics = false;
  200. _frame_ticks = 0;
  201. _frame_step = 0;
  202. editor_hint = false;
  203. _portals_active = false;
  204. }
  205. Engine::Singleton::Singleton(const StringName &p_name, Object *p_ptr) :
  206. name(p_name),
  207. ptr(p_ptr) {
  208. #ifdef DEBUG_ENABLED
  209. Reference *ref = Object::cast_to<Reference>(p_ptr);
  210. if (ref && !ref->is_referenced()) {
  211. WARN_PRINT("You must use Ref<> to ensure the lifetime of a Reference object intended to be used as a singleton.");
  212. }
  213. #endif
  214. }